Ejemplo n.º 1
0
 /**
  * @throws HasNotCircleException
  * @Route("/data/add/{contentType}", name="data.add"))
  */
 public function addAction(ContentType $contentType, Request $request)
 {
     $userCircles = $this->getUser()->getCircles();
     $environment = $contentType->getEnvironment();
     $environmentCircles = $environment->getCircles();
     if (!empty($environmentCircles)) {
         if (empty($userCircles)) {
             throw new HasNotCircleException($environment);
         }
         $found = false;
         foreach ($userCircles as $userCircle) {
             if (in_array($userCircle, $environmentCircles)) {
                 $found = true;
                 break;
             }
         }
         if (!$found) {
             throw new HasNotCircleException($environment);
         }
     }
     $em = $this->getDoctrine()->getManager();
     $repository = $em->getRepository('AppBundle:ContentType');
     $revision = new Revision();
     $form = $this->createFormBuilder($revision)->add('ouuid', IconTextType::class, ['attr' => ['class' => 'form-control', 'placeholder' => 'Auto-generated if left empty'], 'required' => false])->add('save', SubmitType::class, ['label' => 'Create ' . $contentType->getName() . ' draft', 'attr' => ['class' => 'btn btn-primary pull-right']])->getForm();
     $form->handleRequest($request);
     if ($form->isSubmitted() && $form->isValid() || !$contentType->getAskForOuuid()) {
         /** @var Revision $revision */
         $revision = $form->getData();
         if (!$this->get('security.authorization_checker')->isGranted($contentType->getCreateRole())) {
             throw new PrivilegeException($revision);
         }
         if (null != $revision->getOuuid()) {
             $revisionRepository = $em->getRepository('AppBundle:Revision');
             $anotherObject = $revisionRepository->findBy(['contentType' => $contentType, 'ouuid' => $revision->getOuuid(), 'endTime' => null]);
             if (count($anotherObject) != 0) {
                 $form->get('ouuid')->addError(new FormError('Another ' . $contentType->getName() . ' with this identifier already exists'));
                 // 					$form->addError(new FormError('Another '.$contentType->getName().' with this identifier already exists'));
             }
         }
         if ($form->isValid() || !$contentType->getAskForOuuid()) {
             $now = new \DateTime('now');
             $revision->setContentType($contentType);
             $revision->setDraft(true);
             $revision->setDeleted(false);
             $revision->setStartTime($now);
             $revision->setEndTime(null);
             $revision->setLockBy($this->getUser()->getUsername());
             $revision->setLockUntil(new \DateTime($this->getParameter('lock_time')));
             $em->persist($revision);
             $em->flush();
             return $this->redirectToRoute('revision.edit', ['revisionId' => $revision->getId()]);
         }
     }
     return $this->render('data/add.html.twig', ['contentType' => $contentType, 'form' => $form->createView()]);
 }