vlancolor.class.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /*
  3. * @version $Id: vlancolor.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 PluginArchiresVlanColor extends CommonDBTM {
  30. static $rightname = "plugin_archires";
  31. function getFromDBbyVlan($vlan) {
  32. global $DB;
  33. $query = ['FROM' => $this->getTable(),
  34. 'WHERE' => ['vlans_id' => $vlan]];
  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. return false;
  44. }
  45. return false;
  46. }
  47. function addVlanColor($vlan,$color) {
  48. global $DB;
  49. if ($vlan != '-1') {
  50. if ($this->getfromDBbyVlan($vlan)) {
  51. $this->update(['id' => $this->fields['id'],
  52. 'color' => $color]);
  53. } else {
  54. $this->add(['vlans_id' => $vlan,
  55. 'color' => $color]);
  56. }
  57. } else {
  58. $query = ['FROM' => 'glpi_vlans'];
  59. $result = $DB->request($query);
  60. $i = 0;
  61. while ($i < count($result)) {
  62. $vlan_table=$DB->result($result, $i, "id");
  63. if ($this->getfromDBbyVlan($vlan_table)) {
  64. $this->update(['id' => $this->fields['id'],
  65. 'color' => $color]);
  66. } else {
  67. $this->add(['vlans_id' => $vlan_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='vlan_color' action='./config.form.php'>";
  79. echo "<table class='tab_cadre' cellpadding='5' width='50%'><tr ><th colspan='3'>";
  80. echo __('Associate colors to VLANs', 'archires')."</th></tr>";
  81. echo "<tr class='tab_bg_1'><td width='60%'>";
  82. $this->dropdownVlan();
  83. echo "</td>";
  84. echo "<td><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 class='center'><input type='submit' name='add_color_vlan' value=\"".
  90. _sx('button', 'Add')."\" class='submit'></td></tr>";
  91. echo "</table>";
  92. Html::closeForm();
  93. }
  94. $query = ['FROM' => $this->getTable(),
  95. 'ORDER' => 'vlans_id ASC'];
  96. if ($result = $DB->request($query)) {
  97. $number = count($result);
  98. if ($number != 0) {
  99. echo "<div id='liste_vlan'>";
  100. if ($canupdate) {
  101. $rand = mt_rand();
  102. Html::openMassiveActionsForm('mass'.__CLASS__.$rand);
  103. $massiveactionparams = ['num_displayed' => $number,
  104. 'container' => 'mass'.__CLASS__.$rand];
  105. Html::showMassiveActions($massiveactionparams);
  106. }
  107. echo "<table class='tab_cadre' cellpadding='5' width='50%'>";
  108. echo "<tr>";
  109. if ($canupdate) {
  110. echo "<th width='10'>";
  111. Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand);
  112. echo "</th>";
  113. }
  114. echo "<th class='left'>".__('VLAN')."</th>";
  115. echo "<th class='left'>".__('Color', 'archires')."</th><th></th>";
  116. echo "</tr>";
  117. while($ligne = $result->next()) {
  118. $ID = $ligne["id"];
  119. echo "<tr class='tab_bg_1'>";
  120. if ($canupdate) {
  121. echo "<td width='10'>";
  122. Html::showMassiveActionCheckBox(__CLASS__, $ID);
  123. echo "</td>";
  124. }
  125. echo "<td>".Dropdown::getDropdownName("glpi_vlans", $ligne["vlans_id"])."</td>";
  126. echo "<td bgcolor='".$ligne["color"]."'>".$ligne["color"]."</td>";
  127. echo "<td><input type='hidden' name='id' value='$ID'></td>";
  128. }
  129. echo "</table>";
  130. if ($canupdate) {
  131. $massiveactionparams['ontop'] = false;
  132. Html::showMassiveActions($massiveactionparams);
  133. }
  134. echo "</div>";
  135. Html::closeForm();
  136. }
  137. }
  138. }
  139. function dropdownVlan() {
  140. global $DB;
  141. $colors = [];
  142. foreach($DB->request("glpi_plugin_archires_vlancolors") as $color) {
  143. $colors[] = $color['vlans_id'];
  144. }
  145. $query = ['FROM' => 'glpi_vlans',
  146. 'WHERE' => ['NOT' => ['id' => [implode("','",$colors)]]],
  147. 'ORDER' => 'name'];
  148. $result = $DB->request($query);
  149. if (count($result)) {
  150. $values = [1 => __('All VLANs', 'archires')];
  151. while ($data = $result->next()) {
  152. $values[$data['id']] = $data["name"];
  153. }
  154. Dropdown::showFromArray('vlans_id', $values, ['width' => '80%',
  155. 'display_emptychoice' => true]);
  156. }
  157. }
  158. function getVlanbyNetworkPort ($ID) {
  159. global $DB;
  160. $query = ['SELECT' => 'glpi_vlans.id',
  161. 'FROM' => ['glpi_vlans', 'glpi_networkports_vlans'],
  162. 'WHERE' => ['vlans_id' => 'glpi_vlans.id',
  163. 'networkports_id' => $ID]];
  164. if ($result = $DB->request($query)) {
  165. $data_vlan = $result->next();
  166. $vlan = $data_vlan["id"] ;
  167. }
  168. return $vlan;
  169. }
  170. function getForbiddenStandardMassiveAction() {
  171. $forbidden = parent::getForbiddenStandardMassiveAction();
  172. $forbidden[] = 'update';
  173. return $forbidden;
  174. }
  175. }