/**
  * 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);
     }
 }
 /**
  * Parse template tree
  *
  * @param array $tree
  * @param Centreon_Object_Service_Extended $extendedObj 
  */
 protected function parseTemplateTree($tree)
 {
     $commandObj = new Centreon_Object_Command();
     $tpObj = new Centreon_Object_Timeperiod();
     $extendedObj = new Centreon_Object_Service_Extended();
     $macroObj = new Centreon_Object_Service_Macro_Custom();
     foreach ($tree as $element) {
         $addStr = $this->action . $this->delim . "ADD";
         foreach ($this->insertParams as $param) {
             $addStr .= $this->delim;
             if ($param == "service_template_model_stm_id") {
                 $tmp = $this->object->getParameters($element[$param], 'service_description');
                 if (isset($tmp) && isset($tmp['service_description']) && $tmp['service_description']) {
                     $element[$param] = $tmp['service_description'];
                 }
                 if (!$element[$param]) {
                     $element[$param] = "";
                 }
             }
             $addStr .= $element[$param];
         }
         $addStr .= "\n";
         echo $addStr;
         foreach ($element as $parameter => $value) {
             if (!in_array($parameter, $this->exportExcludedParams) && !is_null($value) && $value != "") {
                 if ($parameter == "timeperiod_tp_id" || $parameter == "timeperiod_tp_id2") {
                     $tmpObj = $tpObj;
                 } elseif ($parameter == "command_command_id" || $parameter == "command_command_id2") {
                     $tmpObj = $commandObj;
                 }
                 if (isset($tmpObj)) {
                     $tmp = $tmpObj->getParameters($value, $tmpObj->getUniqueLabelField());
                     if (isset($tmp) && isset($tmp[$tmpObj->getUniqueLabelField()])) {
                         $value = $tmp[$tmpObj->getUniqueLabelField()];
                     }
                     unset($tmpObj);
                 }
                 $value = CentreonUtils::convertLineBreak($value);
                 echo $this->action . $this->delim . "setparam" . $this->delim . $element['service_description'] . $this->delim . $this->getClapiActionName($parameter) . $this->delim . $value . "\n";
             }
         }
         $params = $extendedObj->getParameters($element[$this->object->getPrimaryKey()], array("esi_notes", "esi_notes_url", "esi_action_url", "esi_icon_image", "esi_icon_image_alt"));
         if (isset($params) && is_array($params)) {
             foreach ($params as $k => $v) {
                 if (!is_null($v) && $v != "") {
                     $v = CentreonUtils::convertLineBreak($v);
                     echo $this->action . $this->delim . "setparam" . $this->delim . $element['service_description'] . $this->delim . $this->getClapiActionName($k) . $this->delim . $v . "\n";
                 }
             }
         }
         $macros = $macroObj->getList("*", -1, 0, null, null, array('svc_svc_id' => $element[$this->object->getPrimaryKey()]), "AND");
         foreach ($macros as $macro) {
             echo $this->action . $this->delim . "setmacro" . $this->delim . $element['service_description'] . $this->delim . $this->stripMacro($macro['svc_macro_name']) . $this->delim . $macro['svc_macro_value'] . "\n";
         }
         if (count($element['children'])) {
             $this->parseTemplateTree($element['children'], $extendedObj);
         }
     }
 }
 /**
  * Export
  *
  * @return void
  */
 public function export()
 {
     $hostRel = new Centreon_Object_Relation_Host_Service();
     $elements = $hostRel->getMergedParameters(array("host_name"), array('*'), -1, 0, null, null, array("service_register" => $this->register), "AND");
     $extendedObj = new Centreon_Object_Service_Extended();
     $commandObj = new Centreon_Object_Command();
     $tpObj = new Centreon_Object_Timeperiod();
     $macroObj = new Centreon_Object_Service_Macro_Custom();
     foreach ($elements as $element) {
         $addStr = $this->action . $this->delim . "ADD";
         foreach ($this->insertParams as $param) {
             $addStr .= $this->delim;
             if ($param == "service_template_model_stm_id") {
                 $tmp = $this->object->getParameters($element[$param], 'service_description');
                 if (isset($tmp) && isset($tmp['service_description']) && $tmp['service_description']) {
                     $element[$param] = $tmp['service_description'];
                 }
                 if (!$element[$param]) {
                     $element[$param] = "";
                 }
             }
             $addStr .= $element[$param];
         }
         $addStr .= "\n";
         echo $addStr;
         foreach ($element as $parameter => $value) {
             if (!in_array($parameter, $this->exportExcludedParams) && !is_null($value) && $value != "") {
                 if ($parameter == "timeperiod_tp_id" || $parameter == "timeperiod_tp_id2") {
                     $tmpObj = $tpObj;
                 } elseif ($parameter == "command_command_id" || $parameter == "command_command_id2") {
                     $tmpObj = $commandObj;
                 }
                 if (isset($tmpObj)) {
                     $tmp = $tmpObj->getParameters($value, $tmpObj->getUniqueLabelField());
                     if (isset($tmp) && isset($tmp[$tmpObj->getUniqueLabelField()])) {
                         $value = $tmp[$tmpObj->getUniqueLabelField()];
                     }
                     unset($tmpObj);
                 }
                 $value = CentreonUtils::convertLineBreak($value);
                 echo $this->action . $this->delim . "setparam" . $this->delim . $element['host_name'] . $this->delim . $element['service_description'] . $this->delim . $this->getClapiActionName($parameter) . $this->delim . $value . "\n";
             }
         }
         $params = $extendedObj->getParameters($element[$this->object->getPrimaryKey()], array("esi_notes", "esi_notes_url", "esi_action_url", "esi_icon_image", "esi_icon_image_alt"));
         if (isset($params) && is_array($params)) {
             foreach ($params as $k => $v) {
                 if (!is_null($v) && $v != "") {
                     $v = CentreonUtils::convertLineBreak($v);
                     echo $this->action . $this->delim . "setparam" . $this->delim . $element['host_name'] . $this->delim . $element['service_description'] . $this->delim . $this->getClapiActionName($k) . $this->delim . $v . "\n";
                 }
             }
         }
         $macros = $macroObj->getList("*", -1, 0, null, null, array('svc_svc_id' => $element[$this->object->getPrimaryKey()]), "AND");
         foreach ($macros as $macro) {
             echo $this->action . $this->delim . "setmacro" . $this->delim . $element['host_name'] . $this->delim . $element['service_description'] . $this->delim . $this->stripMacro($macro['svc_macro_name']) . $this->delim . $macro['svc_macro_value'] . "\n";
         }
         $cgRel = new Centreon_Object_Relation_Contact_Group_Service();
         $cgelements = $cgRel->getMergedParameters(array("cg_name"), array('service_description'), -1, 0, null, null, array("service_register" => $this->register, "service_id" => $element['service_id']), "AND");
         foreach ($cgelements as $cgelement) {
             echo $this->action . $this->delim . "addcontactgroup" . $this->delim . $element['host_name'] . $this->delim . $cgelement['service_description'] . $this->delim . $cgelement['cg_name'] . "\n";
         }
         $contactRel = new Centreon_Object_Relation_Contact_Service();
         $celements = $contactRel->getMergedParameters(array("contact_name"), array('service_description'), -1, 0, null, null, array("service_register" => $this->register, "service_id" => $element['service_id']), "AND");
         foreach ($celements as $celement) {
             echo $this->action . $this->delim . "addcontact" . $this->delim . $element['host_name'] . $this->delim . $celement['service_description'] . $this->delim . $celement['contact_name'] . "\n";
         }
         $trapRel = new Centreon_Object_Relation_Trap_Service();
         $telements = $trapRel->getMergedParameters(array("traps_name"), array('service_description'), -1, 0, null, null, array("service_register" => $this->register, "service.service_id" => $element['service_id']), "AND");
         foreach ($telements as $telement) {
             echo $this->action . $this->delim . "addtrap" . $this->delim . $element['host_name'] . $this->delim . $telement['service_description'] . $this->delim . $telement['traps_name'] . "\n";
         }
     }
 }