예제 #1
0
 /**
  * {@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;
 }
예제 #2
0
 /**
  * {@inheritdoc}
  */
 public function call(ActionEvent $event)
 {
     $controller = $event->getController();
     $request = $event->getRequest();
     $dataProvider = $event->getDataProvider();
     $dispatcher = $controller->get('event_dispatcher');
     $formProvider = $controller->get('vardius_crud.form.provider');
     if ($id = $request->get('id')) {
         $data = $dataProvider->get($id);
         $this->checkRole($controller, $data);
     } else {
         $data = $dataProvider->create();
         $this->checkRole($controller);
     }
     $repository = $dataProvider->getSource();
     $form = $formProvider->createForm($event->getFormType(), $data);
     $crudEvent = new CrudEvent($repository, $controller, $form);
     $dispatcher->dispatch(CrudEvents::CRUD_PRE_SAVE, $crudEvent);
     $responseHandler = $this->getResponseHandler($controller);
     if ($request->isMethod('POST')) {
         $form->handleRequest($request);
         if ($form->isValid()) {
             if ($data->getId()) {
                 $dispatcher->dispatch(CrudEvents::CRUD_PRE_UPDATE, $crudEvent);
                 $dataProvider->update($data);
                 $dispatcher->dispatch(CrudEvents::CRUD_POST_UPDATE, $crudEvent);
             } else {
                 $dispatcher->dispatch(CrudEvents::CRUD_PRE_CREATE, $crudEvent);
                 $dataProvider->add($data);
                 $dispatcher->dispatch(CrudEvents::CRUD_POST_CREATE, $crudEvent);
             }
             $dispatcher->dispatch(CrudEvents::CRUD_POST_SAVE, $crudEvent);
             $routeName = rtrim(rtrim($request->get('_route'), 'edit'), 'add') . 'show';
             if (!$controller->get('router')->getRouteCollection()->get($routeName)) {
                 $routeName = rtrim($routeName, 'show') . 'list';
             }
             if (!$controller->get('router')->getRouteCollection()->get($routeName)) {
                 $flashBag = $request->getSession()->getFlashBag();
                 $flashBag->add('success', 'save.success');
                 return $controller->redirect($responseHandler->getRefererUrl($controller, $request, ['id' => $data->getId()]));
             }
             if ($this->options['response_type'] === 'html') {
                 return $controller->redirect($controller->generateUrl($routeName, ['id' => $data->getId()]));
             } else {
                 return $responseHandler->getResponse($this->options['response_type'], '', '', ['data' => $data], self::ACTION_CODE);
             }
         } elseif ($this->options['response_type'] === 'json') {
             $formErrorHandler = $controller->get('vardius_crud.form.error_handler');
             return new JsonResponse(['message' => 'Invalid form data', 'errors' => $formErrorHandler->getErrorMessages($form)], 400);
         }
     }
     $params = ['data' => $data];
     if ($this->options['response_type'] === 'html') {
         $params = array_merge($params, ['form' => $form->createView()]);
     }
     $paramsEvent = new ResponseEvent($params);
     $crudEvent = new CrudEvent($repository, $controller, $paramsEvent);
     $dispatcher->dispatch(CrudEvents::CRUD_SAVE_PRE_RESPONSE, $crudEvent);
     return $responseHandler->getResponse($this->options['response_type'], $event->getView(), $this->getTemplate(), $paramsEvent->getParams());
 }
예제 #3
0
 /**
  * {@inheritdoc}
  */
 public function call(ActionEvent $event, string $format) : Response
 {
     $controller = $event->getController();
     $dataProvider = $event->getDataProvider();
     $request = $event->getRequest();
     $id = $request->get('id');
     $data = $dataProvider->get($id);
     if ($data === null) {
         throw new EntityNotFoundException('Not found error');
     }
     $this->checkRole($controller, $data);
     if ($format === 'html') {
         $params = $data;
     } else {
         if ($this->options['toArray']) {
             if (method_exists($data, 'toArray')) {
                 $data = $data->toArray();
             } else {
                 throw new MethodNotImplementedException('toArray');
             }
         }
         $params = $data;
     }
     $routeName = $request->get('_route');
     if (strpos($routeName, 'export') !== false) {
         $params['ui'] = false;
     }
     $paramsEvent = new ResponseEvent($params);
     $crudEvent = new CrudEvent($dataProvider->getSource(), $controller, $paramsEvent);
     $dispatcher = $controller->get('event_dispatcher');
     $dispatcher->dispatch(CrudEvents::CRUD_SHOW, $crudEvent);
     $responseHandler = $controller->get('vardius_crud.response.handler');
     return $responseHandler->getResponse($format, $event->getView(), $this->getTemplate(), $paramsEvent->getParams(), 200, [], ['groups' => ['show']]);
 }
