locationquery.class.php 15 KB

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