From 8be11a79a8416b20f56ad7dab5f31f2eded764bb Mon Sep 17 00:00:00 2001
From: Kevin Duret <kduret@centreon.com>
Date: Tue, 4 Apr 2017 16:55:30 +0200
Subject: [PATCH] fix service link in kb

---
 .../procedures_Proxy.class.php                | 157 ++++++++----------
 1 file changed, 67 insertions(+), 90 deletions(-)

diff --git a/www/class/centreon-knowledge/procedures_Proxy.class.php b/www/class/centreon-knowledge/procedures_Proxy.class.php
index 0b678b5530..e44b0c1c1c 100644
--- a/www/class/centreon-knowledge/procedures_Proxy.class.php
+++ b/www/class/centreon-knowledge/procedures_Proxy.class.php
@@ -12,6 +12,8 @@
  */
 
 require_once _CENTREON_PATH_ . '/www/include/configuration/configKnowledge/functions.php';
+require_once _CENTREON_PATH_ . '/www/class/centreonHost.class.php';
+require_once _CENTREON_PATH_ . '/www/class/centreonService.class.php';
 
 class procedures_Proxy
 {
@@ -21,12 +23,16 @@ class procedures_Proxy
     private $proc;
     public $url;
     private $wikiUrl;
+    private $hostObj;
+    private $serviceObj;
 
     public function __construct($pearDB, $db_prefix, $host_name, $service_description = null)
     {
         $this->DB = $pearDB;
         $this->hflag = 0;
         $this->sflag = 0;
+        $this->hostObj = new CentreonHost($this->DB);
+        $this->serviceObj = new CentreonService($this->DB);
 
         $conf = getWikiConfig($this->DB);
         $this->wikiUrl = $conf['kb_wiki_url'];
@@ -49,6 +55,51 @@ class procedures_Proxy
         }
     }
 
