PageRenderTime 38ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/inc/taskjoblog.class.php

https://github.com/matsimon/fusioninventory-for-glpi
PHP | 583 lines | 397 code | 80 blank | 106 comment | 18 complexity | 49beae56a0987dec9795e358f4454e90 MD5 | raw file
  1. <?php
  2. /*
  3. ----------------------------------------------------------------------
  4. FusionInventory
  5. Copyright (C) 2010-2011 by the FusionInventory Development Team.
  6. http://www.fusioninventory.org/ http://forge.fusioninventory.org/
  7. ----------------------------------------------------------------------
  8. LICENSE
  9. This file is part of FusionInventory.
  10. FusionInventory is free software: you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation, either version 2 of the License, or
  13. any later version.
  14. FusionInventory is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with FusionInventory. If not, see <http://www.gnu.org/licenses/>.
  20. ------------------------------------------------------------------------
  21. Original Author of file: David DURIEUX
  22. Co-authors of file:
  23. Purpose of file:
  24. ----------------------------------------------------------------------
  25. */
  26. class PluginFusioninventoryTaskjoblog extends CommonDBTM {
  27. /*
  28. * Define different state
  29. *
  30. *
  31. * 1 : started
  32. * 2 : ok
  33. * 3 : error / replaned
  34. * 4 : error
  35. * 5 : Unknown
  36. * 6 : Running
  37. * 7 : prepared
  38. *
  39. */
  40. const TASK_STARTED = 1;
  41. const TASK_OK = 2;
  42. const TASK_ERROR_OR_REPLANNED = 3;
  43. const TASK_ERROR = 4;
  44. const TASK_UNKNOWN = 5;
  45. const TASK_RUNNING = 6;
  46. const TASK_PREPARED = 7;
  47. /**
  48. * Display history of taskjob
  49. *
  50. * @param $taskjobs_id integer id of the taskjob
  51. * @param $width integer how large in pixel display array
  52. * @param $options array to display with specific options
  53. * - items_id integer id of item to display history
  54. * - itemtype value type of item to display
  55. *
  56. * @return bool true if form is ok
  57. *
  58. **/
  59. function showHistory($taskjobs_id, $width="950", $options=array()) {
  60. global $DB,$CFG_GLPI,$LANG;
  61. echo "<script type='text/javascript'>
  62. function close_array(id){
  63. document.getElementById('plusmoins'+id).innerHTML = '<img src=\'".GLPI_ROOT."/pics/collapse.gif\''+
  64. 'onClick=\'Effect.Fade(\"viewfollowup'+id+'\");appear_array('+id+');\' />';
  65. }
  66. function appear_array(id){
  67. document.getElementById('plusmoins'+id).innerHTML = '<img src=\'".GLPI_ROOT."/pics/expand.gif\''+
  68. 'onClick=\'Effect.Appear(\"viewfollowup'+id+'\");close_array('+id+');\' />';
  69. }
  70. </script>";
  71. echo "<script type='text/javascript' src='".GLPI_ROOT."/plugins/fusioninventory/prototype.js'></script>";
  72. echo "<script type='text/javascript' src='".GLPI_ROOT."/plugins/fusioninventory/effects.js'></script>";
  73. $start = 0;
  74. if (isset($_REQUEST["start"])) {
  75. $start = $_REQUEST["start"];
  76. }
  77. $where = '';
  78. if (isset($options['items_id']) AND isset($options['itemtype'])) {
  79. $where = " AND `items_id`='".$options['items_id']."'
  80. AND `itemtype`='".$options['itemtype']."' ";
  81. }
  82. echo "<center><table class='tab_cadrehov' style='width: ".$width."px'>";
  83. echo "<tr>";
  84. echo "<th colspan='8'>".$LANG['title'][38]."</th>";
  85. echo "</tr>";
  86. $query = 'SELECT * FROM `glpi_plugin_fusioninventory_taskjobstatus`
  87. WHERE `plugin_fusioninventory_taskjobs_id`="'.$taskjobs_id.'"
  88. AND `state`!="3"
  89. '.$where.'
  90. GROUP BY uniqid,plugin_fusioninventory_agents_id
  91. ORDER BY `id` DESC';
  92. $result = $DB->query($query);
  93. // ***** Display for all status running / prepared
  94. echo "<tr>";
  95. echo "<th colspan='2'>";
  96. echo "<img src='".GLPI_ROOT."/plugins/fusioninventory/pics/task_running.png'/>";
  97. echo "</th>";
  98. echo "<th colspan='6'>";
  99. echo $LANG['plugin_fusioninventory']['task'][19]."&nbsp;:";
  100. echo "</th>";
  101. echo "</tr>";
  102. echo "<tr>";
  103. echo "<th></th>";
  104. echo "<th>Uniqid</th>";
  105. echo "<th>".$LANG['plugin_fusioninventory']['processes'][38]."</th>";
  106. echo "<th>".$LANG['plugin_fusioninventory']['agents'][28]."</th>";
  107. echo "<th>";
  108. echo $LANG['common'][27];
  109. echo "</th>";
  110. echo "<th>";
  111. echo $LANG['joblist'][0];
  112. echo "</th>";
  113. echo "<th>";
  114. echo $LANG['common'][25];
  115. echo "</th>";
  116. echo "<th></th>";
  117. echo "</tr>";
  118. while ($data=$DB->fetch_array($result)) {
  119. $this->showHistoryLines($data['id']);
  120. }
  121. // ***** Display for statusjob OK
  122. echo "<tr>";
  123. echo "<th colspan='2'>";
  124. echo "<img src='".GLPI_ROOT."/plugins/fusioninventory/pics/task_finished.png'/>";
  125. echo "</th>";
  126. echo "<th colspan='6'>";
  127. echo $LANG['plugin_fusioninventory']['task'][20]."&nbsp;:";
  128. echo "</th>";
  129. echo "</tr>";
  130. echo "<tr>";
  131. echo "<th colspan='8'>";
  132. $querycount = 'SELECT count(*) AS cpt FROM `glpi_plugin_fusioninventory_taskjobstatus`
  133. WHERE `plugin_fusioninventory_taskjobs_id`="'.$taskjobs_id.'"
  134. AND `state`="3"
  135. '.$where.'
  136. GROUP BY uniqid,plugin_fusioninventory_agents_id';
  137. $resultcount = $DB->query($querycount);
  138. $number = $DB->numrows($resultcount);
  139. printAjaxPager('',$start,$number);
  140. echo "</th>";
  141. echo "</tr>";
  142. $query = str_replace('`state`!="3"', '`state`="3"', $query);
  143. $query .= ' LIMIT '.intval($start).','.intval($_SESSION['glpilist_limit']);
  144. $result = $DB->query($query);
  145. echo "<tr>";
  146. echo "<th></th>";
  147. echo "<th>Uniqid</th>";
  148. echo "<th>".$LANG['plugin_fusioninventory']['processes'][38]."</th>";
  149. echo "<th>".$LANG['plugin_fusioninventory']['agents'][28]."</th>";
  150. echo "<th>";
  151. echo $LANG['common'][27];
  152. echo "</th>";
  153. echo "<th>";
  154. echo $LANG['joblist'][0];
  155. echo "</th>";
  156. echo "<th>";
  157. echo $LANG['common'][25];
  158. echo "</th>";
  159. echo "<th></th>";
  160. echo "</tr>";
  161. while ($data=$DB->fetch_array($result)) {
  162. $this->showHistoryLines($data['id']);
  163. }
  164. echo "<tr>";
  165. echo "<th colspan='8'>";
  166. printAjaxPager('',$start,$number);
  167. echo "</th>";
  168. echo "</tr>";
  169. echo "</table></center>";
  170. return true;
  171. }
  172. /**
  173. * Display each history line
  174. *
  175. * @param $taskjobstatus_id integer id of the taskjobstatus
  176. *
  177. * @return nothing
  178. *
  179. **/
  180. function showHistoryLines($taskjobstatus_id) {
  181. global $LANG;
  182. $PluginFusioninventoryTaskjobstatus = new PluginFusioninventoryTaskjobstatus();
  183. $PluginFusioninventoryTaskjobstatus->getFromDB($taskjobstatus_id);
  184. $PluginFusioninventoryAgent = new PluginFusioninventoryAgent();
  185. $displayforceend = 0;
  186. $a_history = $this->find('`plugin_fusioninventory_taskjobstatus_id` = "'.$PluginFusioninventoryTaskjobstatus->fields['id'].'"', 'id');
  187. echo "<tr class='tab_bg_1'>";
  188. echo "<td id='plusmoins".$PluginFusioninventoryTaskjobstatus->fields["id"]."'><img src='".GLPI_ROOT.
  189. "/pics/expand.gif' onClick='Effect.Appear(\"viewfollowup".$PluginFusioninventoryTaskjobstatus->fields["id"].
  190. "\");close_array(".$PluginFusioninventoryTaskjobstatus->fields["id"].");' /></td>";
  191. echo "<td>";
  192. echo $PluginFusioninventoryTaskjobstatus->fields['uniqid'];
  193. echo "</td>";
  194. echo "<td>";
  195. echo $PluginFusioninventoryTaskjobstatus->fields['id'];
  196. echo "</td>";
  197. echo "<td>";
  198. $PluginFusioninventoryAgent->getFromDB($PluginFusioninventoryTaskjobstatus->fields['plugin_fusioninventory_agents_id']);
  199. echo $PluginFusioninventoryAgent->getLink(1);
  200. echo "</td>";
  201. $a_return = $this->displayHistoryDetail(array_pop($a_history));
  202. $count = $a_return[0];
  203. $displayforceend += $count;
  204. echo $a_return[1];
  205. echo "<td align='center'>";
  206. if ($displayforceend == "0") {
  207. echo "<form name='form' method='post' action='".GLPI_ROOT."/plugins/fusioninventory/front/taskjob.form.php'>";
  208. echo "<input type='hidden' name='taskjobstatus_id' value='".$PluginFusioninventoryTaskjobstatus->fields['id']."' />";
  209. echo "<input type='hidden' name='taskjobs_id' value='".$PluginFusioninventoryTaskjobstatus->fields['plugin_fusioninventory_taskjobs_id']."' />";
  210. echo '<input name="forceend" value="'.$LANG['plugin_fusioninventory']['task'][32].'"
  211. class="submit" type="submit">';
  212. echo "</form>";
  213. }
  214. echo "</td>";
  215. echo "</tr>";
  216. echo "<tr style='display: none;' id='viewfollowup".$PluginFusioninventoryTaskjobstatus->fields["id"]."' class='tab_bg_4'>
  217. <td colspan='8'>".$this->showHistoryInDetail($PluginFusioninventoryTaskjobstatus->fields['plugin_fusioninventory_agents_id'], $PluginFusioninventoryTaskjobstatus->fields['uniqid'], "900")."</td>
  218. </tr>";
  219. }
  220. /**
  221. * Display detail of each history line
  222. *
  223. * @param $agents_id integer id of the agent
  224. * @param $uniqid integer uniq id of each taskjobs runing
  225. * @param $width integer how large in pixel display array
  226. *
  227. * @return value all text to display
  228. *
  229. **/
  230. function showHistoryInDetail($agents_id, $uniqid, $width="950") {
  231. global $DB,$CFG_GLPI,$LANG;
  232. $PluginFusioninventoryTaskjobstatus = new PluginFusioninventoryTaskjobstatus();
  233. $PluginFusioninventoryAgent = new PluginFusioninventoryAgent();
  234. $text = "<center><table class='tab_cadrehov' style='width: ".$width."px'>";
  235. $a_jobstatus = $PluginFusioninventoryTaskjobstatus->find('`plugin_fusioninventory_agents_id`="'.$agents_id.'" AND `uniqid`="'.$uniqid.'"', '`id` DESC');
  236. $a_devices_merged = array();
  237. foreach ($a_jobstatus as $data) {
  238. $displayforceend = 0;
  239. $a_history = $this->find('`plugin_fusioninventory_taskjobstatus_id` = "'.$data['id'].'"', 'id');
  240. if (strstr(exportArrayToDB($a_history), "Merged with ")) {
  241. $classname = $data['itemtype'];
  242. $Class = new $classname;
  243. $Class->getFromDB($data['items_id']);
  244. $a_devices_merged[] = $Class->getLink(1)."&nbsp;(".$Class->getTypeName().")";
  245. } else {
  246. $text .= "<tr>";
  247. $text .= "<th colspan='2'><img src='".GLPI_ROOT."/pics/puce.gif' />".$LANG['plugin_fusioninventory']['processes'][38]."&nbsp;: ".$data['id']."</th>";
  248. $text .= "<th>";
  249. $text .= $LANG['common'][27];
  250. $text .= "</th>";
  251. $text .= "<th>";
  252. $text .= $LANG['joblist'][0];
  253. $text .= "</th>";
  254. $text .= "<th>";
  255. $text .= $LANG['common'][25];
  256. $text .= "</th>";
  257. $text .= "</tr>";
  258. $text .= "<tr class='tab_bg_1'>";
  259. $text .= "<th colspan='2'>";
  260. $text .= $LANG['plugin_fusioninventory']['agents'][28];
  261. $text .= "</th>";
  262. $a_return = $this->displayHistoryDetail(array_shift($a_history));
  263. $count = $a_return[0];
  264. $text .= $a_return[1];
  265. $displayforceend += $count;
  266. $text .= "</tr>";
  267. $text .= "<tr class='tab_bg_1'>";
  268. $text .= "<td colspan='2'>";
  269. $PluginFusioninventoryAgent->getFromDB($data['plugin_fusioninventory_agents_id']);
  270. $text .= $PluginFusioninventoryAgent->getLink(1);
  271. $text .= "</td>";
  272. $a_return = $this->displayHistoryDetail(array_shift($a_history));
  273. $count = $a_return[0];
  274. $text .= $a_return[1];
  275. $displayforceend += $count;
  276. $text .= "</tr>";
  277. $text .= "<tr class='tab_bg_1'>";
  278. $text .= "<th colspan='2'>";
  279. $text .= $LANG['plugin_fusioninventory']['task'][27];
  280. $text .= "</th>";
  281. $a_return = $this->displayHistoryDetail(array_shift($a_history));
  282. $count = $a_return[0];
  283. $text .= $a_return[1];
  284. $displayforceend += $count;
  285. $text .= "</tr>";
  286. $text .= "<tr class='tab_bg_1'>";
  287. $text .= "<td colspan='2'>";
  288. if (!empty($data["itemtype"])) {
  289. $device = new $data["itemtype"]();
  290. $device->getFromDB($data["items_id"]);
  291. $text .= $device->getLink(1);
  292. $text .= "&nbsp;";
  293. $text .= "(".$device->getTypeName().")";
  294. }
  295. $text .= "</td>";
  296. $a_return = $this->displayHistoryDetail(array_shift($a_history));
  297. $count = $a_return[0];
  298. $text .= $a_return[1];
  299. $displayforceend += $count;
  300. $text .= "</tr>";
  301. while (count($a_history) != 0) {
  302. if (count($a_devices_merged) > 0) {
  303. $text .= "<tr class='tab_bg_1'>";
  304. $text .= "<th></th>";
  305. $text .= "<td>";
  306. $text .= array_pop($a_devices_merged);
  307. $text .= "</td>";
  308. $a_return = $this->displayHistoryDetail(array_shift($a_history));
  309. $count = $a_return[0];
  310. $text .= $a_return[1];
  311. $displayforceend += $count;
  312. $text .= "</tr>";
  313. } else {
  314. $text .= "<tr class='tab_bg_1'>";
  315. $text .= "<td colspan='2' rowspan='".count($a_history)."'>";
  316. $text .= "</td>";
  317. $a_return = $this->displayHistoryDetail(array_shift($a_history));
  318. $count = $a_return[0];
  319. $text .= $a_return[1];
  320. $displayforceend += $count;
  321. $text .= "</tr>";
  322. foreach ($a_history as $datas) {
  323. $text .= "<tr class='tab_bg_1'>";
  324. $a_return = $this->displayHistoryDetail(array_shift($a_history));
  325. $count = $a_return[0];
  326. $text .= $a_return[1];
  327. $displayforceend += $count;
  328. $text .= "</tr>";
  329. }
  330. }
  331. }
  332. $display = 1;
  333. while (count($a_devices_merged) != 0) {
  334. $text .= "<tr class='tab_bg_1'>";
  335. $text .= "<th></th>";
  336. $text .= "<th>";
  337. $text .= array_pop($a_devices_merged);
  338. $text .= "</th>";
  339. if ($display == "1") {
  340. $text .= "<td colspan='3' rowspan='".(count($a_devices_merged) + 1)."'></td>";
  341. $display = 0;
  342. }
  343. $text .= "</tr>";
  344. }
  345. $text .= "<tr class='tab_bg_4'>";
  346. $text .= "<td colspan='5' height='4'>";
  347. $text .= "</td>";
  348. $text .= "</tr>";
  349. }
  350. }
  351. $text .= "</table></center>";
  352. return $text;
  353. }
  354. /**
  355. * Display high detail of each history line
  356. *
  357. * @param $datas array datas of history
  358. *
  359. * @return value all text to display
  360. *
  361. **/
  362. function displayHistoryDetail($datas) {
  363. global $LANG;
  364. $text = "<td align='center'>";
  365. $text .= convDateTime($datas['date']);
  366. $text .= "</td>";
  367. $finish = 0;
  368. switch ($datas['state']) {
  369. case 7 :
  370. $text .= "<td align='center'>";
  371. $text .= $LANG['plugin_fusioninventory']['taskjoblog'][7];
  372. break;
  373. case 1 :
  374. $text .= "<td align='center'>";
  375. $text .= $LANG['plugin_fusioninventory']['taskjoblog'][1];
  376. break;
  377. case 2 :
  378. $text .= "<td style='background-color: rgb(0, 255, 0);' align='center'>";
  379. $text .= $LANG['plugin_fusioninventory']['taskjoblog'][2];
  380. $finish++;
  381. break;
  382. case 3 :
  383. $text .= "<td style='background-color: rgb(255, 120, 0);' align='center'>";
  384. $text .= "<strong>".$LANG['plugin_fusioninventory']['taskjoblog'][3]."</strong>";
  385. $finish++;
  386. break;
  387. case 4 :
  388. $text .= "<td style='background-color: rgb(255, 0, 0);' align='center'>";
  389. $text .= "<strong>".$LANG['plugin_fusioninventory']['taskjoblog'][4]."</strong>";
  390. $finish++;
  391. break;
  392. case 5 :
  393. $text .= "<td style='background-color: rgb(255, 200, 0);' align='center'>";
  394. $text .= "<strong>".$LANG['plugin_fusioninventory']['taskjoblog'][5]."</strong>";
  395. $finish++;
  396. break;
  397. case 6 :
  398. $text .= "<td style='background-color: rgb(255, 200, 0);' align='center'>";
  399. $text .= "<strong>".$LANG['plugin_fusioninventory']['taskjoblog'][6]."</strong>";
  400. break;
  401. default:
  402. $text .= "<td>";
  403. break;
  404. }
  405. $text .= "</td>";
  406. $text .= "<td align='center'>";
  407. $matches = array();
  408. // Search for replace [[itemtype::items_id]] by link
  409. preg_match_all("/\[\[(.*)\:\:(.*)\]\]/", $datas['comment'], $matches);
  410. foreach($matches[0] as $num=>$commentvalue) {
  411. $classname = $matches[1][$num];
  412. $Class = new $classname;
  413. $Class->getFromDB($matches[2][$num]);
  414. $datas['comment'] = str_replace($commentvalue, $Class->getLink(), $datas['comment']);
  415. }
  416. // Search for code to display lang traduction ==pluginname::9876==
  417. preg_match_all("/==(.*)\:\:([0-9]*)==/", $datas['comment'], $matches);
  418. foreach($matches[0] as $num=>$commentvalue) {
  419. $datas['comment'] = str_replace($commentvalue, $LANG['plugin_'.$matches[1][$num]]["codetasklog"][$matches[2][$num]], $datas['comment']);
  420. }
  421. $text .= $datas['comment'];
  422. $text .= "</td>";
  423. return array($finish, $text);
  424. }
  425. /**
  426. * Add a new line of log for a taskjob status
  427. *
  428. * @param $taskjobs_id integer id of the taskjob
  429. * @param $items_id integer id of the item associated with taskjob status
  430. * @param $itemtype value type name of the item associated with taskjob status
  431. * @param $state value state of this taskjobstatus
  432. * @param $comment value the comment of this insertion
  433. *
  434. * @return value all text to display
  435. *
  436. **/
  437. function addTaskjoblog($taskjobs_id, $items_id, $itemtype, $state, $comment) {
  438. $this->getEmpty();
  439. unset($this->fields['id']);
  440. $this->fields['plugin_fusioninventory_taskjobstatus_id'] = $taskjobs_id;
  441. $this->fields['date'] = date("Y-m-d H:i:s");
  442. $this->fields['items_id'] = $items_id;
  443. $this->fields['itemtype'] = $itemtype;
  444. $this->fields['state'] = $state;
  445. $this->fields['comment'] = $comment;
  446. $this->addToDB();
  447. }
  448. /**
  449. * Display the graph of finished tasks
  450. *
  451. * @param $taskjobs_id integer id of the taskjob
  452. *
  453. * @return nothing
  454. *
  455. **/
  456. function graphFinish($taskjobs_id) {
  457. global $LANG;
  458. $PluginFusioninventoryTaskjobstatus = new PluginFusioninventoryTaskjobstatus();
  459. $finishState = array();
  460. $finishState[2] = 0;
  461. $finishState[3] = 0;
  462. $finishState[4] = 0;
  463. $finishState[5] = 0;
  464. $a_jobstatus = $PluginFusioninventoryTaskjobstatus->find('`plugin_fusioninventory_taskjobs_id`="'.$taskjobs_id.'" GROUP BY uniqid,plugin_fusioninventory_agents_id');
  465. $search = '(';
  466. foreach ($a_jobstatus as $data) {
  467. $search .= $data['id'].",";
  468. }
  469. $search .= ')';
  470. $search = str_replace(',)', ')', $search);
  471. $a_joblogs = $this->find('`plugin_fusioninventory_taskjobstatus_id` IN '.$search.' AND `state` IN (2, 3, 4, 5)');
  472. foreach($a_joblogs as $datajob) {
  473. $finishState[$datajob['state']]++;
  474. }
  475. $input = array();
  476. $input[$LANG['plugin_fusioninventory']['taskjoblog'][2]] = $finishState[2];
  477. $input[$LANG['plugin_fusioninventory']['taskjoblog'][3]] = $finishState[3];
  478. $input[$LANG['plugin_fusioninventory']['taskjoblog'][4]] = $finishState[4];
  479. $input[$LANG['plugin_fusioninventory']['taskjoblog'][5]] = $finishState[5];
  480. Stat::showGraph(array('status'=>$input),
  481. array('title' => '',
  482. 'unit' => '',
  483. 'type' => 'pie',
  484. 'height' => 150,
  485. 'showtotal' => false));
  486. }
  487. static function getByUniqID($uuid) {
  488. $results = getAllDatasFromTable('glpi_plugin_fusioninventory_taskjobstatus',
  489. "`uniqid`='$uuid'");
  490. foreach ($results as $result) {
  491. return $result;
  492. }
  493. return array();
  494. }
  495. }
  496. ?>