dolibarr 21.0.0-alpha
box_scheduled_jobs.php
Go to the documentation of this file.
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
27include_once DOL_DOCUMENT_ROOT . '/core/boxes/modules_boxes.php';
28
29
34{
35 public $boxcode = "scheduledjobs";
36 public $boximg = "object_cron";
37 public $boxlabel = "BoxScheduledJobs";
38 public $depends = array("cron");
39
46 public function __construct($db, $param)
47 {
48 global $user;
49
50 $this->db = $db;
51
52 $this->hidden = !($user->hasRight('cron', 'read'));
53 }
54
61 public function loadBox($max = 5)
62 {
63 global $user, $langs, $conf, $form;
64
65 $langs->load("cron");
66 $this->info_box_head = array('text' => $langs->trans("BoxScheduledJobs", $max));
67
68 if ($user->hasRight('cron', 'read')) {
69 include_once DOL_DOCUMENT_ROOT . '/cron/class/cronjob.class.php';
70 $cronstatic = new Cronjob($this->db);
71 $resultarray = array();
72
73 $result = 0;
74 $sql = "SELECT t.rowid, t.datelastrun, t.datenextrun, t.datestart,";
75 $sql .= " t.label, t.status, t.test, t.lastresult, t.processing";
76 $sql .= " FROM " . MAIN_DB_PREFIX . "cronjob as t";
77 $sql .= " WHERE status <> ".$cronstatic::STATUS_DISABLED;
78 $sql .= " AND entity IN (0, ".$conf->entity.")";
79 $sql .= $this->db->order("t.datelastrun", "DESC");
80
81 $result = $this->db->query($sql);
82 $line = 0;
83 $nbjobsinerror = 0;
84 $nbjobsnotfinished = 0;
85 if ($result) {
86 $num = $this->db->num_rows($result);
87
88 $i = 0;
89 while ($i < $num) {
90 $objp = $this->db->fetch_object($result);
91
92 if ((int) dol_eval($objp->test, 1, 1, '2')) {
93 $nextrun = $this->db->jdate($objp->datenextrun);
94 if (empty($nextrun)) {
95 $nextrun = $this->db->jdate($objp->datestart);
96 }
97
98 if ($line == 0 || ($nextrun < $cronstatic->datenextrun && (empty($objp->nbrun) || empty($objp->maxrun) || $objp->nbrun < $objp->maxrun))) {
99 // Save in cronstatic the job if it is a job to run in future
100 $cronstatic->id = $objp->rowid;
101 $cronstatic->ref = $objp->rowid;
102 $cronstatic->label = $langs->trans($objp->label);
103 $cronstatic->status = $objp->status;
104 $cronstatic->processing = $objp->processing;
105 $cronstatic->lastresult = $objp->lastresult ?? '';
106 $cronstatic->datenextrun = $this->db->jdate($objp->datenextrun);
107 $cronstatic->datelastrun = $this->db->jdate($objp->datelastrun);
108 }
109 if ($line == 0) {
110 // Save the first line in loop that is the most recent executed job (due to the sort on datelastrun DESC)
111 $resultarray[$line] = array(
112 $langs->trans("LastExecutedScheduledJob"),
113 $cronstatic->getNomUrl(1),
114 $cronstatic->datelastrun,
115 $cronstatic->status,
116 $cronstatic->getLibStatut(3)
117 );
118 $line++;
119 }
120
121 if ($objp->processing && $this->db->jdate($objp->datelastrun) < (dol_now() - 3600 * 24)) {
122 $nbjobsnotfinished++;
123 }
124 if (!empty($objp->lastresult)) {
125 $nbjobsinerror++;
126 }
127 }
128 $i++;
129 }
130
131 if ($line) {
132 $resultarray[$line] = array(
133 $langs->trans("NextScheduledJobExecute"),
134 $cronstatic->getNomUrl(1),
135 $cronstatic->datenextrun,
136 $cronstatic->status,
137 $cronstatic->getLibStatut(3)
138 );
139 }
140
141 foreach ($resultarray as $line => $value) {
142 $this->info_box_contents[$line][] = array(
143 'td' => 'class="tdoverflowmax200"',
144 'text' => $resultarray[$line][0]
145 );
146
147 $this->info_box_contents[$line][] = array(
148 'td' => 'class="nowraponall"',
149 'textnoformat' => $resultarray[$line][1]
150 );
151 $this->info_box_contents[$line][] = array(
152 'td' => 'class="right"',
153 'textnoformat' => (empty($resultarray[$line][2]) ? '' : $form->textwithpicto(dol_print_date($resultarray[$line][2], "dayhoursec", 'tzserver'), $langs->trans("CurrentTimeZone")))
154 );
155 $this->info_box_contents[$line][] = array(
156 'td' => 'class="center" ',
157 'textnoformat' => $resultarray[$line][4]
158 );
159 $line++;
160 }
161 $this->info_box_contents[$line][] = array(
162 'td' => 'class="tdoverflowmax300" colspan="3"',
163 'text' => $langs->trans("NumberScheduledJobError")
164 );
165 $textnoformat = '';
166 if ($nbjobsnotfinished) {
167 $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>';
168 }
169 if ($nbjobsinerror) {
170 $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>';
171 }
172 if (empty($nbjobsnotfinished) && empty($nbjobsinerror)) {
173 $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>';
174 }
175 $this->info_box_contents[$line][] = array(
176 'td' => 'class="center"',
177 'textnoformat' => $textnoformat
178 );
179 } else {
180 $this->info_box_contents[0][0] = array(
181 'td' => '',
182 'maxlength' => 500,
183 'text' => ($this->db->lasterror() . ' sql=' . $sql)
184 );
185 }
186 } else {
187 $this->info_box_contents[0][0] = array(
188 'td' => 'class="nohover left"',
189 'text' => '<span class="opacitymedium">'.$langs->trans("ReadPermissionNotAllowed").'</span>'
190 );
191 }
192 }
193
202 public function showBox($head = null, $contents = null, $nooutput = 0)
203 {
204 return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
205 }
206}
Cron Job class.
Class ModeleBoxes.
Class to manage the box to show last contracted products/services lines.
loadBox($max=5)
Load data into info_box_contents array to show array later.
__construct($db, $param)
Constructor.
showBox($head=null, $contents=null, $nooutput=0)
Method to show box.
dol_eval($s, $returnvalue=1, $hideerrors=1, $onlysimplestring='1')
Replace eval function to add more security.
dol_now($mode='auto')
Return date for now.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).