Пример #1
0
 /**
  * @param ObjectManager $manager
  */
 public function load(ObjectManager $manager)
 {
     $tags = ['statybos' => ['tinkas', 'glaistas', 'cementas', 'mūras', 'plytos', 'namas', 'remontas'], 'elektra' => ['rozetė', 'lemputė', 'lituoti', 'laidas', 'kabelis'], 'kompiuteriai' => ['diskas', 'windows', 'linux', 'dox', 'klaviatūra', 'pelytė', 'monitorius'], 'kulinarija' => ['maistas', 'valgyti', 'kalorijos', 'sriuba', 'lėkštė', 'puodas', 'gaminti', 'virti', 'kepti', 'skaniai', 'apetitas', 'tortas', 'varškė', 'sūris', 'pienas', 'patiekalas', 'košė'], 'buitis' => ['televizorius', 'indaplovė', 'šaldytuvas', 'atnešti', 'nupirkti'], 'automobiliai' => ['akumuliatorius', 'variklis', 'sėdynės', 'užvesti', 'starteris', 'sankaba', 'žibintai'], 'santechnika' => ['kranas', 'čiaupas', 'vamzdynas', 'klozetas', 'bakelis', 'vonia', 'tualetas', 'kriauklė']];
     $a = 3;
     $i = 3;
     $randomTags_admin = array_rand($tags, $a);
     $randomTags_info = array_rand($tags, $i);
     foreach ($tags as $p => $c) {
         $parent = new Tags();
         $parent->setTitle($p);
         $parent->setIsEnabled(true);
         if (in_array($p, $randomTags_admin)) {
             $this->addReference('tags_admin' . $a, $parent);
             $a--;
         }
         if (in_array($p, $randomTags_info)) {
             $this->addReference('tags_info' . $i, $parent);
             $i--;
         }
         $manager->persist($parent);
         foreach ($c as $child) {
             if (empty($child)) {
                 continue;
             }
             $tag = new Tags();
             $tag->setTitle($child);
             $tag->setIsEnabled(true);
             $tag->setParent($parent);
             $manager->persist($tag);
         }
     }
     $manager->flush();
 }
Пример #2
0
 /**
  * Remove tag
  *
  * @param \NFQ\AssistanceBundle\Entity\Tags $tag
  */
 public function removeTag(\NFQ\AssistanceBundle\Entity\Tags $tag)
 {
     $this->tags->removeElement($tag);
 }
Пример #3
0
 /**
  * @return array
  */
 public function saveTag()
 {
     $response = [];
     try {
         if (!$this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY')) {
             throw new AccessDeniedException();
         }
         $tag_ar = $this->getRequest()->get('tag', null);
         $parent_id = $this->getRequest()->get('parent_id', null);
         $user = $this->getUser();
         $tagRepo = $this->em->getRepository('NFQAssistanceBundle:Tags');
         //get parent tag object
         if (is_numeric($parent_id)) {
             $parent = $tagRepo->findOneBy(array('id' => $parent_id));
         } else {
             $parent = null;
         }
         if (!is_array($tag_ar)) {
             throw new \InvalidArgumentException('Tags are missing');
         }
         if (!is_numeric($tag_ar['id'])) {
             $tag_id = $this->suggestSpelling($tag_ar['id']);
         } else {
             $tag_id = $tag_ar['id'];
         }
         if (!is_numeric($tag_id) and !($tag = $tagRepo->findOneBy(['title' => $tag_id, 'parent' => $parent]))) {
             //create a new tag
             $tag = new Tags();
             $tag->setTitle($tag_id);
             $tag->setParent($parent);
             $this->em->persist($tag);
             $this->em->flush();
         } elseif (is_numeric($tag_id)) {
             $tag = $tagRepo->findOneBy(['id' => $tag_id, 'parent' => $parent]);
         } else {
             throw new \Exception('Some error occured');
         }
         //check if not present already
         $tagRepo = $this->em->getRepository('NFQAssistanceBundle:Tag2User');
         $tag2userCheck = $tagRepo->findOneBy(['tag' => $tag, 'user' => $user]);
         if (!empty($tag2userCheck)) {
             throw new \Exception('Tag already assigned to user');
         }
         $tag2user = new Tag2User();
         $tag2user->setTag($tag);
         $tag2user->setUser($user);
         $this->em->persist($tag2user);
         $this->em->flush();
         $response['status'] = 'success';
     } catch (\Exception $ex) {
         $response['status'] = 'failed';
         $response['message'] = $ex->getMessage();
     }
     return $response;
 }