PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/glpi/inc/tickettemplatepredefinedfield.class.php

#
PHP | 299 lines | 266 code | 2 blank | 31 comment | 1 complexity | 41c29791116cc44a26b8bf4457503c0c MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /*
  3. * @version $Id: tickettemplatepredefinedfield.class.php 18776 2012-06-29 12:28:45Z remi $
  4. -------------------------------------------------------------------------
  5. GLPI - Gestionnaire Libre de Parc Informatique
  6. Copyright (C) 2003-2012 by the INDEPNET Development Team.
  7. http://indepnet.net/ http://glpi-project.org
  8. -------------------------------------------------------------------------
  9. LICENSE
  10. This file is part of GLPI.
  11. GLPI is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15. GLPI is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with GLPI. If not, see <http://www.gnu.org/licenses/>.
  21. --------------------------------------------------------------------------
  22. */
  23. // ----------------------------------------------------------------------
  24. // Original Author of file:
  25. // Purpose of file:
  26. // ----------------------------------------------------------------------
  27. if (!defined('GLPI_ROOT')) {
  28. die("Sorry. You can't access directly to this file");
  29. }
  30. /// Predefined fields for ticket template class
  31. /// since version 0.83
  32. class TicketTemplatePredefinedField extends CommonDBChild {
  33. /// TODO delete items_id if itemtype is deleted
  34. // From CommonDBChild
  35. public $itemtype = 'TicketTemplate';
  36. public $items_id = 'tickettemplates_id';
  37. public $dohistory = true;
  38. static function getTypeName($nb=0) {
  39. global $LANG;
  40. if ($nb>1) {
  41. return $LANG['job'][61];
  42. }
  43. return $LANG['job'][64];
  44. }
  45. function getName($with_comment=0) {
  46. $tt = new TicketTemplate();
  47. $fields = $tt->getAllowedFieldsNames(true);
  48. if (isset($fields[$this->fields["num"]])) {
  49. return $fields[$this->fields["num"]];
  50. }
  51. return NOT_AVAILABLE;
  52. }
  53. function canCreate() {
  54. return Session::haveRight('tickettemplate', 'w');
  55. }
  56. function canView() {
  57. return Session::haveRight('tickettemplate', 'r');
  58. }
  59. function prepareInputForAdd($input) {
  60. // Use massiveaction system to manage add system.
  61. // Need to update data : value not set but
  62. if (!isset($input['value'])) {
  63. if (isset($input['field']) && isset($input[$input['field']])) {
  64. $input['value'] = $input[$input['field']];
  65. unset($input[$input['field']]);
  66. unset($input['field']);
  67. }
  68. }
  69. return $input;
  70. }
  71. function post_purgeItem() {
  72. global $DB;
  73. parent::post_purgeItem();
  74. $ticket = new Ticket();
  75. $itemtype_id = $ticket->getSearchOptionIDByField('field', 'itemtype',
  76. 'glpi_tickets');
  77. $items_id_id = $ticket->getSearchOptionIDByField('field', 'items_id',
  78. 'glpi_tickets');
  79. // Try to delete itemtype -> delete items_id
  80. if ($this->fields['num'] == $itemtype_id) {
  81. $query = "SELECT `id`
  82. FROM `".$this->getTable()."`
  83. WHERE `".$this->items_id."` = '".$this->fields['tickettemplates_id']."'
  84. AND `num` = '$items_id_id'";
  85. if ($result = $DB->query($query)) {
  86. if ($DB->numrows($result)) {
  87. $a = new TicketTemplatePredefinedField();
  88. $a->delete(array('id'=>$DB->result($result,0,0)));
  89. }
  90. }
  91. }
  92. }
  93. function getTabNameForItem(CommonGLPI $item, $withtemplate=0) {
  94. global $LANG;
  95. // can exists for template
  96. if ($item->getType() == 'TicketTemplate' && Session::haveRight("tickettemplate","r")) {
  97. if ($_SESSION['glpishow_count_on_tabs']) {
  98. return self::createTabEntry($LANG['job'][61],
  99. countElementsInTable($this->getTable(),
  100. "`tickettemplates_id`
  101. = '".$item->getID()."'"));
  102. }
  103. return $LANG['job'][61];
  104. }
  105. return '';
  106. }
  107. static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtemplate=0) {
  108. self::showForTicketTemplate($item, $withtemplate);
  109. return true;
  110. }
  111. /**
  112. * Get predefined fields for a template
  113. *
  114. * @since version 0.83
  115. *
  116. * @param $ID the template ID
  117. * @param $withtypeandcategory bool with type and category
  118. *
  119. * @return an array of predefined fields
  120. **/
  121. function getPredefinedFields($ID, $withtypeandcategory=false) {
  122. global $DB;
  123. $sql = "SELECT *
  124. FROM `".$this->getTable()."`
  125. WHERE `".$this->items_id."` = '$ID'
  126. ORDER BY `id`";
  127. $result = $DB->query($sql);
  128. $tt = new TicketTemplate();
  129. $allowed_fields = $tt->getAllowedFields($withtypeandcategory, true);
  130. $fields = array();
  131. while ($rule = $DB->fetch_assoc($result)) {
  132. if (isset($allowed_fields[$rule['num']])) {
  133. $fields[$allowed_fields[$rule['num']]] = $rule['value'];
  134. }
  135. }
  136. return $fields;
  137. }
  138. /**
  139. * Print the predefined fields
  140. *
  141. * @since version 0.83
  142. *
  143. * @param $tt Ticket Template
  144. * @param $withtemplate='' boolean : Template or basic item.
  145. *
  146. * @return Nothing (call to classes members)
  147. **/
  148. static function showForTicketTemplate(TicketTemplate $tt, $withtemplate='') {
  149. global $DB, $LANG,$CFG_GLPI;
  150. $ID = $tt->fields['id'];
  151. if (!$tt->getFromDB($ID) || !$tt->can($ID, "r")) {
  152. return false;
  153. }
  154. $canedit = $tt->can($ID, "w");
  155. $ttp = new self();
  156. $used_fields = $ttp->getPredefinedFields($ID, true);
  157. $itemtype_used = '';
  158. if (isset($used_fields['itemtype'])) {
  159. $itemtype_used = $used_fields['itemtype'];
  160. }
  161. $fields = $tt->getAllowedFieldsNames(true, isset($used_fields['itemtype']));
  162. $searchOption = Search::getOptions('Ticket');
  163. $ticket = new Ticket();
  164. $rand = mt_rand();
  165. echo "<form name='tickettemplatepredefinedfields_form$rand'
  166. id='tickettemplatepredefinedfields_form$rand' method='post' action='";
  167. echo Toolbox::getItemTypeFormURL(__CLASS__)."'>";
  168. echo "<div class='center'>";
  169. $query = "SELECT `glpi_tickettemplatepredefinedfields`.*
  170. FROM `glpi_tickettemplatepredefinedfields`
  171. WHERE (`tickettemplates_id` = '$ID')
  172. ORDER BY 'id'";
  173. $display_datas = array('itemtype' => $itemtype_used);
  174. $display_options = array('relative_dates' => true,
  175. 'comments' => true,
  176. 'html' => true);
  177. if ($result=$DB->query($query)) {
  178. echo "<table class='tab_cadre_fixe'>";
  179. echo "<tr><th colspan='3'>";
  180. echo self::getTypeName($DB->numrows($result));
  181. echo "</th></tr>";
  182. $used = array();
  183. if ($DB->numrows($result)) {
  184. echo "<tr><th>&nbsp;</th>";
  185. echo "<th>".$LANG['common'][16]."</th>";
  186. echo "<th>".$LANG['rulesengine'][13]."</th>";
  187. echo "</tr>";
  188. while ($data=$DB->fetch_assoc($result)) {
  189. if (!isset($fields[$data['num']])) {
  190. // could happen when itemtype removed and items_id present
  191. continue;
  192. }
  193. echo "<tr class='tab_bg_2'>";
  194. if ($canedit) {
  195. echo "<td><input type='checkbox' name='item[".$data["id"]."]' value='1'></td>";
  196. } else {
  197. echo "<td>&nbsp;</td>";
  198. }
  199. echo "<td>".$fields[$data['num']]."</td>";
  200. echo "<td>";
  201. $display_datas[$searchOption[$data['num']]['field']] = $data['value'];
  202. echo $ticket->getValueToDisplay($searchOption[$data['num']], $display_datas,
  203. $display_options);
  204. echo "</td>";
  205. $used[$data['num']] = $data['value'];
  206. }
  207. } else {
  208. echo "<tr><th colspan='3'>".$LANG['search'][15]."</th></tr>";
  209. }
  210. if ($canedit) {
  211. echo "<tr class='tab_bg_2'><td class='right top' width='30%'>";
  212. echo "<input type='hidden' name='tickettemplates_id' value='$ID'>";
  213. echo "<input type='hidden' name='entities_id' value='".$tt->getEntityID()."'>";
  214. echo "<input type='hidden' name='is_recursive' value='".$tt->isRecursive()."'>";
  215. $display_fields[-1] = Dropdown::EMPTY_VALUE;
  216. $display_fields += $fields;
  217. // Force validation request as used
  218. $used[-2] = -2;
  219. $rand_dp = Dropdown::showFromArray('num', $display_fields, array('used' => $used,
  220. 'toadd'));
  221. echo "</td><td colspan='2' class='top'>";
  222. $paramsmassaction = array('id_field' => '__VALUE__',
  223. 'itemtype' => 'Ticket',
  224. 'itemtype_used' => $itemtype_used,
  225. 'relative_dates' => 1);
  226. Ajax::updateItemOnSelectEvent("dropdown_num".$rand_dp, "show_massiveaction_field",
  227. $CFG_GLPI["root_doc"]."/ajax/dropdownMassiveActionField.php",
  228. $paramsmassaction);
  229. echo "<span id='show_massiveaction_field'>&nbsp;</span>\n";
  230. echo "</td></tr>";
  231. }
  232. echo "</table></div>";
  233. if ($canedit) {
  234. Html::openArrowMassives("tickettemplatepredefinedfields_form$rand", true);
  235. Html::closeArrowMassives(array('delete' => $LANG['buttons'][6]));
  236. }
  237. Html::closeForm();
  238. }
  239. }
  240. }
  241. ?>