PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/inc/event.class.php

https://github.com/ardowz/Thesis-SideB
PHP | 388 lines | 354 code | 2 blank | 32 comment | 1 complexity | d537dd7369d1df0535daf16a424c068e MD5 | raw file
  1. <?php
  2. /*
  3. * @version $Id: event.class.php 14684 2011-06-11 06:32:40Z remi $
  4. -------------------------------------------------------------------------
  5. GLPI - Gestionnaire Libre de Parc Informatique
  6. Copyright (C) 2003-2011 by the INDEPNET Development Team.
  7. http://indepnet.net/ http://glpi-project.org
  8. -------------------------------------------------------------------------
  9. LICENSE
  10. This file is part of GLPI.
  11. GLPI is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15. GLPI is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with GLPI; if not, write to the Free Software Foundation, Inc.,
  21. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. --------------------------------------------------------------------------
  23. */
  24. // ----------------------------------------------------------------------
  25. // Original Author of file: Julien Dombre
  26. // Purpose of file:
  27. // ----------------------------------------------------------------------
  28. if (!defined('GLPI_ROOT')) {
  29. die("Sorry. You can't access directly to this file");
  30. }
  31. // Event class
  32. class Event extends CommonDBTM {
  33. static function getTypeName() {
  34. global $LANG;
  35. return $LANG['Menu'][30];
  36. }
  37. function prepareInputForAdd($input) {
  38. global $CFG_GLPI;
  39. if (isset($input['level']) && $input['level'] <= $CFG_GLPI["event_loglevel"]) {
  40. return $input;
  41. }
  42. return false;
  43. }
  44. /**
  45. * Log an event.
  46. *
  47. * Log the event $event on the glpi_event table with all the others args, if
  48. * $level is above or equal to setting from configuration.
  49. *
  50. * @param $items_id
  51. * @param $type
  52. * @param $level
  53. * @param $service
  54. * @param $event
  55. **/
  56. static function log ($items_id, $type, $level, $service, $event) {
  57. $input = array('items_id' => intval($items_id),
  58. 'type' => addslashes($type),
  59. 'date' => $_SESSION["glpi_currenttime"],
  60. 'service' => addslashes($service),
  61. 'level' => intval($level),
  62. 'message' => addslashes($event));
  63. $tmp = new self();
  64. return $tmp->add($input);
  65. }
  66. /**
  67. * Clean old event - Call by cron
  68. *
  69. * @param $day integer
  70. *
  71. * @return integer number of events deleted
  72. **/
  73. static function cleanOld ($day) {
  74. global $DB;
  75. $secs = $day * DAY_TIMESTAMP;
  76. $query_exp = "DELETE
  77. FROM `glpi_events`
  78. WHERE UNIX_TIMESTAMP(date) < UNIX_TIMESTAMP()-$secs";
  79. $DB->query($query_exp);
  80. return $DB->affected_rows();
  81. }
  82. /**
  83. * Return arrays for function showEvent et lastEvent
  84. **/
  85. static function logArray() {
  86. global $LANG;
  87. static $logItemtype = array();
  88. static $logService = array();
  89. if (count($logItemtype)) {
  90. return array($logItemtype, $logService);
  91. }
  92. $logItemtype = array('system' => $LANG['log'][1],
  93. 'devices' => $LANG['log'][18],
  94. 'planning' => $LANG['log'][16],
  95. 'reservation' => $LANG['log'][42],
  96. 'dropdown' => $LANG['log'][44],
  97. 'rules' => $LANG['log'][65]);
  98. $logService = array('inventory' => $LANG['Menu'][38],
  99. 'tracking' => $LANG['Menu'][5],
  100. 'planning' => $LANG['Menu'][29],
  101. 'tools' => $LANG['Menu'][18],
  102. 'financial' => $LANG['Menu'][26],
  103. 'login' => $LANG['log'][55],
  104. 'setup' => $LANG['common'][12],
  105. 'security' => $LANG['log'][66],
  106. 'reservation' => $LANG['log'][42],
  107. 'cron' => $LANG['log'][59],
  108. 'document' => $LANG['Menu'][27],
  109. 'notification' => $LANG['setup'][704],
  110. 'plugin' => $LANG['common'][29]);
  111. return array($logItemtype, $logService);
  112. }
  113. static function displayItemLogID($type, $items_id) {
  114. global $CFG_GLPI;
  115. if ($items_id=="-1" || $items_id=="0") {
  116. echo "&nbsp;";//$item;
  117. } else {
  118. switch ($type) {
  119. case "rules" :
  120. echo "<a href=\"".$CFG_GLPI["root_doc"]."/front/rule.generic.form.php?id=".
  121. $items_id."\">".$items_id."</a>";
  122. break;
  123. case "infocom" :
  124. echo "<a href='#' onClick=\"window.open('".$CFG_GLPI["root_doc"].
  125. "/front/infocom.form.php?id=$items_id','infocoms','location=infocoms,width=".
  126. "1000,height=400,scrollbars=no')\">$items_id</a>";
  127. break;
  128. case "devices" :
  129. echo $items_id;
  130. break;
  131. case "reservationitem" :
  132. echo "<a href=\"".$CFG_GLPI["root_doc"]."/front/reservation.php?reservationitems_id=".
  133. $items_id."\">$items_id</a>";
  134. break;
  135. default :
  136. $type=getSingular($type);
  137. $url='';
  138. if (class_exists($type)) {
  139. $item = new $type();
  140. $url = $item->getFormURL();
  141. }
  142. if (!empty($url)) {
  143. echo "<a href=\"$url?id=".$items_id;
  144. echo "\">$items_id</a>";
  145. } else {
  146. echo $items_id;
  147. }
  148. break;
  149. }
  150. }
  151. }
  152. /**
  153. * Print a nice tab for last event from inventory section
  154. *
  155. * Print a great tab to present lasts events occured on glpi
  156. *
  157. * @param $user string : name user to search on message
  158. **/
  159. static function showForUser($user="") {
  160. global $DB, $CFG_GLPI, $LANG;
  161. // Show events from $result in table form
  162. list($logItemtype, $logService) = self::logArray();
  163. // define default sorting
  164. $usersearch = "%";
  165. if (!empty($user)) {
  166. $usersearch = $user." ";
  167. }
  168. // Query Database
  169. $query = "SELECT *
  170. FROM `glpi_events`
  171. WHERE `message` LIKE '".$usersearch.addslashes($LANG['log'][20])."%'
  172. ORDER BY `date` DESC
  173. LIMIT 0,".intval($_SESSION['glpilist_limit']);
  174. // Get results
  175. $result = $DB->query($query);
  176. // Number of results
  177. $number = $DB->numrows($result);
  178. // No Events in database
  179. if ($number < 1) {
  180. echo "<br><table class='tab_cadrehov'>";
  181. echo "<tr><th>".$LANG['central'][4]."</th></tr>";
  182. echo "</table><br>";
  183. return;
  184. }
  185. // Output events
  186. $i = 0;
  187. echo "<br><table class='tab_cadrehov'>";
  188. echo "<tr><th colspan='5'>";
  189. echo "<a href=\"".$CFG_GLPI["root_doc"]."/front/event.php\">".$LANG['central'][2]." ".
  190. $_SESSION['glpilist_limit']." ".$LANG['central'][8]."</a>";
  191. echo "</th></tr>";
  192. echo "<tr><th colspan='2'>".$LANG['event'][0]."</th>";
  193. echo "<th>".$LANG['common'][27]."</th>";
  194. echo "<th width='8%'>".$LANG['event'][2]."</th>";
  195. echo "<th width='60%'>".$LANG['event'][4]."</th></tr>";
  196. while ($i < $number) {
  197. $ID = $DB->result($result, $i, "id");
  198. $items_id = $DB->result($result, $i, "items_id");
  199. $type = $DB->result($result, $i, "type");
  200. $date = $DB->result($result, $i, "date");
  201. $service = $DB->result($result, $i, "service");
  202. $message = $DB->result($result, $i, "message");
  203. $itemtype = "&nbsp;";
  204. if (isset($logItemtype[$type])) {
  205. $itemtype = $logItemtype[$type];
  206. } else {
  207. $type = getSingular($type);
  208. if (class_exists($type)) {
  209. $item = new $type();
  210. $itemtype = $item->getTypeName();
  211. }
  212. }
  213. echo "<tr class='tab_bg_2'><td>$itemtype :</td>";
  214. echo "<td class='center'>";
  215. self::displayItemLogID($type,$items_id);
  216. echo "</td><td class='center'>".convDateTime($date)."</td>";
  217. echo "<td class='center'>".$logService[$service]."</td><td>$message</td></tr>";
  218. $i++;
  219. }
  220. echo "</table><br>";
  221. }
  222. /**
  223. * Print a nice tab for last event
  224. *
  225. * Print a great tab to present lasts events occured on glpi
  226. *
  227. * @param $target where to go when complete
  228. * @param $order order by clause occurences (eg: )
  229. * @param $sort order by clause occurences (eg: date)
  230. * @param $start
  231. **/
  232. static function showList($target, $order='DESC', $sort='date', $start=0) {
  233. global $DB, $CFG_GLPI, $LANG;
  234. // Show events from $result in table form
  235. list($logItemtype, $logService) = self::logArray();
  236. // Columns of the Table
  237. $items = array("items_id" => array($LANG['event'][0],
  238. "colspan='2'"),
  239. "date" => array($LANG['common'][27], ""),
  240. "service" => array($LANG['event'][2],
  241. "width='8%'"),
  242. "level" => array($LANG['event'][3],
  243. "width='8%'"),
  244. "message" => array($LANG['event'][4],
  245. "width='50%'"));
  246. // define default sorting
  247. if (!isset($items[$sort])) {
  248. $sort = "date";
  249. }
  250. if ($order!="ASC") {
  251. $order = "DESC";
  252. }
  253. // Query Database
  254. $query_limit = "SELECT *
  255. FROM `glpi_events`
  256. ORDER BY `$sort` $order
  257. LIMIT ".intval($start).",".intval($_SESSION['glpilist_limit']);
  258. // Number of results
  259. $numrows = countElementsInTable("glpi_events");
  260. // Get results
  261. $result = $DB->query($query_limit);
  262. $number = $DB->numrows($result);
  263. // No Events in database
  264. if ($number < 1) {
  265. echo "<div class='center b'>".$LANG['central'][4]."</div>";
  266. return;
  267. }
  268. // Output events
  269. $i = 0;
  270. echo "<div class='center'>";
  271. $parameters = "sort=$sort&amp;order=$order";
  272. printPager($start, $numrows, $target, $parameters);
  273. echo "<table class='tab_cadre_fixe'>";
  274. echo "<tr>";
  275. foreach ($items as $field => $args) {
  276. echo "<th ".$args[1].">";
  277. if ($sort==$field) {
  278. if ($order=="DESC") {
  279. echo "<img src=\"".$CFG_GLPI["root_doc"]."/pics/puce-down.png\" alt='' title=''>";
  280. } else {
  281. echo "<img src=\"".$CFG_GLPI["root_doc"]."/pics/puce-up.png\" alt='' title=''>";
  282. }
  283. }
  284. echo "<a href='$target?sort=$field&amp;order=".($order=="ASC"?"DESC":"ASC")."'>".$args[0].
  285. "</a></th>";
  286. }
  287. echo "</tr>";
  288. while ($i < $number) {
  289. $ID = $DB->result($result, $i, "id");
  290. $items_id = $DB->result($result, $i, "items_id");
  291. $type = $DB->result($result, $i, "type");
  292. $date = $DB->result($result, $i, "date");
  293. $service = $DB->result($result, $i, "service");
  294. $level = $DB->result($result, $i, "level");
  295. $message = $DB->result($result, $i, "message");
  296. $itemtype = "&nbsp;";
  297. if (isset($logItemtype[$type])) {
  298. $itemtype = $logItemtype[$type];
  299. } else {
  300. $type = getSingular($type);
  301. if (class_exists($type)) {
  302. $item = new $type();
  303. $itemtype = $item->getTypeName();
  304. }
  305. }
  306. echo "<tr class='tab_bg_2'>";
  307. echo "<td>$itemtype :</td>";
  308. echo "<td class='center b'>";
  309. self::displayItemLogID($type, $items_id);
  310. echo "</td><td>".convDateTime($date)."</td>";
  311. echo "<td class='center'>".(isset($logService[$service])?$logService[$service]:$service);
  312. echo "</td><td class='center'>$level</td><td>$message</td></tr>";
  313. $i++;
  314. }
  315. echo "</table></div><br>";
  316. }
  317. }
  318. ?>