PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/www/include/configuration/configObject/escalation/formEscalation.php

https://gitlab.com/florianocomercial/centreon
PHP | 342 lines | 234 code | 39 blank | 69 comment | 24 complexity | 974166d1cf2a7f9a43365ba5fe4748b7 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. */
  35. if (!isset($centreon)) {
  36. exit();
  37. }
  38. require_once _CENTREON_PATH_ . 'www/class/centreonLDAP.class.php';
  39. require_once _CENTREON_PATH_ . 'www/class/centreonContactgroup.class.php';
  40. /* Init connection to storage db */
  41. require_once _CENTREON_PATH_ . "/www/class/centreonBroker.class.php";
  42. $brk = new CentreonBroker($pearDB);
  43. /* hosts */
  44. $hosts = $acl->getHostAclConf(
  45. null,
  46. $centreon->broker->getBroker(),
  47. array(
  48. 'fields' => array('host.host_id', 'host.host_name'),
  49. 'keys' => array('host_id'),
  50. 'get_row' => 'host_name',
  51. 'order' => array('host.host_name')
  52. )
  53. );
  54. /* notification contact groups */
  55. $notifCgs = array();
  56. $cg = new CentreonContactgroup($pearDB);
  57. if ($oreon->user->admin) {
  58. $notifCgs = $cg->getListContactgroup(true);
  59. } else {
  60. $cgAcl = $acl->getContactGroupAclConf(
  61. array(
  62. 'fields' => array('cg_id', 'cg_name'),
  63. 'get_row' => 'cg_name',
  64. 'keys' => array('cg_id'),
  65. 'order' => array('cg_name')
  66. )
  67. );
  68. $cgLdap = $cg->getListContactgroup(true, true);
  69. $notifCgs = array_intersect_key($cgLdap, $cgAcl);
  70. }
  71. /*
  72. * Database retrieve information for Escalation
  73. */
  74. $initialValues = array();
  75. $esc = array();
  76. if (($o == "c" || $o == "w") && $esc_id) {
  77. $DBRESULT = $pearDB->query("SELECT * FROM escalation WHERE esc_id = '".$esc_id."' LIMIT 1");
  78. # Set base value
  79. $esc = array_map("myDecode", $DBRESULT->fetchRow());
  80. # Set Host Options
  81. $esc["escalation_options1"] = explode(',', $esc["escalation_options1"]);
  82. foreach ($esc["escalation_options1"] as $key => $value) {
  83. $esc["escalation_options1"][trim($value)] = 1;
  84. }
  85. # Set Service Options
  86. $esc["escalation_options2"] = explode(',', $esc["escalation_options2"]);
  87. foreach ($esc["escalation_options2"] as $key => $value) {
  88. $esc["escalation_options2"][trim($value)] = 1;
  89. }
  90. }
  91. /*
  92. * Database retrieve information for differents elements list we need on the page
  93. */
  94. #
  95. # Host comes from DB -> Store in $hosts Array
  96. $hosts = array();
  97. $DBRESULT = $pearDB->query("SELECT host_id, host_name FROM host WHERE host_register = '1' ORDER BY host_name");
  98. while($host = $DBRESULT->fetchRow()) {
  99. $hosts[$host["host_id"]] = $host["host_name"];
  100. }
  101. $DBRESULT->free();
  102. # Meta Services comes from DB -> Store in $metas Array
  103. $metas = array();
  104. $DBRESULT = $pearDB->query(
  105. "SELECT meta_id, meta_name
  106. FROM meta_service ".
  107. $acl->queryBuilder("WHERE", "meta_id", $acl->getMetaServiceString()).
  108. " ORDER BY meta_name"
  109. );
  110. while ($meta = $DBRESULT->fetchRow()) {
  111. $metas[$meta["meta_id"]] = $meta["meta_name"];
  112. }
  113. $DBRESULT->free();
  114. # Contact Groups comes from DB -> Store in $cgs Array
  115. $cgs = array();
  116. $cg = new CentreonContactgroup($pearDB);
  117. $cgs = $cg->getListContactgroup(true);
  118. # TimePeriods comes from DB -> Store in $tps Array
  119. $tps = array();
  120. $DBRESULT = $pearDB->query("SELECT tp_id, tp_name FROM timeperiod ORDER BY tp_name");
  121. while ($tp = $DBRESULT->fetchRow()) {
  122. $tps[$tp["tp_id"]] = $tp["tp_name"];
  123. }
  124. $DBRESULT->free();
  125. #
  126. # End of "database-retrieved" information
  127. ##########################################################
  128. ##########################################################
  129. # Var information to format the element
  130. #
  131. $attrsText = array("size"=>"30");
  132. $attrsText2 = array("size"=>"10");
  133. $attrsAdvSelect = array("style" => "width: 300px; height: 150px;");
  134. $attrsAdvSelect2 = array("style" => "width: 300px; height: 400px;");
  135. $attrsTextarea = array("rows"=>"5", "cols"=>"80");
  136. $eTemplate = '<table><tr><td><div class="ams">{label_2}</div>{unselected}</td><td align="center">{add}<br /><br /><br />{remove}</td><td><div class="ams">{label_3}</div>{selected}</td></tr></table>';
  137. #
  138. ## Form begin
  139. #
  140. $form = new HTML_QuickForm('Form', 'post', "?p=".$p);
  141. if ($o == "a") {
  142. $form->addElement('header', 'title', _("Add an Escalation"));
  143. } elseif ($o == "c") {
  144. $form->addElement('header', 'title', _("Modify an Escalation"));
  145. } elseif ($o == "w") {
  146. $form->addElement('header', 'title', _("View an Escalation"));
  147. }
  148. #
  149. ## Escalation basic information
  150. #
  151. $form->addElement('header', 'information', _("Information"));
  152. $form->addElement('text', 'esc_name', _("Escalation Name"), $attrsText);
  153. $form->addElement('text', 'esc_alias', _("Alias"), $attrsText);
  154. $form->addElement('text', 'first_notification', _("First Notification"), $attrsText2);
  155. $form->addElement('text', 'last_notification', _("Last Notification"), $attrsText2);
  156. $form->addElement('text', 'notification_interval', _("Notification Interval"), $attrsText2);
  157. $attrTimeperiods = array(
  158. 'datasourceOrigin' => 'ajax',
  159. 'availableDatasetRoute' => './include/common/webServices/rest/internal.php?object=centreon_configuration_timeperiod&action=list',
  160. 'multiple' => false,
  161. 'linkedObject' => 'centreonTimeperiod'
  162. );
  163. $form->addElement('select2', 'escalation_period', _("Escalation Period"), array(), $attrTimeperiods);
  164. $tab = array();
  165. $tab[] = HTML_QuickForm::createElement('checkbox', 'd', '&nbsp;', _("Down"));
  166. $tab[] = HTML_QuickForm::createElement('checkbox', 'u', '&nbsp;', _("Unreachable"));
  167. $tab[] = HTML_QuickForm::createElement('checkbox', 'r', '&nbsp;', _("Recovery"));
  168. $form->addGroup($tab, 'escalation_options1', _("Hosts Escalation Options"), '&nbsp;&nbsp;');
  169. $tab = array();
  170. $tab[] = HTML_QuickForm::createElement('checkbox', 'w', '&nbsp;', _("Warning"));
  171. $tab[] = HTML_QuickForm::createElement('checkbox', 'u', '&nbsp;', _("Unknown"));
  172. $tab[] = HTML_QuickForm::createElement('checkbox', 'c', '&nbsp;', _("Critical"));
  173. $tab[] = HTML_QuickForm::createElement('checkbox', 'r', '&nbsp;', _("Recovery"));
  174. $form->addGroup($tab, 'escalation_options2', _("Services Escalation Options"), '&nbsp;&nbsp;');
  175. $form->addElement('textarea', 'esc_comment', _("Comments"), $attrsTextarea);
  176. $attrContactgroups = array(
  177. 'datasourceOrigin' => 'ajax',
  178. 'defaultDatasetRoute' => './include/common/webServices/rest/internal.php?object=centreon_configuration_contactgroup&action=defaultValues&target=escalation&field=esc_cgs&id=' . $esc_id,
  179. 'availableDatasetRoute' => './include/common/webServices/rest/internal.php?object=centreon_configuration_contactgroup&action=list',
  180. 'multiple' => true,
  181. 'linkedObject' => 'centreonContactgroup'
  182. );
  183. $form->addElement('select2', 'esc_cgs', _("Linked Contact Groups"), array(), $attrContactgroups);
  184. $form->addElement('checkbox', 'host_inheritance_to_services', '', _('Host inheritance to services'));
  185. $form->addElement('checkbox', 'hostgroup_inheritance_to_services', '', _('Hostgroup inheritance to services'));
  186. $attrHosts = array(
  187. 'datasourceOrigin' => 'ajax',
  188. 'defaultDatasetRoute' => './include/common/webServices/rest/internal.php?object=centreon_configuration_host&action=defaultValues&target=escalation&field=esc_hosts&id=' . $esc_id,
  189. 'availableDatasetRoute' => './include/common/webServices/rest/internal.php?object=centreon_configuration_host&action=list',
  190. 'multiple' => true,
  191. 'linkedObject' => 'centreonHost'
  192. );
  193. $form->addElement('select2', 'esc_hosts', _("Hosts"), array(), $attrHosts);
  194. $attrServices = array(
  195. 'datasourceOrigin' => 'ajax',
  196. 'defaultDatasetRoute' => './include/common/webServices/rest/internal.php?object=centreon_configuration_service&action=defaultValues&target=escalation&field=esc_hServices&id=' . $esc_id,
  197. 'availableDatasetRoute' => './include/common/webServices/rest/internal.php?object=centreon_configuration_service&action=list',
  198. 'multiple' => true,
  199. 'linkedObject' => 'centreonService'
  200. );
  201. $form->addElement('select2', 'esc_hServices', _("Services by Host"), array(), $attrServices);
  202. $attrHostgroups = array(
  203. 'datasourceOrigin' => 'ajax',
  204. 'defaultDatasetRoute' => './include/common/webServices/rest/internal.php?object=centreon_configuration_hostgroup&action=defaultValues&target=escalation&field=esc_hgs&id=' . $esc_id,
  205. 'availableDatasetRoute' => './include/common/webServices/rest/internal.php?object=centreon_configuration_hostgroup&action=list',
  206. 'multiple' => true,
  207. 'linkedObject' => 'centreonHostgroups'
  208. );
  209. $form->addElement('select2', 'esc_hgs', _("Host Group"), array(), $attrHostgroups);
  210. $attrMetas = array(
  211. 'datasourceOrigin' => 'ajax',
  212. 'defaultDatasetRoute' => './include/common/webServices/rest/internal.php?object=centreon_configuration_meta&action=defaultValues&target=escalation&field=esc_metas&id=' . $esc_id,
  213. 'availableDatasetRoute' => './include/common/webServices/rest/internal.php?object=centreon_configuration_meta&action=list',
  214. 'multiple' => true,
  215. 'linkedObject' => 'centreonMeta'
  216. );
  217. $form->addElement('select2', 'esc_metas', _("Meta Service"), array(), $attrMetas);
  218. $attrServicegroups = array(
  219. 'datasourceOrigin' => 'ajax',
  220. 'defaultDatasetRoute' => './include/common/webServices/rest/internal.php?object=centreon_configuration_servicegroup&action=defaultValues&target=escalation&field=esc_sgs&id=' . $esc_id,
  221. 'availableDatasetRoute' => './include/common/webServices/rest/internal.php?object=centreon_configuration_servicegroup&action=list',
  222. 'multiple' => true,
  223. 'linkedObject' => 'centreonServicegroups'
  224. );
  225. $form->addElement('select2', 'esc_sgs', _("Service Group"), array(), $attrServicegroups);
  226. $form->addElement('hidden', 'esc_id');
  227. $redirect = $form->addElement('hidden', 'o');
  228. $redirect->setValue($o);
  229. $init = $form->addElement('hidden', 'initialValues');
  230. $init->setValue(serialize($initialValues));
  231. #
  232. ## Form Rules
  233. #
  234. $form->applyFilter('__ALL__', 'myTrim');
  235. $form->addRule('esc_name', _("Compulsory Name"), 'required');
  236. $form->addRule('first_notification', _("Required Field"), 'required');
  237. $form->addRule('last_notification', _("Required Field"), 'required');
  238. $form->addRule('notification_interval', _("Required Field"), 'required');
  239. $form->addRule('esc_cgs', _("Required Field"), 'required');
  240. $form->addRule('dep_hostChilds', _("Required Field"), 'required');
  241. $form->registerRule('exist', 'callback', 'testExistence');
  242. $form->addRule('esc_name', _("Name is already in use"), 'exist');
  243. $form->setRequiredNote("<font style='color: red;'>*</font>&nbsp;". _("Required fields"));
  244. # Smarty template Init
  245. $tpl = new Smarty();
  246. $tpl = initSmartyTpl($path, $tpl);
  247. # Just watch an Escalation information
  248. if ($o == "w") {
  249. if ($centreon->user->access->page($p) != 2) {
  250. $form->addElement(
  251. "button",
  252. "change",
  253. _("Modify"),
  254. array("onClick"=>"javascript:window.location.href='?p=".$p."&o=c&esc_id=".$esc_id."'")
  255. );
  256. }
  257. $form->setDefaults($esc);
  258. $form->freeze();
  259. } elseif ($o == "c") { # Modify an Escalation information
  260. $subC = $form->addElement('submit', 'submitC', _("Save"), array("class" => "btc bt_success"));
  261. $res = $form->addElement('reset', 'reset', _("Reset"), array("class" => "btc bt_default"));
  262. $form->setDefaults($esc);
  263. } elseif ($o == "a") { # Add an Escalation information
  264. $subA = $form->addElement('submit', 'submitA', _("Save"), array("class" => "btc bt_success"));
  265. $res = $form->addElement('reset', 'reset', _("Reset"), array("class" => "btc bt_default"));
  266. }
  267. $tpl->assign('time_unit', " * ".$centreon->optGen["interval_length"]." "._("seconds"));
  268. $tpl->assign(
  269. "helpattr",
  270. 'TITLE, "'._("Help").'", CLOSEBTN, true, FIX, [this, 0, 5], BGCOLOR, "#ffff99", BORDERCOLOR, "orange",'
  271. . ' TITLEFONTCOLOR, "black", TITLEBGCOLOR, "orange", CLOSEBTNCOLORS, ["","black", "white", "red"], WIDTH, -300,'
  272. . ' SHADOW, true, TEXTALIGN, "justify"'
  273. );
  274. # prepare help texts
  275. $helptext = "";
  276. include_once("help.php");
  277. foreach ($help as $key => $text) {
  278. $helptext .= '<span style="display:none" id="help:'.$key.'">'.$text.'</span>'."\n";
  279. }
  280. $tpl->assign("helptext", $helptext);
  281. $valid = false;
  282. if ($form->validate()) {
  283. $escObj = $form->getElement('esc_id');
  284. if ($form->getSubmitValue("submitA")) {
  285. $escObj->setValue(insertEscalationInDB());
  286. } elseif ($form->getSubmitValue("submitC")) {
  287. updateEscalationInDB($escObj->getValue("esc_id"));
  288. }
  289. $o = NULL;
  290. $valid = true;
  291. }
  292. if ($valid) {
  293. require_once("listEscalation.php");
  294. } else {
  295. #Apply a template definition
  296. $renderer = new HTML_QuickForm_Renderer_ArraySmarty($tpl, true);
  297. $renderer->setRequiredTemplate('{$label}&nbsp;<font color="red" size="1">*</font>');
  298. $renderer->setErrorTemplate('<font color="red">{$error}</font><br />{$html}');
  299. $form->accept($renderer);
  300. $tpl->assign('form', $renderer->toArray());
  301. $tpl->assign('o', $o);
  302. $tpl->display("formEscalation.ihtml");
  303. }