コード例 #1
0
ファイル: NodeTest.php プロジェクト: QuangDang212/roadiz
 /**
  * @dataProvider nodeNameProvider
  */
 public function testNodeName($nodeName, $expected)
 {
     // Arrange
     $a = new Node();
     // Act
     $a->setNodeName($nodeName);
     // Assert
     $this->assertEquals($expected, $a->getNodeName());
 }
コード例 #2
0
 protected function doTranstype(Node $node, NodeType $nodeType)
 {
     /*
      * Get an association between old fields and new fields
      * to find data that can be transfered during transtyping.
      */
     $fieldAssociations = [];
     $oldFields = $node->getNodeType()->getFields();
     $er = $this->getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\NodeTypeField');
     foreach ($oldFields as $oldField) {
         $matchingField = $er->findOneBy(['nodeType' => $nodeType, 'name' => $oldField->getName(), 'type' => $oldField->getType()]);
         if (null !== $matchingField) {
             $fieldAssociations[] = [$oldField, $matchingField];
         }
     }
     foreach ($node->getNodeSources() as $existingSource) {
         $sourceClass = "GeneratedNodeSources\\" . $nodeType->getSourceEntityClassName();
         $source = new $sourceClass($node, $existingSource->getTranslation());
         $source->setTitle($existingSource->getTitle());
         foreach ($fieldAssociations as $fields) {
             if (!$fields[0]->isVirtual()) {
                 /*
                  * Copy simple data from source to another
                  */
                 $setter = $fields[0]->getSetterName();
                 $getter = $fields[0]->getGetterName();
                 $source->{$setter}($existingSource->{$getter}());
             } elseif ($fields[0]->getType() === NodeTypeField::DOCUMENTS_T) {
                 /*
                  * Copy documents.
                  */
                 $documents = $this->getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\Document')->findByNodeSourceAndField($existingSource, $fields[0]);
                 foreach ($documents as $document) {
                     $nsDoc = new NodesSourcesDocuments($source, $document, $fields[1]);
                     $source->getDocumentsByFields()->add($nsDoc);
                 }
             }
         }
         // First plan old source deletion.
         $this->getService('em')->remove($existingSource);
         $this->getService('em')->flush();
         $this->getService('em')->persist($source);
         foreach ($source->getDocumentsByFields() as $nsDoc) {
             $this->getService('em')->persist($nsDoc);
         }
         $this->getService('em')->flush();
     }
     $node->setNodeType($nodeType);
     $this->getService('em')->flush();
 }
コード例 #3
0
ファイル: NodeHandlerTest.php プロジェクト: bryanbanda/roadiz
 public static function duplicateProvider()
 {
     $nodeType = Kernel::getService("em")->getRepository('RZ\\Roadiz\\Core\\Entities\\NodeType')->findOneByName('Page');
     $tran = Kernel::getService("em")->getRepository('RZ\\Roadiz\\Core\\Entities\\Translation')->findDefault();
     if (null !== $nodeType && null !== $tran) {
         $node = new Node($nodeType);
         $node->setNodeName("testNode" . uniqid());
         $node->setPublished(true);
         Kernel::getService("em")->persist($node);
         $src = new NSPage($node, $tran);
         $src->setTitle("testNode base");
         $src->setContent("This is TESTNODE!");
         Kernel::getService("em")->persist($src);
         Kernel::getService("em")->flush();
         return array(array($node));
     } else {
         return array();
     }
 }
コード例 #4
0
 /**
  * Generate a node with a unique name.
  *
  * @param  NodeType    $nodeType
  * @param  Translation $translation
  * @param  Node|null   $parent
  * @param  Tag|null    $tag
  * @param  boolean     $pushToTop
  *
  * @return RZ\Roadiz\Core\Entities\NodesSources
  */
 public function generate(NodeType $nodeType, Translation $translation, Node $parent = null, Tag $tag = null, $pushToTop = false)
 {
     $name = $nodeType->getDisplayName() . " " . uniqid();
     $node = new Node($nodeType);
     $node->setParent($parent);
     $node->setNodeName($name);
     if (null !== $tag) {
         $node->addTag($tag);
     }
     $this->entityManager->persist($node);
     if ($pushToTop) {
         $node->setPosition(0.5);
     }
     $sourceClass = "GeneratedNodeSources\\" . $nodeType->getSourceEntityClassName();
     $source = new $sourceClass($node, $translation);
     $source->setTitle($name);
     $this->entityManager->persist($source);
     $this->entityManager->flush();
     return $source;
 }
