dolibarr  13.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 
27 include_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 
45  public $param;
46 
47  public $info_box_head = array();
48  public $info_box_contents = array();
49 
50 
57  public function __construct($db, $param)
58  {
59  global $user;
60 
61  $this->db = $db;
62 
63  $this->hidden = !($user->rights->service->lire && $user->rights->contrat->lire);
64  }
65 
72  public function loadBox($max = 5)
73  {
74  global $user, $langs, $conf, $form;
75 
76  $langs->load("cron");
77  $this->info_box_head = array('text' => $langs->trans("BoxScheduledJobs", $max));
78 
79  if ($user->rights->cron->read) {
80  include_once DOL_DOCUMENT_ROOT . '/cron/class/cronjob.class.php';
81  $cronstatic = new Cronjob($this->db);
82  $resultarray = array();
83 
84  $result = 0;
85  $sql = "SELECT t.rowid, t.datelastrun, t.datenextrun,";
86  $sql .= " t.label, t.status, t.test, t.lastresult";
87  $sql .= " FROM " . MAIN_DB_PREFIX . "cronjob as t";
88  $sql .= " WHERE status <> ".$cronstatic::STATUS_DISABLED;
89  $sql .= " AND entity IN (0, ".$conf->entity.")";
90  $sql .= $this->db->order("t.datelastrun", "DESC");
91 
92  $result = $this->db->query($sql);
93  $line = 0;
94  $nbjobsinerror = 0;
95  if ($result) {
96  $num = $this->db->num_rows($result);
97 
98  $i = 0;
99  while ($i < $num) {
100  $objp = $this->db->fetch_object($result);
101 
102  if (dol_eval($objp->test, 1, 1)) {
103  $nextrun = $this->db->jdate($objp->datenextrun);
104  if (empty($nextrun)) $nextrun = $this->db->jdate($objp->datestart);
105 
106  if ($line == 0 || ($nextrun < $cronstatic->datenextrun && (empty($objp->nbrun) || empty($objp->maxrun) || $objp->nbrun < $objp->maxrun))) {
107  $cronstatic->id = $objp->rowid;
108  $cronstatic->ref = $objp->rowid;
109  $cronstatic->label = $langs->trans($objp->label);
110  $cronstatic->status = $objp->status;
111  $cronstatic->datenextrun = $this->db->jdate($objp->datenextrun);
112  $cronstatic->datelastrun = $this->db->jdate($objp->datelastrun);
113  }
114  if ($line == 0) {
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 (!empty($objp->lastresult)) {
126  $nbjobsinerror++;
127  }
128  }
129  $i++;
130  }
131 
132  if ($line) {
133  $resultarray[$line] = array(
134  $langs->trans("NextScheduledJobExecute"),
135  $cronstatic->getNomUrl(1),
136  $cronstatic->datenextrun,
137  $cronstatic->status,
138  $cronstatic->getLibStatut(3)
139  );
140  }
141 
142  foreach ($resultarray as $line => $value) {
143  $this->info_box_contents[$line][] = array(
144  'td' => 'class="tdoverflowmax200"',
145  'text' => $resultarray[$line][0]
146  );
147 
148  $this->info_box_contents[$line][] = array(
149  'td' => 'class="nowraponall"',
150  'textnoformat' => $resultarray[$line][1]
151  );
152  $this->info_box_contents[$line][] = array(
153  'td' => 'class="right"',
154  'textnoformat' => (empty($resultarray[$line][2]) ? '' : $form->textwithpicto(dol_print_date($resultarray[$line][2], "dayhoursec"), $langs->trans("CurrentTimeZone")))
155  );
156  $this->info_box_contents[$line][] = array(
157  'td' => 'class="center" ',
158  'textnoformat' => $resultarray[$line][4]
159  );
160  $line++;
161  }
162  $this->info_box_contents[$line][] = array(
163  'td' => 'class="tdoverflowmax300" colspan="3"',
164  'text' => $langs->trans("NumberScheduledJobError")
165  );
166  $this->info_box_contents[$line][] = array(
167  'td' => 'class="center"',
168  'textnoformat' => ($nbjobsinerror ? '<a href="'.DOL_URL_ROOT.'/cron/list.php?search_lastresult='.urlencode('<>0').'"><div class="badge badge-danger"><i class="fa fa-exclamation-triangle"></i> '.$nbjobsinerror.'</div></a>' : '<div class="center badge-status4">0</div>')
169  );
170  } else {
171  $this->info_box_contents[0][0] = array(
172  'td' => '',
173  'maxlength' => 500,
174  'text' => ($this->db->lasterror() . ' sql=' . $sql)
175  );
176  }
177  } else {
178  $this->info_box_contents[0][0] = array(
179  'td' => 'class="nohover opacitymedium left"',
180  'text' => $langs->trans("ReadPermissionNotAllowed")
181  );
182  }
183  }
184 
193  public function showBox($head = null, $contents = null, $nooutput = 0)
194  {
195  return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
196  }
197 }
dol_eval($s, $returnvalue=0, $hideerrors=1)
Replace eval function to add more security.
__construct($db, $param)
Constructor.
Class ModeleBoxes.
showBox($head=null, $contents=null, $nooutput=0)
Method to show box.
$conf db
API class for accounts.
Definition: inc.php:54
Class to manage the box to show last contracted products/services lines.
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Cron Job class.
loadBox($max=5)
Load data into info_box_contents array to show array later.