/** * @Route("/content-type/nature/reorder/{contentType}", name="nature.reorder")) */ public function reorderAction(ContentType $contentType, Request $request) { if ($contentType->getOrderField() == null || $contentType->getFieldType()->__get('ems_' . $contentType->getOrderField()) == null) { $this->addFlash('warning', 'This content type does not have any order field'); return $this->redirectToRoute('data.draft_in_progress', ['contentTypeId' => $contentType->getId()]); } if ($contentType->getFieldType()->__get('ems_' . $contentType->getOrderField())->getRestrictionOptions()['minimum_role'] != null && !$this->isGranted($contentType->getFieldType()->__get('ems_' . $contentType->getOrderField())->getRestrictionOptions()['minimum_role'])) { return $this->redirectToRoute('data.draft_in_progress', ['contentTypeId' => $contentType->getId()]); } $result = $this->getElasticsearch()->search(['index' => $contentType->getEnvironment()->getAlias(), 'type' => $contentType->getName(), 'size' => 400, 'body' => ['sort' => $contentType->getOrderField()]]); if ($result['hits']['total'] > $this::MAX_ELEM) { $this->addFlash('warning', 'This content type have to much elements to reorder them all in once'); } $data = []; $form = $this->createForm(ReorderType::class, $data, ['result' => $result]); $form->handleRequest($request); /** @var \AppBundle\Service\DataService $dataService*/ $dataService = $this->get('ems.service.data'); $counter = 1; if ($form->isSubmitted()) { foreach ($request->request->get('reorder')['items'] as $itemKey => $value) { try { $revision = $dataService->initNewDraft($contentType->getName(), $itemKey); $data = $revision->getRawData(); $data[$contentType->getOrderField()] = $counter++; $revision->setRawData($data); $dataService->finalizeDraft($revision); } catch (\Exception $e) { $this->addFlash('warning', 'It was impossible to update the item ' . $itemKey . ': ' . $e->getMessage()); } } $this->addFlash('notice', 'The ' . $contentType->getPluralName() . ' have been reordered'); return $this->redirectToRoute('data.draft_in_progress', ['contentTypeId' => $contentType->getId()]); } return $this->render('nature/reorder.html.twig', ['contentType' => $contentType, 'form' => $form->createView(), 'result' => $result]); }
/** * @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()]); }
/** * @Route("/api/{name}/merge/{ouuid}", defaults={"_format": "json"}) * @ParamConverter("contentType", options={"mapping": {"name": "name", "deleted": 0, "active": 1}}) * @Method({"POST"}) */ public function mergeAction($ouuid, ContentType $contentType, Request $request) { if (!$contentType->getEnvironment()->getManaged()) { throw new BadRequestHttpException('You can not create content for a managed content type'); } $rawdata = json_decode($request->getContent(), true); if (empty($rawdata)) { throw new BadRequestHttpException('Not a valid JSON message'); } try { $revision = $this->dataService()->getNewestRevision($contentType->getName(), $ouuid); $newDraft = $this->dataService()->replaceData($revision, $rawdata, "merge"); $isMerge = $revision->getId() != $newDraft->getId() ? true : false; } catch (\Exception $e) { if ($e instanceof NotFoundHttpException) { $this->addFlash('error', $e->getMessage()); } else { $this->addFlash('error', 'The revision ' . $ouuid . ' can not be replaced'); } $isMerge = false; } return $this->render('ajax/notification.json.twig', ['success' => $isMerge, 'revision_id' => $newDraft->getId()]); }