コード例 #5
0
 /**
  * @param RZ\Roadiz\Core\Entities\Node $node
  * @param RZ\Roadiz\Core\Entities\Tag  $tag
  *
  * @return \Symfony\Component\Form\Form
  */
 protected function buildRemoveTagForm(Node $node, Tag $tag)
 {
     $builder = $this->createFormBuilder()->add('nodeId', 'hidden', ['data' => $node->getId(), 'constraints' => [new NotBlank()]])->add('tagId', 'hidden', ['data' => $tag->getId(), 'constraints' => [new NotBlank()]]);
     return $builder->getForm();
 }
コード例 #6
0
 /**
  * Return a 404 Response or TRUE if node is viewable.
  *
  * @param  Node $node
  *
  * @return boolean|Symfony\Component\HttpFoundation\Response
  */
 public function validateAccessForNodeWithStatus(Node $node)
 {
     if (!$this->isGranted(Role::ROLE_BACKEND_USER) && !$node->isPublished()) {
         /*
          * Not allowed to see unpublished nodes
          */
         return $this->throw404();
     } elseif ($this->isGranted(Role::ROLE_BACKEND_USER) && $node->getStatus() > Node::PUBLISHED) {
         /*
          * Not allowed to see deleted and archived nodes
          * even for Admins
          */
         return $this->throw404();
     } else {
         return true;
     }
 }
コード例 #7
0
 /**
  * @param RZ\Roadiz\Core\Entities\Node         $node
  * @param RZ\Roadiz\Core\Entities\NodesSources $source
  *
  * @return \Symfony\Component\Form\Form
  */
 private function buildEditSourceForm(Node $node, NodesSources $source)
 {
     $fields = $node->getNodeType()->getFields();
     /*
      * Create source default values
      */
     $sourceDefaults = ['title' => $source->getTitle()];
     foreach ($fields as $field) {
         if (!$field->isVirtual()) {
             $getter = $field->getGetterName();
             if (method_exists($source, $getter)) {
                 $sourceDefaults[$field->getName()] = $source->{$getter}();
             } else {
                 throw new \Exception($getter . ' method does not exist in ' . $node->getNodeType()->getName());
             }
         }
     }
     /*
      * Create subform for source
      */
     $sourceBuilder = $this->getService('formFactory')->createNamedBuilder('source', 'form', $sourceDefaults)->add('title', 'text', ['label' => $this->getTranslator()->trans('title'), 'required' => false, 'attr' => ['data-desc' => '', 'data-dev-name' => '{{ nodeSource.' . StringHandler::camelCase('title') . ' }}']]);
     foreach ($fields as $field) {
         $sourceBuilder->add($field->getName(), $this->getFormTypeFromFieldType($source, $field, $this), $this->getFormOptionsFromFieldType($field));
     }
     return $sourceBuilder->getForm();
 }
コード例 #8
0
 protected function getListManager(Node $parent = null)
 {
     $criteria = ['parent' => $parent, 'translation' => $this->translation, 'status' => ['<=', Node::PUBLISHED]];
     if (null !== $this->tag) {
         $criteria['tags'] = $this->tag;
     }
     $ordering = ['position' => 'ASC'];
     if (null !== $parent && $parent->getChildrenOrder() !== 'order' && $parent->getChildrenOrder() !== 'position') {
         $ordering = [$parent->getChildrenOrder() => $parent->getChildrenOrderDirection()];
         $this->canReorder = false;
     }
     /*
      * Manage get request to filter list
      */
     $listManager = $this->controller->createEntityListManager('RZ\\Roadiz\\Core\\Entities\\Node', $criteria, $ordering);
     if (true === $this->stackTree) {
         $listManager->setItemPerPage(20);
         $listManager->handle();
     } else {
         $listManager->setItemPerPage(100);
         $listManager->handle(true);
     }
     return $listManager;
 }
コード例 #9
0
ファイル: NodesCommand.php プロジェクト: justinpocta/roadiz
 /**
  * @param InputInterface  $input
  * @param OutputInterface $output
  * @param NodeType        $type
  * @param Translation     $translation
  *
  * @return string
  */
 private function executeNodeCreation(InputInterface $input, OutputInterface $output, NodeType $type, Translation $translation)
 {
     $nodeName = $input->getArgument('node-name');
     $node = new Node($type);
     $node->setNodeName($nodeName);
     $this->entityManager->persist($node);
     // Source
     $sourceClass = "GeneratedNodeSources\\" . $type->getSourceEntityClassName();
     $source = new $sourceClass($node, $translation);
     $fields = $type->getFields();
     foreach ($fields as $field) {
         if (!$field->isVirtual()) {
             $question = new Question('<question>[Field ' . $field->getLabel() . ']</question> : ', null);
             $fValue = $this->questionHelper->ask($input, $output, $question);
             $setterName = $field->getSetterName();
             $source->{$setterName}($fValue);
         }
     }
     $this->entityManager->persist($source);
     $this->entityManager->flush();
     $text = '<info>Node “' . $nodeName . '” created…</info>' . PHP_EOL;
     return $text;
 }