+    private function getHostId($hostName)
+    {
+        $result = $this->DB->query("SELECT host_id FROM host WHERE host_name LIKE '" . $hostName . "' ");
+        $row = $result->fetchRow();
+        $hostId = 0;
+        if ($row["host_id"]) {
+            $hostId = $row["host_id"];
+        }
+        return $hostId;
+    }
+
+    private function getServiceId($hostName, $serviceDescription)
+    {
+        /*
+         * Get Services attached to hosts
+         */
+        $query = "SELECT s.service_id " .
+            "FROM host h, service s, host_service_relation hsr " .
+            "WHERE hsr.host_host_id = h.host_id " .
+            "AND hsr.service_service_id = service_id " .
+            "AND h.host_name LIKE '" . $hostName . "' " .
+            "AND s.service_description LIKE '" . $serviceDescription . "' ";
+        $result = $this->DB->query($query);
+        while ($row = $result->fetchRow()) {
+            return $row["service_id"];
+        }
+        $result->free();
+        /*
+         * Get Services attached to hostgroups
+         */
+        $query = "SELECT s.service_id " .
+            "FROM hostgroup_relation hgr, host h, service s, host_service_relation hsr " .
+            "WHERE hgr.host_host_id = h.host_id " .
+            "AND hsr.hostgroup_hg_id = hgr.hostgroup_hg_id " .
+            "AND h.host_name LIKE '" . $hostName . "' " .
+            "AND service_id = hsr.service_service_id " .
+            "AND service_description LIKZ '" . $serviceDescription . "' ";
+        $result = $this->DB->query($query);
+        while ($row = $result->fetchRow()) {
+            return $row["service_id"];
+        }
+        $result->free();
+
+    }
+
     private function returnHostWikiUrl($host_name)
     {
         $this->proc->setHostInformations();
@@ -66,10 +117,12 @@ class procedures_Proxy
         /*
          * Check if host can get a procedure on templates
          */
-        $templates = $this->getHostTemplateList($host_name);
-        foreach ($templates as $tpl) {
-            if (isset($procList["Host-Template_:_" . $tpl])) {
-                $this->url = $this->wikiUrl . "/index.php?title=Host-Template_:_" . $tpl;
+        $hostId = $this->getHostId($host_name);
+        $templates = $this->hostObj->getTemplateChain($hostId);
+        foreach ($templates as $template) {
+            $templateName = $template['host_name'];
+            if (isset($procList["Host-Template_:_" . $templateName])) {
+                $this->url = $this->wikiUrl . "/index.php?title=Host-Template_:_" . $templateName;
                 return;
             }
         }
@@ -89,101 +142,25 @@ class procedures_Proxy
          * Check Service
          */
         $service_description = str_replace(' ', '_', $service_description);
-        if (isset($procList["Service_:_" . trim($host_name . "_" . $service_description)])) {
-            $this->url = $this->wikiUrl . "/index.php?title=Service_:_" . $host_name . "_" . $service_description;
+
+        if (isset($procList["Service_:_" . trim($host_name . "_/_" . $service_description)])) {
+            $this->url = $this->wikiUrl . "/index.php?title=Service_:_" . $host_name . "_/_" . $service_description;
             return;
         }
 
         /*
          * Check service Template
          */
-        $host_id = $this->getMyHostID($host_name);
-        $templates = $this->getMyServiceTemplateModels($this->getMyServicesID($host_id, $service_description));
-        foreach ($templates as $key => $value) {
-            if (isset($procList["Service-Template_:_" . trim($value)])) {
-                $this->url = $this->wikiUrl . "/index.php?title=Service-Template_:_" . $value;
+        $serviceId = $this->getServiceId($host_name, $service_description);
+        $templates = $this->serviceObj->getTemplatesChain($serviceId);
+        foreach ($templates as $templateId) {
+            $templateDescription = $this->serviceObj->getServiceDesc($templateId);
+            if (isset($procList["Service-Template_:_" . $templateDescription])) {
+                $this->url = $this->wikiUrl . "/index.php?title=Service-Template_:_" . $templateDescription;
                 return;
             }
         }
-        $this->returnHostWikiUrl($host_name);
-    }
-
-    function getMyHostID($host_name = NULL)
-    {
-        $DBRESULT =& $this->DB->query("SELECT host_id FROM host WHERE host_name = '" . $host_name . "' LIMIT 1");
-        $row =& $DBRESULT->fetchRow();
-        if ($row["host_id"])
-            return $row["host_id"];
-    }
 
-    function getMyServicesID($host_id, $service_description)
-    {
-        /*
-         * Get Services attached to hosts
-         */
-        $query = "SELECT service_id, service_description " .
-            "FROM service, host_service_relation hsr " .
-            "WHERE hsr.host_host_id = '" . $host_id . "' " .
-            "AND hsr.service_service_id = service_id " .
-            "AND service_description = '" . $service_description . "' ";
-        $DBRESULT =& $this->DB->query($query);
-        while ($elem =& $DBRESULT->fetchRow()) {
-            return $elem["service_id"];
-        }
-        $DBRESULT->free();
-
-        /*
-         * Get Services attached to hostgroups
-         */
-        $query = "SELECT service_id, service_description " .
-            "FROM hostgroup_relation hgr, service, host_service_relation hsr " .
-            "WHERE hgr.host_host_id = '" . $host_id . "' AND hsr.hostgroup_hg_id = hgr.hostgroup_hg_id " .
-            "AND service_id = hsr.service_service_id " .
-            "AND service_description = '" . $service_description . "' ";
-        $DBRESULT =& $this->DB->query($query);
-        while ($elem =& $DBRESULT->fetchRow()) {
-            return $elem["service_id"];
-        }
-        $DBRESULT->free();
-    }
-
-
-    private function getHostTemplateList($host_name)
-    {
-        $templates = array();
-
-        $DBRESULT =& $this->DB->query("SELECT host_tpl_id FROM `host_template_relation`, `host` WHERE host_host_id = host_id AND host_name = '" . $host_name . "' ORDER BY `order`");
-        while ($row =& $DBRESULT->fetchRow()) {
-            $DBRESULT2 =& $this->DB->query("SELECT host_name FROM host WHERE host_id = '" . $row['host_tpl_id'] . "' LIMIT 1");
-            $hTpl =& $DBRESULT2->fetchRow();
-            $templates[$row['host_tpl_id']] = html_entity_decode($hTpl["host_name"], ENT_QUOTES);
-        }
-        return $templates;
-    }
-
-    private function getMyServiceTemplateModels($service_id)
-    {
-        $tplArr = array();
-
-        $DBRESULT =& $this->DB->query("SELECT service_description, service_template_model_stm_id FROM service WHERE service_id = '" . $service_id . "' LIMIT 1");
-        $row =& $DBRESULT->fetchRow();
-        $DBRESULT->free();
-        $service_id = $row["service_template_model_stm_id"];
-        if ($row["service_description"])
-            $tplArr[$service_id] = $row["service_description"];
-        while (1) {
-            $DBRESULT =& $this->DB->query("SELECT service_description, service_template_model_stm_id FROM service WHERE service_id = '" . $service_id . "' LIMIT 1");
-            $row =& $DBRESULT->fetchRow();
-            $DBRESULT->free();
-            if ($row["service_description"])
-                $tplArr[$service_id] = $row["service_description"];
-            else
-                break;
-            if ($row["service_template_model_stm_id"])
-                $service_id = $row["service_template_model_stm_id"];
-            else
-                break;
-        }
-        return ($tplArr);
+        $this->returnHostWikiUrl($host_name);
     }
 }
-- 
GitLab