예제 #4
0
 /**
  * {@inheritdoc}
  */
 public function call(ActionEvent $event, string $format) : Response
 {
     $controller = $event->getController();
     $dataProvider = $event->getDataProvider();
     $request = $event->getRequest();
     $data = $dataProvider->get($request->get('id'));
     $this->checkRole($controller, $data);
     $allowed = $this->getOptions()['allow'];
     $properties = $request->get('data', []);
     $accessor = PropertyAccess::createPropertyAccessorBuilder()->enableMagicCall()->getPropertyAccessor();
     foreach ($properties as $property => $value) {
         if (in_array($property, $allowed) && $accessor->isWritable($data, $property)) {
             $accessor->setValue($data, $property, $value);
         }
     }
     $validator = $controller->get('validator');
     $errors = $validator->validate($data, null, ["update"]);
     $responseHandler = $controller->get('vardius_crud.response.handler');
     if (count($errors) > 0) {
         return new JsonResponse(['message' => 'Invalid data', 'errors' => $errors], 400);
     } else {
         $source = $dataProvider->getSource();
         $crudEvent = new CrudEvent($source, $controller);
         $dispatcher = $controller->get('event_dispatcher');
         $dispatcher->dispatch(CrudEvents::CRUD_PRE_UPDATE, $crudEvent);
         $dataProvider->update($data);
         $dispatcher->dispatch(CrudEvents::CRUD_POST_UPDATE, $crudEvent);
         return $responseHandler->getResponse($format, '', '', $data, 200, [], ['groups' => ['update']]);
     }
 }
예제 #5
0
 /**
  * {@inheritdoc}
  */
 public function call(ActionEvent $event)
 {
     $controller = $event->getController();
     $request = $event->getRequest();
     $dataProvider = $event->getDataProvider();
     $dispatcher = $controller->get('event_dispatcher');
     $id = $request->get('id');
     $data = $dataProvider->get($id);
     if ($data === null) {
         throw new EntityNotFoundException('Not found error');
     }
     $this->checkRole($controller, $data);
     if ($this->options['response_type'] === 'html') {
         $params = ['data' => $data];
     } else {
         if ($this->options['toArray']) {
             if (method_exists($data, 'toArray')) {
                 $data = $data->toArray();
             } else {
                 throw new MethodNotImplementedException('toArray');
             }
         }
         $params = ['data' => $data];
     }
     $paramsEvent = new ResponseEvent($params);
     $crudEvent = new CrudEvent($dataProvider->getSource(), $controller, $paramsEvent);
     $dispatcher->dispatch(CrudEvents::CRUD_SHOW, $crudEvent);
     return $this->getResponseHandler($controller)->getResponse($this->options['response_type'], $event->getView(), $this->getTemplate(), $paramsEvent->getParams(), 200, [], ['Default', 'show']);
 }
예제 #6
0
 /**
  * {@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());
 }
예제 #7
0
 /**
  * {@inheritdoc}
  */
 public function call(ActionEvent $event)
 {
     $controller = $event->getController();
     $request = $event->getRequest();
     $dataProvider = $event->getDataProvider();
     $dispatcher = $controller->get('event_dispatcher');
     $id = $request->get('id');
     $data = $dataProvider->get($id);
     if ($data === null) {
         throw new EntityNotFoundException('Not found error');
     }
     $this->checkRole($controller, $data);
     $crudEvent = new CrudEvent($dataProvider->getSource(), $controller);
     $dispatcher->dispatch(CrudEvents::CRUD_PRE_DELETE, $crudEvent);
     try {
         $dataProvider->remove($data->getId());
     } catch (\Exception $e) {
         $message = null;
         if (is_object($data) && method_exists($data, '__toString')) {
             $message = 'Error while deleting "' . $data . '"';
         } else {
             $message = 'Error while deleting element with id "' . $id . '"';
         }
         $flashBag = $request->getSession()->getFlashBag();
         $flashBag->add('error', $message);
     }
     $dispatcher->dispatch(CrudEvents::CRUD_POST_DELETE, $crudEvent);
     $responseHandler = $this->getResponseHandler($controller);
     if ($this->options['response_type'] === 'html') {
         return $controller->redirect($responseHandler->getRefererUrl($controller, $request));
     } else {
         return $responseHandler->getResponse($this->options['response_type'], '', '', ['data' => $data]);
     }
 }
예제 #8
0
 /**
  * {@inheritdoc}
  */
 public function call(ActionEvent $event, string $format) : Response
 {
     $controller = $event->getController();
     $this->checkRole($controller);
     $dataProvider = $event->getDataProvider();
     $params = $dataProvider->findAll();
     $request = $event->getRequest();
     $routeName = $request->get('_route');
     if (strpos($routeName, 'export') !== false) {
         $params['ui'] = false;
     }
     $source = $dataProvider->getSource();
     $paramsEvent = new ResponseEvent($params);
     $crudEvent = new CrudEvent($source, $controller, $paramsEvent);
     $dispatcher = $controller->get('event_dispatcher');
     $dispatcher->dispatch(CrudEvents::CRUD_LIST, $crudEvent);
     $responseHandler = $controller->get('vardius_crud.response.handler');
     return $responseHandler->getResponse($format, $event->getView(), $this->getTemplate(), $paramsEvent->getParams(), 200, [], ['groups' => ['list']]);
 }