コード例 #10
0
 public static function getUrlProvider()
 {
     $sources = array();
     /*
      * Test 1 - regular node
      */
     $n1 = new Node();
     $n1->setNodeName('page');
     $t1 = new Translation();
     $t1->setLocale('fr');
     $t1->setDefaultTranslation(true);
     $t1->setAvailable(true);
     $ns1 = new NodesSources($n1, $t1);
     $sources[] = array($ns1, '/page');
     /*
      * Test 2  - regular node
      */
     $n2 = new Node();
     $n2->setNodeName('page');
     $t2 = new Translation();
     $t2->setLocale('en');
     $t2->setDefaultTranslation(false);
     $t2->setAvailable(true);
     $ns2 = new NodesSources($n2, $t2);
     $sources[] = array($ns2, '/en/page');
     /*
      * Test 3 - home node
      */
     $n3 = new Node();
     $n3->setNodeName('page');
     $n3->setHome(true);
     $t3 = new Translation();
     $t3->setLocale('fr');
     $t3->setDefaultTranslation(true);
     $t3->setAvailable(true);
     $ns3 = new NodesSources($n3, $t3);
     $sources[] = array($ns3, '/');
     /*
      * Test 4 - home node non-default
      */
     $n4 = new Node();
     $n4->setNodeName('page');
     $n4->setHome(true);
     $t4 = new Translation();
     $t4->setLocale('en');
     $t4->setDefaultTranslation(false);
     $t4->setAvailable(true);
     $ns4 = new NodesSources($n4, $t4);
     $sources[] = array($ns4, '/en');
     /*
      * Test 5  - regular node with alias
      */
     $n5 = new Node();
     $n5->setNodeName('page');
     $t5 = new Translation();
     $t5->setLocale('en');
     $t5->setDefaultTranslation(false);
     $t5->setAvailable(true);
     $ns5 = new NodesSources($n5, $t5);
     $a5 = new Urlalias($ns5);
     $a5->setAlias('tralala-en');
     $ns5->getUrlAliases()->add($a5);
     $sources[] = array($ns5, '/tralala-en');
     /*
      * Test 6  - regular node with 1 parent
      */
     $n6 = new Node();
     $n6->setNodeName('other-page');
     $t6 = new Translation();
     $t6->setLocale('en');
     $t6->setDefaultTranslation(true);
     $t6->setAvailable(true);
     $ns6 = new NodesSources($n6, $t6);
     $ns6->getHandler()->setParentNodeSource($ns1);
     $sources[] = array($ns6, '/page/other-page');
     /*
      * Test 7  - regular node with 2 parents
      */
     $n7 = new Node();
     $n7->setNodeName('sub-page');
     $t7 = new Translation();
     $t7->setLocale('en');
     $t7->setDefaultTranslation(true);
     $t7->setAvailable(true);
     $ns7 = new NodesSources($n7, $t7);
     $ns7->getHandler()->setParentNodeSource($ns6);
     $sources[] = array($ns7, '/page/other-page/sub-page');
     /*
      * Test 8  - regular node with 1 parent and 2 alias
      */
     $n8 = new Node();
     $n8->setNodeName('other-page-alias');
     $t8 = new Translation();
     $t8->setLocale('en');
     $t8->setDefaultTranslation(true);
     $t8->setAvailable(true);
     $ns8 = new NodesSources($n8, $t8);
     $a8 = new Urlalias($ns8);
     $a8->setAlias('other-tralala-en');
     $ns8->getUrlAliases()->add($a8);
     $ns8->getHandler()->setParentNodeSource($ns5);
     $sources[] = array($ns8, '/tralala-en/other-tralala-en');
     /*
      * Test 9 - hidden node
      */
     $n9 = new Node();
     $n9->setNodeName('pagehidden');
     $n9->setVisible(false);
     $t9 = new Translation();
     $t9->setLocale('fr');
     $t9->setDefaultTranslation(true);
     $t9->setAvailable(true);
     $ns9 = new NodesSources($n9, $t9);
     $sources[] = array($ns9, '/pagehidden');
     /*
      * Test 10 - regular node with hidden parent
      */
     $n10 = new Node();
     $n10->setNodeName('page-with-hidden-parent');
     $t10 = new Translation();
     $t10->setLocale('fr');
     $t10->setDefaultTranslation(true);
     $t10->setAvailable(true);
     $ns10 = new NodesSources($n10, $t10);
     $ns10->getHandler()->setParentNodeSource($ns9);
     $sources[] = array($ns10, '/page-with-hidden-parent');
     return $sources;
 }
