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

/inc/item_problem.class.php

https://gitlab.com/OnBlox/OnBlox-Template
PHP | 342 lines | 289 code | 17 blank | 36 comment | 15 complexity | d0763e97fe6344fe9bc482d92abbbc8a MD5 | raw file
  1. <?php
  2. /*
  3. * @version $Id$
  4. -------------------------------------------------------------------------
  5. GLPI - Gestionnaire Libre de Parc Informatique
  6. Copyright (C) 2015 Teclib'.
  7. http://glpi-project.org
  8. based on GLPI - Gestionnaire Libre de Parc Informatique
  9. Copyright (C) 2003-2014 by the INDEPNET Development Team.
  10. -------------------------------------------------------------------------
  11. LICENSE
  12. This file is part of GLPI.
  13. GLPI is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17. GLPI is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License for more details.
  21. You should have received a copy of the GNU General Public License
  22. along with GLPI. If not, see <http://www.gnu.org/licenses/>.
  23. --------------------------------------------------------------------------
  24. */
  25. /** @file
  26. * @brief
  27. */
  28. if (!defined('GLPI_ROOT')) {
  29. die("Sorry. You can't access directly to this file");
  30. }
  31. /**
  32. * Item_Problem Class
  33. *
  34. * Relation between Problems and Items
  35. **/
  36. class Item_Problem extends CommonDBRelation{
  37. // From CommonDBRelation
  38. static public $itemtype_1 = 'Problem';
  39. static public $items_id_1 = 'problems_id';
  40. static public $itemtype_2 = 'itemtype';
  41. static public $items_id_2 = 'items_id';
  42. static public $checkItem_2_Rights = self::HAVE_VIEW_RIGHT_ON_ITEM;
  43. /**
  44. * @since version 0.84
  45. **/
  46. function getForbiddenStandardMassiveAction() {
  47. $forbidden = parent::getForbiddenStandardMassiveAction();
  48. $forbidden[] = 'update';
  49. return $forbidden;
  50. }
  51. /**
  52. * @see CommonDBTM::prepareInputForAdd()
  53. **/
  54. function prepareInputForAdd($input) {
  55. // Avoid duplicate entry
  56. $restrict = " `problems_id` = '".$input['problems_id']."'
  57. AND `itemtype` = '".$input['itemtype']."'
  58. AND `items_id` = '".$input['items_id']."'";
  59. if (countElementsInTable($this->getTable(),$restrict)>0) {
  60. return false;
  61. }
  62. return parent::prepareInputForAdd($input);
  63. }
  64. /**
  65. * @param $item CommonDBTM object
  66. **/
  67. static function countForItem(CommonDBTM $item) {
  68. $restrict = "`glpi_items_problems`.`problems_id` = `glpi_problems`.`id`
  69. AND `glpi_items_problems`.`items_id` = '".$item->getField('id')."'
  70. AND `glpi_items_problems`.`itemtype` = '".$item->getType()."'".
  71. getEntitiesRestrictRequest(" AND ", "glpi_problems", '', '', true);
  72. $nb = countElementsInTable(array('glpi_items_problems', 'glpi_problems'), $restrict);
  73. return $nb ;
  74. }
  75. /**
  76. * Print the HTML array for Items linked to a problem
  77. *
  78. * @param $problem Problem object
  79. *
  80. * @return Nothing (display)
  81. **/
  82. static function showForProblem(Problem $problem) {
  83. global $DB, $CFG_GLPI;
  84. $instID = $problem->fields['id'];
  85. if (!$problem->can($instID, READ)) {
  86. return false;
  87. }
  88. $canedit = $problem->canEdit($instID);
  89. $rand = mt_rand();
  90. $query = "SELECT DISTINCT `itemtype`
  91. FROM `glpi_items_problems`
  92. WHERE `glpi_items_problems`.`problems_id` = '$instID'
  93. ORDER BY `itemtype`";
  94. $result = $DB->query($query);
  95. $number = $DB->numrows($result);
  96. if ($canedit) {
  97. echo "<div class='firstbloc'>";
  98. echo "<form name='problemitem_form$rand' id='problemitem_form$rand' method='post'
  99. action='".Toolbox::getItemTypeFormURL(__CLASS__)."'>";
  100. echo "<table class='tab_cadre_fixe'>";
  101. echo "<tr class='tab_bg_2'><th colspan='2'>".__('Add an item')."</th></tr>";
  102. echo "<tr class='tab_bg_1'><td>";
  103. $types = array();
  104. foreach ($problem->getAllTypesForHelpdesk() as $key => $val) {
  105. $types[] = $key;
  106. }
  107. Dropdown::showSelectItemFromItemtypes(array('itemtypes'
  108. => $types,
  109. 'entity_restrict'
  110. => ($problem->fields['is_recursive']
  111. ?getSonsOf('glpi_entities',
  112. $problem->fields['entities_id'])
  113. :$problem->fields['entities_id'])));
  114. echo "</td><td class='center' width='30%'>";
  115. echo "<input type='submit' name='add' value=\""._sx('button', 'Add')."\" class='submit'>";
  116. echo "<input type='hidden' name='problems_id' value='$instID'>";
  117. echo "</td></tr>";
  118. echo "</table>";
  119. Html::closeForm();
  120. echo "</div>";
  121. }
  122. echo "<div class='spaced'>";
  123. if ($canedit && $number) {
  124. Html::openMassiveActionsForm('mass'.__CLASS__.$rand);
  125. $massiveactionparams = array('container' => 'mass'.__CLASS__.$rand);
  126. Html::showMassiveActions($massiveactionparams);
  127. }
  128. echo "<table class='tab_cadre_fixehov'>";
  129. $header_begin = "<tr>";
  130. $header_top = '';
  131. $header_bottom = '';
  132. $header_end = '';
  133. if ($canedit && $number) {
  134. $header_top .= "<th width='10'>".Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand);
  135. $header_top .= "</th>";
  136. $header_bottom .= "<th width='10'>".Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand);
  137. $header_bottom .= "</th>";
  138. }
  139. $header_end .= "<th>".__('Type')."</th>";
  140. $header_end .= "<th>".__('Entity')."</th>";
  141. $header_end .= "<th>".__('Name')."</th>";
  142. $header_end .= "<th>".__('Serial number')."</th>";
  143. $header_end .= "<th>".__('Inventory number')."</th></tr>";
  144. echo $header_begin.$header_top.$header_end;
  145. $totalnb = 0;
  146. for ($i=0 ; $i<$number ; $i++) {
  147. $itemtype = $DB->result($result, $i, "itemtype");
  148. if (!($item = getItemForItemtype($itemtype))) {
  149. continue;
  150. }
  151. if ($item->canView()) {
  152. $itemtable = getTableForItemType($itemtype);
  153. $query = "SELECT `$itemtable`.*,
  154. `glpi_items_problems`.`id` AS IDD,
  155. `glpi_entities`.`id` AS entity
  156. FROM `glpi_items_problems`,
  157. `$itemtable`";
  158. if ($itemtype != 'Entity') {
  159. $query .= " LEFT JOIN `glpi_entities`
  160. ON (`$itemtable`.`entities_id`=`glpi_entities`.`id`) ";
  161. }
  162. $query .= " WHERE `$itemtable`.`id` = `glpi_items_problems`.`items_id`
  163. AND `glpi_items_problems`.`itemtype` = '$itemtype'
  164. AND `glpi_items_problems`.`problems_id` = '$instID'";
  165. if ($item->maybeTemplate()) {
  166. $query .= " AND `$itemtable`.`is_template` = '0'";
  167. }
  168. $query .= getEntitiesRestrictRequest(" AND", $itemtable, '', '',
  169. $item->maybeRecursive())."
  170. ORDER BY `glpi_entities`.`completename`, `$itemtable`.`name`";
  171. $result_linked = $DB->query($query);
  172. $nb = $DB->numrows($result_linked);
  173. for ($prem=true ; $data=$DB->fetch_assoc($result_linked) ; $prem=false) {
  174. $name = $data["name"];
  175. if ($_SESSION["glpiis_ids_visible"]
  176. || empty($data["name"])) {
  177. $name = sprintf(__('%1$s (%2$s)'), $name, $data["id"]);
  178. }
  179. $link = $itemtype::getFormURLWithID($data['id']);
  180. $namelink = "<a href=\"".$link."\">".$name."</a>";
  181. echo "<tr class='tab_bg_1'>";
  182. if ($canedit) {
  183. echo "<td width='10'>";
  184. Html::showMassiveActionCheckBox(__CLASS__, $data["IDD"]);
  185. echo "</td>";
  186. }
  187. if ($prem) {
  188. $typename = $item->getTypeName($nb);
  189. echo "<td class='center top' rowspan='$nb'>".
  190. (($nb > 1) ? sprintf(__('%1$s: %2$s'), $typename, $nb) : $typename)."</td>";
  191. }
  192. echo "<td class='center'>";
  193. echo Dropdown::getDropdownName("glpi_entities", $data['entity'])."</td>";
  194. echo "<td class='center".
  195. (isset($data['is_deleted']) && $data['is_deleted'] ? " tab_bg_2_2'" : "'");
  196. echo ">".$namelink."</td>";
  197. echo "<td class='center'>".(isset($data["serial"])? "".$data["serial"]."" :"-").
  198. "</td>";
  199. echo "<td class='center'>".
  200. (isset($data["otherserial"])? "".$data["otherserial"]."" :"-")."</td>";
  201. echo "</tr>";
  202. }
  203. $totalnb += $nb;
  204. }
  205. }
  206. if ($number) {
  207. echo $header_begin.$header_bottom.$header_end;
  208. }
  209. echo "</table>";
  210. if ($canedit && $number) {
  211. $massiveactionparams['ontop'] = false;
  212. Html::showMassiveActions($massiveactionparams);
  213. Html::closeForm();
  214. }
  215. echo "</div>";
  216. }
  217. function getTabNameForItem(CommonGLPI $item, $withtemplate=0) {
  218. if (!$withtemplate) {
  219. $nb = 0;
  220. switch ($item->getType()) {
  221. case 'Problem' :
  222. if ($_SESSION['glpishow_count_on_tabs']) {
  223. $nb = countElementsInTable('glpi_items_problems',
  224. "`problems_id` = '".$item->getID()."'");
  225. }
  226. return self::createTabEntry(_n('Item', 'Items', Session::getPluralNumber()), $nb);
  227. case 'User' :
  228. if ($_SESSION['glpishow_count_on_tabs']) {
  229. $nb = countDistinctElementsInTable('glpi_problems_users', 'problems_id',
  230. "`users_id` = '".$item->getID()."'");
  231. }
  232. return self::createTabEntry(Problem::getTypeName(Session::getPluralNumber()), $nb);
  233. case 'Group' :
  234. if ($_SESSION['glpishow_count_on_tabs']) {
  235. $nb = countDistinctElementsInTable('glpi_groups_problems', 'problems_id',
  236. "`groups_id` = '".$item->getID()."'");
  237. }
  238. return self::createTabEntry(Problem::getTypeName(Session::getPluralNumber()), $nb);
  239. case 'Supplier' :
  240. if ($_SESSION['glpishow_count_on_tabs']) {
  241. $nb = countDistinctElementsInTable('glpi_problems_suppliers','problems_id',
  242. "`suppliers_id` = '".$item->getID()."'");
  243. }
  244. return self::createTabEntry(Problem::getTypeName(Session::getPluralNumber()), $nb);
  245. default :
  246. if (Session::haveRight("problem", Problem::READALL)) {
  247. if ($_SESSION['glpishow_count_on_tabs']) {
  248. // Direct one
  249. $nb = countElementsInTable('glpi_items_problems',
  250. " `itemtype` = '".$item->getType()."'
  251. AND `items_id` = '".$item->getID()."'");
  252. // Linked items
  253. $linkeditems = $item->getLinkedItems();
  254. if (count($linkeditems)) {
  255. foreach ($linkeditems as $type => $tab) {
  256. foreach ($tab as $ID) {
  257. $nb += countElementsInTable('glpi_items_problems',
  258. " `itemtype` = '$type'
  259. AND `items_id` = '$ID'");
  260. }
  261. }
  262. }
  263. }
  264. return self::createTabEntry(Problem::getTypeName(Session::getPluralNumber()), $nb);
  265. }
  266. }
  267. }
  268. return '';
  269. }
  270. static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtemplate=0) {
  271. switch ($item->getType()) {
  272. case 'Problem' :
  273. self::showForProblem($item);
  274. break;
  275. default :
  276. Problem::showListForItem($item);
  277. }
  278. return true;
  279. }
  280. }
  281. ?>