networkinterfacecolor.class.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /*
  3. * @version $Id: networkinterfacecolor.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-2021 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 PluginArchiresNetworkInterfaceColor extends CommonDBTM {
  30. static $rightname = "plugin_archires";
  31. function getFromDBbyNetworkInterface($networkinterfaces_id) {
  32. global $DB;
  33. $query = ['FROM' => $this->getTable(),
  34. 'WHERE' => ['networkinterfaces_id' => $networkinterfaces_id]];
  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 addNetworkInterfaceColor($networkinterfaces_id,$color) {
  47. global $DB;
  48. if ($networkinterfaces_id != '-1') {
  49. if ($this->getFromDBbyNetworkInterface($networkinterfaces_id)) {
  50. $this->update(['id' => $this->fields['id'],
  51. 'color' => $color]);
  52. } else {
  53. $this->add(['networkinterfaces_id' => $networkinterfaces_id,
  54. 'color' => $color]);
  55. }
  56. } else {
  57. $query = ['FROM' => 'glpi_networkinterfaces'];
  58. $result = $DB->request($query);
  59. $i = 0;
  60. while ($i < count($result)) {
  61. $row = $result->next();
  62. $networkinterface_table = $rox['id'];
  63. if ($this->getFromDBbyNetworkInterface($networkinterface_table)) {
  64. $this->update(['id' => $this->fields['id'],
  65. 'color' => $color]);
  66. } else {
  67. $this->add(['networkinterfaces_id' => $networkinterface_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='networkinterface_color' action='./config.form.php'>";
  79. echo "<table class='tab_cadre' cellpadding='5' width='50%'><tr ><th colspan='3'>";
  80. echo __('Associate colors with network types', 'archires')."</th></tr>";
  81. echo "<tr class='tab_bg_1'><td width='70%'>";
  82. $this->dropdownNetworkInterface();
  83. echo "</td><td>";
  84. echo "<input type='text' name=\"color\">";
  85. echo "&nbsp;";
  86. Html::showToolTip(nl2br(__('Please use this color format', 'archires')),
  87. ['link' => 'http://www.graphviz.org/doc/info/colors.html',
  88. 'linktarget' => '_blank']);
  89. echo "</td><td></div>";
  90. echo "<div class='center'>";
  91. echo "<input type='submit' name='add_color_networkinterface' value=\"".
  92. _sx('button', 'Add')."\" class='submit' ></div></td></tr>";
  93. echo "</table>";
  94. Html::closeForm();
  95. echo "</div>";
  96. }
  97. $query = ['FROM' => $this->getTable(),
  98. 'ORDER' => 'networkinterfaces_id ASC'];
  99. if ($result = $DB->request($query)) {
  100. $number = count($result);
  101. if ($number) {
  102. echo "<div id='liste_color' class='spaced center'>";
  103. if ($canupdate) {
  104. $rand = mt_rand();
  105. Html::openMassiveActionsForm('mass'.__CLASS__.$rand);
  106. $massiveactionparams = ['num_displayed' => $number,
  107. 'container' => 'mass'.__CLASS__.$rand];
  108. Html::showMassiveActions($massiveactionparams);
  109. }
  110. echo "<table class='tab_cadre' cellpadding='5' width='50%'>";
  111. echo "<tr>";
  112. if ($canupdate) {
  113. echo "<th width='10'>";
  114. Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand);
  115. echo "</th>";
  116. }
  117. echo "<th class='left'>".__('Type of network', 'archires')."</th>";
  118. echo "<th class='left'>".__('Color', 'archires')."</th><th></th>";
  119. echo "</tr>";
  120. while ($ligne = $result->next()) {
  121. $ID = $ligne["id"];
  122. echo "<tr class='tab_bg_1'>";
  123. if ($canupdate) {
  124. echo "<td width='10'>";
  125. Html::showMassiveActionCheckBox(__CLASS__, $ID);
  126. echo "</td>";
  127. }
  128. echo "</td><td>".Dropdown::getDropdownName("glpi_networkinterfaces",
  129. $ligne["networkinterfaces_id"])."</td><";
  130. echo "td bgcolor='".$ligne["color"]."'>".$ligne["color"]."</td>";
  131. }
  132. echo "</table>";
  133. if ($canupdate) {
  134. $massiveactionparams['ontop'] = false;
  135. Html::showMassiveActions($massiveactionparams);
  136. }
  137. echo "</div>";
  138. Html::closeForm();
  139. }
  140. }
  141. }
  142. function dropdownNetworkInterface() {
  143. global $DB;
  144. $colors = [];
  145. foreach($DB->request("glpi_plugin_archires_networkinterfacecolors") as $color) {
  146. $colors[] = $color['networkinterfaces_id'];
  147. }
  148. $query = ['FROM' => 'glpi_networkinterfaces',
  149. 'WHERE' => ['NOT' => ['id' => [implode("','",$colors)]]],
  150. 'ORDER' => 'name'];
  151. $result = $DB->request($query);
  152. if (count($result)) {
  153. while ($data = $result->next()) {
  154. $values[$data['id']] = $data["name"];
  155. }
  156. Dropdown::showFromArray('networkinterfaces_id', $values, ['width' => '80%',
  157. 'display_emptychoice' => true]);
  158. }
  159. }
  160. function getForbiddenStandardMassiveAction() {
  161. $forbidden = parent::getForbiddenStandardMassiveAction();
  162. $forbidden[] = 'update';
  163. return $forbidden;
  164. }
  165. }