dolibarr 21.0.0-beta
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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
28include_once DOL_DOCUMENT_ROOT . '/core/boxes/modules_boxes.php';
29
30
35{
36 public $boxcode = "scheduledjobs";
37 public $boximg = "object_cron";
38 public $boxlabel = "BoxScheduledJobs";
39 public $depends = array("cron");
40
47 public function __construct($db, $param)
48 {
49 global $user;
50
51 $this->db = $db;
52
53 $this->hidden = !($user->hasRight('cron', 'read'));
54
55 $this->urltoaddentry = DOL_URL_ROOT.'/cron/card.php?action=create';
56 $this->msgNoRecords = 'NoScheduledJobs';
57 }
58
65 public function loadBox($max = 5)
66 {
67 global $user, $langs, $conf, $form;
68
69 $langs->load("cron");
70 $this->info_box_head = array('text' => $langs->trans("BoxScheduledJobs", $max), 'nbcol' => 4);
71
72 if ($user->hasRight('cron', 'read')) {
73 include_once DOL_DOCUMENT_ROOT . '/cron/class/cronjob.class.php';
74 $cronstatic = new Cronjob($this->db);
75 $resultarray = array();
76
77 $result = 0;
78 $sql = "SELECT t.rowid, t.datelastrun, t.datenextrun, t.datestart,";
79 $sql .= " t.label, t.status, t.test, t.lastresult, t.processing";
80 $sql .= " FROM " . MAIN_DB_PREFIX . "cronjob as t";
81 $sql .= " WHERE status <> ".$cronstatic::STATUS_DISABLED;
82 $sql .= " AND entity IN (0, ".$conf->entity.")";
83 $sql .= $this->db->order("t.datelastrun", "DESC");
84
85 $result = $this->db->query($sql);
86 $line = 0;
87 $nbjobsinerror = 0;
88 $nbjobsnotfinished = 0;
89 if ($result) {
90 $num = $this->db->num_rows($result);
91
92 $i = 0;
93 while ($i < $num) {
94 $objp = $this->db->fetch_object($result);
95
96 if ((int) dol_eval($objp->test, 1, 1, '2')) {
97 $nextrun = $this->db->jdate($objp->datenextrun);
98 if (empty($nextrun)) {
99 $nextrun = $this->db->jdate($objp->datestart);
100 }
101
102 if ($line == 0 || ($nextrun < $cronstatic->datenextrun && (empty($objp->nbrun) || empty($objp->maxrun) || $objp->nbrun < $objp->maxrun))) {
103 // Save in cronstatic the job if it is a job to run in future
104 $cronstatic->id = $objp->rowid;
105 $cronstatic->ref = $objp->rowid;
106 $cronstatic->label = $langs->trans($objp->label);
107 $cronstatic->status = $objp->status;
108 $cronstatic->processing = $objp->processing;
109 $cronstatic->lastresult = $objp->lastresult ?? '';
110 $cronstatic->datenextrun = $this->db->jdate($objp->datenextrun);
111 $cronstatic->datelastrun = $this->db->jdate($objp->datelastrun);
112 }
113 if ($line == 0) {
114 // Save the first line in loop that is the most recent executed job (due to the sort on datelastrun DESC)
115 $resultarray[$line] = array(
116 $langs->trans("LastExecutedScheduledJob"),
117 $cronstatic->getNomUrl(1),
118 $cronstatic->datelastrun,
119 $cronstatic->status,
120 $cronstatic->getLibStatut(3)
121 );
122 $line++;
123 }
124
125 if ($objp->processing && $this->db->jdate($objp->datelastrun) < (dol_now() - 3600 * 24)) {
126 $nbjobsnotfinished++;
127 }
128 if (!empty($objp->lastresult)) {
129 $nbjobsinerror++;
130 }
131 }
132 $i++;
133 }
134
135 if ($line) {
136 $resultarray[$line] = array(
137 $langs->trans("NextScheduledJobExecute"),
138 $cronstatic->getNomUrl(1),
139 $cronstatic->datenextrun,
140 $cronstatic->status,
141 $cronstatic->getLibStatut(3)
142 );
143 }
144
145 foreach ($resultarray as $line => $value) {
146 $this->info_box_contents[$line][] = array(
147 'td' => 'class="tdoverflowmax200"',
148 'text' => $resultarray[$line][0]
149 );
150
151 $this->info_box_contents[$line][] = array(
152 'td' => 'class="nowraponall"',
153 'textnoformat' => $resultarray[$line][1]
154 );
155 $this->info_box_contents[$line][] = array(
156 'td' => 'class="right"',
157 'textnoformat' => (empty($resultarray[$line][2]) ? '' : $form->textwithpicto(dol_print_date($resultarray[$line][2], "dayhoursec", 'tzserver'), $langs->trans("CurrentTimeZone")))
158 );
159 $this->info_box_contents[$line][] = array(
160 'td' => 'class="right" ',
161 'textnoformat' => $resultarray[$line][4]
162 );
163 $line++;
164 }
165 if ($num > 0) {
166 // Line nb job in error
167 $this->info_box_contents[$line][] = array(
168 'td' => 'class="tdoverflowmax300" colspan="3"',
169 'text' => $langs->trans("NumberScheduledJobError")
170 );
171 $textnoformat = '';
172 if ($nbjobsnotfinished) {
173 $textnoformat .= '<a class="inline-block paddingleft paddingright marginleftonly'.($nbjobsinerror ? ' 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>';
174 }
175 if ($nbjobsinerror) {
176 $textnoformat .= '<a class="inline-block paddingleft paddingright marginleftonly 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>';
177 }
178 if (empty($nbjobsnotfinished) && empty($nbjobsinerror)) {
179 $textnoformat .= '<a class="inline-block paddingleft marginleftonly minwidth25 nounderlineimp" href="'.DOL_URL_ROOT.'/cron/list.php"><div class="center badge badge-status4 nounderline">0</div></a>';
180 }
181 $this->info_box_contents[$line][] = array(
182 'td' => 'class="right"',
183 'textnoformat' => $textnoformat
184 );
185 }
186 } else {
187 $this->info_box_contents[0][0] = array(
188 'td' => '',
189 'maxlength' => 500,
190 'text' => ($this->db->lasterror() . ' sql=' . $sql)
191 );
192 }
193 } else {
194 $this->info_box_contents[0][0] = array(
195 'td' => 'class="nohover left"',
196 'text' => '<span class="opacitymedium">'.$langs->trans("ReadPermissionNotAllowed").'</span>'
197 );
198 }
199 }
200
201
202
211 public function showBox($head = null, $contents = null, $nooutput = 0)
212 {
213 return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
214 }
215}
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).
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79