imageitem.class.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /*
  3. * @version $Id: imageitem.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/aarchires
  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 PluginArchiresImageItem extends CommonDBTM {
  30. static $rightname = "plugin_archires";
  31. function getFromDBbyType($itemtype, $type) {
  32. global $DB;
  33. $query = ['FROM' => $this->getTable(),
  34. 'WHERE' => ['itemtype' => $itemtype,
  35. 'type' => $type]];
  36. if ($result = $DB->request($query)) {
  37. if (count($result) != 1) {
  38. return false;
  39. }
  40. $this->fields = $result->next();
  41. if (is_array($this->fields) && count($this->fields)) {
  42. return true;
  43. }
  44. }
  45. return false;
  46. }
  47. function addItemImage($type,$itemtype,$img) {
  48. global $DB;
  49. $dbu = new DbUtils();
  50. if ($type != '-1') {
  51. if ($this->getfromDBbyType($itemtype, $type)) {
  52. $this->update(['id' => $this->fields['id'],
  53. 'img' => $img]);
  54. } else {
  55. $this->add(['itemtype' => $itemtype,
  56. 'type' => $type,
  57. 'img' => $img]);
  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)) {
  67. $this->update(['id' => $this->fields['id'],
  68. 'img' => $img]);
  69. } else {
  70. $this->add(['itemtype' => $itemtype,
  71. 'type' => $type_table,
  72. 'img' => $img]);
  73. }
  74. $i++;
  75. }
  76. }
  77. }
  78. function showConfigForm() {
  79. global $DB, $CFG_GLPI;
  80. echo "<form method='post' action='./config.form.php'>";
  81. echo "<table class='tab_cadre' cellpadding='5' width='50%'>";
  82. echo "<tr><th colspan='4'>".__('Associate pictures with item types', 'archires')."</th></tr>";
  83. echo "<tr class='tab_bg_1'><td>";
  84. $PluginArchiresArchires = new PluginArchiresArchires();
  85. $PluginArchiresArchires->showAllItems("type",0,0,$_SESSION["glpiactive_entity"]);
  86. echo "</td><td>";
  87. //file
  88. $rep = "../pics/";
  89. $dir = opendir($rep);
  90. while ($f = readdir($dir)) {
  91. if (is_file($rep.$f)) {
  92. $values[$f] = $f;
  93. }
  94. }
  95. Dropdown::showFromArray('img', $values);
  96. closedir($dir);
  97. Html::showToolTip(nl2br(__('Some types of items must be created so that the association can exist',
  98. 'archires')));
  99. echo "</td><td>";
  100. echo "<div class='center'><input type='submit' name='add' value=\""._sx('button', 'Add').
  101. "\" class='submit'></div>";
  102. echo "</td></tr>";
  103. echo "</table>";
  104. Html::closeForm();
  105. $query = ['FROM' => $this->getTable(),
  106. 'ORDER' => ['itemtype ASC', 'type ASC']];
  107. if ($result = $DB->request($query)) {
  108. $number = count($result);
  109. if ($number) {
  110. $rand = mt_rand();
  111. echo "<div id='liste'>";
  112. Html::openMassiveActionsForm('mass'.__CLASS__.$rand);
  113. $massiveactionparams = ['num_displayed' => $number,
  114. 'container' => 'mass'.__CLASS__.$rand];
  115. Html::showMassiveActions($massiveactionparams);
  116. echo "<table class='tab_cadre' cellpadding='5' width='50%'>";
  117. echo "<tr>";
  118. echo "<th width='10'>";
  119. Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand);
  120. echo "</th>";
  121. echo "<th class='left'>".__('Item')."</th>";
  122. echo "<th class='left'>".__('Item type')."</th>";
  123. echo "<th class='left'>".__('Picture', 'archires')."</th><th></th>";
  124. echo "</tr>";
  125. while ($ligne = $result->next()) {
  126. $ID = $ligne["id"];
  127. echo "<tr class='tab_bg_1'>";
  128. echo "<td width='10'>";
  129. $PluginArchiresArchires = new PluginArchiresArchires();
  130. $item = new $ligne["itemtype"]();
  131. Html::showMassiveActionCheckBox(__CLASS__, $ID);
  132. echo "</td><td>".$item->getTypeName()."</td>";
  133. $class = $ligne["itemtype"]."Type";
  134. $typeclass = new $class();
  135. $typeclass->getFromDB($ligne["type"]);
  136. $name = '';
  137. if (isset($typeclass->fields["name"])) {
  138. $name = $typeclass->fields["name"];
  139. }
  140. echo "<td>".$name."</td>";
  141. echo "<td><img src=\"".$CFG_GLPI["root_doc"]."/plugins/archires/pics/".$ligne["img"].
  142. "\" alt=\"".$ligne["img"]."\" title=\"".$ligne["img"]."\"></td>";
  143. echo "<td width='10'>";
  144. echo "</td>";
  145. }
  146. echo "</table>";
  147. $massiveactionparams['ontop'] = false;
  148. Html::showMassiveActions($massiveactionparams);
  149. echo "</div>";
  150. Html::closeForm();
  151. }
  152. }
  153. }
  154. function displayItemImage($type,$itemtype,$test) {
  155. global $DB;
  156. $path = "";
  157. if ($test)
  158. $path="../";
  159. $image_name = $path."pics/nothing.png";
  160. $query = ['FROM' => 'glpi_plugin_archires_imageitems',
  161. 'WHERE' => ['itemtype' => $itemtype]];
  162. if ($result = $DB->request($query)) {
  163. while ($ligne= $result->next()) {
  164. $config_img = $ligne["img"];
  165. if ($type == $ligne["type"]) {
  166. $image_name = $path."pics/$config_img";
  167. }
  168. }
  169. }
  170. return $image_name;
  171. }
  172. function getForbiddenStandardMassiveAction() {
  173. $forbidden = parent::getForbiddenStandardMassiveAction();
  174. $forbidden[] = 'update';
  175. return $forbidden;
  176. }
  177. }