Example #1
0
 /**
  * @param ObjectManager $manager
  */
 public function load(ObjectManager $manager)
 {
     $users = ['admin', 'info'];
     foreach ($users as $user) {
         $a = 1;
         $ref = $this->getReference($user);
         while ($this->hasReference('tags_' . $user . $a)) {
             $tag = $this->getReference('tags_' . $user . $a);
             $t2u = new Tag2User();
             $t2u->setUser($ref);
             $t2u->setTag($tag);
             $manager->persist($t2u);
             $a++;
         }
         $a = 1;
     }
     $manager->flush();
 }
Example #2
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;
 }