setPage($page); $form = $this->getToolbarForm($query); if ($this->handleSearch($form, $request)) { return $this->redirectToRoute('tags'); } $tags = $repository->getTagCount($query); $multiUpdateForm = $this->getMultiUpdateForm($repository); if ($multiUpdateForm !== null) { $multiUpdateForm = $multiUpdateForm->createView(); } return $this->render('tags/index.html.twig', [ 'tags' => $tags, 'query' => $query, 'toolbarForm' => $form->createView(), 'multiUpdateForm' => $multiUpdateForm, ]); } /** * @Route(path="/{id}/edit", name="tags_edit", methods={"GET", "POST"}) * @Security("is_granted('manage_tag')") */ public function editAction(Tag $tag, TagRepository $repository, Request $request) { $editForm = $this->createForm(TagEditForm::class, $tag, [ 'action' => $this->generateUrl('tags_edit', ['id' => $tag->getId()]), 'method' => 'POST', ]); $editForm->handleRequest($request); if ($editForm->isSubmitted() && $editForm->isValid()) { try { $repository->saveTag($tag); $this->flashSuccess('action.update.success'); return $this->redirectToRoute('tags'); } catch (\Exception $ex) { $this->flashUpdateException($ex); } } return $this->render('tags/edit.html.twig', [ 'tag' => $tag, 'form' => $editForm->createView() ]); } /** * @Route(path="/create", name="tags_create", methods={"GET", "POST"}) * @Security("is_granted('manage_tag')") */ public function createAction(TagRepository $repository, Request $request) { $tag = new Tag(); $editForm = $this->createForm(TagEditForm::class, $tag, [ 'action' => $this->generateUrl('tags_create'), 'method' => 'POST', ]); $editForm->handleRequest($request); if ($editForm->isSubmitted() && $editForm->isValid()) { try { $repository->saveTag($tag); $this->flashSuccess('action.update.success'); return $this->redirectToRoute('tags'); } catch (\Exception $ex) { $this->flashUpdateException($ex); } } return $this->render('tags/edit.html.twig', [ 'tag' => $tag, 'form' => $editForm->createView() ]); } /** * @Route(path="/multi-delete", name="tags_multi_delete", methods={"POST"}) * @Security("is_granted('delete_tag')") */ public function multiDelete(TagRepository $repository, Request $request) { $form = $this->getMultiUpdateForm($repository); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { try { /** @var MultiUpdateTableDTO $dto */ $dto = $form->getData(); $repository->multiDelete($dto->getEntities()); $this->flashSuccess('action.delete.success'); } catch (\Exception $ex) { $this->flashDeleteException($ex); } } return $this->redirectToRoute('tags'); } protected function getMultiUpdateForm(TagRepository $repository): ?FormInterface { $dto = new MultiUpdateTableDTO(); if ($this->isGranted('delete_tag')) { $dto->addDelete($this->generateUrl('tags_multi_delete')); } if (!$dto->hasAction()) { return null; } return $this->createForm(MultiUpdateTable::class, $dto, [ 'action' => $this->generateUrl('tags'), 'repository' => $repository, 'method' => 'POST', ]); } /** * @param TagQuery $query * @return \Symfony\Component\Form\FormInterface */ protected function getToolbarForm(TagQuery $query) { return $this->createForm(TagToolbarForm::class, $query, [ 'action' => $this->generateUrl('tags', [ 'page' => $query->getPage(), ]), 'method' => 'GET', ]); } }