PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/www/include/configuration/configObject/servicegroup_dependency/DB-Func.php

https://gitlab.com/florianocomercial/centreon
PHP | 263 lines | 208 code | 17 blank | 38 comment | 56 complexity | cb0fb3097de3159c53ae3d0ab95d06f1 MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright 2005-2015 Centreon
  4. * Centreon is developped by : Julien Mathis and Romain Le Merlus under
  5. * GPL Licence 2.0.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it under
  8. * the terms of the GNU General Public License as published by the Free Software
  9. * Foundation ; either version 2 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT ANY
  12. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  13. * PARTICULAR PURPOSE. See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, see <http://www.gnu.org/licenses>.
  17. *
  18. * Linking this program statically or dynamically with other modules is making a
  19. * combined work based on this program. Thus, the terms and conditions of the GNU
  20. * General Public License cover the whole combination.
  21. *
  22. * As a special exception, the copyright holders of this program give Centreon
  23. * permission to link this program with independent modules to produce an executable,
  24. * regardless of the license terms of these independent modules, and to copy and
  25. * distribute the resulting executable under terms of Centreon choice, provided that
  26. * Centreon also meet, for each linked independent module, the terms and conditions
  27. * of the license of that module. An independent module is a module which is not
  28. * derived from this program. If you modify this program, you may extend this
  29. * exception to your version of the program, but you are not obliged to do so. If you
  30. * do not wish to do so, delete this exception statement from your version.
  31. *
  32. * For more information : contact@centreon.com
  33. *
  34. * SVN : $URL$
  35. * SVN : $Id$
  36. *
  37. */
  38. if (!isset ($oreon))
  39. exit ();
  40. function testServiceGroupDependencyExistence ($name = NULL) {
  41. global $pearDB;
  42. global $form;
  43. CentreonDependency::purgeObsoleteDependencies($pearDB);
  44. $id = NULL;
  45. if (isset($form))
  46. $id = $form->getSubmitValue('dep_id');
  47. $DBRESULT = $pearDB->query("SELECT dep_name, dep_id FROM dependency WHERE dep_name = '".htmlentities($name, ENT_QUOTES, "UTF-8")."'");
  48. $dep = $DBRESULT->fetchRow();
  49. #Modif case
  50. if ($DBRESULT->numRows() >= 1 && $dep["dep_id"] == $id)
  51. return true;
  52. #Duplicate entry
  53. else if ($DBRESULT->numRows() >= 1 && $dep["dep_id"] != $id)
  54. return false;
  55. else
  56. return true;
  57. }
  58. function testServiceGroupDependencyCycle ($childs = NULL) {
  59. global $pearDB;
  60. global $form;
  61. $parents = array();
  62. $childs = array();
  63. if (isset($form)) {
  64. $parents = $form->getSubmitValue('dep_sgParents');
  65. $childs = $form->getSubmitValue('dep_sgChilds');
  66. $childs = array_flip($childs);
  67. }
  68. foreach ($parents as $parent)
  69. if (array_key_exists($parent, $childs))
  70. return false;
  71. return true;
  72. }
  73. function deleteServiceGroupDependencyInDB ($dependencies = array()) {
  74. global $pearDB, $oreon;
  75. foreach($dependencies as $key=>$value) {
  76. $DBRESULT2 = $pearDB->query("SELECT dep_name FROM `dependency` WHERE `dep_id` = '".$key."' LIMIT 1");
  77. $row = $DBRESULT2->fetchRow();
  78. $DBRESULT = $pearDB->query("DELETE FROM dependency WHERE dep_id = '".$key."'");
  79. $oreon->CentreonLogAction->insertLog("servicegroup dependency", $key, $row['dep_name'], "d");
  80. }
  81. }
  82. function multipleServiceGroupDependencyInDB ($dependencies = array(), $nbrDup = array()) {
  83. foreach($dependencies as $key=>$value) {
  84. global $pearDB, $oreon;
  85. $DBRESULT = $pearDB->query("SELECT * FROM dependency WHERE dep_id = '".$key."' LIMIT 1");
  86. $row = $DBRESULT->fetchRow();
  87. $row["dep_id"] = '';
  88. for ($i = 1; $i <= $nbrDup[$key]; $i++) {
  89. $val = null;
  90. foreach ($row as $key2=>$value2) {
  91. $key2 == "dep_name" ? ($dep_name = $value2 = $value2."_".$i) : null;
  92. $val ? $val .= ($value2!=NULL?(", '".$value2."'"):", NULL") : $val .= ($value2!=NULL?("'".$value2."'"):"NULL");
  93. if ($key2 != "dep_id") {
  94. $fields[$key2] = $value2;
  95. }
  96. if (isset($dep_name)) {
  97. $fields["dep_name"] = $dep_name;
  98. }
  99. }
  100. if (isset($dep_name) && testServiceGroupDependencyExistence($dep_name)) {
  101. $val ? $rq = "INSERT INTO dependency VALUES (".$val.")" : $rq = null;
  102. $DBRESULT = $pearDB->query($rq);
  103. $DBRESULT = $pearDB->query("SELECT MAX(dep_id) FROM dependency");
  104. $maxId = $DBRESULT->fetchRow();
  105. if (isset($maxId["MAX(dep_id)"])) {
  106. $DBRESULT = $pearDB->query("SELECT DISTINCT servicegroup_sg_id FROM dependency_servicegroupParent_relation WHERE dependency_dep_id = '".$key."'");
  107. $fields["dep_sgParents"] = "";
  108. while($sg = $DBRESULT->fetchRow()){
  109. $DBRESULT2 = $pearDB->query("INSERT INTO dependency_servicegroupParent_relation VALUES ('', '".$maxId["MAX(dep_id)"]."', '".$sg["servicegroup_sg_id"]."')");
  110. $fields["dep_sgParents"] .= $sg["servicegroup_sg_id"] . ",";
  111. }
  112. $fields["dep_sgParents"] = trim($fields["dep_sgParents"], ",");
  113. $DBRESULT->free();
  114. $DBRESULT = $pearDB->query("SELECT DISTINCT servicegroup_sg_id FROM dependency_servicegroupChild_relation WHERE dependency_dep_id = '".$key."'");
  115. $fields["dep_sgChilds"] = "";
  116. while($sg = $DBRESULT->fetchRow()) {
  117. $DBRESULT2 = $pearDB->query("INSERT INTO dependency_servicegroupChild_relation VALUES ('', '".$maxId["MAX(dep_id)"]."', '".$sg["servicegroup_sg_id"]."')");
  118. $fields["dep_sgChilds"] .= $sg["servicegroup_sg_id"] . ",";
  119. }
  120. $fields["dep_sgChilds"] = trim($fields["dep_sgChilds"], ",");
  121. $oreon->CentreonLogAction->insertLog("servicegroup dependency", $maxId["MAX(dep_id)"], $dep_name, "a", $fields);
  122. $DBRESULT->free();
  123. }
  124. }
  125. }
  126. }
  127. }
  128. function updateServiceGroupDependencyInDB ($dep_id = NULL) {
  129. if (!$dep_id) exit();
  130. updateServiceGroupDependency($dep_id);
  131. updateServiceGroupDependencyServiceGroupParents($dep_id);
  132. updateServiceGroupDependencyServiceGroupChilds($dep_id);
  133. }
  134. function insertServiceGroupDependencyInDB ($ret = array()) {
  135. $dep_id = insertServiceGroupDependency($ret);
  136. updateServiceGroupDependencyServiceGroupParents($dep_id, $ret);
  137. updateServiceGroupDependencyServiceGroupChilds($dep_id, $ret);
  138. return ($dep_id);
  139. }
  140. function insertServiceGroupDependency($ret = array()) {
  141. global $form;
  142. global $pearDB, $oreon;
  143. if (!count($ret))
  144. $ret = $form->getSubmitValues();
  145. $rq = "INSERT INTO dependency ";
  146. $rq .= "(dep_name, dep_description, inherits_parent, execution_failure_criteria, notification_failure_criteria, dep_comment) ";
  147. $rq .= "VALUES (";
  148. isset($ret["dep_name"]) && $ret["dep_name"] != NULL ? $rq .= "'".htmlentities($ret["dep_name"], ENT_QUOTES, "UTF-8")."', " : $rq .= "NULL, ";
  149. isset($ret["dep_description"]) && $ret["dep_description"] != NULL ? $rq .= "'".htmlentities($ret["dep_description"], ENT_QUOTES, "UTF-8")."', " : $rq .= "NULL, ";
  150. isset($ret["inherits_parent"]["inherits_parent"]) && $ret["inherits_parent"]["inherits_parent"] != NULL ? $rq .= "'".$ret["inherits_parent"]["inherits_parent"]."', " : $rq .= "NULL, ";
  151. isset($ret["execution_failure_criteria"]) && $ret["execution_failure_criteria"] != NULL ? $rq .= "'".implode(",", array_keys($ret["execution_failure_criteria"]))."', " : $rq .= "NULL, ";
  152. isset($ret["notification_failure_criteria"]) && $ret["notification_failure_criteria"] != NULL ? $rq .= "'".implode(",", array_keys($ret["notification_failure_criteria"]))."', " : $rq .= "NULL, ";
  153. isset($ret["dep_comment"]) && $ret["dep_comment"] != NULL ? $rq .= "'".htmlentities($ret["dep_comment"], ENT_QUOTES, "UTF-8")."' " : $rq .= "NULL ";
  154. $rq .= ")";
  155. $DBRESULT = $pearDB->query($rq);
  156. $DBRESULT = $pearDB->query("SELECT MAX(dep_id) FROM dependency");
  157. $dep_id = $DBRESULT->fetchRow();
  158. $fields["dep_name"] = htmlentities($ret["dep_name"], ENT_QUOTES, "UTF-8");
  159. $fields["dep_description"] = htmlentities($ret["dep_description"], ENT_QUOTES, "UTF-8");
  160. $fields["inherits_parent"] = $ret["inherits_parent"]["inherits_parent"];
  161. $fields["execution_failure_criteria"] = implode(",", array_keys($ret["execution_failure_criteria"]));
  162. $fields["notification_failure_criteria"] = implode(",", array_keys($ret["notification_failure_criteria"]));
  163. $fields["dep_comment"] = htmlentities($ret["dep_comment"], ENT_QUOTES, "UTF-8");
  164. $fields["dep_sgParents"] = "";
  165. if (isset($ret["dep_sgParents"]))
  166. $fields["dep_sgParents"] = implode(",", $ret["dep_sgParents"]);
  167. $fields["dep_sgChilds"] = "";
  168. if (isset($ret["dep_sgChilds"]))
  169. $fields["dep_sgChilds"] = implode(",", $ret["dep_sgChilds"]);
  170. $oreon->CentreonLogAction->insertLog("servicegroup dependency", $dep_id["MAX(dep_id)"], htmlentities($ret["dep_name"], ENT_QUOTES, "UTF-8"), "a", $fields);
  171. return ($dep_id["MAX(dep_id)"]);
  172. }
  173. function updateServiceGroupDependency($dep_id = null) {
  174. if (!$dep_id) exit();
  175. global $form;
  176. global $pearDB, $oreon;
  177. $ret = array();
  178. $ret = $form->getSubmitValues();
  179. $rq = "UPDATE dependency SET ";
  180. $rq .= "dep_name = ";
  181. isset($ret["dep_name"]) && $ret["dep_name"] != NULL ? $rq .= "'".htmlentities($ret["dep_name"], ENT_QUOTES, "UTF-8")."', " : $rq .= "NULL, ";
  182. $rq .= "dep_description = ";
  183. isset($ret["dep_description"]) && $ret["dep_description"] != NULL ? $rq .= "'".htmlentities($ret["dep_description"], ENT_QUOTES, "UTF-8")."', " : $rq .= "NULL, ";
  184. $rq .= "inherits_parent = ";
  185. isset($ret["inherits_parent"]["inherits_parent"]) && $ret["inherits_parent"]["inherits_parent"] != NULL ? $rq .= "'".$ret["inherits_parent"]["inherits_parent"]."', " : $rq .= "NULL, ";
  186. $rq .= "execution_failure_criteria = ";
  187. isset($ret["execution_failure_criteria"]) && $ret["execution_failure_criteria"] != NULL ? $rq .= "'".implode(",", array_keys($ret["execution_failure_criteria"]))."', " : $rq .= "NULL, ";
  188. $rq .= "notification_failure_criteria = ";
  189. isset($ret["notification_failure_criteria"]) && $ret["notification_failure_criteria"] != NULL ? $rq .= "'".implode(",", array_keys($ret["notification_failure_criteria"]))."', " : $rq .= "NULL, ";
  190. $rq .= "dep_comment = ";
  191. isset($ret["dep_comment"]) && $ret["dep_comment"] != NULL ? $rq .= "'".htmlentities($ret["dep_comment"], ENT_QUOTES, "UTF-8")."' " : $rq .= "NULL ";
  192. $rq .= "WHERE dep_id = '".$dep_id."'";
  193. $DBRESULT = $pearDB->query($rq);
  194. $fields["dep_name"] = htmlentities($ret["dep_name"], ENT_QUOTES, "UTF-8");
  195. $fields["dep_description"] = htmlentities($ret["dep_description"], ENT_QUOTES, "UTF-8");
  196. $fields["inherits_parent"] = $ret["inherits_parent"]["inherits_parent"];
  197. $fields["execution_failure_criteria"] = implode(",", array_keys($ret["execution_failure_criteria"]));
  198. $fields["notification_failure_criteria"] = implode(",", array_keys($ret["notification_failure_criteria"]));
  199. $fields["dep_comment"] = htmlentities($ret["dep_comment"], ENT_QUOTES, "UTF-8");
  200. $fields["dep_sgParents"] = "";
  201. if (isset($ret["dep_sgParents"]))
  202. $fields["dep_sgParents"] = implode(",", $ret["dep_sgParents"]);
  203. $fields["dep_sgChilds"] = "";
  204. if (isset($ret["dep_sgChilds"]))
  205. $fields["dep_sgChilds"] = implode(",", $ret["dep_sgChilds"]);
  206. $oreon->CentreonLogAction->insertLog("servicegroup dependency", $dep_id, htmlentities($ret["dep_name"], ENT_QUOTES, "UTF-8"), "c", $fields);
  207. }
  208. function updateServiceGroupDependencyServiceGroupParents($dep_id = null, $ret = array()) {
  209. if (!$dep_id) exit();
  210. global $form;
  211. global $pearDB;
  212. $rq = "DELETE FROM dependency_servicegroupParent_relation ";
  213. $rq .= "WHERE dependency_dep_id = '".$dep_id."'";
  214. $DBRESULT = $pearDB->query($rq);
  215. if (isset($ret["dep_sgParents"]))
  216. $ret = $ret["dep_sgParents"];
  217. else
  218. $ret = CentreonUtils::mergeWithInitialValues($form, 'dep_sgParents');
  219. for($i = 0; $i < count($ret); $i++) {
  220. $rq = "INSERT INTO dependency_servicegroupParent_relation ";
  221. $rq .= "(dependency_dep_id, servicegroup_sg_id) ";
  222. $rq .= "VALUES ";
  223. $rq .= "('".$dep_id."', '".$ret[$i]."')";
  224. $DBRESULT = $pearDB->query($rq);
  225. }
  226. }
  227. function updateServiceGroupDependencyServiceGroupChilds($dep_id = null, $ret = array()) {
  228. if (!$dep_id) exit();
  229. global $form;
  230. global $pearDB;
  231. $rq = "DELETE FROM dependency_servicegroupChild_relation ";
  232. $rq .= "WHERE dependency_dep_id = '".$dep_id."'";
  233. $DBRESULT = $pearDB->query($rq);
  234. if (isset($ret["dep_sgChilds"]))
  235. $ret = $ret["dep_sgChilds"];
  236. else
  237. $ret = CentreonUtils::mergeWithInitialValues($form, 'dep_sgChilds');
  238. for($i = 0; $i < count($ret); $i++) {
  239. $rq = "INSERT INTO dependency_servicegroupChild_relation ";
  240. $rq .= "(dependency_dep_id, servicegroup_sg_id) ";
  241. $rq .= "VALUES ";
  242. $rq .= "('".$dep_id."', '".$ret[$i]."')";
  243. $DBRESULT = $pearDB->query($rq);
  244. }
  245. }
  246. ?>