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

/inc/change_item.class.php

https://gitlab.com/OnBlox/OnBlox-Template
PHP | 340 lines | 285 code | 19 blank | 36 comment | 15 complexity | 60734029a46e0fd3f8305d70b2ad6cff 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. * Change_Item Class
  33. *
  34. * Relation between Changes and Items
  35. **/
  36. class Change_Item extends CommonDBRelation{
  37. // From CommonDBRelation
  38. static public $itemtype_1 = 'Change';
  39. static public $items_id_1 = 'changes_id';
  40. static public $itemtype_2 = 'itemtype';
  41. static public $items_id_2 = 'items_id';
  42. static public $checkItem_2_Rights = self::DONT_CHECK_ITEM_RIGHTS;
  43. function getForbiddenStandardMassiveAction() {
  44. $forbidden = parent::getForbiddenStandardMassiveAction();
  45. $forbidden[] = 'update';
  46. return $forbidden;
  47. }
  48. function prepareInputForAdd($input) {
  49. // Well, if I remember my PHP: empty(0) == true ...
  50. if (empty($input['changes_id']) || ($input['changes_id'] == 0)) {
  51. return false;
  52. }
  53. // Avoid duplicate entry
  54. $restrict = "`changes_id` = '".$input['changes_id']."'
  55. AND `itemtype` = '".$input['itemtype']."'
  56. AND `items_id` = '".$input['items_id']."'";
  57. if (countElementsInTable($this->getTable(),$restrict)>0) {
  58. return false;
  59. }
  60. return parent::prepareInputForAdd($input);
  61. }
  62. static function countForItem(CommonDBTM $item) {
  63. $restrict = "`glpi_changes_items`.`changes_id` = `glpi_changes`.`id`
  64. AND `glpi_changes_items`.`items_id` = '".$item->getField('id')."'
  65. AND `glpi_changes_items`.`itemtype` = '".$item->getType()."'".
  66. getEntitiesRestrictRequest(" AND ", "glpi_changes", '', '', true);
  67. $nb = countElementsInTable(array('glpi_changes_items', 'glpi_changes'), $restrict);
  68. return $nb ;
  69. }
  70. /**
  71. * Print the HTML array for Items linked to a change
  72. *
  73. * @param $change Change object
  74. *
  75. * @return Nothing (display)
  76. **/
  77. static function showForChange(Change $change) {
  78. global $DB, $CFG_GLPI;
  79. $instID = $change->fields['id'];
  80. if (!$change->can($instID, READ)) {
  81. return false;
  82. }
  83. $canedit = $change->canEdit($instID);
  84. $rand = mt_rand();
  85. $query = "SELECT DISTINCT `itemtype`
  86. FROM `glpi_changes_items`
  87. WHERE `glpi_changes_items`.`changes_id` = '$instID'
  88. ORDER BY `itemtype`";
  89. $result = $DB->query($query);
  90. $number = $DB->numrows($result);
  91. if ($canedit) {
  92. echo "<div class='firstbloc'>";
  93. echo "<form name='changeitem_form$rand' id='changeitem_form$rand' method='post'
  94. action='".Toolbox::getItemTypeFormURL(__CLASS__)."'>";
  95. echo "<table class='tab_cadre_fixe'>";
  96. echo "<tr class='tab_bg_2'><th colspan='2'>".__('Add an item')."</th></tr>";
  97. echo "<tr class='tab_bg_1'><td>";
  98. $types = array();
  99. foreach ($change->getAllTypesForHelpdesk() as $key => $val) {
  100. $types[] = $key;
  101. }
  102. Dropdown::showSelectItemFromItemtypes(array('itemtypes'
  103. => $types,
  104. 'entity_restrict'
  105. => ($change->fields['is_recursive']
  106. ?getSonsOf('glpi_entities',
  107. $change->fields['entities_id'])
  108. :$change->fields['entities_id'])));
  109. echo "</td><td class='center' width='30%'>";
  110. echo "<input type='submit' name='add' value=\""._sx('button', 'Add')."\" class='submit'>";
  111. echo "<input type='hidden' name='changes_id' value='$instID'>";
  112. echo "</td></tr>";
  113. echo "</table>";
  114. Html::closeForm();
  115. echo "</div>";
  116. }
  117. echo "<div class='spaced'>";
  118. if ($canedit && $number) {
  119. Html::openMassiveActionsForm('mass'.__CLASS__.$rand);
  120. $massiveactionparams = array('container' => 'mass'.__CLASS__.$rand);
  121. Html::showMassiveActions($massiveactionparams);
  122. }
  123. echo "<table class='tab_cadre_fixehov'>";
  124. $header_begin = "<tr>";
  125. $header_top = '';
  126. $header_bottom = '';
  127. $header_end = '';
  128. if ($canedit && $number) {
  129. $header_top .= "<th width='10'>".Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand);
  130. $header_top .= "</th>";
  131. $header_bottom .= "<th width='10'>".Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand);
  132. $header_bottom .= "</th>";
  133. }
  134. $header_end .= "<th>".__('Type')."</th>";
  135. $header_end .= "<th>".__('Entity')."</th>";
  136. $header_end .= "<th>".__('Name')."</th>";
  137. $header_end .= "<th>".__('Serial number')."</th>";
  138. $header_end .= "<th>".__('Inventory number')."</th></tr>";
  139. echo $header_begin.$header_top.$header_end;
  140. $totalnb = 0;
  141. for ($i=0 ; $i<$number ; $i++) {
  142. $itemtype = $DB->result($result, $i, "itemtype");
  143. if (!($item = getItemForItemtype($itemtype))) {
  144. continue;
  145. }
  146. if ($item->canView()) {
  147. $itemtable = getTableForItemType($itemtype);
  148. $query = "SELECT `$itemtable`.*,
  149. `glpi_changes_items`.`id` AS IDD,
  150. `glpi_entities`.`id` AS entity
  151. FROM `glpi_changes_items`,
  152. `$itemtable`";
  153. if ($itemtype != 'Entity') {
  154. $query .= " LEFT JOIN `glpi_entities`
  155. ON (`$itemtable`.`entities_id`=`glpi_entities`.`id`) ";
  156. }
  157. $query .= " WHERE `$itemtable`.`id` = `glpi_changes_items`.`items_id`
  158. AND `glpi_changes_items`.`itemtype` = '$itemtype'
  159. AND `glpi_changes_items`.`changes_id` = '$instID'";
  160. if ($item->maybeTemplate()) {
  161. $query .= " AND `$itemtable`.`is_template` = '0'";
  162. }
  163. $query .= getEntitiesRestrictRequest(" AND", $itemtable, '', '',
  164. $item->maybeRecursive())."
  165. ORDER BY `glpi_entities`.`completename`, `$itemtable`.`name`";
  166. $result_linked = $DB->query($query);
  167. $nb = $DB->numrows($result_linked);
  168. for ($prem=true ; $data=$DB->fetch_assoc($result_linked) ; $prem=false) {
  169. $link = $itemtype::getFormURLWithID($data['id']);
  170. $linkname = $data["name"];
  171. if ($_SESSION["glpiis_ids_visible"]
  172. || empty($data["name"])) {
  173. $linkname = sprintf(__('%1$s (%2$s)'), $linkname, $data["id"]);
  174. }
  175. $name = "<a href=\"".$link."\">".$linkname."</a>";
  176. echo "<tr class='tab_bg_1'>";
  177. if ($canedit) {
  178. echo "<td width='10'>";
  179. Html::showMassiveActionCheckBox(__CLASS__, $data["IDD"]);
  180. echo "</td>";
  181. }
  182. if ($prem) {
  183. $itemname = $item->getTypeName($nb);
  184. echo "<td class='center top' rowspan='$nb'>".
  185. ($nb>1 ? sprintf(__('%1$s: %2$s'), $itemname, $nb) : $itemname)."</td>";
  186. }
  187. echo "<td class='center'>";
  188. echo Dropdown::getDropdownName("glpi_entities", $data['entity'])."</td>";
  189. echo "<td class='center".
  190. (isset($data['is_deleted']) && $data['is_deleted'] ? " tab_bg_2_2'" : "'");
  191. echo ">".$name."</td>";
  192. echo "<td class='center'>".
  193. (isset($data["serial"])? "".$data["serial"]."" :"-")."</td>";
  194. echo "<td class='center'>".
  195. (isset($data["otherserial"])? "".$data["otherserial"]."" :"-")."</td>";
  196. echo "</tr>";
  197. }
  198. $totalnb += $nb;
  199. }
  200. }
  201. if ($number) {
  202. echo $header_begin.$header_bottom.$header_end;
  203. }
  204. echo "</table>";
  205. if ($canedit && $number) {
  206. $massiveactionparams['ontop'] = false;
  207. Html::showMassiveActions($massiveactionparams);
  208. Html::closeForm();
  209. }
  210. echo "</div>";
  211. }
  212. function getTabNameForItem(CommonGLPI $item, $withtemplate=0) {
  213. if (!$withtemplate) {
  214. $nb = 0;
  215. switch ($item->getType()) {
  216. case 'Change' :
  217. if ($_SESSION['glpishow_count_on_tabs']) {
  218. $nb = countElementsInTable('glpi_changes_items',
  219. "`changes_id` = '".$item->getID()."'");
  220. }
  221. return self::createTabEntry(_n('Item', 'Items', Session::getPluralNumber()), $nb);
  222. case 'User' :
  223. if ($_SESSION['glpishow_count_on_tabs']) {
  224. $nb = countDistinctElementsInTable('glpi_changes_users','changes_id',
  225. "`users_id` = '".$item->getID()."'");
  226. }
  227. return self::createTabEntry(Change::getTypeName(Session::getPluralNumber()), $nb);
  228. case 'Group' :
  229. if ($_SESSION['glpishow_count_on_tabs']) {
  230. $nb = countDistinctElementsInTable('glpi_changes_groups','changes_id',
  231. "`groups_id` = '".$item->getID()."'");
  232. }
  233. return self::createTabEntry(Change::getTypeName(Session::getPluralNumber()), $nb);
  234. case 'Supplier' :
  235. if ($_SESSION['glpishow_count_on_tabs']) {
  236. $nb = countDistinctElementsInTable('glpi_changes_suppliers','changes_id',
  237. "`suppliers_id` = '".$item->getID()."'");
  238. }
  239. return self::createTabEntry(Change::getTypeName(Session::getPluralNumber()), $nb);
  240. default :
  241. if (Session::haveRight("change", Change::READALL)) {
  242. if ($_SESSION['glpishow_count_on_tabs']) {
  243. // Direct one
  244. $nb = countElementsInTable('glpi_changes_items',
  245. " `itemtype` = '".$item->getType()."'
  246. AND `items_id` = '".$item->getID()."'");
  247. // Linked items
  248. $linkeditems = $item->getLinkedItems();
  249. if (count($linkeditems)) {
  250. foreach ($linkeditems as $type => $tab) {
  251. foreach ($tab as $ID) {
  252. $nb += countElementsInTable('glpi_changes_items',
  253. " `itemtype` = '$type'
  254. AND `items_id` = '$ID'");
  255. }
  256. }
  257. }
  258. }
  259. return self::createTabEntry(Change::getTypeName(Session::getPluralNumber()), $nb);
  260. }
  261. }
  262. }
  263. return '';
  264. }
  265. static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtemplate=0) {
  266. switch ($item->getType()) {
  267. case 'Change' :
  268. self::showForChange($item);
  269. break;
  270. default :
  271. Change::showListForItem($item);
  272. }
  273. return true;
  274. }
  275. }
  276. ?>