view.class.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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-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 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='".Plugin::getWebDir('archires')."/front/archires.graph.php'>";
  181. echo "<table class='tab_cadre' cellpadding='5'>";
  182. echo "<tr class='tab_bg_1'>";
  183. echo "<td class='center'>".__('Display', 'archires');
  184. $this->dropdownObject($obj);
  185. echo "</td>";
  186. echo "<td class='center'>".self::getTypeName(2);
  187. $this->dropdownView(-1, $plugin_archires_views_id);
  188. echo "</td>";
  189. echo "<td>";
  190. echo "<input type='hidden' name='querytype' value=\"".$querytype."\"> ";
  191. echo "<input type='submit' class='submit' name='displayview' value=\"".
  192. _sx('button', 'Post')."\"> ";
  193. echo "</td>";
  194. echo "</tr>";
  195. echo "</table>";
  196. Html::closeForm();
  197. }
  198. echo "<a href='".Plugin::getWebDir('archires')."/front/archires.map.php?format=".
  199. self::PLUGIN_ARCHIRES_SVG_FORMAT."&amp;id=".$ID."&amp;querytype=".$querytype.
  200. "&amp;plugin_archires_views_id=".
  201. $plugin_archires_views_id."'>[".__('SVG')."]</a>";
  202. }
  203. function showForm($ID, $options=[]) {
  204. $this->initForm($ID, $options);
  205. $this->showFormHeader($options);
  206. echo "<tr class='tab_bg_1'>";
  207. echo "<td colspan='1'>".__('Name')."</td>";
  208. echo "<td colspan='3'>";
  209. Html::autocompletionTextField($this, "name", ['size' => 20]);
  210. echo "</td></tr>";
  211. echo "<tr class='tab_bg_2'><th colspan='4'>".__('Display of items', 'archires')."</th></tr>";
  212. echo "<tr class='tab_bg_1'>";
  213. echo "<td>"._n('Computer', 'Computers', 2)."</td>";
  214. echo "<td>";
  215. Dropdown::showYesNo("computer",$this->fields["computer"]);
  216. echo "</td>";
  217. echo "<td>"._n('Network equipment', 'Network equipments', 2, 'archires')."</td>";
  218. echo "<td>";
  219. Dropdown::showYesNo("networking",$this->fields["networking"]);
  220. echo "</td></tr>";
  221. echo "<tr class='tab_bg_1'>";
  222. echo "<td>"._n('Printer', 'Printers', 2)."</td>";
  223. echo "<td>";
  224. Dropdown::showYesNo("printer",$this->fields["printer"]);
  225. echo "</td>";
  226. echo "<td>"._n('Device', 'Devices', 2)."</td>";
  227. echo "<td>";
  228. Dropdown::showYesNo("peripheral",$this->fields["peripheral"]);
  229. echo "</td></tr>";
  230. echo "<tr class='tab_bg_1'>";
  231. echo "<td>"._n('Phone', 'Phones', 2)."</td>";
  232. echo "<td>";
  233. Dropdown::showYesNo("phone",$this->fields["phone"]);
  234. echo "</td><td>&nbsp;</td></tr>";
  235. echo "<tr class='tab_bg_2'><th colspan='4'>".__('Display description', 'archires')."</th></tr>";
  236. echo "<tr class='tab_bg_1'>";
  237. echo "<td>".__('Display sockets', 'archires')."</td>";
  238. echo "<td><select name='display_ports'> ";
  239. echo "<option ";
  240. if ($this->fields["display_ports"] == '0') {
  241. echo "selected ";
  242. }
  243. echo "value='0'>".__('No')."</option>";
  244. echo "<option ";
  245. if ($this->fields["display_ports"] == '1') {
  246. echo "selected ";
  247. }
  248. echo "value='1'>".__('See numbers', 'archires')."</option>";
  249. echo "<option ";
  250. if ($this->fields["display_ports"] == '2') {
  251. echo "selected ";
  252. }
  253. echo "value='2'>".__('See names', 'archires')."</option>";
  254. echo "</select></td>";
  255. echo "<td>".__('Display IP/Mask', 'archires')."</td>";
  256. echo "<td>";
  257. Dropdown::showYesNo("display_ip",$this->fields["display_ip"]);
  258. echo "</td></tr>";
  259. echo "<tr class='tab_bg_1'>";
  260. echo "<td>".__('Display item types', 'archires')."</td>";
  261. echo "<td>";
  262. Dropdown::showYesNo("display_type",$this->fields["display_type"]);
  263. echo "</td>";
  264. echo "<td>".__('Display item statuses', 'archires')."</td>";
  265. echo "<td>";
  266. Dropdown::showYesNo("display_state",$this->fields["display_state"]);
  267. echo "</td></tr>";
  268. echo "<tr class='tab_bg_1'>";
  269. echo "<td>".__('Display item locations', 'archires')."</td>";
  270. echo "<td>";
  271. Dropdown::showYesNo("display_location",$this->fields["display_location"]);
  272. echo "</td>";
  273. echo "<td>".__('Display item entities', 'archires')."</td>";
  274. echo "<td>";
  275. Dropdown::showYesNo("display_entity",$this->fields["display_entity"]);
  276. echo "</td></tr>";
  277. echo "<tr class='tab_bg_2'><th colspan='4'>".__('Generation', 'archires')."</th></tr>";
  278. echo "<tr class='tab_bg_1'>";
  279. echo "<td>".__('Rendering engine', 'archires')."</td>";
  280. echo "<td><select name='engine'> ";
  281. echo "<option ";
  282. if ($this->fields["engine"] == '0') {
  283. echo "selected ";
  284. }
  285. echo "value='0'>Dot</option>";
  286. echo "<option ";
  287. if ($this->fields["engine"] == '1') {
  288. echo "selected ";
  289. }
  290. echo "value='1'>Neato</option>";
  291. echo "</select>&nbsp;";
  292. Html::showToolTip(nl2br(__('With neato, the sockets will not be displayed', 'archires')));
  293. echo "</td>";
  294. echo "<td>".__('Image format', 'archires')."</td>";
  295. echo "<td><select name='format'> ";
  296. echo "<option ";
  297. if ($this->fields["format"] == self::PLUGIN_ARCHIRES_JPEG_FORMAT) {
  298. echo "selected ";
  299. }
  300. echo "value='0'>jpeg</option>";
  301. echo "<option ";
  302. if ($this->fields["format"] == self::PLUGIN_ARCHIRES_PNG_FORMAT) {
  303. echo "selected ";
  304. }
  305. echo "value='1'>png</option>";
  306. echo "<option ";
  307. if ($this->fields["format"] == self::PLUGIN_ARCHIRES_GIF_FORMAT) {
  308. echo "selected ";
  309. }
  310. echo "value='2'>gif</option>";
  311. echo "</select></td>";
  312. echo "<tr class='tab_bg_1'>";
  313. echo "<td>".__('Color', 'archires')."</td>";
  314. echo "<td colspan='3'><select name='color'> ";
  315. echo "<option ";
  316. if ($this->fields["color"]=='0') {
  317. echo "selected ";
  318. }
  319. echo "value='0'>".__('Type of network', 'archires')."</option>";
  320. echo "<option ";
  321. if ($this->fields["color"] == '1') {
  322. echo "selected ";
  323. }
  324. echo "value='1'>".__('VLAN')."</option>";
  325. echo "</select></td></tr>";
  326. $this->showFormButtons($options);
  327. return true;
  328. }
  329. static function showView($item) {
  330. global $DB;
  331. $plugin_archires_views_id = $item->fields["plugin_archires_views_id"];
  332. if (!$plugin_archires_views_id) {
  333. return false;
  334. }
  335. $view = new self();
  336. $view->getFromDB($plugin_archires_views_id);
  337. $name_config = $view->fields["name"];
  338. echo "<table class='tab_cadre_fixe' cellpadding='2'width='75%'>";
  339. echo "<tr>";
  340. echo "<th colspan='3'>".sprintf(__('%1$s: %2$s'), self::getTypeName(1), $name_config);
  341. echo "</th></tr>";
  342. echo "<tr class='tab_bg_2 top'>";
  343. echo "<th>".__('Display of items', 'archires')."</th>";
  344. echo "<th>".__('Display description', 'archires')."</th>";
  345. echo "<th>".__('Generation', 'archires').
  346. "</th></tr>";
  347. echo "<tr class='tab_bg_1 top'><td class='center'>";
  348. if ($view->fields["computer"] != 0) {
  349. printf(__('%1$s: %2$s'), _n('Computer', 'Computers', 2), __('Yes'));
  350. } else {
  351. printf(__('%1$s: %2$s'), _n('Computer', 'Computers', 2), __('No'));
  352. }
  353. echo "<br>";
  354. if ($view->fields["networking"] != 0) {
  355. printf(__('%1$s: %2$s'), _n('Network equipment', 'Network equipments', 2, 'archires'),
  356. __('Yes'));
  357. } else {
  358. printf(__('%1$s: %2$s'), _n('Network equipment', 'Network equipments', 2, 'archires'),
  359. __('No'));
  360. }
  361. echo "<br>";
  362. if ($view->fields["printer"] !=0 ) {
  363. printf(__('%1$s: %2$s'), _n('Printer', 'Printers', 2), __('Yes'));
  364. } else {
  365. printf(__('%1$s: %2$s'), _n('Printer', 'Printers', 2), __('No'));
  366. }
  367. echo "<br>";
  368. if ($view->fields["peripheral"]!=0) {
  369. printf(__('%1$s: %2$s'), _n('Device', 'Devices', 2), __('Yes'));
  370. } else {
  371. printf(__('%1$s: %2$s'), _n('Device', 'Devices', 2), __('No'));
  372. }
  373. echo "<br>";
  374. if ($view->fields["phone"] != 0) {
  375. printf(__('%1$s: %2$s'), _n('Phone', 'Phones', 2), __('Yes'));
  376. } else {
  377. printf(__('%1$s: %2$s'), _n('Phone', 'Phones', 2), __('No'));
  378. }
  379. echo "</td>";
  380. echo "<td class='center'>";
  381. if ($view->fields["display_ports"] != 0) {
  382. printf(__('%1$s: %2$s'), __('Display sockets', 'archires'), __('Yes'));
  383. } else {
  384. printf(__('%1$s: %2$s'), __('Display sockets', 'archires'), __('No'));
  385. }
  386. echo "<br>";
  387. if ($view->fields["display_ip"] != 0) {
  388. printf(__('%1$s: %2$s'), __('Display IP/Mask', 'archires'), __('Yes'));
  389. } else {
  390. printf(__('%1$s: %2$s'), __('Display IP/Mask', 'archires'), __('No'));
  391. }
  392. echo "<br>";
  393. if ($view->fields["display_type"] != 0) {
  394. printf(__('%1$s: %2$s'), __('Display item types', 'archires'), __('Yes'));
  395. } else {
  396. printf(__('%1$s: %2$s'), __('Display item types', 'archires'), __('No'));
  397. }
  398. echo "<br>";
  399. if ($view->fields["display_state"] != 0) {
  400. printf(__('%1$s: %2$s'), __('Display item statuses', 'archires'), __('Yes'));
  401. } else {
  402. printf(__('%1$s: %2$s'), __('Display item statuses', 'archires'), __('No'));
  403. }
  404. echo "<br>";
  405. if ($view->fields["display_location"] != 0) {
  406. printf(__('%1$s: %2$s'), __('Display item locations', 'archires'), __('Yes'));
  407. } else {
  408. printf(__('%1$s: %2$s'), __('Display item locations', 'archires'), __('No'));
  409. }
  410. echo "<br>";
  411. if ($view->fields["display_entity"] != 0) {
  412. printf(__('%1$s: %2$s'), __('Display item entities', 'archires'), __('Yes'));
  413. } else {
  414. printf(__('%1$s: %2$s'), __('Display item entities', 'archires'), __('No'));
  415. }
  416. echo "</td>";
  417. $engine = '';
  418. if ($view->fields["engine"] != 0) {
  419. $engine = "Neato";
  420. } else {
  421. $engine = "Dot";
  422. }
  423. echo "<td class='center'>". sprintf(__('%1$s: %2$s'), __('All'),
  424. sprintf(__('%1$s %2$s'),
  425. __('Rendering engine', 'archires'), $engine));
  426. echo "<br>";
  427. $format_graph = '';
  428. if ($view->fields["format"] == self::PLUGIN_ARCHIRES_JPEG_FORMAT) {
  429. $format_graph = "jpeg";
  430. } else if ($view->fields["format"] == self::PLUGIN_ARCHIRES_PNG_FORMAT) {
  431. $format_graph = "png";
  432. } else if ($view->fields["format"] == self::PLUGIN_ARCHIRES_GIF_FORMAT) {
  433. $format_graph = "gif";
  434. }
  435. printf(__('%1$s: %2$s'), __('Image format', 'archires'), $format_graph);
  436. echo "</td></tr>";
  437. echo "</table>";
  438. }
  439. static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtemplate=0) {
  440. switch ($item->getType()) {
  441. case 'PluginArchiresApplianceQuery' :
  442. case 'PluginArchiresLocationQuery' :
  443. case 'PluginArchiresNetworkEquipmentQuery' :
  444. switch ($tabnum) {
  445. case 1 :
  446. self::showView($item);
  447. break;
  448. }
  449. break;
  450. }
  451. return true;
  452. }
  453. function getTabNameForItem(CommonGLPI $item, $withtemplate=0) {
  454. if (!$withtemplate && Session::haveRight("plugin_archires", READ)) {
  455. switch ($item->getType()) {
  456. case 'PluginArchiresApplianceQuery' :
  457. case 'PluginArchiresLocationQuery' :
  458. case 'PluginArchiresNetworkEquipmentQuery' :
  459. return self::getTypeName(1);
  460. }
  461. }
  462. return '';
  463. }
  464. }