PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/inc/change_project.class.php

https://gitlab.com/OnBlox/OnBlox-Template
PHP | 316 lines | 280 code | 2 blank | 34 comment | 1 complexity | da0bb4c17cc009124409ab245931b271 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. * @since version 0.85
  33. *
  34. * Change_Project Class
  35. *
  36. * Relation between Changes and Projects
  37. **/
  38. class Change_Project extends CommonDBRelation{
  39. // From CommonDBRelation
  40. static public $itemtype_1 = 'Change';
  41. static public $items_id_1 = 'changes_id';
  42. static public $itemtype_2 = 'Project';
  43. static public $items_id_2 = 'projects_id';
  44. function getForbiddenStandardMassiveAction() {
  45. $forbidden = parent::getForbiddenStandardMassiveAction();
  46. $forbidden[] = 'update';
  47. return $forbidden;
  48. }
  49. static function getTypeName($nb=0) {
  50. return _n('Link Project/Change','Links Project/Change',$nb);
  51. }
  52. /**
  53. * Get search function for the class
  54. *
  55. * @return array of search option
  56. **/
  57. function getSearchOptions() {
  58. return parent::getSearchOptions();
  59. }
  60. /**
  61. * @see CommonGLPI::getTabNameForItem()
  62. **/
  63. function getTabNameForItem(CommonGLPI $item, $withtemplate=0) {
  64. if (static::canView()) {
  65. $nb = 0;
  66. switch ($item->getType()) {
  67. case 'Change' :
  68. if ($_SESSION['glpishow_count_on_tabs']) {
  69. $nb = countElementsInTable('glpi_changes_projects',
  70. "`changes_id` = '".$item->getID()."'");
  71. }
  72. return self::createTabEntry(Project::getTypeName(Session::getPluralNumber()), $nb);
  73. case 'Project' :
  74. if ($_SESSION['glpishow_count_on_tabs']) {
  75. $nb = countElementsInTable('glpi_changes_projects',
  76. "`projects_id` = '".$item->getID()."'");
  77. }
  78. return self::createTabEntry(Change::getTypeName(Session::getPluralNumber()), $nb);
  79. }
  80. }
  81. return '';
  82. }
  83. static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtemplate=0) {
  84. switch ($item->getType()) {
  85. case 'Change' :
  86. self::showForChange($item);
  87. break;
  88. case 'Project' :
  89. self::showForProject($item);
  90. break;
  91. }
  92. return true;
  93. }
  94. /**
  95. * Show tickets for a project
  96. *
  97. * @param $project Project object
  98. **/
  99. static function showForProject(Project $project) {
  100. global $DB, $CFG_GLPI;
  101. $ID = $project->getField('id');
  102. if (!$project->can($ID, READ)) {
  103. return false;
  104. }
  105. $canedit = $project->canEdit($ID);
  106. $rand = mt_rand();
  107. $showentities = Session::isMultiEntitiesMode();
  108. $query = "SELECT DISTINCT `glpi_changes_projects`.`id` AS linkID,
  109. `glpi_changes`.*
  110. FROM `glpi_changes_projects`
  111. LEFT JOIN `glpi_changes`
  112. ON (`glpi_changes_projects`.`changes_id` = `glpi_changes`.`id`)
  113. WHERE `glpi_changes_projects`.`projects_id` = '$ID'
  114. ORDER BY `glpi_changes`.`name`";
  115. $result = $DB->query($query);
  116. $changes = array();
  117. $used = array();
  118. if ($numrows = $DB->numrows($result)) {
  119. while ($data = $DB->fetch_assoc($result)) {
  120. $changes[$data['id']] = $data;
  121. $used[$data['id']] = $data['id'];
  122. }
  123. }
  124. if ($canedit) {
  125. echo "<div class='firstbloc'>";
  126. echo "<form name='changeproject_form$rand' id='changeproject_form$rand' method='post'
  127. action='".Toolbox::getItemTypeFormURL(__CLASS__)."'>";
  128. echo "<table class='tab_cadre_fixe'>";
  129. echo "<tr class='tab_bg_2'><th colspan='2'>".__('Add a change')."</th></tr>";
  130. echo "<tr class='tab_bg_2'><td>";
  131. echo "<input type='hidden' name='projects_id' value='$ID'>";
  132. Change::dropdown(array('used' => $used,
  133. 'entity' => $project->getEntityID(),
  134. 'entity_sons' => $project->isRecursive()));
  135. echo "</td><td class='center'>";
  136. echo "<input type='submit' name='add' value=\""._sx('button', 'Add')."\" class='submit'>";
  137. echo "</td></tr></table>";
  138. Html::closeForm();
  139. echo "</div>";
  140. }
  141. echo "<div class='spaced'>";
  142. if ($canedit && $numrows) {
  143. Html::openMassiveActionsForm('mass'.__CLASS__.$rand);
  144. $massiveactionparams = array('num_displayed' => $numrows,
  145. 'container' => 'mass'.__CLASS__.$rand);
  146. Html::showMassiveActions($massiveactionparams);
  147. }
  148. echo "<table class='tab_cadre_fixehov'>";
  149. echo "<tr class='noHover'><th colspan='12'>".Change::getTypeName($numrows)."</th></tr>";
  150. if ($numrows) {
  151. Change::commonListHeader(Search::HTML_OUTPUT, 'mass'.__CLASS__.$rand);
  152. Session::initNavigateListItems('Change',
  153. //TRANS : %1$s is the itemtype name,
  154. // %2$s is the name of the item (used for headings of a list)
  155. sprintf(__('%1$s = %2$s'), Project::getTypeName(1),
  156. $project->fields["name"]));
  157. $i = 0;
  158. foreach ($changes as $data) {
  159. Session::addToNavigateListItems('Change', $data["id"]);
  160. Change::showShort($data['id'], array('row_num' => $i,
  161. 'type_for_massiveaction' => __CLASS__,
  162. 'id_for_massiveaction' => $data['linkID']));
  163. $i++;
  164. }
  165. Change::commonListHeader(Search::HTML_OUTPUT, 'mass'.__CLASS__.$rand);
  166. }
  167. echo "</table>";
  168. if ($canedit && $numrows) {
  169. $massiveactionparams['ontop'] = false;
  170. Html::showMassiveActions($massiveactionparams);
  171. Html::closeForm();
  172. }
  173. echo "</div>";
  174. }
  175. /**
  176. * Show projects for a change
  177. *
  178. * @param $change Change object
  179. **/
  180. static function showForChange(Change $change) {
  181. global $DB, $CFG_GLPI;
  182. $ID = $change->getField('id');
  183. if (!$change->can($ID, READ)) {
  184. return false;
  185. }
  186. $canedit = $change->canEdit($ID);
  187. $rand = mt_rand();
  188. $showentities = Session::isMultiEntitiesMode();
  189. $query = "SELECT DISTINCT `glpi_changes_projects`.`id` AS linkID,
  190. `glpi_projects`.*
  191. FROM `glpi_changes_projects`
  192. LEFT JOIN `glpi_projects`
  193. ON (`glpi_changes_projects`.`projects_id` = `glpi_projects`.`id`)
  194. WHERE `glpi_changes_projects`.`changes_id` = '$ID'
  195. ORDER BY `glpi_projects`.`name`";
  196. $result = $DB->query($query);
  197. $projects = array();
  198. $used = array();
  199. if ($numrows = $DB->numrows($result)) {
  200. while ($data = $DB->fetch_assoc($result)) {
  201. $projects[$data['id']] = $data;
  202. $used[$data['id']] = $data['id'];
  203. }
  204. }
  205. if ($canedit) {
  206. echo "<div class='firstbloc'>";
  207. echo "<form name='changeproject_form$rand' id='changeproject_form$rand' method='post'
  208. action='".Toolbox::getItemTypeFormURL(__CLASS__)."'>";
  209. echo "<table class='tab_cadre_fixe'>";
  210. echo "<tr class='tab_bg_2'><th colspan='2'>".__('Add a project')."</th></tr>";
  211. echo "<tr class='tab_bg_2'><td>";
  212. echo "<input type='hidden' name='changes_id' value='$ID'>";
  213. Project::dropdown(array('used' => $used,
  214. 'entity' => $change->getEntityID()));
  215. echo "</td><td class='center'>";
  216. echo "<input type='submit' name='add' value=\""._sx('button', 'Add')."\" class='submit'>";
  217. echo "</td></tr></table>";
  218. Html::closeForm();
  219. echo "</div>";
  220. }
  221. echo "<div class='spaced'>";
  222. if ($canedit && $numrows) {
  223. Html::openMassiveActionsForm('mass'.__CLASS__.$rand);
  224. $massiveactionparams = array('num_displayed' => $numrows,
  225. 'container' => 'mass'.__CLASS__.$rand);
  226. Html::showMassiveActions($massiveactionparams);
  227. }
  228. echo "<table class='tab_cadre_fixehov'>";
  229. echo "<tr class='noHover'><th colspan='12'>".Project::getTypeName($numrows)."</th></tr>";
  230. if ($numrows) {
  231. Project::commonListHeader(Search::HTML_OUTPUT, 'mass'.__CLASS__.$rand);
  232. Session::initNavigateListItems('Project',
  233. //TRANS : %1$s is the itemtype name,
  234. // %2$s is the name of the item (used for headings of a list)
  235. sprintf(__('%1$s = %2$s'), Change::getTypeName(1),
  236. $change->fields["name"]));
  237. $i = 0;
  238. foreach ($projects as $data) {
  239. Session::addToNavigateListItems('Project', $data["id"]);
  240. Project::showShort($data['id'], array('row_num' => $i,
  241. 'type_for_massiveaction' => __CLASS__,
  242. 'id_for_massiveaction' => $data['linkID']));
  243. $i++;
  244. }
  245. Project::commonListHeader(Search::HTML_OUTPUT, 'mass'.__CLASS__.$rand);
  246. }
  247. echo "</table>";
  248. if ($canedit && $numrows) {
  249. $massiveactionparams['ontop'] = false;
  250. Html::showMassiveActions($massiveactionparams);
  251. Html::closeForm();
  252. }
  253. echo "</div>";
  254. }
  255. }
  256. ?>