1 | <?php |
||
50 | abstract class AbstractController extends BaseController { |
||
51 | |||
52 | /** |
||
53 | * Build a response. |
||
54 | * |
||
55 | * @param Request $request The request. |
||
56 | * @param string $name The provider name. |
||
57 | * @param ActionResponse $output The output. |
||
58 | * @return Response Returns the response. |
||
59 | */ |
||
60 | protected function buildDataTablesResponse(Request $request, $name, ActionResponse $output) { |
||
88 | |||
89 | /** |
||
90 | * Dispatch an event. |
||
91 | * |
||
92 | * @param string $eventName The event name. |
||
93 | * @param array $entities The entities. |
||
94 | * @return Event Returns the event in case of succes, null otherwise. |
||
95 | */ |
||
96 | protected function dispatchDataTablesEvent($eventName, array $entities) { |
||
110 | |||
111 | /** |
||
112 | * Export callback. |
||
113 | * |
||
114 | * @param DataTablesProviderInterface $dtProvider The provider. |
||
115 | * @param DataTablesRepositoryInterface $repository The repository. |
||
116 | * @param DataTablesCSVExporterInterface $dtExporter The exporter. |
||
117 | * @return void |
||
118 | */ |
||
119 | protected function exportDataTablesCallback(DataTablesProviderInterface $dtProvider, DataTablesRepositoryInterface $repository, DataTablesCSVExporterInterface $dtExporter) { |
||
120 | |||
121 | // Get the entities manager. |
||
122 | $em = $this->getDoctrine()->getManager(); |
||
123 | |||
124 | // Open the file. |
||
125 | $stream = fopen("php://output", "w+"); |
||
126 | |||
127 | // Export the columns. |
||
128 | fputcsv($stream, $dtExporter->exportColumns(), ";"); |
||
129 | |||
130 | // Paginates. |
||
131 | $total = $repository->dataTablesCountExported($dtProvider); |
||
132 | $pages = PaginateHelper::getPagesCount($total, DataTablesRepositoryInterface::REPOSITORY_LIMIT); |
||
133 | |||
134 | // Handle each page. |
||
135 | for ($i = 0; $i < $pages; ++$i) { |
||
136 | |||
137 | // Get the offset and limit. |
||
138 | list($offset, $limit) = PaginateHelper::getPageOffsetAndLimit($i, DataTablesRepositoryInterface::REPOSITORY_LIMIT, $total); |
||
139 | |||
140 | // Get the export query with offset and limit. |
||
141 | $result = $repository->dataTablesExportAll($dtProvider) |
||
142 | ->setFirstResult($offset) |
||
143 | ->setMaxResults($limit) |
||
144 | ->getQuery() |
||
145 | ->iterate(); |
||
146 | |||
147 | // Handle each entity. |
||
148 | while (false !== ($row = $result->next())) { |
||
149 | |||
150 | // Dispatch an event. |
||
151 | $this->dispatchDataTablesEvent(DataTablesEvents::DATATABLES_PRE_EXPORT, [$row[0]]); |
||
152 | |||
153 | // Export the entity. |
||
154 | fputcsv($stream, $dtExporter->exportRow($row[0]), ";"); |
||
155 | |||
156 | // Detach the entity to avoid memory consumption. |
||
157 | $em->detach($row[0]); |
||
158 | |||
159 | // Dispatch an event. |
||
160 | $this->dispatchDataTablesEvent(DataTablesEvents::DATATABLES_PRE_EXPORT, [$row[0]]); |
||
161 | } |
||
162 | } |
||
163 | |||
164 | // Close the file. |
||
165 | fclose($stream); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Get a column. |
||
170 | * |
||
171 | * @param DataTablesProviderInterface $dtProvider The provider. |
||
172 | * @param string $data The data. |
||
173 | * @return DataTablesColumnInterface Returns the column. |
||
174 | * @throws BadDataTablesColumnException Throws a bad column exception. |
||
175 | */ |
||
176 | protected function getDataTablesColumn(DataTablesProviderInterface $dtProvider, $data) { |
||
195 | |||
196 | /** |
||
197 | * Get a CSV exporter. |
||
198 | * |
||
199 | * @param DataTablesProviderInterface $dtProvider The provider. |
||
200 | * @return DataTablesCSVExporterInterface Returns the CSV exporter. |
||
201 | * @throws UnregisteredDataTablesProviderException Throws an unregistered provider exception. |
||
202 | * @throws BadDataTablesCSVExporterException Throws a bad CSV exporter exception. |
||
203 | */ |
||
204 | protected function getDataTablesCSVExporter(DataTablesProviderInterface $dtProvider) { |
||
223 | |||
224 | /** |
||
225 | * Get an editor. |
||
226 | * |
||
227 | * @param DataTablesProviderInterface $dtProvider The provider. |
||
228 | * @return DataTablesEditorInterface Returns the editor. |
||
229 | * @throws UnregisteredDataTablesProviderException Throws an unregistered provider exception. |
||
230 | * @throws BadDataTablesEditorException Throws a bad editor exception. |
||
231 | */ |
||
232 | protected function getDataTablesEditor(DataTablesProviderInterface $dtProvider) { |
||
251 | |||
252 | /** |
||
253 | * Get an entity by id. |
||
254 | * |
||
255 | * @param DataTablesProviderInterface $dtProvider The provider. |
||
256 | * @param int $id The entity id. |
||
257 | * @return object Returns the entity. |
||
258 | * @throws BadDataTablesRepositoryException Throws a bad repository exception. |
||
259 | * @throws EntityNotFoundException Throws an Entity not found exception. |
||
260 | */ |
||
261 | protected function getDataTablesEntityById(DataTablesProviderInterface $dtProvider, $id) { |
||
281 | |||
282 | /** |
||
283 | * Get the manager. |
||
284 | * |
||
285 | * @return DataTablesManager Returns the manager. |
||
286 | */ |
||
287 | protected function getDataTablesManager() { |
||
290 | |||
291 | /** |
||
292 | * Get a notification. |
||
293 | * |
||
294 | * @param string $notificationId The notification id. |
||
295 | * @return string Returns the notification. |
||
296 | */ |
||
297 | protected function getDataTablesNotification($notificationId) { |
||
300 | |||
301 | /** |
||
302 | * Get the provider. |
||
303 | * |
||
304 | * @param string $name The provider name. |
||
305 | * @return DataTablesProviderInterface Returns the provider. |
||
306 | * @throws UnregisteredDataTablesProviderException Throws an unregistered provider exception. |
||
307 | */ |
||
308 | protected function getDataTablesProvider($name) { |
||
322 | |||
323 | /** |
||
324 | * Get a repository. |
||
325 | * |
||
326 | * @param DataTablesProviderInterface $dtProvider The provider. |
||
327 | * @return EntityRepository Returns the repository. |
||
328 | * @throws BadDataTablesRepositoryException Throws a bad repository exception. |
||
329 | */ |
||
330 | protected function getDataTablesRepository(DataTablesProviderInterface $dtProvider) { |
||
347 | |||
348 | /** |
||
349 | * Get a serializer. |
||
350 | * |
||
351 | * @return Serializer Returns the serializer. |
||
352 | */ |
||
353 | protected function getDataTablesSerializer() { |
||
356 | |||
357 | /** |
||
358 | * Get a wrapper. |
||
359 | * |
||
360 | * @param DataTablesProviderInterface $dtProvider The provider. |
||
361 | * @return DataTablesWrapperInterface Returns the wrapper. |
||
362 | */ |
||
363 | protected function getDataTablesWrapper(DataTablesProviderInterface $dtProvider) { |
||
389 | |||
390 | /** |
||
391 | * Handle an exception. |
||
392 | * |
||
393 | * @param Exception $ex The exception. |
||
394 | * @param string $notificationBaseId The notification base id. |
||
395 | * @return ActionResponse Returns the action response. |
||
396 | */ |
||
397 | protected function handleDataTablesException(Exception $ex, $notificationBaseId) { |
||
410 | |||
411 | /** |
||
412 | * Prepare an action response. |
||
413 | * |
||
414 | * @param int $status The status. |
||
415 | * @param string $notificationId The notification id. |
||
416 | * @return ActionResponse Returns the action response. |
||
417 | */ |
||
418 | protected function prepareActionResponse($status, $notificationId) { |
||
428 | |||
429 | } |
||
430 |