box_scheduled_jobs.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2005-2017 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2011 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2017 Nicolas Zabouri <info@inovea-conseil.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU 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. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/core/boxes/box_scheduled_jobs.php
  22. * \ingroup task
  23. * \brief Widget of scheduled jobs
  24. */
  25. include_once DOL_DOCUMENT_ROOT . '/core/boxes/modules_boxes.php';
  26. /**
  27. * Class to manage the box to show last contracted products/services lines
  28. */
  29. class box_scheduled_jobs extends ModeleBoxes
  30. {
  31. public $boxcode = "scheduledjobs";
  32. public $boximg = "object_cron";
  33. public $boxlabel = "BoxScheduledJobs";
  34. public $depends = array("cron");
  35. /**
  36. * @var DoliDB Database handler.
  37. */
  38. public $db;
  39. /**
  40. * @var string params
  41. */
  42. public $param;
  43. public $info_box_head = array();
  44. public $info_box_contents = array();
  45. /**
  46. * Constructor
  47. *
  48. * @param DoliDB $db Database handler
  49. * @param string $param More parameters
  50. */
  51. public function __construct($db, $param)
  52. {
  53. global $user;
  54. $this->db = $db;
  55. $this->hidden = !($user->hasRight('service', 'lire') && $user->hasRight('contrat', 'lire'));
  56. }
  57. /**
  58. * Load data into info_box_contents array to show array later.
  59. *
  60. * @param int $max Maximum number of records to load
  61. * @return void
  62. */
  63. public function loadBox($max = 5)
  64. {
  65. global $user, $langs, $conf, $form;
  66. $langs->load("cron");
  67. $this->info_box_head = array('text' => $langs->trans("BoxScheduledJobs", $max));
  68. if ($user->rights->cron->read) {
  69. include_once DOL_DOCUMENT_ROOT . '/cron/class/cronjob.class.php';
  70. $cronstatic = new Cronjob($this->db);
  71. $resultarray = array();
  72. $result = 0;
  73. $sql = "SELECT t.rowid, t.datelastrun, t.datenextrun, t.datestart,";
  74. $sql .= " t.label, t.status, t.test, t.lastresult, t.processing";
  75. $sql .= " FROM " . MAIN_DB_PREFIX . "cronjob as t";
  76. $sql .= " WHERE status <> ".$cronstatic::STATUS_DISABLED;
  77. $sql .= " AND entity IN (0, ".$conf->entity.")";
  78. $sql .= $this->db->order("t.datelastrun", "DESC");
  79. $result = $this->db->query($sql);
  80. $line = 0;
  81. $nbjobsinerror = 0;
  82. $nbjobsnotfinished = 0;
  83. if ($result) {
  84. $num = $this->db->num_rows($result);
  85. $i = 0;
  86. while ($i < $num) {
  87. $objp = $this->db->fetch_object($result);
  88. if (dol_eval($objp->test, 1, 1, '')) {
  89. $nextrun = $this->db->jdate($objp->datenextrun);
  90. if (empty($nextrun)) {
  91. $nextrun = $this->db->jdate($objp->datestart);
  92. }
  93. if ($line == 0 || ($nextrun < $cronstatic->datenextrun && (empty($objp->nbrun) || empty($objp->maxrun) || $objp->nbrun < $objp->maxrun))) {
  94. // Save in cronstatic the job if it is a job to run in future
  95. $cronstatic->id = $objp->rowid;
  96. $cronstatic->ref = $objp->rowid;
  97. $cronstatic->label = $langs->trans($objp->label);
  98. $cronstatic->status = $objp->status;
  99. $cronstatic->datenextrun = $this->db->jdate($objp->datenextrun);
  100. $cronstatic->datelastrun = $this->db->jdate($objp->datelastrun);
  101. }
  102. if ($line == 0) {
  103. // Save the first line in loop that is the most recent executed job (due to the sort on datelastrun DESC)
  104. $resultarray[$line] = array(
  105. $langs->trans("LastExecutedScheduledJob"),
  106. $cronstatic->getNomUrl(1),
  107. $cronstatic->datelastrun,
  108. $cronstatic->status,
  109. $cronstatic->getLibStatut(3)
  110. );
  111. $line++;
  112. }
  113. if ($objp->processing && $this->db->jdate($objp->datelastrun) < (dol_now() - 3600 * 24)) {
  114. $nbjobsnotfinished++;
  115. }
  116. if (!empty($objp->lastresult)) {
  117. $nbjobsinerror++;
  118. }
  119. }
  120. $i++;
  121. }
  122. if ($line) {
  123. $resultarray[$line] = array(
  124. $langs->trans("NextScheduledJobExecute"),
  125. $cronstatic->getNomUrl(1),
  126. $cronstatic->datenextrun,
  127. $cronstatic->status,
  128. $cronstatic->getLibStatut(3)
  129. );
  130. }
  131. foreach ($resultarray as $line => $value) {
  132. $this->info_box_contents[$line][] = array(
  133. 'td' => 'class="tdoverflowmax200"',
  134. 'text' => $resultarray[$line][0]
  135. );
  136. $this->info_box_contents[$line][] = array(
  137. 'td' => 'class="nowraponall"',
  138. 'textnoformat' => $resultarray[$line][1]
  139. );
  140. $this->info_box_contents[$line][] = array(
  141. 'td' => 'class="right"',
  142. 'textnoformat' => (empty($resultarray[$line][2]) ? '' : $form->textwithpicto(dol_print_date($resultarray[$line][2], "dayhoursec", 'tzserver'), $langs->trans("CurrentTimeZone")))
  143. );
  144. $this->info_box_contents[$line][] = array(
  145. 'td' => 'class="center" ',
  146. 'textnoformat' => $resultarray[$line][4]
  147. );
  148. $line++;
  149. }
  150. $this->info_box_contents[$line][] = array(
  151. 'td' => 'class="tdoverflowmax300" colspan="3"',
  152. 'text' => $langs->trans("NumberScheduledJobError")
  153. );
  154. $textnoformat = '';
  155. if ($nbjobsnotfinished) {
  156. $textnoformat .= '<a class="inline-block paddingleft paddingright marginleftonly marginrightonly minwidth25 nounderlineimp" href="'.DOL_URL_ROOT.'/cron/list.php" title="'.$langs->trans("NumberScheduledJobNeverFinished").'"><div class="center badge badge-warning nounderlineimp"><i class="fa fa-exclamation-triangle"></i> '.$nbjobsnotfinished.'</div></a>';
  157. }
  158. if ($nbjobsinerror) {
  159. $textnoformat .= '<a class="inline-block paddingleft paddingright marginleftonly marginrightonly minwidth25 nounderlineimp" href="'.DOL_URL_ROOT.'/cron/list.php?search_lastresult='.urlencode('<>0').'" title="'.$langs->trans("NumberScheduledJobError").'"><div class="badge badge-danger nounderlineimp"><i class="fa fa-exclamation-triangle"></i> '.$nbjobsinerror.'</div></a>';
  160. }
  161. if (empty($nbjobsnotfinished) && empty($nbjobsinerror)) {
  162. $textnoformat .= '<a class="inline-block paddingleft paddingright marginleftonly marginrightonly minwidth25 nounderlineimp" href="'.DOL_URL_ROOT.'/cron/list.php"><div class="center badge badge-status4 nounderline">0</div></a>';
  163. }
  164. $this->info_box_contents[$line][] = array(
  165. 'td' => 'class="center"',
  166. 'textnoformat' => $textnoformat
  167. );
  168. } else {
  169. $this->info_box_contents[0][0] = array(
  170. 'td' => '',
  171. 'maxlength' => 500,
  172. 'text' => ($this->db->lasterror() . ' sql=' . $sql)
  173. );
  174. }
  175. } else {
  176. $this->info_box_contents[0][0] = array(
  177. 'td' => 'class="nohover opacitymedium left"',
  178. 'text' => $langs->trans("ReadPermissionNotAllowed")
  179. );
  180. }
  181. }
  182. /**
  183. * Method to show box
  184. *
  185. * @param array $head Array with properties of box title
  186. * @param array $contents Array with properties of box lines
  187. * @param int $nooutput No print, only return string
  188. * @return string
  189. */
  190. public function showBox($head = null, $contents = null, $nooutput = 0)
  191. {
  192. return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
  193. }
  194. }