querytype.class.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /*
  3. * @version $Id: querytype.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 PluginArchiresQueryType extends CommonDBTM {
  30. static $rightname = "plugin_archires";
  31. function getFromDBbyType($itemtype, $type,$type_query,$query_ID) {
  32. global $DB;
  33. $query = ['FROM' => $this->getTable(),
  34. 'WHERE' => ['itemtype' => $itemtype,
  35. 'type' => $type,
  36. 'querytype' => $type_query,
  37. 'plugin_archires_queries_id' => $query_ID]];
  38. if ($result = $DB->request($query)) {
  39. if (count($result) != 1) {
  40. return false;
  41. }
  42. $this->fields = $result->next();
  43. if (is_array($this->fields) && count($this->fields)) {
  44. return true;
  45. }
  46. }
  47. return false;
  48. }
  49. function addType($querytype, $type, $itemtype, $plugin_archires_queries_id) {
  50. global $DB;
  51. $dbu = new DbUtils();
  52. if ($type != '-1') {
  53. if (!$this->getFromDBbyType($itemtype, $type, $querytype, $plugin_archires_queries_id)) {
  54. $this->add(['itemtype' => $itemtype,
  55. 'type' => $type,
  56. 'querytype' => $querytype,
  57. 'plugin_archires_queries_id' => $plugin_archires_queries_id]);
  58. }
  59. } else {
  60. $query = ['FROM' => $dbu->getTableForItemType($itemtype."Type")];
  61. $result = $DB->request($query);
  62. $i = 0;
  63. while ($i < count($result)) {
  64. $row = $result->next();
  65. $type_table = $row['id'];
  66. if (!$this->getFromDBbyType($itemtype, $type_table, $querytype,
  67. $plugin_archires_queries_id)) {
  68. $this->add(['itemtype' => $itemtype,
  69. 'type' => $type_table,
  70. 'querytype' => $querytype,
  71. 'plugin_archires_queries_id' => $plugin_archires_queries_id]);
  72. }
  73. $i++;
  74. }
  75. }
  76. }
  77. function queryTypeCheck($querytype, $plugin_archires_views_id, $val) {
  78. global $DB;
  79. $dbu = new DbUtils();
  80. $query0 = ['FROM' => $this->getTable(),
  81. 'WHERE' => ['querytype' => $querytype,
  82. 'plugin_archires_queries_id' => $plugin_archires_views_id,
  83. 'itemtype' => $val]];
  84. $result0 = $DB->request($query0);
  85. $query = "";
  86. if (count($result0)) {
  87. $itemtable = $dbu->getTableForItemType($val);
  88. $query = "AND `$itemtable`.`".getForeignKeyFieldForTable($dbu->getTableForItemType($val."Type"))."`
  89. IN (0 ";
  90. while ($data0 = $result0->next()) {
  91. $query .= ", ".$data0["type"];
  92. }
  93. $query .= ") ";
  94. }
  95. return $query;
  96. }
  97. static function showTypes($item) {
  98. global $DB;
  99. $type = $item->getType();
  100. $ID = $item->getID();
  101. if ($type == 'PluginArchiresLocationQuery') {
  102. $page = "locationquery";
  103. } else if ($type == 'PluginArchiresNetworkEquipmentQuery') {
  104. $page = "networkequipmentquery";
  105. } else if ($type == 'PluginArchiresApplianceQuery') {
  106. $page = "appliancequery";
  107. }
  108. $PluginArchiresArchires = new PluginArchiresArchires();
  109. if (Session::haveRight("plugin_archires", UPDATE)) {
  110. echo "<form method='post' action=\"./".$page.".form.php\">";
  111. echo "<table class='tab_cadre' cellpadding='5' width='34%'><tr><th colspan='2'>";
  112. echo __('Display types of items', 'archires')."</th></tr>";
  113. echo "<tr class='tab_bg_1'><td>";
  114. $PluginArchiresArchires->showAllItems("type", 0, 0, $_SESSION["glpiactive_entity"]);
  115. echo "</td>";
  116. echo "<td>";
  117. echo "<input type='hidden' name='query' value='$ID'>";
  118. echo "<input type='submit' name='addtype' value=\""._sx('button', 'Add')."\" class='submit'>";
  119. echo "</td></tr>";
  120. echo "</table>";
  121. Html::closeForm();
  122. }
  123. $query = ['FROM' => 'glpi_plugin_archires_querytypes',
  124. 'WHERE' => ['plugin_archires_queries_id' => $ID,
  125. 'querytype' => $type],
  126. 'ORDER' => ['itemtype ASC', 'type ASC']];
  127. if ($result = $DB->request($query)) {
  128. $number = count($result);
  129. if ($number) {
  130. echo "<div id='liste'>";
  131. if (Session::haveRight("plugin_archires", UPDATE)) {
  132. $rand = mt_rand();
  133. Html::openMassiveActionsForm('mass'.__CLASS__.$rand);
  134. $massiveactionparams = ['num_displayed' => $number,
  135. 'container' => 'mass'.__CLASS__.$rand];
  136. Html::showMassiveActions($massiveactionparams);
  137. }
  138. echo "<table class='tab_cadre' cellpadding='5' width='63%'>";
  139. echo "<tr>";
  140. if (Session::haveRight("plugin_archires", UPDATE)) {
  141. echo "<th width='10'>";
  142. Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand);
  143. echo "</th>";
  144. }
  145. echo "<th class='left'>".__('Item')."</th>";
  146. echo "<th class='left'>".__('Item type')."</th><th></th>";
  147. echo "</tr>";
  148. while ($ligne = $result->next()) {
  149. $ID = $ligne["id"];
  150. echo "<tr class='tab_bg_1'>";
  151. echo "<td width='10'>";
  152. if (Session::haveRight("plugin_archires", UPDATE)) {
  153. Html::showMassiveActionCheckBox(__CLASS__, $ID);
  154. } else {
  155. echo "&nbsp;";
  156. }
  157. $item = new $ligne["itemtype"]();
  158. echo "<td>".$item->getTypeName()."</td>";
  159. $class = $ligne["itemtype"]."Type";
  160. $typeclass = new $class();
  161. $typeclass->getFromDB($ligne["type"]);
  162. echo "<td>".$typeclass->fields["name"]."</td>";
  163. echo "<td>";
  164. echo "<input type='hidden' name='id' value='$ID'>";
  165. echo "</td>";
  166. }
  167. echo "</table>";
  168. if (Session::haveRight("plugin_archires", UPDATE)) {
  169. $massiveactionparams['ontop'] = false;
  170. Html::showMassiveActions($massiveactionparams);
  171. }
  172. echo "</div>";
  173. Html::closeForm();
  174. }
  175. }
  176. }
  177. static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtemplate=0) {
  178. switch ($item->getType()) {
  179. case 'PluginArchiresApplianceQuery' :
  180. case 'PluginArchiresLocationQuery' :
  181. case 'PluginArchiresNetworkEquipmentQuery' :
  182. self::showTypes($item);
  183. break;
  184. }
  185. return true;
  186. }
  187. function getTabNameForItem(CommonGLPI $item, $withtemplate=0) {
  188. if (!$withtemplate && Session::haveRight("plugin_archires", READ)) {
  189. switch ($item->getType()) {
  190. case 'PluginArchiresApplianceQuery' :
  191. case 'PluginArchiresLocationQuery' :
  192. case 'PluginArchiresNetworkEquipmentQuery' :
  193. return __('Item type');
  194. }
  195. }
  196. return '';
  197. }
  198. function getForbiddenStandardMassiveAction() {
  199. $forbidden = parent::getForbiddenStandardMassiveAction();
  200. $forbidden[] = 'update';
  201. return $forbidden;
  202. }
  203. }