示例#1
0
 /**
  * Add a service
  *
  * @method post
  * @route /service/add
  */
 public function createAction()
 {
     $macroList = array();
     $aTagList = array();
     $aTags = array();
     $givenParameters = $this->getParams('post');
     if (isset($givenParameters['macro_name']) && isset($givenParameters['macro_value'])) {
         $macroName = $givenParameters['macro_name'];
         $macroValue = $givenParameters['macro_value'];
         $macroHidden = $givenParameters['macro_hidden'];
         foreach ($macroName as $key => $name) {
             if (!empty($name)) {
                 if (isset($macroHidden[$key])) {
                     $isPassword = '******';
                 } else {
                     $isPassword = '******';
                 }
                 $macroList[$name] = array('value' => $macroValue[$key], 'ispassword' => $isPassword);
             }
         }
     }
     $id = parent::createAction(false);
     if (count($macroList) > 0) {
         try {
             CustomMacroRepository::saveServiceCustomMacro(self::$objectName, $id, $macroList);
         } catch (\Exception $ex) {
             $errorMessage = $ex->getMessage();
             $this->router->response()->json(array('success' => false, 'error' => $errorMessage));
         }
     }
     if (isset($givenParameters['service_tags'])) {
         $aTagList = explode(",", $givenParameters['service_tags']);
         foreach ($aTagList as $var) {
             $var = trim($var);
             if (!empty($var)) {
                 array_push($aTags, $var);
             }
         }
         if (count($aTags) > 0) {
             TagsRepository::saveTagsForResource(self::$objectName, $id, $aTags, '', false, 1);
         }
     }
     $this->router->response()->json(array('success' => true));
 }
示例#2
0
 /**
  * 
  * @param string $object
  * @param array $params
  */
 public function addMacroAction($object, $params)
 {
     $paramList = $this->parseObjectParams($params);
     //try {
     $repository = $this->repository;
     $sName = $this->objectName;
     $repository::transco($object);
     $aId = $repository::getListBySlugName($object[$sName]);
     if (count($aId) > 0) {
         $objectId = $aId[0]['id'];
     } else {
         throw new \Exception(static::OBJ_NOT_EXIST, 1);
     }
     switch ($this->objectName) {
         case 'hosttemplate':
         case 'host':
             CustomMacroRepository::setAttributesMap(self::$attributesMapHost);
             CustomMacroRepository::transco($paramList);
             if (isset($paramList['host_macro_name']) && isset($paramList['host_macro_value'])) {
                 $formatedParams = array($paramList['host_macro_name'] => array('value' => $paramList['host_macro_value'], 'is_password' => $paramList['is_password']));
             }
             CustomMacroRepository::saveHostCustomMacro($this->objectName, $objectId, $formatedParams, false);
             InputOutput::display("The macro '" . $paramList['host_macro_name'] . "' has been successfully added to the object", true, 'green');
             break;
         case 'servicetemplate':
             CustomMacroRepository::setAttributesMap(self::$attributesMapService);
             CustomMacroRepository::transco($paramList);
             if (isset($paramList['svc_macro_name']) && isset($paramList['svc_macro_value'])) {
                 $formatedParams = array($paramList['svc_macro_name'] => array('value' => $paramList['svc_macro_value'], 'is_password' => $paramList['is_password']));
             }
             CustomMacroRepository::saveServiceCustomMacro($this->objectName, $objectId, $formatedParams, false);
             InputOutput::display("The macro '" . $paramList['svc_macro_name'] . "' has been successfully added to the object", true, 'green');
             break;
         case 'service':
             CustomMacroRepository::setAttributesMap(self::$attributesMapService);
             CustomMacroRepository::transco($paramList);
             $aData = \CentreonConfiguration\Repository\ServiceRepository::getServiceBySlugs($object['service']);
             if (count($aData) == 0) {
                 throw new \Exception(static::OBJ_NOT_EXIST, 1);
             } else {
                 $objectId = $aData['service_id'];
                 $hostId = $aData['host_id'];
             }
             if (isset($paramList['svc_macro_name']) && isset($paramList['svc_macro_value'])) {
                 $formatedParams = array($paramList['svc_macro_name'] => array('value' => $paramList['svc_macro_value'], 'is_password' => $paramList['is_password']));
             }
             CustomMacroRepository::saveServiceCustomMacro($this->objectName, $objectId, $formatedParams, false, $hostId);
             InputOutput::display("The macro '" . $paramList['svc_macro_name'] . "' has been successfully added to the object", true, 'green');
             break;
         default:
             break;
     }
     /*} catch(\Exception $ex) {
           InputOutput::display($ex->getMessage(), true, 'red');
       }*/
 }