statecolor.class.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /*
  3. * @version $Id: statecolor.class.php 234 2019-12-12 14:34:31Z yllen $
  4. -------------------------------------------------------------------------
  5. LICENSE
  6. This file is part of Archires plugin for GLPI.
  7. Archires is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU Affero General Public License as published by
  9. the Free Software Foundation, either version 3 of the License, or
  10. (at your option) any later version.
  11. Archires is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Affero General Public License for more details.
  15. You should have received a copy of the GNU Affero General Public License
  16. along with Archires. If not, see <http://www.gnu.org/licenses/>.
  17. @package archires
  18. @author Nelly Mahu-Lasson, Xavier Caillaud
  19. @copyright Copyright (c) 2016-2018 Archires plugin team
  20. @license AGPL License 3.0 or (at your option) any later version
  21. http://www.gnu.org/licenses/agpl-3.0-standalone.html
  22. @link https://forge.glpi-project.org/projects/archires
  23. @since version 2.2
  24. --------------------------------------------------------------------------
  25. */
  26. if (!defined('GLPI_ROOT')) {
  27. die("Sorry. You can't access directly to this file");
  28. }
  29. class PluginArchiresStateColor extends CommonDBTM {
  30. static $rightname = "plugin_archires";
  31. function getFromDBbyState($state) {
  32. global $DB;
  33. $query = ['FROM' => $this->getTable(),
  34. 'WHERE' => ['states_id' => $state]];
  35. if ($result = $DB->request($query)) {
  36. if (count($result) != 1) {
  37. return false;
  38. }
  39. $this->fields = $result->next();
  40. if (is_array($this->fields) && count($this->fields)) {
  41. return true;
  42. }
  43. }
  44. return false;
  45. }
  46. function addStateColor($state,$color) {
  47. global $DB;
  48. if ($state != '-1') {
  49. if ($this->getfromDBbyState($state)) {
  50. $this->update(['id' => $this->fields['id'],
  51. 'color' => $color]);
  52. } else {
  53. $this->add(['states_id' => $state,
  54. 'color' => $color]);
  55. }
  56. } else {
  57. $query = ['FROM' => 'glpi_states'];
  58. $result = $DB->request($query);
  59. $i = 0;
  60. while ($i < count($result)) {
  61. $row = $result->next();
  62. $state_table = $row['id'];
  63. if ($this->getfromDBbyState($state_table)) {
  64. $this->update(['id' => $this->fields['id'],
  65. 'color' => $color]);
  66. } else {
  67. $this->add(['states_id' => $state_table,
  68. 'color' => $color]);
  69. }
  70. $i++;
  71. }
  72. }
  73. }
  74. function showConfigForm($canupdate=false) {
  75. global $DB;
  76. if ($canupdate) {
  77. echo "<div class='firstbloc'>";
  78. echo "<form method='post' name='state_color' action='./config.form.php'>";
  79. echo "<table class='tab_cadre' cellpadding='5' width='50%'>";
  80. echo "<tr><th colspan='3'>".__('Associate colors with items statuses', 'archires').
  81. "</th></tr>";
  82. echo "<tr class='tab_bg_1'><td width='70%'>";
  83. $this->dropdownState();
  84. echo "</td>";
  85. echo "<td><input type='text' name='color'>";
  86. echo "&nbsp;";
  87. Html::showToolTip(nl2br(__('Please use this color format', 'archires')),
  88. ['link' => 'http://www.graphviz.org/doc/info/colors.html',
  89. 'linktarget' => '_blank']);
  90. echo "</td><td></div>";
  91. echo "<div class='center'><input type='submit' name='add_color_state' value=\"".
  92. _sx('button', 'Add')."\" class='submit' ></div></td></tr>";
  93. echo "</table></div>";
  94. Html::closeForm();
  95. }
  96. $query = ['FROM' => $this->getTable(),
  97. 'ORDER' => ['states_id ASC']];
  98. if ($result = $DB->request($query)) {
  99. $number = count($result);
  100. if ($number) {
  101. echo "<div id='liste_color'>";
  102. if ($canupdate) {
  103. $rand = mt_rand();
  104. Html::openMassiveActionsForm('mass'.__CLASS__.$rand);
  105. $massiveactionparams = ['num_displayed' => $number,
  106. 'container' => 'mass'.__CLASS__.$rand];
  107. Html::showMassiveActions($massiveactionparams);
  108. }
  109. echo "<table class='tab_cadre' cellpadding='5' width='50%'>";
  110. echo "<tr>";
  111. if ($canupdate) {
  112. echo "<th width='10'>";
  113. Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand);
  114. echo "</th>";
  115. }
  116. echo "<th class='left' widht='50%'>".__('Status')."</th>";
  117. echo "<th class='left'>".__('Color', 'archires')."</th><th></th>";
  118. echo "</tr>";
  119. while ($ligne = $result->next()) {
  120. $ID = $ligne["id"];
  121. echo "<tr class='tab_bg_1'>";
  122. if ($canupdate) {
  123. echo "<td width='10'>";
  124. Html::showMassiveActionCheckBox(__CLASS__, $ID);
  125. echo "</td>";
  126. }
  127. echo "</td><td>".Dropdown::getDropdownName("glpi_states",$ligne["states_id"])."</td>";
  128. echo "<td bgcolor='".$ligne["color"]."'>".$ligne["color"]."</td>";
  129. echo "<td><input type='hidden' name='id' value='$ID'>";
  130. }
  131. echo "</table>";
  132. if ($canupdate) {
  133. $massiveactionparams['ontop'] = false;
  134. Html::showMassiveActions($massiveactionparams);
  135. }
  136. echo "</div>";
  137. Html::closeForm();
  138. }
  139. }
  140. }
  141. function dropdownState() {
  142. global $DB;
  143. $colors = [];
  144. foreach ($DB->request("glpi_plugin_archires_statecolors") as $color) {
  145. $colors[] = $color['states_id'];
  146. }
  147. $query = ['FROM' => 'glpi_states',
  148. 'WHERE' => ['NOT' => ['id' => [implode("','",$colors)]]],
  149. 'ORDER' => 'name'];
  150. $result = $DB->request($query);
  151. if (count($result)) {
  152. $values = [1 => __('All statuses', 'archires')];
  153. while ($data = $result->next()) {
  154. $values[$data['id']] = $data["name"];
  155. }
  156. Dropdown::showFromArray('states_id', $values, ['width' => '80%',
  157. 'display_emptychoice' => true]);
  158. }
  159. }
  160. function displayColorState($device) {
  161. global $DB;
  162. $graph = "";
  163. $query_state = ['FROM' => $this->getTable(),
  164. 'WHERE' => ['states_id' => $device["states_id"]]];
  165. $result_state = $DB->request($query_state);
  166. $number_state = count($result_state);
  167. if ($number_state && ($device["states_id"] > 0)) {
  168. $row = $result_state->next();
  169. $color_state = $row['color'];
  170. $graph ="<font color=\"$color_state\">".Dropdown::getDropdownName("glpi_states",
  171. $device["states_id"])
  172. ."</font>";
  173. } else if (!$number_state && ($device["states_id"] > 0)) {
  174. $graph = Dropdown::getDropdownName("glpi_states",$device["states_id"]);
  175. }
  176. return $graph;
  177. }
  178. function getForbiddenStandardMassiveAction() {
  179. $forbidden = parent::getForbiddenStandardMassiveAction();
  180. $forbidden[] = 'update';
  181. return $forbidden;
  182. }
  183. }