protected function makeTagRec($data) { $tag = new Tag(); $tag->setTagName($data['tag_name']); $tag->setVisible($data['visible']); $tag->setLocked($data['locked']); foreach ($data["tag_translation"] as $source) { $trans = new Translation(); $trans->setLocale($source['translation']); $trans->setName(Translation::$availableLocales[$source['translation']]); $tagSource = new TagTranslation($tag, $trans); $tagSource->setName($source["title"]); $tagSource->setDescription($source["description"]); $tag->getTranslatedTags()->add($tagSource); } foreach ($data['children'] as $child) { $tmp = $this->makeTagRec($child); $tag->addChild($tmp); } return $tag; }
public function addChildAction(Request $request, $parentTagId) { $this->validateAccessForRole('ROLE_ACCESS_TAGS'); $translation = $this->getService('defaultTranslation'); $parentTag = $this->getService('em')->find('RZ\\Roadiz\\Core\\Entities\\Tag', (int) $parentTagId); if (null !== $parentTag) { $form = $this->buildAddForm(); $form->handleRequest($request); if ($form->isValid()) { $data = $form->getData(); $names = explode(',', $data['names']); foreach ($names as $name) { $name = strip_tags(trim($name)); $tag = new Tag(); $tag->setTagName($name); $tag->setParent($parentTag); $this->getService('em')->persist($tag); $translatedTag = new TagTranslation($tag, $translation); $this->getService('em')->persist($translatedTag); $this->getService('em')->flush(); /* * Dispatch event */ $event = new FilterTagEvent($tag); $this->getService('dispatcher')->dispatch(TagEvents::TAG_CREATED, $event); $msg = $this->getTranslator()->trans('child.tag.%name%.created', ['%name%' => $tag->getTagName()]); $this->publishConfirmMessage($request, $msg); } return $this->redirect($this->generateUrl('tagsTreePage', ['tagId' => $parentTagId])); } $this->assignation['translation'] = $translation; $this->assignation['form'] = $form->createView(); $this->assignation['tag'] = $parentTag; return $this->render('tags/add-multiple.html.twig', $this->assignation); } else { return $this->throw404(); } }
/** * Find a tag according to the given path or create it. * * @param string $tagPath * * @return RZ\Roadiz\Core\Entities\Tag */ public function findOrCreateByPath($tagPath) { $tagPath = trim($tagPath); $tags = explode('/', $tagPath); $tags = array_filter($tags); $tagName = $tags[count($tags) - 1]; $parentName = null; $parentTag = null; if (count($tags) > 1) { $parentName = $tags[count($tags) - 2]; $parentTag = $this->findOneByTagName($parentName); if (null === $parentTag) { $ttagParent = $this->_em->getRepository('RZ\\Roadiz\\Core\\Entities\\TagTranslation')->findOneByName($parentName); if (null !== $ttagParent) { $parentTag = $ttagParent->getTag(); } } } $tag = $this->findOneByTagName($tagName); if (null === $tag) { $ttag = $this->_em->getRepository('RZ\\Roadiz\\Core\\Entities\\TagTranslation')->findOneByName($tagName); if (null !== $ttag) { $tag = $ttag->getTag(); } } if (null === $tag) { /* * Creation of a new tag * before linking it to the node */ $trans = $this->_em->getRepository('RZ\\Roadiz\\Core\\Entities\\Translation')->findDefault(); $tag = new Tag(); $tag->setTagName($tagName); $translatedTag = new TagTranslation($tag, $trans); $translatedTag->setName($tagName); $tag->getTranslatedTags()->add($translatedTag); if (null !== $parentTag) { $tag->setParent($parentTag); } $this->_em->persist($translatedTag); $this->_em->persist($tag); $this->_em->flush(); } return $tag; }