44
55namespace FreezyBee \DataGridBundle ;
66
7- use FreezyBee \DataGridBundle \Column \ActionColumn ;
87use FreezyBee \DataGridBundle \DataSource \DataSourceInterface ;
8+ use FreezyBee \DataGridBundle \Export \DataGridExporterInterface ;
99use Symfony \Component \HttpFoundation \JsonResponse ;
1010use Symfony \Component \HttpFoundation \Request ;
11+ use Symfony \Component \HttpFoundation \Response ;
1112use Symfony \Component \Templating \EngineInterface ;
1213
1314/**
@@ -18,6 +19,9 @@ class DataGrid
1819 /** @var EngineInterface */
1920 private $ engine ;
2021
22+ /** @var DataGridExporterInterface */
23+ private $ exporter ;
24+
2125 /** @var DataSourceInterface */
2226 private $ dataSource ;
2327
@@ -29,17 +33,20 @@ class DataGrid
2933
3034 /**
3135 * @param EngineInterface $engine
36+ * @param DataGridExporterInterface $exporter
3237 * @param DataSourceInterface $dataSource
3338 * @param DataGridConfig $config
3439 * @param string $name
3540 */
3641 public function __construct (
3742 EngineInterface $ engine ,
43+ DataGridExporterInterface $ exporter ,
3844 DataSourceInterface $ dataSource ,
3945 DataGridConfig $ config ,
4046 string $ name
4147 ) {
4248 $ this ->engine = $ engine ;
49+ $ this ->exporter = $ exporter ;
4350 $ this ->dataSource = $ dataSource ;
4451 $ this ->config = $ config ;
4552 $ this ->name = $ name ;
@@ -50,6 +57,33 @@ public function __construct(
5057 * @return JsonResponse
5158 */
5259 public function ajax (Request $ request ): JsonResponse
60+ {
61+ $ result = $ this ->processData ($ request , false );
62+
63+ return (new JsonResponse ([
64+ 'draw ' => $ request ->query ->getInt ('draw ' ),
65+ 'recordsTotal ' => $ result ['totalCount ' ],
66+ 'recordsFiltered ' => $ result ['filteredCount ' ],
67+ 'data ' => $ result ['data ' ],
68+ ]))->setEncodingOptions (JSON_UNESCAPED_UNICODE );
69+ }
70+
71+
72+ /**
73+ * @param Request $request
74+ * @return Response
75+ */
76+ public function export (Request $ request ): Response
77+ {
78+ return $ this ->exporter ->export ($ this ->processData ($ request , true )['data ' ]);
79+ }
80+
81+ /**
82+ * @param Request $request
83+ * @param bool $export
84+ * @return array
85+ */
86+ private function processData (Request $ request , bool $ export ): array
5387 {
5488 $ totalCount = $ this ->dataSource ->getTotalCount ();
5589
@@ -63,7 +97,7 @@ public function ajax(Request $request): JsonResponse
6397
6498 // filters
6599 foreach ($ query ['columns ' ] as $ index => $ ajaxColumn ) {
66- $ value = $ ajaxColumn ['search ' ]['value ' ];
100+ $ value = $ ajaxColumn ['search ' ]['value ' ] ?? '' ;
67101 $ column = $ this ->config ->getColumns ()[$ index ] ?? null ;
68102
69103 if ($ value !== '' && $ column !== null && $ column ->isFilterable ()) {
@@ -73,22 +107,27 @@ public function ajax(Request $request): JsonResponse
73107
74108 $ filteredCount = $ this ->dataSource ->getFilteredCount ();
75109
76- // limit and offset
77- $ this -> dataSource -> applyLimitAndOffset (( int ) $ query [ ' length ' ], ( int ) $ query [ ' start ' ]);
78-
79- $ items = $ this -> dataSource -> getData ();
110+ if (! $ export ) {
111+ // limit and offset
112+ $ this -> dataSource -> applyLimitAndOffset (( int ) $ query [ ' length ' ], ( int ) $ query [ ' start ' ]);
113+ }
80114
81115 $ data = [];
82- foreach ($ items as $ item ) {
116+ foreach ($ this ->dataSource ->getData () as $ item ) {
117+ // custom export
118+ if ($ export && $ this ->config ->getCustomExportCallback () !== null ) {
119+ $ data [] = $ this ->config ->getCustomExportCallback ()($ item );
120+ continue ;
121+ }
122+
83123 $ row = [];
84124 foreach ($ this ->config ->getColumns () as $ column ) {
85- if ($ column instanceof ActionColumn ) {
86- continue ;
125+ if (( $ export && $ column-> isAllowExport ()) || (! $ export && $ column -> isAllowRender ()) ) {
126+ $ row [] = $ column -> renderContent ( $ item , $ this -> engine , [ ' export ' => $ export ]) ;
87127 }
88- $ row [] = $ column ->renderContent ($ item , $ this ->engine );
89128 }
90129
91- if ($ this ->config ->getActionColumn ()->hasActions ()) {
130+ if (! $ export && $ this ->config ->getActionColumn ()->hasActions ()) {
92131 $ row [] = $ this ->engine ->render ('@FreezyBeeDataGrid/action.html.twig ' , [
93132 'item ' => $ item ,
94133 'actions ' => $ this ->config ->getActionColumn ()->getActions ()
@@ -97,14 +136,14 @@ public function ajax(Request $request): JsonResponse
97136 $ data [] = $ row ;
98137 }
99138
100- return (new JsonResponse ([
101- 'draw ' => $ request ->query ->getInt ('draw ' ),
102- 'recordsTotal ' => $ totalCount ,
103- 'recordsFiltered ' => $ filteredCount ,
139+ return [
104140 'data ' => $ data ,
105- ]))->setEncodingOptions (JSON_UNESCAPED_UNICODE );
141+ 'totalCount ' => $ totalCount ,
142+ 'filteredCount ' => $ filteredCount ,
143+ ];
106144 }
107145
146+
108147 /**
109148 * @return string
110149 */
@@ -123,7 +162,8 @@ public function render(): string
123162 'perPage ' => $ this ->config ->getDefaultPerPage (), $ this ->config ->getColumns (),
124163 'sortIndex ' => $ sortIndex ,
125164 'sortDir ' => $ this ->config ->getDefaultSortColumnDirection () ?? 'desc ' ,
126- ]
165+ ],
166+ 'allowExport ' => $ this ->config ->isAllowExport (),
127167 ]);
128168 }
129169}
0 commit comments