コード例 #1
0
ファイル: ListAction.php プロジェクト: auamarto/crud-bundle
 /**
  * {@inheritdoc}
  */
 public function call(ActionEvent $event)
 {
     $controller = $event->getController();
     $this->checkRole($controller);
     $repository = $event->getDataProvider()->getSource();
     $listView = $event->getListView();
     $listDataEvent = new ListDataEvent($repository, $event->getRequest());
     $dispatcher = $controller->get('event_dispatcher');
     $crudEvent = new CrudEvent($listView, $controller, $listDataEvent);
     $dispatcher->dispatch(CrudEvents::CRUD_LIST, $crudEvent);
     if ($this->options['response_type'] === 'html') {
         $params = ['list' => $listView->render($listDataEvent), 'title' => $listView->getTitle()];
     } else {
         $columns = $listView->getColumns();
         $results = $listView->getData($listDataEvent, true);
         $results = $results->toArray();
         foreach ($results as $key => $result) {
             $rowData = [];
             foreach ($columns as $column) {
                 $columnData = $column->getData($result, $this->options['response_type']);
                 if ($columnData) {
                     $rowData[$column->getLabel()] = $columnData;
                 }
             }
             $results[$key] = $rowData;
         }
         $params = ['results' => $results];
     }
     $paramsEvent = new ResponseEvent($params);
     $crudEvent = new CrudEvent($repository, $controller, $paramsEvent);
     $dispatcher->dispatch(CrudEvents::CRUD_LIST_PRE_RESPONSE, $crudEvent);
     return $this->getResponseHandler($controller)->getResponse($this->options['response_type'], $event->getView(), $this->getTemplate(), $paramsEvent->getParams());
 }
コード例 #2
0
ファイル: ExportAction.php プロジェクト: auamarto/crud-bundle
 /**
  * {@inheritdoc}
  */
 public function call(ActionEvent $event)
 {
     $controller = $event->getController();
     $this->checkRole($controller);
     $repository = $event->getDataProvider()->getSource();
     $request = $event->getRequest();
     $dispatcher = $controller->get('event_dispatcher');
     $snappy = $controller->get('knp_snappy.pdf');
     $responseHandler = $this->getResponseHandler($controller);
     $crudEvent = new CrudEvent($repository, $controller);
     $dispatcher->dispatch(CrudEvents::CRUD_EXPORT, $crudEvent);
     $this->id = $request->get('id');
     $options['template'] = !empty($this->options['template'] ?: (is_numeric($this->id) ? 'show' : 'list'));
     if (is_numeric($this->id)) {
         $dataProvider = $event->getDataProvider();
         $html = $responseHandler->getHtml($event->getView(), $this->getTemplate(), ['data' => $dataProvider->get($this->id), 'ui' => false]);
         $response = new Response($snappy->getOutputFromHtml($html, ['margin-bottom' => 3, 'margin-top' => 3, 'margin-left' => 4, 'margin-right' => 14]), 200, array('Content-Type' => 'application/pdf', 'Content-Disposition' => 'attachment; filename="export.pdf"'));
     } else {
         $listView = $event->getListView();
         $listDataEvent = new ListDataEvent($repository, $request);
         $type = $request->get('type');
         if ($type === 'pdf') {
             $html = $responseHandler->getHtml($event->getView(), $this->getTemplate(), ['list' => $listView->render($listDataEvent), 'title' => $listView->getTitle(), 'ui' => false]);
             $response = new Response($snappy->getOutputFromHtml($html, ['margin-bottom' => 3, 'margin-top' => 3, 'margin-left' => 4, 'margin-right' => 14]), 200, array('Content-Type' => 'application/pdf', 'Content-Disposition' => 'attachment; filename="export.pdf"'));
         } else {
             $response = new StreamedResponse();
             $listView->setPagination(false);
             $queryBuilder = $listView->getData($listDataEvent, true, true);
             $response->setCallback(function () use($queryBuilder, $controller) {
                 $handle = fopen('php://output', 'w+');
                 $headers = $controller->getHeaders();
                 if (!empty($headers)) {
                     fputcsv($handle, $headers, ';');
                 }
                 $entityManager = $controller->get('doctrine.orm.entity_manager');
                 $results = $queryBuilder->getQuery()->iterate();
                 while (false !== ($row = $results->next())) {
                     $element = $controller->getRow($row[0]);
                     $entityManager->detach($row[0]);
                     fputcsv($handle, $element);
                 }
                 fclose($handle);
             });
             $response->setStatusCode(200);
             $response->headers->set('Content-Type', 'text/csv; charset=utf-8');
             $response->headers->set('Content-Disposition', 'attachment; filename="export.csv"');
         }
     }
     return $response;
 }