コード例 #11
0
ファイル: NodeRepository.php プロジェクト: bryanbanda/roadiz
 /**
  * @param RZ\Roadiz\Core\Entities\Node $node
  *
  * @return array
  */
 public function findAllOffspringIdByNode(Node $node)
 {
     $theOffprings = [];
     $in = [$node->getId()];
     do {
         $theOffprings = array_merge($theOffprings, $in);
         $query = $this->_em->createQuery('
             SELECT n.id FROM RZ\\Roadiz\\Core\\Entities\\Node n
             WHERE n.parent IN (:tab)')->setParameter('tab', $in);
         $result = $query->getScalarResult();
         $in = [];
         //For memory optimizations
         foreach ($result as $item) {
             $in[] = (int) $item['id'];
         }
     } while (!empty($in));
     return $theOffprings;
 }
コード例 #12
0
 /**
  * @param RZ\Roadiz\Core\Entities\Node $node
  *
  * @return array
  */
 public function findAllParentsIdByNode(Node $node)
 {
     $theParents = [];
     $parent = $node->getParent();
     while (null !== $parent) {
         $theParents[] = $parent->getId();
         $parent = $parent->getParent();
     }
     return $theParents;
 }
コード例 #13
0
 protected function makeNodeRec($data)
 {
     $nodetype = $this->em->getRepository('RZ\\Roadiz\\Core\\Entities\\NodeType')->findOneByName($data["node_type"]);
     $node = new Node($nodetype);
     $node->setNodeName($data['node_name']);
     $node->setHome($data['home']);
     $node->setVisible($data['visible']);
     $node->setStatus($data['status']);
     $node->setLocked($data['locked']);
     $node->setPriority($data['priority']);
     $node->setHidingChildren($data['hiding_children']);
     $node->setArchived($data['archived']);
     $node->setSterile($data['sterile']);
     $node->setChildrenOrder($data['children_order']);
     $node->setChildrenOrderDirection($data['children_order_direction']);
     foreach ($data["nodes_sources"] as $source) {
         $trans = new Translation();
         $trans->setLocale($source['translation']);
         $trans->setName(Translation::$availableLocales[$source['translation']]);
         $namespace = NodeType::getGeneratedEntitiesNamespace();
         $classname = $nodetype->getSourceEntityClassName();
         $class = $namespace . "\\" . $classname;
         $nodeSource = new $class($node, $trans);
         $nodeSource->setTitle($source["title"]);
         $nodeSource->setMetaTitle($source["meta_title"]);
         $nodeSource->setMetaKeywords($source["meta_keywords"]);
         $nodeSource->setMetaDescription($source["meta_description"]);
         $fields = $nodetype->getFields();
         foreach ($fields as $field) {
             if (!$field->isVirtual() && isset($source[$field->getName()])) {
                 if ($field->getType() == NodeTypeField::DATETIME_T || $field->getType() == NodeTypeField::DATE_T) {
                     $date = new \DateTime($source[$field->getName()]['date'], new \DateTimeZone($source[$field->getName()]['timezone']));
                     $setter = $field->getSetterName();
                     $nodeSource->{$setter}($date);
                 } else {
                     $setter = $field->getSetterName();
                     $nodeSource->{$setter}($source[$field->getName()]);
                 }
             }
         }
         if (!empty($source['url_aliases'])) {
             foreach ($source['url_aliases'] as $url) {
                 $alias = new UrlAlias($nodeSource);
                 $alias->setAlias($url['alias']);
                 $nodeSource->addUrlAlias($alias);
             }
         }
         $node->getNodeSources()->add($nodeSource);
     }
     if (!empty($data['tags'])) {
         foreach ($data["tags"] as $tag) {
             $tmp = $this->em->getRepository('RZ\\Roadiz\\Core\\Entities\\Tag')->findOneBy(["tagName" => $tag]);
             $node->getTags()->add($tmp);
         }
     }
     if (!empty($data['children'])) {
         foreach ($data['children'] as $child) {
             $tmp = $this->makeNodeRec($child);
             $node->addChild($tmp);
         }
     }
     return $node;
 }
コード例 #14
0
ファイル: NodeHandler.php プロジェクト: justinpocta/roadiz
 private function duplicateRec(Node $node, $level)
 {
     $childrenArray = [];
     $sourceArray = [];
     $childs = new ArrayCollection($node->getChildren()->toArray());
     $node->getChildren()->clear();
     foreach ($childs as $child) {
         $childrenArray[] = $this->duplicateRec($child, $level + 1);
     }
     $nodeSources = new ArrayCollection($node->getNodeSources()->toArray());
     $node->getNodeSources()->clear();
     foreach ($nodeSources as $nodeSource) {
         $nodeSource->setNode(null);
         $tran = Kernel::getService('em')->merge($nodeSource->getTranslation());
         $nodeSource->setTranslation($tran);
         Kernel::getService('em')->persist($nodeSource);
         $nsdocs = $nodeSource->getDocumentsByFields();
         foreach ($nsdocs as $nsdoc) {
             $nsdoc->setNodeSource($nodeSource);
             $doc = Kernel::getService('em')->merge($nsdoc->getDocument());
             $nsdoc->setDocument($doc);
             $f = Kernel::getService('em')->merge($nsdoc->getField());
             $nsdoc->setField($f);
             Kernel::getService('em')->persist($nsdoc);
         }
         Kernel::getService('em')->flush();
         $sourceArray[] = $nodeSource;
     }
     $nodetype = Kernel::getService('em')->merge($node->getNodeType());
     $node->setNodeType($nodetype);
     $node->setParent(null);
     Kernel::getService('em')->persist($node);
     foreach ($childrenArray as $child) {
         $child->setParent($node);
     }
     foreach ($sourceArray as $source) {
         $source->setNode($node);
     }
     Kernel::getService('em')->flush();
     return $node;
 }
コード例 #15
0
 /**
  * @param RZ\Roadiz\Core\Entities\Node $node
  *
  * @return \Symfony\Component\Form\Form
  */
 private function buildAddUrlAliasForm(Node $node)
 {
     $defaults = ['nodeId' => $node->getId()];
     $builder = $this->createFormBuilder($defaults)->add('nodeId', 'hidden', ['data' => $node->getId(), 'constraints' => [new NotBlank()]])->add('alias', 'text', ['label' => 'urlAlias'])->add('translationId', new \RZ\Roadiz\CMS\Forms\TranslationsType(), ['label' => 'translation']);
     return $builder->getForm();
 }
コード例 #16
0
ファイル: UrlExtension.php プロジェクト: QuangDang212/roadiz
 /**
  * Get node url using its first source.
  *
  * @param  Node   $node
  * @param  array  $criteria
  * @return string
  */
 public function getNodeUrl(Node $node, array $criteria = [])
 {
     return $this->getNodesSourceUrl($node->getNodeSources()->first(), $criteria);
 }
コード例 #17
0
 /**
  * @param array $parameters
  * @param Node  $node
  */
 protected function updatePosition($parameters, Node $node)
 {
     /*
      * First, we set the new parent
      */
     $parent = null;
     if (!empty($parameters['newParent']) && $parameters['newParent'] > 0) {
         $parent = $this->getService('em')->find('RZ\\Roadiz\\Core\\Entities\\Node', (int) $parameters['newParent']);
         if ($parent !== null) {
             $node->setParent($parent);
         }
     } else {
         // if no parent or null we place node at root level
         $node->setParent(null);
     }
     /*
      * Then compute new position
      */
     if (!empty($parameters['nextNodeId']) && $parameters['nextNodeId'] > 0) {
         $nextNode = $this->getService('em')->find('RZ\\Roadiz\\Core\\Entities\\Node', (int) $parameters['nextNodeId']);
         if ($nextNode !== null) {
             $node->setPosition($nextNode->getPosition() - 0.5);
         }
     } elseif (!empty($parameters['prevNodeId']) && $parameters['prevNodeId'] > 0) {
         $prevNode = $this->getService('em')->find('RZ\\Roadiz\\Core\\Entities\\Node', (int) $parameters['prevNodeId']);
         if ($prevNode !== null) {
             $node->setPosition($prevNode->getPosition() + 0.5);
         }
     } elseif (!empty($parameters['firstPosition']) && (bool) $parameters['firstPosition'] === true) {
         $node->setPosition(-0.5);
     } elseif (!empty($parameters['lastPosition']) && (bool) $parameters['lastPosition'] === true) {
         $node->setPosition(9999999);
     }
     // Apply position update before cleaning
     $this->getService('em')->flush();
     if ($parent !== null) {
         $parent->getHandler()->cleanChildrenPositions();
     } else {
         NodeHandler::cleanRootNodesPositions();
     }
     /*
      * Dispatch event
      */
     $event = new FilterNodeEvent($node);
     $this->getService('dispatcher')->dispatch(NodeEvents::NODE_UPDATED, $event);
 }