dolibarr 20.0.2
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), 'nbcol' => 4);
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
162 // Line nb job in error
163 $this->info_box_contents[$line][] = array(
164 'td' => 'class="tdoverflowmax300" colspan="3"',
165 'text' => $langs->trans("NumberScheduledJobError")
166 );
167 $textnoformat = '';
168 if ($nbjobsnotfinished) {
169 $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>';
170 }
171 if ($nbjobsinerror) {
172 $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>';
173 }
174 if (empty($nbjobsnotfinished) && empty($nbjobsinerror)) {
175 $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>';
176 }
177 $this->info_box_contents[$line][] = array(
178 'td' => 'class="center"',
179 'textnoformat' => $textnoformat
180 );
181 } else {
182 $this->info_box_contents[0][0] = array(
183 'td' => '',
184 'maxlength' => 500,
185 'text' => ($this->db->lasterror() . ' sql=' . $sql)
186 );
187 }
188 } else {
189 $this->info_box_contents[0][0] = array(
190 'td' => 'class="nohover left"',
191 'text' => '<span class="opacitymedium">'.$langs->trans("ReadPermissionNotAllowed").'</span>'
192 );
193 }
194 }
195
204 public function showBox($head = null, $contents = null, $nooutput = 0)
205 {
206 return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
207 }
208}
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).