From 46560da67d45e0bf0fed7bd714e86503fac4ca3f Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Fri, 3 Sep 2021 12:20:34 +0200 Subject: [PATCH] start of controller and formtype: adaptation from DocumentPersonController --- .../DocumentAccompanyingCourseController.php | 229 ++++++++++++++++++ .../Entity/AccompanyingCourseDocument.php | 4 +- .../Form/AccompanyingCourseDocumentType.php | 96 ++++++++ 3 files changed, 327 insertions(+), 2 deletions(-) create mode 100644 src/Bundle/ChillDocStoreBundle/Controller/DocumentAccompanyingCourseController.php create mode 100644 src/Bundle/ChillDocStoreBundle/Form/AccompanyingCourseDocumentType.php diff --git a/src/Bundle/ChillDocStoreBundle/Controller/DocumentAccompanyingCourseController.php b/src/Bundle/ChillDocStoreBundle/Controller/DocumentAccompanyingCourseController.php new file mode 100644 index 000000000..9905c363e --- /dev/null +++ b/src/Bundle/ChillDocStoreBundle/Controller/DocumentAccompanyingCourseController.php @@ -0,0 +1,229 @@ +translator = $translator; + $this->eventDispatcher = $eventDispatcher; + $this->authorizationHelper = $authorizationHelper; + } + + /** + * @Route("/", name="accompanying_course_document_index", methods="GET") + */ + public function index(AccompanyingPeriod $course): Response + { + $em = $this->getDoctrine()->getManager(); + + if ($course === NULL) { + throw $this->createNotFoundException('Accompanying period not found'); + } + + $this->denyAccessUnlessGranted(AccompanyingPeriodVoter::SEE, $course); + + // $reachableScopes = $this->authorizationHelper + // ->getReachableScopes( + // $this->getUser(), new Role(PersonDocumentVoter::SEE), + // $person->getCenter()); + + $documents = $em + ->getRepository("ChillDocStoreBundle:AccompanyingCourseDocument") + ->findBy( + ['course' => $course], + ['date' => 'DESC'] + ); + + // $event = new PrivacyEvent($course, [ + // 'element_class' => AccompanyingCourseDocument::class, + // 'action' => 'index' + // ]); + // $this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event); + + return $this->render( + 'ChillDocStoreBundle:AccompanyingCourseDocument:index.html.twig', + [ + 'documents' => $documents, + 'course' => $course + ]); + } + + /** + * @Route("/new", name="accompanying_course_document_new", methods="GET|POST") + */ + public function new(Request $request, AccompanyingPeriod $course): Response + { + if ($course === NULL) { + throw $this->createNotFoundException('Accompanying period not found'); + } + + $this->denyAccessUnlessGranted(AccompanyingPeriodVoter::SEE, $course); + + $document = new AccompanyingCourseDocument(); + $document->setUser($this->getUser()); + $document->setCourse($course); + $document->setDate(new \DateTime('Now')); + + $form = $this->createForm(AccompanyingCourseDocumentType::class, $document); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $this->denyAccessUnlessGranted( + 'CHILL_ACCOMPANYING_COURSE_DOCUMENT_CREATE', $document, + 'creation of this activity not allowed'); + + $em = $this->getDoctrine()->getManager(); + $em->persist($document); + $em->flush(); + + $this->addFlash('success', $this->translator->trans("The document is successfully registered")); + + return $this->redirectToRoute('accompanying_course_document_index', ['course' => $course->getId()]); + } elseif ($form->isSubmitted() and !$form->isValid()) { + $this->addFlash('error', $this->translator->trans("This form contains errors")); + } + + return $this->render('ChillDocStoreBundle:AccompanyingCourseDocument:new.html.twig', [ + 'document' => $document, + 'form' => $form->createView(), + 'course' => $course, + ]); + } + + /** + * @Route("/{id}", name="accompanying_course_document_show", methods="GET") + */ + public function show(AccompanyingPeriod $course, AccompanyingCourseDocument $document): Response + { + $this->denyAccessUnlessGranted('CHILL_PERSON_ACCOMPANYING_PERIOD_SEE', $course); + $this->denyAccessUnlessGranted('CHILL_ACCOMPANYING_COURSE_DOCUMENT_SEE', $document); + + // $event = new PrivacyEvent($person, array( + // 'element_class' => PersonDocument::class, + // 'element_id' => $document->getId(), + // 'action' => 'show' + // )); + // $this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event); + + return $this->render( + 'ChillDocStoreBundle:AccompanyingCourseDocument:show.html.twig', + ['document' => $document, 'course' => $course]); + } + + /** + * @Route("/{id}/edit", name="accompanying_course_document_edit", methods="GET|POST") + */ + public function edit(Request $request, AccompanyingPeriod $course, AccompanyingCourseDocument $document): Response + { + $this->denyAccessUnlessGranted('CHILL_PERSON_ACCOMPANYING_PERIOD_SEE', $course); + $this->denyAccessUnlessGranted('CHILL_PERSON_DOCUMENT_UPDATE', $document); + + $document->setUser($this->getUser()); + $document->setDate(new \DateTime('Now')); + + $form = $this->createForm( + AccompanyingCourseDocumentType::class, $document); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $this->getDoctrine()->getManager()->flush(); + + $this->addFlash('success', $this->translator->trans("The document is successfully updated")); + + // $event = new PrivacyEvent($person, array( + // 'element_class' => PersonDocument::class, + // 'element_id' => $document->getId(), + // 'action' => 'update' + // )); + // $this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event); + + return $this->redirectToRoute( + 'accompanying_course_document_edit', + ['id' => $document->getId(), 'course' => $course->getId()]); + + } elseif ($form->isSubmitted() and !$form->isValid()) { + $this->addFlash('error', $this->translator->trans("This form contains errors")); + } + + // $event = new PrivacyEvent($person, array( + // 'element_class' => PersonDocument::class, + // 'element_id' => $document->getId(), + // 'action' => 'edit' + // )); + // $this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event); + + return $this->render( + 'ChillDocStoreBundle:AccompanyingCourseDocument:edit.html.twig', + [ + 'document' => $document, + 'form' => $form->createView(), + 'person' => $course, + ]); + } + + /** + * @Route("/{id}", name="accompanying_course_document_delete", methods="DELETE") + */ + public function delete(Request $request, AccompanyingPeriod $course, AccompanyingCourseDocument $document): Response + { + $this->denyAccessUnlessGranted('CHILL_PERSON_ACCOMPANYING_PERIOD_SEE', $course); + $this->denyAccessUnlessGranted('CHILL_PERSON_DOCUMENT_DELETE', $document); + + if ($this->isCsrfTokenValid('delete'.$document->getId(), $request->request->get('_token'))) { + $em = $this->getDoctrine()->getManager(); + $em->remove($document); + $em->flush(); + } + + return $this->redirectToRoute( + 'accompanying_course_document_index', ['course' => $course->getId()]); + } +} diff --git a/src/Bundle/ChillDocStoreBundle/Entity/AccompanyingCourseDocument.php b/src/Bundle/ChillDocStoreBundle/Entity/AccompanyingCourseDocument.php index d83b3e8e0..beadbc27d 100644 --- a/src/Bundle/ChillDocStoreBundle/Entity/AccompanyingCourseDocument.php +++ b/src/Bundle/ChillDocStoreBundle/Entity/AccompanyingCourseDocument.php @@ -2,7 +2,7 @@ namespace App\Entity; -use App\Repository\AccompanyingCourseDocumentRepository; +use Chill\DocStoreBundle\Entity\Document; use Chill\PersonBundle\Entity\AccompanyingPeriod; use Doctrine\ORM\Mapping as ORM; @@ -10,7 +10,7 @@ use Doctrine\ORM\Mapping as ORM; * @ORM\Entity(repositoryClass=AccompanyingCourseDocumentRepository::class) * @ORM\Table("chill_doc.accompanyingcourse_document") */ -class AccompanyingCourseDocument +class AccompanyingCourseDocument extends Document { /** * @ORM\Id diff --git a/src/Bundle/ChillDocStoreBundle/Form/AccompanyingCourseDocumentType.php b/src/Bundle/ChillDocStoreBundle/Form/AccompanyingCourseDocumentType.php new file mode 100644 index 000000000..c24d7a910 --- /dev/null +++ b/src/Bundle/ChillDocStoreBundle/Form/AccompanyingCourseDocumentType.php @@ -0,0 +1,96 @@ +translatableStringHelper = $translatableStringHelper; + } + + + public function buildForm(FormBuilderInterface $builder, array $options) + { + $builder + ->add('title', TextType::class) + ->add('description', ChillTextareaType::class, [ + 'required' => false + ]) + ->add('object', StoredObjectType::class, [ + 'error_bubbling' => true + ]) + ->add('date', ChillDateType::class) + ->add('category', EntityType::class, array( + 'placeholder' => 'Choose a document category', + 'class' => 'ChillDocStoreBundle:DocumentCategory', + 'query_builder' => function (EntityRepository $er) { + return $er->createQueryBuilder('c') + ->where('c.documentClass = :docClass') + ->setParameter('docClass', AccompanyingCourseDocument::class); + }, + 'choice_label' => function ($entity = null) { + return $entity ? $this->translatableStringHelper->localize($entity->getName()) : ''; + }, + )) + ; + + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'data_class' => Document::class, + ]); + + // $resolver->setRequired(['role', 'center']) + // ->setAllowedTypes('role', [ \Symfony\Component\Security\Core\Role\Role::class ]) + // ->setAllowedTypes('center', [ \Chill\MainBundle\Entity\Center::class ]) + // ; + } +}