Ejemplo n.º 1
0
 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;
 }
Ejemplo n.º 2
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->waitForGreen($output);
     /** @var EntityManager $em */
     $em = $this->doctrine->getManager();
     /** @var  Client $client */
     $client = $this->client;
     $name = $input->getArgument('name');
     /** @var JobRepository $envRepo */
     $envRepo = $em->getRepository('AppBundle:Environment');
     /** @var Environment $environment */
     $environment = $envRepo->findBy(['name' => $name, 'managed' => true]);
     if ($environment && count($environment) == 1) {
         $environment = $environment[0];
         $indexName = $environment->getAlias() . AppController::getFormatedTimestamp();
         /** @var \AppBundle\Repository\ContentTypeRepository $contentTypeRepository */
         $contentTypeRepository = $em->getRepository('AppBundle:ContentType');
         $contentTypes = $contentTypeRepository->findAll();
         /** @var ContentType $contentType */
         $client->indices()->create(['index' => $indexName, 'body' => ContentType::getIndexAnalysisConfiguration()]);
         $output->writeln('A new index ' . $indexName . ' has been created');
         $this->waitForGreen($output);
         // create a new progress bar
         $progress = new ProgressBar($output, count($contentTypes));
         // start and displays the progress bar
         $progress->start();
         $progressMessage = " creating content type's mappings in " . $indexName;
         /** @var ContentType $contentType */
         foreach ($contentTypes as $contentType) {
             if ($contentType->getEnvironment()->getManaged() && !$contentType->getDeleted()) {
                 $this->contentTypeService->updateMapping($contentType, $indexName);
             }
             $progress->advance();
             $output->write($progressMessage);
         }
         $progress->finish();
         $output->writeln($progressMessage);
         $this->flushFlash($output);
         $command = $this->getReindexCommand();
         $arguments = array('name' => $name, 'index' => $indexName);
         $reindexInput = new ArrayInput($arguments);
         $returnCode = $command->run($reindexInput, $output);
         if ($returnCode) {
             $output->writeln('Reindexed with return code: ' . $returnCode);
         }
         $this->waitForGreen($output);
         $this->switchAlias($environment->getAlias(), $indexName, true, $output);
         $output->writeln('The alias <info>' . $environment->getName() . '</info> is now pointing to ' . $indexName);
     } else {
         $output->writeln("WARNING: Environment named " . $name . " not found");
     }
     $this->flushFlash($output);
 }
Ejemplo n.º 3
0
 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;
 }
Ejemplo n.º 4
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()]);
 }
Ejemplo n.º 5
0
 /**
  * Export a content type in Json format
  *
  * @param integer $id        	
  * @param Request $request
  *        	@Route("/content-type/export/{contentType}.{_format}", defaults={"_format" = "json"}, name="contenttype.export"))
  */
 public function exportAction(ContentType $contentType, Request $request)
 {
     //Sanitize the CT
     $contentType->setCreated(NULL);
     $contentType->setModified(NULL);
     $contentType->getFieldType()->removeCircularReference();
     $contentType->setEnvironment(NULL);
     //$contentType->getTemplates()->clear();
     //$contentType->getViews()->clear();
     //Serialize the CT
     $encoders = array(new JsonEncoder());
     $normalizers = array(new JsonNormalizer());
     $serializer = new Serializer($normalizers, $encoders);
     $jsonContent = $serializer->serialize($contentType, 'json');
     $response = new Response($jsonContent);
     $diposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $contentType->getName() . '.json');
     $response->headers->set('Content-Disposition', $diposition);
     return $response;
 }
Ejemplo n.º 6
0
 /**
  * Add a new environement
  *
  * @param Request $request
  *        	@Route("/environment/add", name="environment.add"))
  */
 public function addAction(Request $request)
 {
     $environment = new Environment();
     $form = $this->createFormBuilder($environment)->add('name', IconTextType::class, ['icon' => 'fa fa-database', 'required' => false])->add('color', ColorPickerType::class, ['required' => false])->add('save', SubmitEmsType::class, ['label' => 'Create', 'icon' => 'fa fa-plus', 'attr' => ['class' => 'btn btn-primary pull-right']])->getForm();
     $form->handleRequest($request);
     if ($form->isSubmitted() && $form->isValid()) {
         /** @var Environment $environment */
         $environment = $form->getData();
         /** @var EntityManager $em */
         $em = $this->getDoctrine()->getManager();
         $environmetRepository = $em->getRepository('AppBundle:Environment');
         $anotherObject = $environmetRepository->findBy(['name' => $environment->getName()]);
         if (count($anotherObject) != 0) {
             //TODO: test name format
             $form->get('name')->addError(new FormError('Another environment named ' . $environment->getName() . ' already exists'));
         } else {
             $environment->setAlias($this->getParameter('instance_id') . $environment->getName());
             $environment->setManaged(true);
             $em = $this->getDoctrine()->getManager();
             $em->persist($environment);
             $em->flush();
             $indexName = $environment->getAlias() . AppController::getFormatedTimestamp();
             $this->getElasticsearch()->indices()->create(['index' => $indexName, 'body' => ContentType::getIndexAnalysisConfiguration()]);
             foreach ($this->getContentTypeService()->getAll() as $contentType) {
                 $this->getContentTypeService()->updateMapping($contentType, $indexName);
             }
             $this->getElasticsearch()->indices()->putAlias(['index' => $indexName, 'name' => $environment->getAlias()]);
             $this->addFlash('notice', 'A new environement ' . $environment->getName() . ' has been created');
             return $this->redirectToRoute('environment.index');
         }
     }
     return $this->render('environment/add.html.twig', ['form' => $form->createView()]);
 }
Ejemplo n.º 7
0
    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>');
        }
    }
Ejemplo n.º 8
0
 /**
  * @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]);
 }
Ejemplo n.º 9
0
 public function deleteRevisions(ContentType $contentType = null)
 {
     if ($contentType == null) {
         $qb = $this->createQueryBuilder('r')->update()->set('r.delete', 1);
         return $qb->getQuery()->execute();
     } else {
         $qb = $this->createQueryBuilder('r')->update()->set('r.delete', 1)->where('r.contentTypeId = ?1')->setParameter(1, $contentType->getId());
         return $qb->getQuery()->execute();
     }
 }
Ejemplo n.º 10
0
 private function createContentType($name, $pluralName, $icon, $description, $indexTwig, $extra, $color, $orderKey, $rootContentType, $active, $environment, $labelField, $locationField)
 {
     $contentType = new ContentType();
     $contentType->setName($name);
     $contentType->setPluralName($pluralName);
     $contentType->setIcon($icon);
     $contentType->setDescription($description);
     $contentType->setIndexTwig($indexTwig);
     $contentType->setExtra($extra);
     $contentType->setColor($color);
     $contentType->setOrderKey($orderKey);
     $contentType->setRootContentType($rootContentType);
     $contentType->setActive($active);
     $contentType->setEnvironment($this->getReference($environment));
     $contentType->setLabelField($labelField);
     $contentType->setLocationField($locationField);
     return $contentType;
 }
Ejemplo n.º 11
0
 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;
 }
Ejemplo n.º 12
0
 /**
  * @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()]);
 }