/**
  * Deploy services
  * Recursive method
  *
  * @param int $hostId
  * @param mixed $hostTemplateId
  * @return void
  */
 protected function deployServices($hostId, $hostTemplateId = null)
 {
     static $tmplRel;
     static $svcObj;
     static $hostSvcRel;
     static $svcExtended;
     if (!isset($tmplRel) && !isset($svcObj) && !isset($hostSvcRel)) {
         $tmplRel = new Centreon_Object_Relation_Host_Template_Host();
         $svcObj = new Centreon_Object_Service();
         $hostSvcRel = new Centreon_Object_Relation_Host_Service();
         $svcExtended = new Centreon_Object_Service_Extended();
     }
     if (!isset($hostTemplateId)) {
         $id = $hostId;
     } else {
         $id = $hostTemplateId;
     }
     $templates = $tmplRel->gethost_tpl_idFromhost_host_id($id);
     foreach ($templates as $templateId) {
         $serviceTemplates = $hostSvcRel->getservice_service_idFromhost_host_id($templateId);
         foreach ($serviceTemplates as $serviceTemplateId) {
             $params = $svcObj->getParameters($serviceTemplateId, array('service_alias'));
             $sql = "SELECT service_id\n                \t\tFROM service s, host_service_relation hsr\n                \t\tWHERE s.service_id = hsr.service_service_id\n                \t\tAND s.service_description = :servicedescription\n                \t\tAND hsr.host_host_id = :hostid\n                \t\tUNION\n                \t\tSELECT service_id\n                \t\tFROM service s, host_service_relation hsr\n                \t\tWHERE s.service_id = hsr.service_service_id\n                \t\tAND s.service_description = :servicedescription\n                \t\tAND hsr.hostgroup_hg_id IN (SELECT hostgroup_hg_id FROM hostgroup_relation WHERE host_host_id = :hostid)";
             $res = $this->db->query($sql, array(':servicedescription' => $params['service_alias'], ':hostid' => $hostId));
             $result = $res->fetchAll();
             if (!count($result)) {
                 $svcId = $svcObj->insert(array('service_description' => $params['service_alias'], 'service_activate' => '1', 'service_register' => '1', 'service_template_model_stm_id' => $serviceTemplateId));
                 $hostSvcRel->insert($hostId, $svcId);
                 $svcExtended->insert(array($svcExtended->getUniqueLabelField() => $svcId));
             }
             unset($res);
         }
         $this->deployServices($hostId, $templateId);
     }
 }
 /**
  * Export
  *
  * @return void
  */
 public function export()
 {
     parent::export();
     $scs = $this->object->getList(array($this->object->getPrimaryKey(), $this->object->getUniqueLabelField()));
     $relobj = new Centreon_Object_Relation_Service_Category_Service();
     $hostServiceRel = new Centreon_Object_Relation_Host_Service();
     $svcObj = new Centreon_Object_Service();
     foreach ($scs as $sc) {
         $scId = $sc[$this->object->getPrimaryKey()];
         $scName = $sc[$this->object->getUniqueLabelField()];
         $relations = $relobj->getTargetIdFromSourceId($relobj->getSecondKey(), $relobj->getFirstKey(), $scId);
         foreach ($relations as $serviceId) {
             $svcParam = $svcObj->getParameters($serviceId, array('service_description', 'service_register'));
             if ($svcParam['service_register'] == 1) {
                 $elements = $hostServiceRel->getMergedParameters(array('host_name'), array('service_description'), -1, 0, null, null, array("service_id" => $serviceId), "AND");
                 foreach ($elements as $element) {
                     echo $this->action . $this->delim . "addservice" . $this->delim . $scName . $this->delim . $element['host_name'] . "," . $element['service_description'] . "\n";
                 }
             } else {
                 echo $this->action . $this->delim . "addservicetemplate" . $this->delim . $scName . $this->delim . $svcParam['service_description'] . "\n";
             }
         }
     }
 }