PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/www/include/configuration/configObject/timeperiod/formTimeperiod.php

https://gitlab.com/florianocomercial/centreon
PHP | 302 lines | 171 code | 31 blank | 100 comment | 44 complexity | e77104812a73eebcdec711325832452b 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. $tp = array();
  39. if (($o == "c" || $o == "w") && $tp_id) {
  40. $DBRESULT = $pearDB->query("SELECT * FROM timeperiod WHERE tp_id = '".$tp_id."' LIMIT 1");
  41. /*
  42. * Set base value
  43. */
  44. $tp = array_map("myDecode", $DBRESULT->fetchRow());
  45. $tp["contact_exclude"] = array();
  46. /*
  47. * Retrieves inclusions
  48. */
  49. $res = $pearDB->query("SELECT * FROM timeperiod_include_relations WHERE timeperiod_id = '".$tp_id."'");
  50. $tp["tp_include"] = array();
  51. while ($row = $res->fetchRow()) {
  52. $tp["tp_include"][] = $row['timeperiod_include_id'];
  53. }
  54. /*
  55. * Retrieves exclusions
  56. */
  57. $res = $pearDB->query("SELECT * FROM timeperiod_exclude_relations WHERE timeperiod_id = '". $tp_id."'");
  58. $tp["tp_exclude"] = array();
  59. while ($row = $res->fetchRow()) {
  60. $tp["tp_exclude"][] = $row['timeperiod_exclude_id'];
  61. }
  62. }
  63. $includeTP = array();
  64. $excludeTP = array();
  65. $DBRESULT = $pearDB->query("SELECT tp_name, tp_id FROM timeperiod WHERE tp_id != '". $tp_id ."'");
  66. while ($data = $DBRESULT->fetchRow()) {
  67. $excludeTP[$data["tp_id"]] = $data["tp_name"];
  68. $includeTP[$data["tp_id"]] = $data["tp_name"];
  69. }
  70. $DBRESULT->free();
  71. unset($data);
  72. /*
  73. * Gets list of timeperiod exceptions
  74. */
  75. $j = 0;
  76. $DBRESULT = $pearDB->query("SELECT exception_id, timeperiod_id, days, timerange FROM timeperiod_exceptions WHERE timeperiod_id = '". $tp_id ."' ORDER BY `days`");
  77. while ($exceptionTab = $DBRESULT->fetchRow()){
  78. $exception_id[$j] = $exceptionTab["exception_id"];
  79. $exception_days[$j] = $exceptionTab["days"];
  80. $exception_timerange[$j] = $exceptionTab["timerange"];
  81. $exception_timeperiod_id[$j] = $exceptionTab["timeperiod_id"];
  82. $j++;
  83. }
  84. $DBRESULT->free();
  85. /*
  86. * Var information to format the element
  87. */
  88. $attrsText = array("size"=>"35");
  89. $attrsTextLong = array("size"=>"55", "maxlength" => "200");
  90. $attrsAdvSelect = array("style" => "width: 300px; height: 130px;");
  91. $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>';
  92. $attrTimeperiods = array(
  93. 'datasourceOrigin' => 'ajax',
  94. 'availableDatasetRoute' => './include/common/webServices/rest/internal.php?object=centreon_configuration_timeperiod&action=list',
  95. 'multiple' => true,
  96. 'linkedObject' => 'centreonTimeperiod'
  97. );
  98. /*
  99. * Form begin
  100. */
  101. $form = new HTML_QuickForm('Form', 'post', "?p=".$p);
  102. if ($o == "a")
  103. $form->addElement('header', 'title', _("Add a Time Period"));
  104. else if ($o == "c")
  105. $form->addElement('header', 'title', _("Modify a Time Period"));
  106. else if ($o == "w")
  107. $form->addElement('header', 'title', _("View a Time Period"));
  108. /*
  109. * Time Period basic information
  110. */
  111. $form->addElement('header', 'information', _("General Information"));
  112. $form->addElement('text', 'tp_name', _("Time Period Name"), $attrsText);
  113. $form->addElement('text', 'tp_alias', _("Alias"), $attrsTextLong);
  114. /*
  115. * Notification informations
  116. */
  117. $form->addElement('header', 'include', _("Extended Settings"));
  118. $form->addElement('text', 'tp_sunday', _("Sunday"), $attrsTextLong);
  119. $form->addElement('text', 'tp_monday', _("Monday"), $attrsTextLong);
  120. $form->addElement('text', 'tp_tuesday', _("Tuesday"), $attrsTextLong);
  121. $form->addElement('text', 'tp_wednesday', _("Wednesday"), $attrsTextLong);
  122. $form->addElement('text', 'tp_thursday', _("Thursday"), $attrsTextLong);
  123. $form->addElement('text', 'tp_friday', _("Friday"), $attrsTextLong);
  124. $form->addElement('text', 'tp_saturday', _("Saturday"), $attrsTextLong);
  125. /*
  126. * Include Timeperiod
  127. */
  128. $attrTimeperiod1 = array_merge(
  129. $attrTimeperiods,
  130. array('defaultDatasetRoute' => './include/common/webServices/rest/internal.php?object=centreon_configuration_timeperiod&action=defaultValues&target=timeperiodRenderer&field=tp_include&id=' . $tp_id)
  131. );
  132. $form->addElement('select2', 'tp_include', _("Include Timeperiods"), array(), $attrTimeperiod1);
  133. /*
  134. * Multiple exceptions relations stored in DB
  135. */
  136. $mTp = array();
  137. $k = 0;
  138. $DBRESULT = $pearDB->query("SELECT exception_id FROM timeperiod_exceptions WHERE timeperiod_id = '". $tp_id ."'");
  139. while ($multiTp = $DBRESULT->fetchRow()){
  140. $mTp[$k] = $multiTp["exception_id"];
  141. $k++;
  142. }
  143. $DBRESULT->free();
  144. /*
  145. * Include javascript for dynamique entries
  146. */
  147. require_once "./include/configuration/configObject/timeperiod/timeperiod_JS.php";
  148. if ($o == "c" || $o == "a" || $o == "mc") {
  149. for ($k = 0 ; isset($mTp[$k]); $k++) {
  150. print "<script type=\"text/javascript\">";
  151. print "tab[$k] = ".$mTp[$k].";";
  152. print "</script>";
  153. }
  154. for ($k = 0; isset($exception_id[$k]); $k++) { ?>
  155. <script type="text/javascript">
  156. globalExceptionTabId[<?php echo $k;?>] = <?php echo $exception_id[$k];?>;
  157. globalExceptionTabName[<?php echo $k;?>] = '<?php echo $exception_days[$k];?>';
  158. globalExceptionTabTimerange[<?php echo $k;?>] = '<?php echo $exception_timerange[$k];?>';
  159. globalExceptionTabTimeperiodId[<?php echo $k;?>] = <?php echo $exception_timeperiod_id[$k];?>;
  160. </script>
  161. <?php
  162. }
  163. }
  164. /*
  165. * Further informations
  166. */
  167. $form->addElement('hidden', 'tp_id');
  168. $redirect = $form->addElement('hidden', 'o');
  169. $redirect->setValue($o);
  170. /*
  171. * Form Rules
  172. */
  173. function myReplace() {
  174. global $form;
  175. $ret = $form->getSubmitValues();
  176. return (str_replace(" ", "_", $ret["tp_name"]));
  177. }
  178. /*
  179. * Set rules
  180. */
  181. $form->applyFilter('__ALL__', 'myTrim');
  182. $form->applyFilter('tp_name', 'myReplace');
  183. $form->registerRule('exist', 'callback', 'testTPExistence');
  184. $form->registerRule('format', 'callback', 'checkHours');
  185. /*
  186. * Name Check
  187. */
  188. $form->addRule('tp_name', _("Compulsory Name"), 'required');
  189. $form->addRule('tp_name', _("Name is already in use"), 'exist');
  190. $form->addRule('tp_alias', _("Compulsory Alias"), 'required');
  191. /*
  192. * Check Hours format
  193. */
  194. $form->addRule('tp_sunday', _('Error in hour definition'), 'format');
  195. $form->addRule('tp_monday', _('Error in hour definition'), 'format');
  196. $form->addRule('tp_tuesday', _('Error in hour definition'), 'format');
  197. $form->addRule('tp_wednesday', _('Error in hour definition'), 'format');
  198. $form->addRule('tp_thursday', _('Error in hour definition'), 'format');
  199. $form->addRule('tp_friday', _('Error in hour definition'), 'format');
  200. $form->addRule('tp_saturday', _('Error in hour definition'), 'format');
  201. $form->setRequiredNote("<font style='color: red;'>*</font>&nbsp;". _("Required fields"));
  202. /*
  203. * Smarty template Init
  204. */
  205. $tpl = new Smarty();
  206. $tpl = initSmartyTpl($path, $tpl);
  207. if ($o == "w") {
  208. /*
  209. * Just watch a Time Period information
  210. */
  211. if ($centreon->user->access->page($p) != 2)
  212. $form->addElement("button", "change", _("Modify"), array("onClick"=>"javascript:window.location.href='?p=".$p."&o=c&tp_id=".$tp_id."'"));
  213. $form->setDefaults($tp);
  214. $form->freeze();
  215. } else if ($o == "c") {
  216. /*
  217. * Modify a Time Period information
  218. */
  219. $subC = $form->addElement('submit', 'submitC', _("Save"), array("class" => "btc bt_success"));
  220. $res = $form->addElement('reset', 'reset', _("Reset"), array("class" => "btc bt_default"));
  221. $form->setDefaults($tp);
  222. } else if ($o == "a") {
  223. /*
  224. * Add a Time Period information
  225. */
  226. $subA = $form->addElement('submit', 'submitA', _("Save"), array("class" => "btc bt_success"));
  227. $res = $form->addElement('reset', 'reset', _("Reset"), array("class" => "btc bt_default"));
  228. }
  229. /*
  230. * Translations
  231. */
  232. $tpl->assign("tRDay", _("Days"));
  233. $tpl->assign("tRHours", _("Time Range"));
  234. $tpl->assign("helpattr", 'TITLE, "'._("Help").'", CLOSEBTN, true, FIX, [this, 0, 5], BGCOLOR, "#ffff99", BORDERCOLOR, "orange", TITLEFONTCOLOR, "black", TITLEBGCOLOR, "orange", CLOSEBTNCOLORS, ["","black", "white", "red"], WIDTH, -300, SHADOW, true, TEXTALIGN, "justify"' );
  235. # prepare help texts
  236. $helptext = "";
  237. include_once("help.php");
  238. foreach ($help as $key => $text) {
  239. $helptext .= '<span style="display:none" id="help:'.$key.'">'.$text.'</span>'."\n";
  240. }
  241. $tpl->assign("helptext", $helptext);
  242. $valid = false;
  243. if ($form->validate()) {
  244. $tpObj = $form->getElement('tp_id');
  245. if ($form->getSubmitValue("submitA"))
  246. $tpObj->setValue(insertTimeperiodInDB());
  247. else if ($form->getSubmitValue("submitC"))
  248. updateTimeperiodInDB($tpObj->getValue());
  249. $o = NULL;
  250. $valid = true;
  251. }
  252. if ($valid)
  253. require_once($path."listTimeperiod.php");
  254. else {
  255. /*
  256. * Apply a template definition
  257. */
  258. $renderer = new HTML_QuickForm_Renderer_ArraySmarty($tpl, true);
  259. $renderer->setRequiredTemplate('{$label}&nbsp;<font color="red" size="1">*</font>');
  260. $renderer->setErrorTemplate('<font color="red">{$error}</font><br />{$html}');
  261. $form->accept($renderer);
  262. $tpl->assign('form', $renderer->toArray());
  263. $tpl->assign('o', $o);
  264. $tpl->assign('gmtUsed', $oreon->CentreonGMT->used());
  265. $tpl->assign('noExceptionMessage', _('GMT is activated on your system. Exceptions will not be generated.'));
  266. $tpl->assign('exceptionLabel', _('Exceptions'));
  267. $tpl->display("formTimeperiod.ihtml");
  268. }
  269. ?><script type="text/javascript">
  270. displayExistingExceptions(<?php echo $k;?>);
  271. </script>