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