/** * 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); } }