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); }
/** * 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); } }
/** * @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; }
/** * {@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; }