public function generateMapping(ContentType $contentType, $withPipeline = false) { $out = [$contentType->getName() => ["_all" => ["store" => true, "enabled" => true], "properties" => []]]; if (null != $contentType->getFieldType()) { $out[$contentType->getName()]['properties'] = $this->fieldTypeType->generateMapping($contentType->getFieldType(), $withPipeline); } return $out; }
/** * @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]); }
public function __toString() { $out = 'New instance'; if ($this->ouuid) { $out = $this->ouuid; } if ($this->contentType) { $out = $this->contentType->getName() . ':' . $out; if (!empty($this->id)) { $out .= '#' . $this->id; } } if ($this->contentType && $this->contentType->getLabelField() && $this->rawData && isset($this->rawData[$this->contentType->getLabelField()])) { return $this->rawData[$this->contentType->getLabelField()] . " ({$out})"; } return $out; }
/** * @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()]); }
/** * Migrate a content type from its default index * * @param integer $id * @param Request $request * @Method({"POST"}) * @Route("/content-type/migrate/{contentType}", name="contenttype.migrate")) */ public function migrateAction(ContentType $contentType, Request $request) { return $this->startJob('ems.contenttype.migrate', ['contentTypeName' => $contentType->getName()]); }
public function updateMapping(ContentType $contentType, $envs = false) { $contentType->setHavePipelines(FALSE); try { if (!empty($contentType->getFieldType())) { $pipelines = []; /**@var \AppBundle\Entity\FieldType $child */ foreach ($contentType->getFieldType()->getChildren() as $child) { if (!$child->getDeleted()) { /**@var \AppBundle\Form\DataField\DataFieldType $dataFieldType */ $dataFieldType = $this->formRegistry->getType($child->getType())->getInnerType(); $pipeline = $dataFieldType->generatePipeline($child); if ($pipeline) { $pipelines[] = $pipeline; } } } if (!empty($pipelines)) { $body = ["description" => "Extract attachment information for the content type " . $contentType->getName(), "processors" => $pipelines]; $this->client->ingest()->putPipeline(['id' => $this->instanceId . $contentType->getName(), 'body' => $body]); $contentType->setHavePipelines(TRUE); $this->session->getFlashBag()->add('notice', 'Pipelines updated/created for ' . $contentType->getName()); } } } catch (BadRequest400Exception $e) { $contentType->setHavePipelines(false); $message = json_decode($e->getPrevious()->getMessage(), true); $this->session->getFlashBag()->add('error', '<p><strong>We was not able to generate pipelines, they are disabled</strong> Please consider to update your elasticsearch cluster (>=5.0) and/or install the ingest attachment plugin (bin/elasticsearch-plugin install ingest-attachment)</p> <p>Message from Elasticsearch: <b>' . $message['error']['type'] . '</b>' . $message['error']['reason'] . '</p>'); } try { if (!$envs) { $envs = array_reduce($this->environmentService->getManagedEnvironement(), function ($envs, $item) { /**@var \AppBundle\Entity\Environment $item*/ if (isset($envs)) { $envs .= ',' . $item->getAlias(); } else { $envs = $item->getAlias(); } return $envs; }); } $body = $this->mappingService->generateMapping($contentType, $contentType->getHavePipelines()); $out = $this->client->indices()->putMapping(['index' => $envs, 'type' => $contentType->getName(), 'body' => $body]); if (isset($out['acknowledged']) && $out['acknowledged']) { $contentType->setDirty(false); if ($this->session->isStarted()) { $this->session->getFlashBag()->add('notice', 'Mappings successfully updated/created for ' . $contentType->getName() . ' in ' . $envs); } } else { $contentType->setDirty(true); $this->session->getFlashBag()->add('warning', '<p><strong>Something went wrong. Try again</strong></p> <p>Message from Elasticsearch: ' . print_r($out, true) . '</p>'); } $em = $this->doctrine->getManager(); $em->persist($contentType); $em->flush(); } catch (BadRequest400Exception $e) { $contentType->setDirty(true); $message = json_decode($e->getPrevious()->getMessage(), true); $this->session->getFlashBag()->add('error', '<p><strong>You should try to rebuild the indexes</strong></p> <p>Message from Elasticsearch: <b>' . $message['error']['type'] . '</b>' . $message['error']['reason'] . '</p>'); } }
public function getRevisionById($id, ContentType $type) { $em = $this->doctrine->getManager(); /** @var ContentTypeRepository $contentTypeRepo */ $contentTypeRepo = $em->getRepository('AppBundle:ContentType'); $contentTypes = $contentTypeRepo->findBy(['name' => $type->getName(), 'deleted' => false]); if (count($contentTypes) != 1) { throw new NotFoundHttpException('Unknown content type'); } $contentType = $contentTypes[0]; /** @var RevisionRepository $repository */ $repository = $em->getRepository('AppBundle:Revision'); /** @var Revision $revision */ $revisions = $repository->findBy(['id' => $id, 'endTime' => null, 'contentType' => $contentType, 'deleted' => false]); if (count($revisions) != 1 || null != $revisions[0]->getEndTime()) { throw new NotFoundHttpException('Unknown revision'); } $revision = $revisions[0]; return $revision; }
/** * @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()]); }