예제 #9
0
 /**
  * {@inheritdoc}
  */
 public function call(ActionEvent $event, string $format) : Response
 {
     $controller = $event->getController();
     $dataProvider = $event->getDataProvider();
     $request = $event->getRequest();
     $id = $request->get('id');
     $data = $dataProvider->get($id);
     if ($data === null) {
         throw new EntityNotFoundException('Not found error');
     }
     $this->checkRole($controller, $data);
     $crudEvent = new CrudEvent($dataProvider->getSource(), $controller);
     $dispatcher = $controller->get('event_dispatcher');
     $dispatcher->dispatch(CrudEvents::CRUD_PRE_DELETE, $crudEvent);
     try {
         $dataProvider->remove($data->getId());
         $status = 200;
         $response = $data;
     } catch (\Exception $e) {
         $message = null;
         if (is_object($data) && method_exists($data, '__toString')) {
             $message = 'Error while deleting "' . $data . '"';
         } else {
             $message = 'Error while deleting element with id "' . $id . '"';
         }
         $response = ['success' => false, 'error' => $message];
         $status = 400;
         if ($format === 'html') {
             /** @var Session $session */
             $session = $request->getSession();
             /** @var FlashBagInterface $flashBag */
             $flashBag = $session->getFlashBag();
             $flashBag->add('error', $message);
         }
     }
     $dispatcher->dispatch(CrudEvents::CRUD_POST_DELETE, $crudEvent);
     $responseHandler = $controller->get('vardius_crud.response.handler');
     if ($format === 'html') {
         return $controller->redirect($responseHandler->getRefererUrl($controller, $request));
     } else {
         return $responseHandler->getResponse($format, '', '', $response, $status);
     }
 }
예제 #10
0
 /**
  * {@inheritdoc}
  */
 public function call(ActionEvent $event, string $format) : Response
 {
     $controller = $event->getController();
     $dataProvider = $event->getDataProvider();
     $request = $event->getRequest();
     if ($id = $request->get('id')) {
         $data = $dataProvider->get($id);
         $this->checkRole($controller, $data);
     } else {
         $data = $dataProvider->create();
         $this->checkRole($controller);
     }
     $formProvider = $controller->get('vardius_crud.form.provider');
     $form = $formProvider->createForm($event->getFormType(), $data, ['method' => $request->getMethod()]);
     $source = $dataProvider->getSource();
     $crudEvent = new CrudEvent($source, $controller, $form);
     $dispatcher = $controller->get('event_dispatcher');
     $dispatcher->dispatch(CrudEvents::CRUD_PRE_SAVE, $crudEvent);
     $responseHandler = $controller->get('vardius_crud.response.handler');
     if (in_array($request->getMethod(), ['POST', 'PUT'])) {
         $form->handleRequest($request);
         if ($form->isValid()) {
             if ($data->getId()) {
                 $dispatcher->dispatch(CrudEvents::CRUD_PRE_UPDATE, $crudEvent);
                 $dataProvider->update($data);
                 $dispatcher->dispatch(CrudEvents::CRUD_POST_UPDATE, $crudEvent);
             } else {
                 $dispatcher->dispatch(CrudEvents::CRUD_PRE_CREATE, $crudEvent);
                 $dataProvider->add($data);
                 $dispatcher->dispatch(CrudEvents::CRUD_POST_CREATE, $crudEvent);
             }
             $dispatcher->dispatch(CrudEvents::CRUD_POST_SAVE, $crudEvent);
             if ($format === 'html') {
                 $routeName = rtrim(rtrim($request->get('_route'), 'edit'), 'add') . 'show';
                 if (!$controller->get('router')->getRouteCollection()->get($routeName)) {
                     $routeName = rtrim($routeName, 'show') . 'list';
                 }
                 if (!$controller->get('router')->getRouteCollection()->get($routeName)) {
                     /** @var Session $session */
                     $session = $request->getSession();
                     /** @var FlashBagInterface $flashBag */
                     $flashBag = $session->getFlashBag();
                     $flashBag->add('success', 'save.success');
                     return $controller->redirect($responseHandler->getRefererUrl($controller, $request, ['id' => $data->getId()]));
                 }
                 return $controller->redirect($controller->generateUrl($routeName, ['id' => $data->getId()]));
             } else {
                 return $responseHandler->getResponse($format, '', '', $data, self::ACTION_CODE, [], ['groups' => ['update']]);
             }
         } elseif ($format === 'json') {
             $formErrorHandler = $controller->get('vardius_crud.form.error_handler');
             return new JsonResponse(['message' => 'Invalid form data', 'errors' => $formErrorHandler->getErrorMessages($form)], 400);
         }
     }
     $params = $data;
     if ($format === 'html') {
         $params = array_merge($params, ['form' => $form->createView()]);
     }
     $paramsEvent = new ResponseEvent($params);
     $crudEvent = new CrudEvent($source, $controller, $paramsEvent);
     $dispatcher->dispatch(CrudEvents::CRUD_SAVE, $crudEvent);
     return $responseHandler->getResponse($format, $event->getView(), $this->getTemplate(), $paramsEvent->getParams(), 200, [], ['groups' => ['update']]);
 }