Esempio n. 1
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']]);
 }
Esempio n. 2
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']);
 }
Esempio n. 3
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());
 }
Esempio n. 4
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']]);
 }
Esempio n. 5
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());
 }
Esempio n. 6
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']]);
 }