예제 #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;
 }
예제 #2
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]);
 }
예제 #3
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;
 }
예제 #4
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>');
        }
    }