/**
  * 
  * @param int $depId
  * @param string $objectToDelete
  * @param string $relType | 'parent' or 'child'
  */
 protected function delServiceRelations($depId, $objectToDelete, $relType)
 {
     if ($relType == 'parent') {
         $sql = "DELETE FROM dependency_serviceParent_relation\n                WHERE dependency_dep_id = ?\n                AND host_host_id = ?\n                AND service_service_id = ?";
         if (!strstr($objectToDelete, ',')) {
             throw new CentreonClapiException('Invalid service definition');
         }
         list($host, $service) = explode(",", $objectToDelete);
         $idTab = $this->serviceObj->getHostAndServiceId($host, $service);
         if (!count($idTab)) {
             throw new CentreonClapiException(sprintf('Could not find service %s on host %s', $service, $host));
         }
         $params = array($depId, $idTab[0], $idTab[1]);
     } elseif ($relType == 'child' && strstr($objectToDelete, ',')) {
         // service child
         $sql = "DELETE FROM dependency_serviceChild_relation\n                WHERE dependency_dep_id = ?\n                AND host_host_id = ?\n                AND service_service_id = ?";
         list($host, $service) = explode(",", $objectToDelete);
         $idTab = $this->serviceObj->getHostAndServiceId($host, $service);
         if (!count($idTab)) {
             throw new CentreonClapiException(sprintf('Could not find service %s on host %s', $service, $host));
         }
         $params = array($depId, $idTab[0], $idTab[1]);
     } elseif ($relType == 'child') {
         // host child
         $sql = "DELETE FROM dependency_hostChild_relation\n                WHERE dependency_dep_id = ?\n                AND host_host_id = ?";
         $hostObj = new Centreon_Object_Host();
         $hostIds = $hostObj->getIdByParameter($hostObj->getUniqueLabelField(), array($objectToDelete));
         if (!count($hostIds)) {
             throw new CentreonClapiException(sprintf('Could not find host %s', $objectToDelete));
         }
         $params = array($depId, $hostIds[0]);
     }
     $this->db->query($sql, $params);
 }
 /**
  * Add a service
  *
  * @param string $parameters
  * @return void
  * @throws CentreonClapiException
  */
 public function add($parameters)
 {
     $params = explode($this->delim, $parameters);
     if (count($params) < $this->nbOfCompulsoryParams) {
         throw new CentreonClapiException(self::MISSINGPARAMETER);
     }
     if ($this->serviceExists($params[self::ORDER_HOSTNAME], $params[self::ORDER_SVCDESC]) == true) {
         throw new CentreonClapiException(self::OBJECTALREADYEXISTS);
     }
     $hostObject = new Centreon_Object_Host();
     $tmp = $hostObject->getIdByParameter($hostObject->getUniqueLabelField(), $params[self::ORDER_HOSTNAME]);
     if (!count($tmp)) {
         throw new CentreonClapiException(self::OBJECT_NOT_FOUND . ":" . $params[self::ORDER_HOSTNAME]);
     }
     $hostId = $tmp[0];
     $addParams = array();
     $addParams['service_description'] = $params[self::ORDER_SVCDESC];
     $template = $params[self::ORDER_SVCTPL];
     $tmp = $this->object->getList($this->object->getPrimaryKey(), -1, 0, null, null, array('service_description' => $template, 'service_register' => '0'), "AND");
     if (!count($tmp)) {
         throw new CentreonClapiException(self::OBJECT_NOT_FOUND . ":" . $template);
     }
     $addParams['service_template_model_stm_id'] = $tmp[0][$this->object->getPrimaryKey()];
     $this->params = array_merge($this->params, $addParams);
     $serviceId = parent::add();
     $relObject = new Centreon_Object_Relation_Host_Service();
     $relObject->insert($hostId, $serviceId);
     $extended = new Centreon_Object_Service_Extended();
     $extended->insert(array($extended->getUniqueLabelField() => $serviceId));
 }
 /**
  * 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|set|add|del)member\$/", $name, $matches)) {
         $relobj = new Centreon_Object_Relation_Host_Category_Host();
         $obj = new Centreon_Object_Host();
         if ($matches[1] == "get") {
             $tab = $relobj->getTargetIdFromSourceId($relobj->getSecondKey(), $relobj->getFirstKey(), $hcIds);
             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($categoryId);
             }
             $existingRelationIds = $relobj->getTargetIdFromSourceId($relobj->getSecondKey(), $relobj->getFirstKey(), array($categoryId));
             foreach ($relationTable as $relationId) {
                 if ($matches[1] == "del") {
                     $relobj->delete($categoryId, $relationId);
                 } elseif ($matches[1] == "set" || $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);
     }
 }
 /**
  * Export
  *
  * @return void
  */
 public function export()
 {
     parent::export();
     $relObj = new Centreon_Object_Relation_Host_Group_Host();
     $hostObj = new Centreon_Object_Host();
     $hgFieldName = $this->object->getUniqueLabelField();
     $hFieldName = $hostObj->getUniqueLabelField();
     $elements = $relObj->getMergedParameters(array($hgFieldName), array($hFieldName), -1, 0, $hgFieldName, null);
     foreach ($elements as $element) {
         echo $this->action . $this->delim . "addhost" . $this->delim . $element[$hgFieldName] . $this->delim . $element[$hFieldName] . "\n";
     }
 }