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();
     }
 }
 /**
  * @param array $parameters
  * @param Tag   $tag
  */
 protected function updatePosition($parameters, Tag $tag)
 {
     /*
      * First, we set the new parent
      */
     $parent = null;
     if (!empty($parameters['newParent']) && $parameters['newParent'] > 0) {
         $parent = $this->getService('em')->find('RZ\\Roadiz\\Core\\Entities\\Tag', (int) $parameters['newParent']);
         if ($parent !== null) {
             $tag->setParent($parent);
         }
     } else {
         $tag->setParent(null);
     }
     /*
      * Then compute new position
      */
     if (!empty($parameters['nextTagId']) && $parameters['nextTagId'] > 0) {
         $nextTag = $this->getService('em')->find('RZ\\Roadiz\\Core\\Entities\\Tag', (int) $parameters['nextTagId']);
         if ($nextTag !== null) {
             $tag->setPosition($nextTag->getPosition() - 0.5);
         }
     } elseif (!empty($parameters['prevTagId']) && $parameters['prevTagId'] > 0) {
         $prevTag = $this->getService('em')->find('RZ\\Roadiz\\Core\\Entities\\Tag', (int) $parameters['prevTagId']);
         if ($prevTag !== null) {
             $tag->setPosition($prevTag->getPosition() + 0.5);
         }
     }
     // Apply position update before cleaning
     $this->getService('em')->flush();
     if ($parent !== null) {
         $parent->getHandler()->cleanChildrenPositions();
     } else {
         TagHandler::cleanRootTagsPositions();
     }
     /*
      * Dispatch event
      */
     $event = new FilterTagEvent($tag);
     $this->getService('dispatcher')->dispatch(TagEvents::TAG_UPDATED, $event);
 }
 /**
  * @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();
 }
Example #5
0
 /**
  * @param RZ\Roadiz\Core\Entities\Tag $tag
  *
  * @return \Symfony\Component\Form\Form
  */
 private function buildDeleteForm(Tag $tag)
 {
     $builder = $this->createFormBuilder()->add('tagId', 'hidden', ['data' => $tag->getId(), 'constraints' => [new NotBlank()]]);
     return $builder->getForm();
 }
Example #6
0
 /**
  * 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;
 }