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(); }
/** * 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); }
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; }
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; }
/** * Create a new node-source for given translation. * * @param array $data * @param RZ\Roadiz\Core\Entities\Node $node * * @return void */ protected function translateNode($data, Node $node) { $newTranslation = $this->getService('em')->find('RZ\\Roadiz\\Core\\Entities\\Translation', (int) $data['translationId']); $baseSource = $node->getNodeSources()->first(); $source = clone $baseSource; foreach ($source->getDocumentsByFields() as $document) { $this->getService('em')->persist($document); } $source->setTranslation($newTranslation); $source->setNode($node); $this->getService('em')->persist($source); $this->getService('em')->flush(); /* * Dispatch event */ $event = new FilterNodesSourcesEvent($source); $this->getService('dispatcher')->dispatch(NodesSourcesEvents::NODE_SOURCE_CREATED, $event); }