Esempio n. 1
0
 /**
  * remove Hosts from HostGroups. All Hosts are removed from all HostGroups.
  *
  * @param array $data
  * @param array $data['hostids']
  * @param array $data['groupids']
  * @param array $data['templateids']
  * @param array $data['macroids']
  * @return array
  */
 public static function massRemove($data)
 {
     $hostids = zbx_toArray($data['hostids']);
     try {
         self::BeginTransaction(__METHOD__);
         $options = array('hostids' => $hostids, 'editable' => 1, 'preservekeys' => 1, 'output' => API_OUTPUT_SHORTEN);
         $upd_hosts = self::get($options);
         foreach ($hostids as $hostid) {
             if (!isset($upd_hosts[$hostid])) {
                 self::exception(ZBX_API_ERROR_PERMISSIONS, S_NO_PERMISSION);
             }
         }
         if (isset($data['groupids'])) {
             $options = array('hostids' => $hostids, 'groupids' => zbx_toArray($data['groupids']));
             $result = CHostGroup::massRemove($options);
             if (!$result) {
                 self::exception();
             }
         }
         if (isset($data['templateids'])) {
             $options = array('hostids' => $hostids, 'templateids' => zbx_toArray($data['templateids']));
             $result = CTemplate::massRemove($options);
             if (!$result) {
                 self::exception();
             }
         }
         if (isset($data['macros'])) {
             $options = array('hostids' => $hostids, 'macros' => zbx_toArray($data['macros']));
             $result = CUserMacro::massRemove($options);
             if (!$result) {
                 self::exception();
             }
         }
         self::EndTransaction(true, __METHOD__);
         return array('hostids' => $hostids);
     } catch (APIException $e) {
         self::EndTransaction(false, __METHOD__);
         $error = $e->getErrors();
         $error = reset($error);
         self::setError(__METHOD__, $e->getCode(), $error);
         return false;
     }
 }
 /**
  * remove Hosts to HostGroups. All Hosts are added to all HostGroups.
  *
  * @param array $data
  * @param array $data['templateids']
  * @param array $data['groupids']
  * @param array $data['hostids']
  * @param array $data['macroids']
  * @return boolean
  */
 public static function massRemove($data)
 {
     $templateids = zbx_toArray($data['templateids']);
     try {
         self::BeginTransaction(__METHOD__);
         $upd_templates = self::get(array('templateids' => $templateids, 'editable' => 1, 'preservekeys' => 1));
         foreach ($templateids as $templateid) {
             if (!isset($upd_templates[$templateid])) {
                 self::exception(ZBX_API_ERROR_PERMISSIONS, S_NO_PERMISSION);
             }
         }
         if (isset($data['groupids'])) {
             $options = array('groupids' => zbx_toArray($data['groupids']), 'templateids' => $templateids);
             $result = CHostGroup::massRemove($options);
             if (!$result) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, 'Can\'t unlink groups');
             }
         }
         if (isset($data['hostids'])) {
             $hostids = zbx_toArray($data['hostids']);
             foreach ($hostids as $hostid) {
                 foreach ($templateids as $templateid) {
                     $result = unlink_template($hostid, $templateid, true);
                     if (!$result) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, 'Can\'t unlink hosts');
                     }
                 }
             }
         }
         if (isset($data['templateids_link'])) {
             $templateids_link = zbx_toArray($data['templateids_link']);
             foreach ($templateids_link as $templateid_link) {
                 foreach ($templateids as $templateid) {
                     $result = unlink_template($templateid, $templateid_link, true);
                     if (!$result) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, 'Can\'t unlink templates');
                     }
                 }
             }
         }
         if (isset($data['macros'])) {
             $options = array('templateids' => $templateids, 'macros' => zbx_toArray($data['macros']));
             $result = CUserMacro::massRemove($options);
             if (!$result) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, 'Can\'t remove macros');
             }
         }
         self::EndTransaction(true, __METHOD__);
         return array('templateids' => $templateids);
     } catch (APIException $e) {
         self::EndTransaction(false, __METHOD__);
         $error = $e->getErrors();
         $error = reset($error);
         self::setError(__METHOD__, $e->getCode(), $error);
         return false;
     }
 }