view.class.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. <?php
  2. /*
  3. * @version $Id: view.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 PluginArchiresView extends CommonDBTM {
  30. static $rightname = "plugin_archires";
  31. const PLUGIN_ARCHIRES_NETWORK_COLOR = 0;
  32. const PLUGIN_ARCHIRES_VLAN_COLOR = 1;
  33. const PLUGIN_ARCHIRES_JPEG_FORMAT = 0;
  34. const PLUGIN_ARCHIRES_PNG_FORMAT = 1;
  35. const PLUGIN_ARCHIRES_GIF_FORMAT = 2;
  36. const PLUGIN_ARCHIRES_SVG_FORMAT = 3;
  37. static function getTypeName($nb=0) {
  38. return _n('View', 'Views', $nb);
  39. }
  40. function rawSearchOptions() {
  41. $tab = [];
  42. $tab[] = ['id' => 'common',
  43. 'name' => self::getTypeName(2)];
  44. $tab[] = ['id' => '1',
  45. 'table' => $this->getTable(),
  46. 'field' =>'name',
  47. 'name' => __('Name'),
  48. 'datatype' => 'itemlink',
  49. 'itemlink_type' => $this->getType()];
  50. $tab[] = ['id' => '2',
  51. 'table' => $this->getTable(),
  52. 'field' => 'computer',
  53. 'name' => _n('Computer', 'Computers', 2),
  54. 'datatype' => 'bool'];
  55. $tab[] = ['id' => '3',
  56. 'table' => $this->getTable(),
  57. 'field' => 'networking',
  58. 'name' => _n('Network equipment', 'Network equipments', 2, 'archires'),
  59. 'datatype' => 'bool'];
  60. $tab[] = ['id' => '4',
  61. 'table' => $this->getTable(),
  62. 'field' => 'printer',
  63. 'name' => _n('Printer', 'Printers', 2),
  64. 'datatype' => 'bool'];
  65. $tab[] = ['id' => '5',
  66. 'table' => $this->getTable(),
  67. 'field' => 'peripheral',
  68. 'name' => _n('Device', 'Devices', 2),
  69. 'datatype' => 'bool'];
  70. $tab[] = ['id' => '6',
  71. 'table' => $this->getTable(),
  72. 'field' => 'phone',
  73. 'name' => _n('Phone', 'Phones', 2),
  74. 'datatype' => 'bool'];
  75. $tab[] = ['id' => '7',
  76. 'table' => $this->getTable(),
  77. 'field' => 'display_ports',
  78. 'name' => __('Display sockets', 'archires'),
  79. 'datatype' => 'text'];
  80. $tab[] = ['id' => '8',
  81. 'table' => $this->getTable(),
  82. 'field' => 'display_ip',
  83. 'name' => __('Display IP/Mask', 'archires'),
  84. 'datatype' => 'bool'];
  85. $tab[] = ['id' => '9',
  86. 'table' => $this->getTable(),
  87. 'field' => 'display_type',
  88. 'name' => __('Display item types', 'archires'),
  89. 'datatype' => 'bool'];
  90. $tab[] = ['id' => '10',
  91. 'table' => $this->getTable(),
  92. 'field' => 'display_state',
  93. 'name' => __('Display item statuses', 'archires'),
  94. 'datatype' => 'bool'];
  95. $tab[] = ['id' => '11',
  96. 'table' => $this->getTable(),
  97. 'field' => 'display_location',
  98. 'name' => __('Display item locations', 'archires'),
  99. 'datatype' => 'bool'];
  100. $tab[] = ['id' => '12',
  101. 'table' => $this->getTable(),
  102. 'field' => 'display_entity',
  103. 'name' => __('Display item entities', 'archires'),
  104. 'datatype' => 'bool'];
  105. $tab[] = ['id' => '13',
  106. 'table' => $this->getTable(),
  107. 'field' => 'engine',
  108. 'name' => __('Rendering engine', 'archires')];
  109. $tab[] = ['id' => '14',
  110. 'table' => $this->getTable(),
  111. 'field' => 'format',
  112. 'name' => __('Image format', 'archires')];
  113. $tab[] = ['id' => '15',
  114. 'table' => $this->getTable(),
  115. 'field' => 'color',
  116. 'name' => __('Color', 'archires')];
  117. return $tab;
  118. }
  119. function dropdownObject($obj) {
  120. global $DB;
  121. $dbu = new DbUtils();
  122. $ID = $obj->fields["id"];
  123. $query = ['SELECT' => ['id', 'name'],
  124. 'FROM' => $obj->getTable(),
  125. 'WHERE' => ['is_deleted' => 0]];
  126. // Add Restrict to current entities
  127. if ($obj->isEntityAssign()) {
  128. $query['WHERE'] += $dbu->getEntitiesRestrictCriteria($obj->getTable());
  129. }
  130. $query['ORDER'] = 'name ASC';
  131. if ($result = $DB->request($query)) {
  132. if (count($result)) {
  133. echo "<select name='plugin_archires_queries_id' size='1'> ";
  134. while ($ligne = $result->next()) {
  135. echo "<option value='".$ligne["id"]."' ".(($ligne["id"] == "".$ID."")?" selected ":"").">".
  136. $ligne["name"]."</option>";
  137. }
  138. echo "</select>";
  139. }
  140. }
  141. }
  142. function dropdownView($obj,$default) {
  143. global $DB;
  144. if (isset($obj->fields["id"])) {
  145. $default = $obj->fields["plugin_archires_views_id"];
  146. }
  147. $query = ['SELECT' => ['id', 'name'],
  148. 'FROM' => $this->getTable(),
  149. 'WHERE' => ['is_deleted' => 0,
  150. 'entities_id' => $_SESSION["glpiactive_entity"]],
  151. 'ORDER' => 'name ASC'];
  152. echo "<select name='plugin_archires_views_id' size='1'> ";
  153. echo "<option value='0'>".Dropdown::EMPTY_VALUE."</option>\n";
  154. if ($result = $DB->request($query)) {
  155. while ($ligne= $result->next()) {
  156. $view_name = $ligne["name"];
  157. $view_id = $ligne["id"];
  158. echo "<option value='".$view_id."' ".($view_id=="".$default.""?" selected ":"").">".
  159. $view_name."</option>";
  160. }
  161. }
  162. echo "</select>";
  163. }
  164. static function linkToAllViews($item) {
  165. echo "<div class='center'>";
  166. echo "<a href=\"./archires.graph.php?id=".$item->getID()."&querytype=".$item->getType()."\">".
  167. __('See all views', 'archires');
  168. echo "</a></div>";
  169. }
  170. function viewSelect($obj,$plugin_archires_views_id,$select=0) {
  171. global $CFG_GLPI,$DB;
  172. $querytype = get_class($obj);
  173. $ID = $obj->fields["id"];
  174. $object_view = $obj->fields["plugin_archires_views_id"];
  175. if (!isset($plugin_archires_views_id)) {
  176. $plugin_archires_views_id = $object_view;
  177. }
  178. if ($select) {
  179. // display only
  180. echo "<form method='get' name='selecting' action='".$CFG_GLPI["root_doc"].
  181. "/plugins/archires/front/archires.graph.php'>";
  182. echo "<table class='tab_cadre' cellpadding='5'>";
  183. echo "<tr class='tab_bg_1'>";
  184. echo "<td class='center'>".__('Display', 'archires');
  185. $this->dropdownObject($obj);
  186. echo "</td>";
  187. echo "<td class='center'>".self::getTypeName(2);
  188. $this->dropdownView(-1, $plugin_archires_views_id);
  189. echo "</td>";
  190. echo "<td>";
  191. echo "<input type='hidden' name='querytype' value=\"".$querytype."\"> ";
  192. echo "<input type='submit' class='submit' name='displayview' value=\"".
  193. _sx('button', 'Post')."\"> ";
  194. echo "</td>";
  195. echo "</tr>";
  196. echo "</table>";
  197. Html::closeForm();
  198. }
  199. echo "<a href='".$CFG_GLPI["root_doc"]."/plugins/archires/front/archires.map.php?format=".
  200. self::PLUGIN_ARCHIRES_SVG_FORMAT."&amp;id=".$ID."&amp;querytype=".$querytype.
  201. "&amp;plugin_archires_views_id=".
  202. $plugin_archires_views_id."'>[".__('SVG')."]</a>";
  203. }
  204. function showForm($ID, $options=[]) {
  205. $this->initForm($ID, $options);
  206. $this->showFormHeader($options);
  207. echo "<tr class='tab_bg_1'>";
  208. echo "<td colspan='1'>".__('Name')."</td>";
  209. echo "<td colspan='3'>";
  210. Html::autocompletionTextField($this, "name", ['size' => 20]);
  211. echo "</td></tr>";
  212. echo "<tr class='tab_bg_2'><th colspan='4'>".__('Display of items', 'archires')."</th></tr>";
  213. echo "<tr class='tab_bg_1'>";
  214. echo "<td>"._n('Computer', 'Computers', 2)."</td>";
  215. echo "<td>";
  216. Dropdown::showYesNo("computer",$this->fields["computer"]);
  217. echo "</td>";
  218. echo "<td>"._n('Network equipment', 'Network equipments', 2, 'archires')."</td>";
  219. echo "<td>";
  220. Dropdown::showYesNo("networking",$this->fields["networking"]);
  221. echo "</td></tr>";
  222. echo "<tr class='tab_bg_1'>";
  223. echo "<td>"._n('Printer', 'Printers', 2)."</td>";
  224. echo "<td>";
  225. Dropdown::showYesNo("printer",$this->fields["printer"]);
  226. echo "</td>";
  227. echo "<td>"._n('Device', 'Devices', 2)."</td>";
  228. echo "<td>";
  229. Dropdown::showYesNo("peripheral",$this->fields["peripheral"]);
  230. echo "</td></tr>";
  231. echo "<tr class='tab_bg_1'>";
  232. echo "<td>"._n('Phone', 'Phones', 2)."</td>";
  233. echo "<td>";
  234. Dropdown::showYesNo("phone",$this->fields["phone"]);
  235. echo "</td><td>&nbsp;</td></tr>";
  236. echo "<tr class='tab_bg_2'><th colspan='4'>".__('Display description', 'archires')."</th></tr>";
  237. echo "<tr class='tab_bg_1'>";
  238. echo "<td>".__('Display sockets', 'archires')."</td>";
  239. echo "<td><select name='display_ports'> ";
  240. echo "<option ";
  241. if ($this->fields["display_ports"] == '0') {
  242. echo "selected ";
  243. }
  244. echo "value='0'>".__('No')."</option>";
  245. echo "<option ";
  246. if ($this->fields["display_ports"] == '1') {
  247. echo "selected ";
  248. }
  249. echo "value='1'>".__('See numbers', 'archires')."</option>";
  250. echo "<option ";
  251. if ($this->fields["display_ports"] == '2') {
  252. echo "selected ";
  253. }
  254. echo "value='2'>".__('See names', 'archires')."</option>";
  255. echo "</select></td>";
  256. echo "<td>".__('Display IP/Mask', 'archires')."</td>";
  257. echo "<td>";
  258. Dropdown::showYesNo("display_ip",$this->fields["display_ip"]);
  259. echo "</td></tr>";
  260. echo "<tr class='tab_bg_1'>";
  261. echo "<td>".__('Display item types', 'archires')."</td>";
  262. echo "<td>";
  263. Dropdown::showYesNo("display_type",$this->fields["display_type"]);
  264. echo "</td>";
  265. echo "<td>".__('Display item statuses', 'archires')."</td>";
  266. echo "<td>";
  267. Dropdown::showYesNo("display_state",$this->fields["display_state"]);
  268. echo "</td></tr>";
  269. echo "<tr class='tab_bg_1'>";
  270. echo "<td>".__('Display item locations', 'archires')."</td>";
  271. echo "<td>";
  272. Dropdown::showYesNo("display_location",$this->fields["display_location"]);
  273. echo "</td>";
  274. echo "<td>".__('Display item entities', 'archires')."</td>";
  275. echo "<td>";
  276. Dropdown::showYesNo("display_entity",$this->fields["display_entity"]);
  277. echo "</td></tr>";
  278. echo "<tr class='tab_bg_2'><th colspan='4'>".__('Generation', 'archires')."</th></tr>";
  279. echo "<tr class='tab_bg_1'>";
  280. echo "<td>".__('Rendering engine', 'archires')."</td>";
  281. echo "<td><select name='engine'> ";
  282. echo "<option ";
  283. if ($this->fields["engine"] == '0') {
  284. echo "selected ";
  285. }
  286. echo "value='0'>Dot</option>";
  287. echo "<option ";
  288. if ($this->fields["engine"] == '1') {
  289. echo "selected ";
  290. }
  291. echo "value='1'>Neato</option>";
  292. echo "</select>&nbsp;";
  293. Html::showToolTip(nl2br(__('With neato, the sockets will not be displayed', 'archires')));
  294. echo "</td>";
  295. echo "<td>".__('Image format', 'archires')."</td>";
  296. echo "<td><select name='format'> ";
  297. echo "<option ";
  298. if ($this->fields["format"] == self::PLUGIN_ARCHIRES_JPEG_FORMAT) {
  299. echo "selected ";
  300. }
  301. echo "value='0'>jpeg</option>";
  302. echo "<option ";
  303. if ($this->fields["format"] == self::PLUGIN_ARCHIRES_PNG_FORMAT) {
  304. echo "selected ";
  305. }
  306. echo "value='1'>png</option>";
  307. echo "<option ";
  308. if ($this->fields["format"] == self::PLUGIN_ARCHIRES_GIF_FORMAT) {
  309. echo "selected ";
  310. }
  311. echo "value='2'>gif</option>";
  312. echo "</select></td>";
  313. echo "<tr class='tab_bg_1'>";
  314. echo "<td>".__('Color', 'archires')."</td>";
  315. echo "<td colspan='3'><select name='color'> ";
  316. echo "<option ";
  317. if ($this->fields["color"]=='0') {
  318. echo "selected ";
  319. }
  320. echo "value='0'>".__('Type of network', 'archires')."</option>";
  321. echo "<option ";
  322. if ($this->fields["color"] == '1') {
  323. echo "selected ";
  324. }
  325. echo "value='1'>".__('VLAN')."</option>";
  326. echo "</select></td></tr>";
  327. $this->showFormButtons($options);
  328. return true;
  329. }
  330. static function showView($item) {
  331. global $DB;
  332. $plugin_archires_views_id = $item->fields["plugin_archires_views_id"];
  333. if (!$plugin_archires_views_id) {
  334. return false;
  335. }
  336. $view = new self();
  337. $view->getFromDB($plugin_archires_views_id);
  338. $name_config = $view->fields["name"];
  339. echo "<table class='tab_cadre_fixe' cellpadding='2'width='75%'>";
  340. echo "<tr>";
  341. echo "<th colspan='3'>".sprintf(__('%1$s: %2$s'), self::getTypeName(1), $name_config);
  342. echo "</th></tr>";
  343. echo "<tr class='tab_bg_2 top'>";
  344. echo "<th>".__('Display of items', 'archires')."</th>";
  345. echo "<th>".__('Display description', 'archires')."</th>";
  346. echo "<th>".__('Generation', 'archires').
  347. "</th></tr>";
  348. echo "<tr class='tab_bg_1 top'><td class='center'>";
  349. if ($view->fields["computer"] != 0) {
  350. printf(__('%1$s: %2$s'), _n('Computer', 'Computers', 2), __('Yes'));
  351. } else {
  352. printf(__('%1$s: %2$s'), _n('Computer', 'Computers', 2), __('No'));
  353. }
  354. echo "<br>";
  355. if ($view->fields["networking"] != 0) {
  356. printf(__('%1$s: %2$s'), _n('Network equipment', 'Network equipments', 2, 'archires'),
  357. __('Yes'));
  358. } else {
  359. printf(__('%1$s: %2$s'), _n('Network equipment', 'Network equipments', 2, 'archires'),
  360. __('No'));
  361. }
  362. echo "<br>";
  363. if ($view->fields["printer"] !=0 ) {
  364. printf(__('%1$s: %2$s'), _n('Printer', 'Printers', 2), __('Yes'));
  365. } else {
  366. printf(__('%1$s: %2$s'), _n('Printer', 'Printers', 2), __('No'));
  367. }
  368. echo "<br>";
  369. if ($view->fields["peripheral"]!=0) {
  370. printf(__('%1$s: %2$s'), _n('Device', 'Devices', 2), __('Yes'));
  371. } else {
  372. printf(__('%1$s: %2$s'), _n('Device', 'Devices', 2), __('No'));
  373. }
  374. echo "<br>";
  375. if ($view->fields["phone"] != 0) {
  376. printf(__('%1$s: %2$s'), _n('Phone', 'Phones', 2), __('Yes'));
  377. } else {
  378. printf(__('%1$s: %2$s'), _n('Phone', 'Phones', 2), __('No'));
  379. }
  380. echo "</td>";
  381. echo "<td class='center'>";
  382. if ($view->fields["display_ports"] != 0) {
  383. printf(__('%1$s: %2$s'), __('Display sockets', 'archires'), __('Yes'));
  384. } else {
  385. printf(__('%1$s: %2$s'), __('Display sockets', 'archires'), __('No'));
  386. }
  387. echo "<br>";
  388. if ($view->fields["display_ip"] != 0) {
  389. printf(__('%1$s: %2$s'), __('Display IP/Mask', 'archires'), __('Yes'));
  390. } else {
  391. printf(__('%1$s: %2$s'), __('Display IP/Mask', 'archires'), __('No'));
  392. }
  393. echo "<br>";
  394. if ($view->fields["display_type"] != 0) {
  395. printf(__('%1$s: %2$s'), __('Display item types', 'archires'), __('Yes'));
  396. } else {
  397. printf(__('%1$s: %2$s'), __('Display item types', 'archires'), __('No'));
  398. }
  399. echo "<br>";
  400. if ($view->fields["display_state"] != 0) {
  401. printf(__('%1$s: %2$s'), __('Display item statuses', 'archires'), __('Yes'));
  402. } else {
  403. printf(__('%1$s: %2$s'), __('Display item statuses', 'archires'), __('No'));
  404. }
  405. echo "<br>";
  406. if ($view->fields["display_location"] != 0) {
  407. printf(__('%1$s: %2$s'), __('Display item locations', 'archires'), __('Yes'));
  408. } else {
  409. printf(__('%1$s: %2$s'), __('Display item locations', 'archires'), __('No'));
  410. }
  411. echo "<br>";
  412. if ($view->fields["display_entity"] != 0) {
  413. printf(__('%1$s: %2$s'), __('Display item entities', 'archires'), __('Yes'));
  414. } else {
  415. printf(__('%1$s: %2$s'), __('Display item entities', 'archires'), __('No'));
  416. }
  417. echo "</td>";
  418. $engine = '';
  419. if ($view->fields["engine"] != 0) {
  420. $engine = "Neato";
  421. } else {
  422. $engine = "Dot";
  423. }
  424. echo "<td class='center'>". sprintf(__('%1$s: %2$s'), __('All'),
  425. sprintf(__('%1$s %2$s'),
  426. __('Rendering engine', 'archires'), $engine));
  427. echo "<br>";
  428. $format_graph = '';
  429. if ($view->fields["format"] == self::PLUGIN_ARCHIRES_JPEG_FORMAT) {
  430. $format_graph = "jpeg";
  431. } else if ($view->fields["format"] == self::PLUGIN_ARCHIRES_PNG_FORMAT) {
  432. $format_graph = "png";
  433. } else if ($view->fields["format"] == self::PLUGIN_ARCHIRES_GIF_FORMAT) {
  434. $format_graph = "gif";
  435. }
  436. printf(__('%1$s: %2$s'), __('Image format', 'archires'), $format_graph);
  437. echo "</td></tr>";
  438. echo "</table>";
  439. }
  440. static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtemplate=0) {
  441. switch ($item->getType()) {
  442. case 'PluginArchiresApplianceQuery' :
  443. case 'PluginArchiresLocationQuery' :
  444. case 'PluginArchiresNetworkEquipmentQuery' :
  445. switch ($tabnum) {
  446. case 1 :
  447. self::showView($item);
  448. break;
  449. }
  450. break;
  451. }
  452. return true;
  453. }
  454. function getTabNameForItem(CommonGLPI $item, $withtemplate=0) {
  455. if (!$withtemplate && Session::haveRight("plugin_archires", READ)) {
  456. switch ($item->getType()) {
  457. case 'PluginArchiresApplianceQuery' :
  458. case 'PluginArchiresLocationQuery' :
  459. case 'PluginArchiresNetworkEquipmentQuery' :
  460. return self::getTypeName(1);
  461. }
  462. }
  463. return '';
  464. }
  465. }