networkequipmentquery.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <?php
  2. /*
  3. * @version $Id: networkequipmentquery.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 PluginArchiresNetworkEquipmentQuery extends CommonDBTM {
  30. static $rightname = "plugin_archires";
  31. protected $usenotepad = true;
  32. static function getTypeName($nb=0) {
  33. return _n('Network equipment', 'Network equipments', 1, 'archires');
  34. }
  35. function cleanDBonPurge() {
  36. $querytype = new PluginArchiresQueryType;
  37. $querytype->deleteByCriteria(['plugin_archires_queries_id' => $this->fields['id']]);
  38. }
  39. function rawSearchOptions() {
  40. $tab = [];
  41. $tab[] = ['id' => 'common',
  42. 'name' => self::getTypeName(2)];
  43. $tab[] = ['id' => '1',
  44. 'table' => $this->getTable(),
  45. 'field' =>'name',
  46. 'name' => __('Name'),
  47. 'datatype' => 'itemlink',
  48. 'itemlink_type' => $this->getType()];
  49. $tab[] = ['id' => '2',
  50. 'table' => 'glpi_networkequipments',
  51. 'field' => 'name',
  52. 'name' => _n('Network equipment', 'Network equipments', 1, 'archires'),
  53. 'datatype' => 'dropdown'];
  54. $tab[] = ['id' => '3',
  55. 'table' => 'glpi_networks',
  56. 'field' => 'name',
  57. 'name' => __('Network'),
  58. 'datatype' => 'dropdown'];
  59. $tab[] = ['id' => '4',
  60. 'table' => 'glpi_states',
  61. 'field' => 'name',
  62. 'name' => __('State'),
  63. 'datatype' => 'dropdown'];
  64. $tab[] = ['id' => '5',
  65. 'table' => 'glpi_groups',
  66. 'field' => 'completename',
  67. 'name' => __('Group'),
  68. 'datatype' => 'dropdown'];
  69. $tab[] = ['id' => '6',
  70. 'table' => 'glpi_vlans',
  71. 'field' => 'name',
  72. 'name' => __('VLAN'),
  73. 'datatype' => 'dropdown'];
  74. $tab[] = ['id' => '7',
  75. 'table' => 'glpi_plugin_archires_views',
  76. 'field' => 'name',
  77. 'name' => PluginArchiresView::getTypeName(1),
  78. 'datatype' => 'dropdown'];
  79. $tab[] = ['id' => '30',
  80. 'table' => $this->getTable(),
  81. 'field' => 'id',
  82. 'name' => __('ID'),
  83. 'datatype' => 'number'];
  84. $tab[] = ['id' => '80',
  85. 'table' => 'glpi_entities',
  86. 'field' => 'completename',
  87. 'name' => __('Entity'),
  88. 'datatype' => 'dropdown'];
  89. return $tab;
  90. }
  91. function prepareInputForAdd($input) {
  92. if (!isset ($input["plugin_archires_views_id"])
  93. || ($input["plugin_archires_views_id"] == 0)) {
  94. Session::addMessageAfterRedirect(__('Thanks to specify a default used view', 'archires'),
  95. false, ERROR);
  96. return [];
  97. }
  98. return $input;
  99. }
  100. function defineTabs($options=[]) {
  101. $ong = [];
  102. $this->addDefaultFormTab($ong)
  103. ->addStandardTab('PluginArchiresQueryType', $ong, $options)
  104. ->addStandardTab('PluginArchiresView', $ong, $options)
  105. ->addStandardTab('PluginArchiresPrototype', $ong, $options)
  106. ->addStandardTab('Notepad',$ong, $options);
  107. return $ong;
  108. }
  109. function showForm ($ID, $options=[]) {
  110. $this->initForm($ID, $options);
  111. $this->showFormHeader($options);
  112. echo "<tr class='tab_bg_1'>";
  113. echo "<td>".__('Name')."</td>";
  114. echo "<td>";
  115. Html::autocompletionTextField($this,"name");
  116. echo "</td>";
  117. echo "<td>".__('Group')."</td><td>";
  118. Group::dropdown(['name' => "groups_id",
  119. 'value' => $this->fields["groups_id"],
  120. 'entity' => $this->fields["entities_id"]]);
  121. echo "</td></tr>";
  122. echo "<tr class='tab_bg_1'>";
  123. echo "<td>"._n('Network equipment', 'Network equipments', 1, 'archires')."</td><td>";
  124. NetworkEquipment::dropdown(['name' => "networkequipments_id",
  125. 'value' => $this->fields["networkequipments_id"],
  126. 'entity' => $this->fields["entities_id"]]);
  127. echo "</td>";
  128. echo "<td>".__('VLAN')."</td><td>";
  129. Vlan::dropdown(['name' => "vlans_id",
  130. 'value' => $this->fields["vlans_id"]]);
  131. echo "</td></tr>";
  132. echo "<tr class='tab_bg_1'>";
  133. echo "<td>".__('Network')."</td><td>";
  134. Network::dropdown(['name' => "networks_id",
  135. 'value' => $this->fields["networks_id"]]);
  136. echo "</td>";
  137. echo "<td>".PluginArchiresView::getTypeName(1)."</td><td>";
  138. //View
  139. Dropdown::show('PluginArchiresView',
  140. ['name' => "plugin_archires_views_id",
  141. 'value' => $this->fields["plugin_archires_views_id"]]);
  142. echo "</td></tr>";
  143. echo "<tr class='tab_bg_1'>";
  144. echo "<td>".__('State')."</td><td colspan='3'>";
  145. State::dropdown(['name' => "states_id",
  146. 'value' => $this->fields["states_id"]]);
  147. echo "</td></tr>";
  148. $this->showFormButtons($options);
  149. return true;
  150. }
  151. function Query($ID,$PluginArchiresView,$for) {
  152. global $DB;
  153. $dbu = new DbUtils();
  154. $this->getFromDB($ID);
  155. $types = [];
  156. $devices = [];
  157. $ports = [];
  158. if ($PluginArchiresView->fields["computer"] != 0) {
  159. $types[] = 'Computer';
  160. }
  161. if ($PluginArchiresView->fields["printer"] != 0) {
  162. $types[] = 'Printer';
  163. }
  164. if ($PluginArchiresView->fields["peripheral"] != 0) {
  165. $types[] = 'Peripheral';
  166. }
  167. if ($PluginArchiresView->fields["phone"] != 0) {
  168. $types[]='Phone';
  169. }
  170. if ($PluginArchiresView->fields["networking"] != 0) {
  171. $types[] = 'NetworkEquipment';
  172. }
  173. $query_switch = ['SELECT' => ['glpi_networkports.name', 'glpi_networkports.id'],
  174. 'FROM' => 'glpi_networkports',
  175. 'LEFT JOIN' => ['glpi_networkequipments'
  176. =>['FKEY' => ['glpi_networkports' => 'items_id',
  177. 'glpi_networkequipments' => 'id']]],
  178. 'WHERE' => ['glpi_networkequipments.is_deleted' => 0,
  179. 'is_template' => 0,
  180. 'itemtype' => 'NetworkEquipment']
  181. + $dbu->getEntitiesRestrictCriteria('glpi_networkequipments')];
  182. if ($this->fields["networkequipments_id"]) {
  183. $query_switch['WHERE']['glpi_networkequipments.id'] = $this->fields["networkequipments_id"];
  184. }
  185. if ($result_switch = $DB->request($query_switch)) {
  186. while ($ligne = $result_switch->next()) {
  187. $port = $ligne['name'];
  188. $nw = new NetworkPort_NetworkPort();
  189. $end = $nw->getOppositeContact($ligne['id']);
  190. if ($end) {
  191. foreach ($types as $key => $val) {
  192. $itemtable = $dbu->getTableForItemType($val);
  193. $fieldsnp = "`np`.`id`, `np`.`items_id`, `np`.`logical_number`,
  194. `np`.`instantiation_type`, `glpi_ipaddresses`.`name` AS ip,
  195. `ipn`.`netmask`, `np`.`name` AS namep";
  196. $query = "SELECT `$itemtable`.`id` AS idc, $fieldsnp , `$itemtable`.`name`,
  197. `$itemtable`.`".getForeignKeyFieldForTable($dbu->getTableForItemType($val."Type"))."`
  198. AS `type`,
  199. `$itemtable`.`users_id`, `$itemtable`.`groups_id`,
  200. `$itemtable`.`contact`, `$itemtable`.`states_id`,
  201. `$itemtable`.`entities_id`,`$itemtable`.`locations_id`
  202. FROM `$itemtable`,
  203. `glpi_ipnetworks` AS ipn,
  204. `glpi_networkports` np";
  205. if ($this->fields["vlans_id"] > "0") {
  206. $query .= ", `glpi_networkports_vlans` nv";
  207. }
  208. $query .= " LEFT JOIN `glpi_networknames`
  209. ON (`glpi_networknames`.`itemtype` = 'NetworkPort'
  210. AND `np`.`id` = `glpi_networknames`.`items_id`)
  211. LEFT JOIN `glpi_ipaddresses`
  212. ON (`glpi_ipaddresses`.`itemtype` = 'NetworkName'
  213. AND `glpi_networknames`.`id` = `glpi_ipaddresses`.`items_id`)
  214. WHERE `np`.`instantiation_type` = 'NetworkPortEthernet'
  215. AND `np`.`itemtype` = '$val'
  216. AND `np`.`items_id` = `$itemtable`.`id`
  217. AND `np`.`id` ='$end'
  218. AND `$itemtable`.`is_deleted` = '0'
  219. AND `$itemtable`.`is_template` = '0' ".
  220. $dbu->getEntitiesRestrictRequest(" AND", $itemtable);
  221. if ($this->fields["vlans_id"] > "0") {
  222. $query .= " AND `nv`.`networkports_id` = `np`.`id`
  223. AND vlans_id= '".$this->fields["vlans_id"]."'";
  224. }
  225. if (($this->fields["networks_id"] > "0")
  226. && ($val != 'Phone')
  227. && ($val != 'Peripheral')) {
  228. $query .= " AND `$itemtable`.`networks_id` = '".$this->fields["networks_id"]."'";
  229. }
  230. if ($this->fields["states_id"] > "0") {
  231. $query .= " AND `$itemtable`.`states_id` = '".$this->fields["states_id"]."'";
  232. }
  233. if ($this->fields["groups_id"] > "0") {
  234. $query .= " AND `$itemtable`.`groups_id` = '".$this->fields["groups_id"]."'";
  235. }
  236. //types
  237. $PluginArchiresQueryType = new PluginArchiresQueryType();
  238. $query .= $PluginArchiresQueryType->queryTypeCheck($this->getType(), $ID, $val);
  239. $query .= "GROUP BY `np`.`id` ORDER BY `np`.`id` ASC ";
  240. if ($result = $DB->query($query)) {
  241. while ($data = $DB->fetchArray($result)) {
  242. if ($PluginArchiresView->fields["display_state"] != 0) {
  243. $devices[$val][$data["items_id"]]["states_id"] = $data["states_id"];
  244. }
  245. $devices[$val][$data["items_id"]]["type"] = $data["type"];
  246. $devices[$val][$data["items_id"]]["name"] = $data["name"];
  247. $devices[$val][$data["items_id"]]["users_id"] = $data["users_id"];
  248. $devices[$val][$data["items_id"]]["groups_id"] = $data["groups_id"];
  249. $devices[$val][$data["items_id"]]["contact"] = $data["contact"];
  250. $devices[$val][$data["items_id"]]["entity"] = $data["entities_id"];
  251. $devices[$val][$data["items_id"]]["locations_id"] = $data["locations_id"];
  252. $ports[$data["id"]]["items_id"] = $data["items_id"];
  253. $ports[$data["id"]]["logical_number"] = $data["logical_number"];
  254. $ports[$data["id"]]["ip"] = $data["ip"];
  255. $ports[$data["id"]]["netmask"] = $data["netmask"];
  256. $ports[$data["id"]]["namep"] = $data["namep"];
  257. $ports[$data["id"]]["idp"] = $data["id"];
  258. $ports[$data["id"]]["itemtype"] = $val;
  259. //ip
  260. if ($data["ip"]) {
  261. if (!empty($devices[$val][$data["items_id"]]["ip"])) {
  262. $devices[$val][$data["items_id"]]["ip"] .= " - ";
  263. $devices[$val][$data["items_id"]]["ip"] .= $data["ip"];
  264. } else {
  265. $devices[$val][$data["items_id"]]["ip"] = $data["ip"];
  266. }
  267. }
  268. //fin ip
  269. }
  270. }
  271. }
  272. }
  273. }
  274. }
  275. //The networking
  276. $query = "SELECT `n`.`id` AS `idn`, `np`.`id`, `np`.`items_id`, `np`.`logical_number`,
  277. `np`.`instantiation_type`, `glpi_ipaddresses`.`name` AS ip,
  278. `np`.`name` AS `namep`,
  279. `ipn`.`address` AS `nip`, `ipn`.`netmask`, `n`.`name`,
  280. `n`.`networkequipmenttypes_id` AS `type`, `n`.`users_id`, `n`.`groups_id`,
  281. `n`.`contact`, `n`.`states_id`, `n`.`entities_id`,`n`.`locations_id`
  282. FROM `glpi_networkports` `np`, `glpi_networkequipments` `n`,
  283. `glpi_ipnetworks` `ipn`";
  284. if ($this->fields["vlans_id"] > "0") {
  285. $query .= ", `glpi_networkports_vlans` nv ";
  286. }
  287. $query .= " LEFT JOIN `glpi_networknames`
  288. ON (`glpi_networknames`.`itemtype` = 'NetworkPort')
  289. LEFT JOIN `glpi_ipaddresses`
  290. ON (`glpi_ipaddresses`.`itemtype` = 'NetworkName'
  291. AND `glpi_networknames`.`id` = `glpi_ipaddresses`.`items_id`)
  292. WHERE `np`.`instantiation_type` = 'NetworkPortEthernet'
  293. AND `np`.`itemtype` = 'NetworkEquipment'
  294. AND `np`.`items_id` = `n`.`id`
  295. AND `n`.`is_deleted` = '0'
  296. AND `n`.`is_template` = '0'
  297. AND `glpi_networknames`.`items_id` = `np`.`id`".
  298. $dbu->getEntitiesRestrictRequest(' AND', 'n')."
  299. AND `n`.`id` = '".$this->fields["networkequipments_id"]."' ";
  300. if ($this->fields["vlans_id"] > "0") {
  301. $query .= " AND `nv`.`networkports_id` = `np`.`id`
  302. AND vlans_id= '".$this->fields["vlans_id"]."' ";
  303. }
  304. $query .= " ORDER BY `ip` ASC ";
  305. if ($result = $DB->request($query)) {
  306. while ($data = $result->next()) {
  307. if ($PluginArchiresView->fields["display_state"] != 0) {
  308. $devices['NetworkEquipment'][$data["items_id"]]["states_id"] = $data["states_id"];
  309. }
  310. $devices['NetworkEquipment'][$data["items_id"]]["name"] = $data["name"];
  311. $devices['NetworkEquipment'][$data["items_id"]]["type"] = $data["type"];
  312. $devices['NetworkEquipment'][$data["items_id"]]["users_id"] = $data["users_id"];
  313. $devices['NetworkEquipment'][$data["items_id"]]["groups_id"] = $data["groups_id"];
  314. $devices['NetworkEquipment'][$data["items_id"]]["contact"] = $data["contact"];
  315. $devices['NetworkEquipment'][$data["items_id"]]["ip"] = $data["nip"];
  316. $devices['NetworkEquipment'][$data["items_id"]]["entity"] = $data["entities_id"];
  317. $devices['NetworkEquipment'][$data["items_id"]]["locations_id"] = $data["locations_id"];
  318. $ports[$data["id"]]["items_id"] = $data["items_id"];
  319. $ports[$data["id"]]["logical_number"] = $data["logical_number"];
  320. $ports[$data["id"]]["ip"] = $data["ip"];
  321. $ports[$data["id"]]["netmask"] = $data["netmask"];
  322. $ports[$data["id"]]["namep"] = $data["namep"];
  323. $ports[$data["id"]]["idp"] = $data["id"];
  324. $ports[$data["id"]]["itemtype"] = 'NetworkEquipment';
  325. }
  326. }
  327. if ($for) {
  328. return $devices;
  329. }
  330. return $ports;
  331. }
  332. }