/**
  * Add a contact
  *
  * @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);
     }
     $addParams = array();
     $addParams[$this->object->getUniqueLabelField()] = $params[self::ORDER_UNIQUENAME];
     $addParams['host_alias'] = $params[self::ORDER_ALIAS];
     $addParams['host_address'] = $params[self::ORDER_ADDRESS];
     $templates = explode("|", $params[self::ORDER_TEMPLATE]);
     $templateIds = array();
     foreach ($templates as $template) {
         if ($template) {
             $tmp = $this->object->getIdByParameter($this->object->getUniqueLabelField(), $template);
             if (count($tmp)) {
                 $templateIds[] = $tmp[0];
             } else {
                 throw new CentreonClapiException(self::OBJECT_NOT_FOUND . ":" . $template);
             }
         }
     }
     $instanceName = $params[self::ORDER_POLLER];
     $instanceObject = new Centreon_Object_Instance();
     if ($this->action == "HOST") {
         if ($instanceName) {
             $tmp = $instanceObject->getIdByParameter($instanceObject->getUniqueLabelField(), $instanceName);
             if (!count($tmp)) {
                 throw new CentreonClapiException(self::OBJECT_NOT_FOUND . ":" . $instanceName);
             }
             $instanceId = $tmp[0];
         } else {
             throw new CentreonClapiException(self::MISSING_INSTANCE);
         }
     }
     $hostgroups = explode("|", $params[self::ORDER_HOSTGROUP]);
     $hostgroupIds = array();
     $hostgroupObject = new Centreon_Object_Host_Group();
     foreach ($hostgroups as $hostgroup) {
         if ($hostgroup) {
             $tmp = $hostgroupObject->getIdByParameter($hostgroupObject->getUniqueLabelField(), $hostgroup);
             if (count($tmp)) {
                 $hostgroupIds[] = $tmp[0];
             } else {
                 throw new CentreonClapiException(self::OBJECT_NOT_FOUND . ":" . $hostgroup);
             }
         }
     }
     $this->params = array_merge($this->params, $addParams);
     $this->checkParameters();
     $hostId = parent::add();
     $i = 1;
     $templateRelationObject = new Centreon_Object_Relation_Host_Template_Host();
     foreach ($templateIds as $templateId) {
         $templateRelationObject->insert($templateId, $hostId, $i);
         $i++;
     }
     $hostgroupRelationObject = new Centreon_Object_Relation_Host_Group_Host();
     foreach ($hostgroupIds as $hostgroupId) {
         $hostgroupRelationObject->insert($hostgroupId, $hostId);
     }
     if (isset($instanceId)) {
         $instanceRelationObject = new Centreon_Object_Relation_Instance_Host();
         $instanceRelationObject->insert($instanceId, $hostId);
     }
     $extended = new Centreon_Object_Host_Extended();
     $extended->insert(array($extended->getUniqueLabelField() => $hostId));
 }
 /**
  * 
  * @param int $depId
  * @param string $objectToDelete
  * @param string $relType | 'parent' or 'child'
  */
 protected function delHostgroupRelations($depId, $objectToDelete, $relType)
 {
     $table = "dependency_hostgroup" . ucfirst($relType) . "_relation";
     $sql = "DELETE FROM {$table} \n            WHERE dependency_dep_id = ?\n            AND hostgroup_hg_id = ?";
     $obj = new Centreon_Object_Host_Group();
     $ids = $obj->getIdByParameter($obj->getUniqueLabelField(), array($objectToDelete));
     if (!count($ids)) {
         throw new CentreonClapiException(sprintf('Could not find host group %s', $objectToDelete));
     }
     $this->db->query($sql, array($depId, $ids[0]));
 }
 /**
  * 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);
     }
     $hgObject = new Centreon_Object_Host_Group();
     $tmp = $hgObject->getIdByParameter($hgObject->getUniqueLabelField(), $params[self::ORDER_HOSTNAME]);
     if (!count($tmp)) {
         throw new CentreonClapiException(self::OBJECT_NOT_FOUND . ":" . $params[self::ORDER_HOSTNAME]);
     }
     $hgId = $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_Group_Service();
     $relObject->insert($hgId, $serviceId);
     $extended = new Centreon_Object_Service_Extended();
     $extended->insert(array($extended->getUniqueLabelField() => $serviceId));
 }