/** * @dataProvider setNameProvider */ public function testSetName($sourceName, $expected) { // Arrange $a = new NodeType(); $a->setName($sourceName); // Assert $this->assertEquals($expected, $a->getName()); }
/** * @param array $data * @param RZ\Roadiz\Core\Entities\NodeType $type * @param RZ\Roadiz\Core\Entities\Translation $translation * * @return RZ\Roadiz\Core\Entities\Node */ protected function createNode($data, NodeType $type, Translation $translation) { $node = new Node($type); $node->setNodeName($data['nodeName']); $this->getService('em')->persist($node); $sourceClass = "GeneratedNodeSources\\" . $type->getSourceEntityClassName(); $source = new $sourceClass($node, $translation); $source->setTitle($data['nodeName']); $this->getService('em')->persist($source); $this->getService('em')->flush(); return $node; }
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(); }
public function serializeToJsonProvider() { // Node type #1 $nt1 = new NodeType(); $nt1->setName('page type'); // Node type #2 $nt2 = new NodeType(); $nt2->setName('blog post'); $nt2->setDisplayName('Un blog post'); $ntf1 = new NodeTypeField(); $ntf1->setName('Title'); $ntf1->setType(NodeTypeField::MARKDOWN_T); $nt2->addField($ntf1); return array(array(new NodeType(), ROADIZ_ROOT . '/tests/Fixtures/Handlers/nodeTypeHandler01.json'), array($nt1, ROADIZ_ROOT . '/tests/Fixtures/Handlers/nodeTypeHandler02.json'), array($nt2, ROADIZ_ROOT . '/tests/Fixtures/Handlers/nodeTypeHandler03.json')); }
protected function getAvailableTypes($em, NodeType $currentType) { $qb = $em->createQueryBuilder(); $qb->select('n')->from('RZ\\Roadiz\\Core\\Entities\\NodeType', 'n')->where($qb->expr()->neq('n.id', $currentType->getId()))->orderBy('n.name', 'ASC'); try { $types = $qb->getQuery()->getResult(); $choices = []; foreach ($types as $type) { $choices[$type->getId()] = $type->getName(); } return $choices; } catch (\Doctrine\ORM\NoResultException $e) { return []; } }
/** * 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; }
/** * {@inheritdoc} */ protected function getRepositoryName(array $criteria = null) { if (isset($criteria['node.nodeType']) && $criteria['node.nodeType'] instanceof NodeType) { $rep = NodeType::getGeneratedEntitiesNamespace() . "\\" . $criteria['node.nodeType']->getSourceEntityClassName(); unset($criteria['node.nodeType']); } else { $rep = "RZ\\Roadiz\\Core\\Entities\\NodesSources"; } $this->repository = $rep; return $rep; }
protected function getXlsxResults(NodeType $nodetype, array $entities = []) { $fields = $nodetype->getFields(); $keys = []; $answers = []; $keys[] = "title"; foreach ($fields as $field) { if (!$field->isVirtual()) { $keys[] = $field->getName(); } } foreach ($entities as $idx => $nodesSource) { $array = []; foreach ($keys as $key) { $getter = 'get' . str_replace('_', '', ucwords($key)); $tmp = $nodesSource->{$getter}(); if (is_array($tmp)) { $tmp = implode(',', $tmp); } $array[] = $tmp; } $answers[$idx] = $array; } return XlsxExporter::exportXlsx($answers, $keys); }
/** * @param RZ\Roadiz\Core\Entities\NodeType $nodeType * * @return \Symfony\Component\Form\Form */ private function buildDeleteForm(NodeType $nodeType) { $builder = $this->createFormBuilder()->add('nodeTypeId', 'hidden', ['data' => $nodeType->getId(), 'constraints' => [new NotBlank()]]); return $builder->getForm(); }
/** * @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; }
/** * Get NodesSources class metadata. * * @return Doctrine\ORM\Mapping\ClassMetadata */ public static function getNodesSourcesMetadata() { $metadata = new ClassMetadata('RZ\\Roadiz\\Core\\Entities\\NodesSources'); try { /** * List node types */ $nodeTypes = Kernel::getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\NodeType')->findAll(); $map = []; foreach ($nodeTypes as $type) { $map[strtolower($type->getName())] = NodeType::getGeneratedEntitiesNamespace() . '\\' . $type->getSourceEntityClassName(); } $metadata->setDiscriminatorMap($map); return $metadata; } catch (\PDOException $e) { /* * Database tables don't exist yet * Need Install */ return null; } }
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; }
/** * Update current node-type using a new one. * * Update diff will update only non-critical fields such as : * * * description * * displayName * * It will only create absent node-type fields won't delete fields * not to lose any data. * * This method does not flush ORM. You'll need to manually call it. * * @param RZ\Roadiz\Core\Entities\NodeType $newNodeType * * @throws \RuntimeException If newNodeType param is null */ public function diff(NodeType $newNodeType) { if (null !== $newNodeType) { /* * options */ if ("" != $newNodeType->getDisplayName()) { $this->nodeType->setDisplayName($newNodeType->getDisplayName()); } if ("" != $newNodeType->getDescription()) { $this->nodeType->setDescription($newNodeType->getDescription()); } /* * make fields diff */ $existingFieldsNames = $this->nodeType->getFieldsNames(); foreach ($newNodeType->getFields() as $newField) { if (false === in_array($newField->getName(), $existingFieldsNames)) { /* * Field does not exist in type, * creating it. */ $newField->setNodeType($this->nodeType); Kernel::getService('em')->persist($newField); } } } else { throw new \RuntimeException("New node-type is null", 1); } }
private function executeCreation(InputInterface $input, OutputInterface $output) { $name = $input->getArgument('name'); $nt = new NodeType(); $nt->setName($name); $question0 = new Question('<question>Enter your node-type display name</question>: ', 'Neutral'); $displayName = $this->questionHelper->ask($input, $output, $question0); $nt->setDisplayName($displayName); $question1 = new Question('<question>Enter your node-type description</question>: ', ''); $description = $this->questionHelper->ask($input, $output, $question1); $nt->setDescription($description); $this->entityManager->persist($nt); $i = 1; while (true) { // Fields $field = new NodeTypeField(); $field->setPosition($i); $questionfName = new Question('<question>[Field ' . $i . '] Enter field name</question>: ', 'content'); $fName = $this->questionHelper->ask($input, $output, $questionfName); $field->setName($fName); $questionfLabel = new Question('<question>[Field ' . $i . '] Enter field label</question>: ', 'Your content'); $fLabel = $this->questionHelper->ask($input, $output, $questionfLabel); $field->setLabel($fLabel); $questionfType = new Question('<question>[Field ' . $i . '] Enter field type</question>: ', 'MARKDOWN_T'); $fType = $this->questionHelper->ask($input, $output, $questionfType); $fType = constant('RZ\\Roadiz\\Core\\Entities\\NodeTypeField::' . $fType); $field->setType($fType); $questionIndexed = new ConfirmationQuestion('<question>[Field ' . $i . '] Must field be indexed?</question>: ', false); if ($this->questionHelper->ask($input, $output, $questionIndexed)) { $field->setIndexed(true); } // Need to populate each side $nt->getFields()->add($field); $this->entityManager->persist($field); $field->setNodeType($nt); $questionAdd = new ConfirmationQuestion('<question>Do you want to add another field?</question>: ', true); if (!$this->questionHelper->ask($input, $output, $questionAdd)) { break; } $i++; } $this->entityManager->flush(); $nt->getHandler()->regenerateEntityClass(); $success = '<question>Node type ' . $nt->getName() . ' has been created.</question>' . PHP_EOL . '<info>Do not forget to update database schema!</info>'; return $success; }
/** * {@inheritDoc} * * @param string $string * @param RZ\Roadiz\Core\Entities\NodeType $type * * @return RZ\Roadiz\Core\Entities\NodeSource */ public function deserializeWithNodeType($string, NodeType $type) { $fields = $type->getFields(); /* * Create source default values */ $sourceDefaults = ["title", "meta_title", "meta_keywords", "meta_description"]; foreach ($fields as $field) { if (!$field->isVirtual()) { $sourceDefaults[] = $field->getName(); } } $encoder = new JsonEncoder(); $nameConverter = new CamelCaseToSnakeCaseNameConverter($sourceDefaults); $normalizer = new GetSetMethodNormalizer(null, $nameConverter); $serializer = new Serializer([$normalizer], [$encoder]); $node = $serializer->deserialize($string, NodeType::getGeneratedEntitiesNamespace() . '\\' . $type->getSourceEntityClassName(), 'json'); return $node; }