/** * Display or save datagrid views * * @param Request $request * @param string $alias * @param DatagridView|null $view * * @return Response|JsonResponse */ public function indexAction(Request $request, $alias, DatagridView $view = null) { $user = $this->getUser(); if (!$view || $view->getOwner() !== $user) { $view = new DatagridView(); $view->setOwner($user); $view->setDatagridAlias($alias); } $form = $this->createForm('pim_datagrid_view', $view); if ($request->isMethod('POST')) { $creation = !(bool) $view->getId(); if (!$creation) { $form->remove('label'); } $form->submit($request); // If the view was created based on the default view, set the default columns if (!$view->getColumns()) { $view->setColumns(array_keys($this->datagridViewManager->getColumnChoices($alias, true))); } $violations = $this->validator->validate($view); if ($violations->count()) { $messages = []; foreach ($violations as $violation) { $messages[] = $this->getTranslator()->trans($violation->getMessage()); } return new JsonResponse(['errors' => $messages]); } else { $this->datagridViewManager->save($view); if ($creation) { $this->addFlash('success', 'flash.datagrid view.created'); } return new JsonResponse(['id' => $view->getId()]); } } $views = $this->datagridViewManager->findPublic($alias); return $this->render('PimDataGridBundle:Datagrid:_views.html.twig', ['alias' => $alias, 'views' => $views, 'form' => $form->createView()]); }
/** * Create a datagrid view * * @param array $data * * @return DatagridView */ protected function createDatagridView(array $data) { $columns = array_map(function ($column) { return trim($column); }, explode(',', $data['columns'])); $view = new DatagridView(); $view->setLabel($data['label']); $view->setDatagridAlias($data['alias']); $view->setFilters(urlencode($data['filters'])); $view->setColumns($columns); $view->setOwner($this->getUser('Peter')); $this->validate($view); $this->persist($view); return $view; }
/** * {@inheritDoc} */ public function setOwner(\Pim\Bundle\UserBundle\Entity\UserInterface $owner) { $this->__initializer__ && $this->__initializer__->__invoke($this, 'setOwner', array($owner)); return parent::setOwner($owner); }
/** * Save the Datagrid View received through the $request for the grid with the given $alias. * * If any errors occur during the writing process, a Json response is sent with {'errors' => 'Error message'}. * If success, return a Json response with the id of the saved View. * * @param Request $request * @param string $alias * * @return JsonResponse */ public function saveAction(Request $request, $alias) { $view = $request->request->get('view', null); if (null === $view) { return new BadRequestHttpException('Parameter "view" needed in the request.'); } if (isset($view['id'])) { $creation = false; $datagridView = $this->datagridViewRepo->find($view['id']); } else { $creation = true; $user = $this->tokenStorage->getToken()->getUser(); $datagridView = new DatagridView(); $datagridView->setOwner($user); $datagridView->setDatagridAlias($alias); $datagridView->setLabel($view['label']); } if (null === $datagridView) { return new NotFoundHttpException(); } $datagridView->setColumns(explode(',', $view['columns'])); $datagridView->setFilters($view['filters']); $violations = $this->validator->validate($datagridView); if ($violations->count()) { $messages = []; foreach ($violations as $violation) { $messages[] = $this->translator->trans($violation->getMessage()); } return new JsonResponse($messages, 400); } $this->saver->save($datagridView); if ($creation) { $request->getSession()->getFlashBag()->add('success', new Message('grid.view_selector.flash.created')); } return new JsonResponse(['id' => $datagridView->getId()]); }