/**
  * Magic method for get/set/add/del relations
  *
  * @param string $name
  * @param array $arg
  * @throws CentreonClapiException
  */
 public function __call($name, $arg)
 {
     $name = strtolower($name);
     if (!isset($arg[0])) {
         throw new CentreonClapiException(self::MISSINGPARAMETER);
     }
     $args = explode($this->delim, $arg[0]);
     $cgIds = $this->object->getIdByParameter($this->object->getUniqueLabelField(), array($args[0]));
     if (!count($cgIds)) {
         throw new CentreonClapiException(self::OBJECT_NOT_FOUND . ":" . $args[0]);
     }
     $cgId = $cgIds[0];
     if (preg_match("/^(get|set|add|del)contact\$/", $name, $matches)) {
         $relobj = new Centreon_Object_Relation_Contact_Group_Contact();
         $obj = new Centreon_Object_Contact();
         if ($matches[1] == "get") {
             $tab = $relobj->getTargetIdFromSourceId($relobj->getSecondKey(), $relobj->getFirstKey(), $cgIds);
             echo "id" . $this->delim . "name" . "\n";
             foreach ($tab as $value) {
                 $tmp = $obj->getParameters($value, array($obj->getUniqueLabelField()));
                 echo $value . $this->delim . $tmp[$obj->getUniqueLabelField()] . "\n";
             }
         } else {
             if (!isset($args[1])) {
                 throw new CentreonClapiException(self::MISSINGPARAMETER);
             }
             $relation = $args[1];
             $relations = explode("|", $relation);
             $relationTable = array();
             foreach ($relations as $rel) {
                 $tab = $obj->getIdByParameter($obj->getUniqueLabelField(), array($rel));
                 if (!count($tab)) {
                     throw new CentreonClapiException(self::OBJECT_NOT_FOUND . ":" . $rel);
                 }
                 $relationTable[] = $tab[0];
             }
             if ($matches[1] == "set") {
                 $relobj->delete($cgId);
             }
             $existingRelationIds = $relobj->getTargetIdFromSourceId($relobj->getSecondKey(), $relobj->getFirstKey(), array($cgId));
             foreach ($relationTable as $relationId) {
                 if ($matches[1] == "del") {
                     $relobj->delete($cgId, $relationId);
                 } elseif ($matches[1] == "set" || $matches[1] == "add") {
                     if (!in_array($relationId, $existingRelationIds)) {
                         $relobj->insert($cgId, $relationId);
                     }
                 }
             }
             $acl = new CentreonACL();
             $acl->reload(true);
         }
     } else {
         throw new CentreonClapiException(self::UNKNOWN_METHOD);
     }
 }
 /**
  * Magic method for get/set/add/del relations
  *
  * @param string $name
  * @param array $arg
  * @throws CentreonClapiException
  */
 public function __call($name, $arg)
 {
     $name = strtolower($name);
     if (!isset($arg[0])) {
         throw new CentreonClapiException(self::MISSINGPARAMETER);
     }
     $args = explode($this->delim, $arg[0]);
     $sgIds = $this->object->getIdByParameter($this->object->getUniqueLabelField(), array($args[0]));
     if (!count($sgIds)) {
         throw new CentreonClapiException(self::OBJECT_NOT_FOUND . ":" . $args[0]);
     }
     $sgId = $sgIds[0];
     if (preg_match("/^(get|add|del|set)(service|hostgroupservice)\$/", $name, $matches)) {
         if ($matches[2] == "service") {
             $relobj = new Centreon_Object_Relation_Service_Group_Service();
             $obj = new Centreon_Object_Relation_Host_Service();
             $existingRelationIds = $relobj->getHostIdServiceIdFromServicegroupId($sgId);
             $hstring = "host_id";
         } else {
             $relobj = new Centreon_Object_Relation_Service_Group_Host_Group_Service();
             $obj = new Centreon_Object_Relation_Host_Group_Service();
             $existingRelationIds = $relobj->getHostGroupIdServiceIdFromServicegroupId($sgId);
             $hstring = "hostgroup_id";
         }
         if ($matches[1] == "get") {
             if ($matches[2] == "service") {
                 echo "host id" . $this->delim . "host name" . $this->delim . "service id" . $this->delim . "service description\n";
             } elseif ($matches[2] == "hostgroupservice") {
                 echo "hostgroup id" . $this->delim . "hostgroup name" . $this->delim . "service id" . $this->delim . "service description\n";
             }
             foreach ($existingRelationIds as $val) {
                 if ($matches[2] == "service") {
                     $elements = $obj->getMergedParameters(array('host_name', 'host_id'), array('service_description', 'service_id'), -1, 0, "host_name,service_description", "ASC", array("service_id" => $val['service_id'], "host_id" => $val['host_id']), "AND");
                     if (isset($elements[0])) {
                         echo $elements[0]['host_id'] . $this->delim . $elements[0]['host_name'] . $this->delim . $elements[0]['service_id'] . $this->delim . $elements[0]['service_description'] . "\n";
                     }
                 } else {
                     $elements = $obj->getMergedParameters(array('hg_name', 'hg_id'), array('service_description', 'service_id'), -1, 0, "hg_name,service_description", "ASC", array("service_id" => $val['service_id'], "hg_id" => $val['hostgroup_id']), "AND");
                     if (isset($elements[0])) {
                         echo $elements[0]['hg_id'] . $this->delim . $elements[0]['hg_name'] . $this->delim . $elements[0]['service_id'] . $this->delim . $elements[0]['service_description'] . "\n";
                     }
                 }
             }
         } else {
             if (!isset($args[1])) {
                 throw new CentreonClapiException(self::MISSINGPARAMETER);
             }
             $relation = $args[1];
             $relations = explode("|", $relation);
             $relationTable = array();
             $i = 0;
             foreach ($relations as $rel) {
                 $tmp = explode(",", $rel);
                 if (count($tmp) < 2) {
                     throw new CentreonClapiException(self::MISSINGPARAMETER);
                 }
                 if ($matches[2] == "service") {
                     $elements = $obj->getMergedParameters(array('host_id'), array('service_id'), -1, 0, null, null, array("host_name" => $tmp[0], "service_description" => $tmp[1]), "AND");
                     if (!count($elements)) {
                         throw new CentreonClapiException(self::OBJECT_NOT_FOUND . ":" . $tmp[0] . "/" . $tmp[1]);
                     }
                     $relationTable[$i]['host_id'] = $elements[0]['host_id'];
                     $relationTable[$i]['service_id'] = $elements[0]['service_id'];
                 } elseif ($matches[2] == "hostgroupservice") {
                     $elements = $obj->getMergedParameters(array('hg_id'), array('service_id'), -1, 0, null, null, array("hg_name" => $tmp[0], "service_description" => $tmp[1]), "AND");
                     if (!count($elements)) {
                         throw new CentreonClapiException(self::OBJECT_NOT_FOUND . ":" . $tmp[0] . "/" . $tmp[1]);
                     }
                     $relationTable[$i]['hostgroup_id'] = $elements[0]['hg_id'];
                     $relationTable[$i]['service_id'] = $elements[0]['service_id'];
                 }
                 $i++;
             }
             if ($matches[1] == "set") {
                 foreach ($existingRelationIds as $key => $existrel) {
                     $relobj->delete($sgId, $existrel[$hstring], $existrel['service_id']);
                     unset($existingRelationIds[$key]);
                 }
             }
             foreach ($relationTable as $relation) {
                 if ($matches[1] == "del") {
                     $relobj->delete($sgId, $relation[$hstring], $relation['service_id']);
                 } elseif ($matches[1] == "add" || $matches[1] == "set") {
                     $insert = true;
                     foreach ($existingRelationIds as $existrel) {
                         if ($existrel[$hstring] == $relation[$hstring] && $existrel['service_id'] == $relation['service_id']) {
                             $insert = false;
                             break;
                         }
                     }
                     if ($insert == true) {
                         $relobj->insert($sgId, $relation[$hstring], $relation['service_id']);
                     }
                 }
             }
             $acl = new CentreonACL();
             $acl->reload(true);
         }
     } else {
         throw new CentreonClapiException(self::UNKNOWN_METHOD);
     }
 }
 /**
  * Magic method
  *
  * @param string $name
  * @param array $args
  * @return void
  * @throws CentreonClapiException
  */
 public function __call($name, $arg)
 {
     $name = strtolower($name);
     if (!isset($arg[0])) {
         throw new CentreonClapiException(self::MISSINGPARAMETER);
     }
     $args = explode($this->delim, $arg[0]);
     $hostIds = $this->object->getIdByParameter($this->object->getUniqueLabelField(), array($args[0]));
     if (!count($hostIds)) {
         throw new CentreonClapiException(self::OBJECT_NOT_FOUND . ":" . $args[0]);
     }
     $hostId = $hostIds[0];
     if (preg_match("/^(get|set|add|del)([a-zA-Z_]+)/", $name, $matches)) {
         switch ($matches[2]) {
             case "contact":
                 $class = "Centreon_Object_Contact";
                 $relclass = "Centreon_Object_Relation_Contact_Host";
                 break;
             case "contactgroup":
                 $class = "Centreon_Object_Contact_Group";
                 $relclass = "Centreon_Object_Relation_Contact_Group_Host";
                 break;
             case "hostgroup":
                 $class = "Centreon_Object_Host_Group";
                 $relclass = "Centreon_Object_Relation_Host_Group_Host";
                 break;
             case "template":
                 $class = "Centreon_Object_Host";
                 $relclass = "Centreon_Object_Relation_Host_Template_Host";
                 break;
             case "parent":
                 $class = "Centreon_Object_Host";
                 $relclass = "Centreon_Object_Relation_Host_Parent_Host";
                 break;
             case "hostcategory":
                 $class = "Centreon_Object_Host_Category";
                 $relclass = "Centreon_Object_Relation_Host_Category_Host";
                 break;
             default:
                 throw new CentreonClapiException(self::UNKNOWN_METHOD);
                 break;
         }
         if (class_exists($relclass) && class_exists($class)) {
             $relobj = new $relclass();
             $obj = new $class();
             if ($matches[1] == "get") {
                 $tab = $relobj->getTargetIdFromSourceId($relobj->getFirstKey(), $relobj->getSecondKey(), $hostId);
                 echo "id" . $this->delim . "name" . "\n";
                 foreach ($tab as $value) {
                     $tmp = $obj->getParameters($value, array($obj->getUniqueLabelField()));
                     echo $value . $this->delim . $tmp[$obj->getUniqueLabelField()] . "\n";
                 }
             } else {
                 if (!isset($args[1])) {
                     throw new CentreonClapiException(self::MISSINGPARAMETER);
                 }
                 if ($matches[2] == "contact") {
                     $args[1] = str_replace(" ", "_", $args[1]);
                 }
                 $relation = $args[1];
                 $relations = explode("|", $relation);
                 $relationTable = array();
                 foreach ($relations as $rel) {
                     $tab = $obj->getIdByParameter($obj->getUniqueLabelField(), array($rel));
                     if (!count($tab)) {
                         throw new CentreonClapiException(self::OBJECT_NOT_FOUND . ":" . $rel);
                     }
                     $relationTable[] = $tab[0];
                 }
                 if ($matches[1] == "set") {
                     $relobj->delete(null, $hostId);
                 }
                 $existingRelationIds = $relobj->getTargetIdFromSourceId($relobj->getFirstKey(), $relobj->getSecondKey(), $hostId);
                 foreach ($relationTable as $relationId) {
                     if ($matches[1] == "del") {
                         $relobj->delete($relationId, $hostId);
                     } elseif ($matches[1] == "set" || $matches[1] == "add") {
                         if (!in_array($relationId, $existingRelationIds)) {
                             $relobj->insert($relationId, $hostId);
                         }
                     }
                 }
                 if ($matches[2] == "hostgroup") {
                     $aclObj = new CentreonACL();
                     $aclObj->reload(true);
                 }
             }
         } else {
             throw new CentreonClapiException(self::UNKNOWN_METHOD);
         }
     } else {
         throw new CentreonClapiException(self::UNKNOWN_METHOD);
     }
 }
 /**
  * Magic method for get/set/add/del relations
  *
  * @param string $name
  * @param array $arg
  */
 public function __call($name, $arg)
 {
     $name = strtolower($name);
     if (!isset($arg[0])) {
         throw new CentreonClapiException(self::MISSINGPARAMETER);
     }
     $args = explode($this->delim, $arg[0]);
     $hcIds = $this->object->getIdByParameter($this->object->getUniqueLabelField(), array($args[0]));
     if (!count($hcIds)) {
         throw new CentreonClapiException(self::OBJECT_NOT_FOUND . ":" . $args[0]);
     }
     $categoryId = $hcIds[0];
     if (preg_match("/^(get|add|del)(service|servicetemplate)\$/", $name, $matches)) {
         $obj = new Centreon_Object_Service();
         $relobj = new Centreon_Object_Relation_Service_Category_Service();
         $hostServiceRel = new Centreon_Object_Relation_Host_Service();
         if ($matches[1] == "get") {
             $tab = $relobj->getTargetIdFromSourceId($relobj->getSecondKey(), $relobj->getFirstKey(), $hcIds);
             if ($matches[2] == "servicetemplate") {
                 echo "template id" . $this->delim . "service template description\n";
             } elseif ($matches[2] == "service") {
                 echo "host id" . $this->delim . "host name" . $this->delim . "service id" . $this->delim . "service description\n";
             }
             foreach ($tab as $value) {
                 $p = $obj->getParameters($value, array('service_description', 'service_register'));
                 if ($p['service_register'] == 1 && $matches[2] == "service") {
                     $elements = $hostServiceRel->getMergedParameters(array('host_name', 'host_id'), array('service_description'), -1, 0, "host_name,service_description", "ASC", array("service_id" => $value), "AND");
                     if (isset($elements[0])) {
                         echo $elements[0]['host_id'] . $this->delim . $elements[0]['host_name'] . $this->delim . $value . $this->delim . $elements[0]['service_description'] . "\n";
                     }
                 } elseif ($p['service_register'] == 0 && $matches[2] == "servicetemplate") {
                     echo $value . $this->delim . $p['service_description'] . "\n";
                 }
             }
         } else {
             if (!isset($args[1])) {
                 throw new CentreonClapiException(self::MISSINGPARAMETER);
             }
             $relation = $args[1];
             $relations = explode("|", $relation);
             $relationTable = array();
             foreach ($relations as $rel) {
                 if ($matches[2] == "service") {
                     $tmp = explode(",", $rel);
                     if (count($tmp) < 2) {
                         throw new CentreonClapiException(self::MISSINGPARAMETER);
                     }
                     $elements = $hostServiceRel->getMergedParameters(array('host_id'), array('service_id'), -1, 0, null, null, array("host_name" => $tmp[0], "service_description" => $tmp[1]), "AND");
                     if (!count($elements)) {
                         throw new CentreonClapiException(self::OBJECT_NOT_FOUND . ":" . $tmp[0] . "/" . $tmp[1]);
                     }
                     $relationTable[] = $elements[0]['service_id'];
                 } elseif ($matches[2] == "servicetemplate") {
                     $tab = $obj->getList("service_id", -1, 0, null, null, array('service_description' => $rel, 'service_register' => 0), "AND");
                     if (!count($tab)) {
                         throw new CentreonClapiException(self::OBJECT_NOT_FOUND . ":" . $rel);
                     }
                     $relationTable[] = $tab[0]['service_id'];
                 }
             }
             $existingRelationIds = $relobj->getTargetIdFromSourceId($relobj->getSecondKey(), $relobj->getFirstKey(), array($categoryId));
             foreach ($relationTable as $relationId) {
                 if ($matches[1] == "del") {
                     $relobj->delete($categoryId, $relationId);
                 } elseif ($matches[1] == "add") {
                     if (!in_array($relationId, $existingRelationIds)) {
                         $relobj->insert($categoryId, $relationId);
                     }
                 }
             }
             $acl = new CentreonACL();
             $acl->reload(true);
         }
     } else {
         throw new CentreonClapiException(self::UNKNOWN_METHOD);
     }
 }