Example #1
0
 /**
  * 
  * @param string $object
  * @param string $tag
  */
 public function addTagAction($object, $tag)
 {
     $iNbAdded = 0;
     $sLibTag = 'tag';
     $aError = array();
     $repository = $this->repository;
     $sName = $this->objectName;
     $repository::transco($object);
     $aId = $repository::getListBySlugName($object[$sName]);
     if (count($aId) > 0) {
         $object = $aId[0]['id'];
     } else {
         throw new \Exception(static::OBJ_NOT_EXIST, 1);
     }
     $aTags = explode(",", $tag);
     foreach ($aTags as $sTag) {
         $bOkyToAdd = true;
         $iIdTag = TagsRepository::getTagId($sTag, 1, 0);
         if (!empty($iIdTag)) {
             $bLink = TagsRepository::isLink($sName, $object, $iIdTag);
             if ($bLink) {
                 $aError[] = $sTag;
                 $bOkyToAdd = false;
             }
         }
         if ($bOkyToAdd) {
             TagsRepository::add($sTag, $this->objectName, $object, 1);
             $iNbAdded++;
         }
     }
     if (count($aTags) == 1) {
         $sLibTag = $aTags[0];
     } else {
         if (count($aTags) > 1) {
             $sLibTag .= "s";
         }
     }
     if ($iNbAdded > 0) {
         InputOutput::display($sLibTag . " has been successfully added to the object", true, 'green');
         if ($iNbAdded > 0 && count($aError) > 0) {
             throw new \Exception("but some tags already exists : '" . implode("', '", $aError) . "'", 1);
         }
     } else {
         throw new \Exception($sLibTag . " already exists", 1);
     }
 }
Example #2
0
 /**
  * Add a tag
  *
  * @method post
  * @route /tag/add
  */
 public function addAction()
 {
     $di = Di::getDefault();
     $router = $di->get('router');
     $post = $router->request()->paramsPost();
     $sGlobal = 0;
     if (!is_array($post['resourceId'])) {
         $listResources = array($post['resourceId']);
     } else {
         $listResources = $post['resourceId'];
     }
     if (isset($post['typeTag']) && $post['typeTag'] == 1) {
         $sGlobal = 1;
     }
     foreach ($listResources as $resourceId) {
         $tagId = TagsRepository::add($post['tagName'], $post['resourceName'], $resourceId, $sGlobal);
     }
     return $router->response()->json(array('success' => true, 'tagId' => $tagId));
 }
 public function testAddWithBadResourceType()
 {
     $this->setExpectedException('Centreon\\Internal\\Exception', 'This resource type does not support tags.');
     TagsRepository::add('Tag1', 'badtype', 1);
 }