dolibarr  16.0.5
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 
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('service', 'lire') && $user->hasRight('contrat', 'lire'));
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->rights->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";
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  if ($result) {
99  $num = $this->db->num_rows($result);
100 
101  $i = 0;
102  while ($i < $num) {
103  $objp = $this->db->fetch_object($result);
104 
105  if (dol_eval($objp->test, 1, 1, '')) {
106  $nextrun = $this->db->jdate($objp->datenextrun);
107  if (empty($nextrun)) {
108  $nextrun = $this->db->jdate($objp->datestart);
109  }
110 
111  if ($line == 0 || ($nextrun < $cronstatic->datenextrun && (empty($objp->nbrun) || empty($objp->maxrun) || $objp->nbrun < $objp->maxrun))) {
112  $cronstatic->id = $objp->rowid;
113  $cronstatic->ref = $objp->rowid;
114  $cronstatic->label = $langs->trans($objp->label);
115  $cronstatic->status = $objp->status;
116  $cronstatic->datenextrun = $this->db->jdate($objp->datenextrun);
117  $cronstatic->datelastrun = $this->db->jdate($objp->datelastrun);
118  }
119  if ($line == 0) {
120  $resultarray[$line] = array(
121  $langs->trans("LastExecutedScheduledJob"),
122  $cronstatic->getNomUrl(1),
123  $cronstatic->datelastrun,
124  $cronstatic->status,
125  $cronstatic->getLibStatut(3)
126  );
127  $line++;
128  }
129 
130  if (!empty($objp->lastresult)) {
131  $nbjobsinerror++;
132  }
133  }
134  $i++;
135  }
136 
137  if ($line) {
138  $resultarray[$line] = array(
139  $langs->trans("NextScheduledJobExecute"),
140  $cronstatic->getNomUrl(1),
141  $cronstatic->datenextrun,
142  $cronstatic->status,
143  $cronstatic->getLibStatut(3)
144  );
145  }
146 
147  foreach ($resultarray as $line => $value) {
148  $this->info_box_contents[$line][] = array(
149  'td' => 'class="tdoverflowmax200"',
150  'text' => $resultarray[$line][0]
151  );
152 
153  $this->info_box_contents[$line][] = array(
154  'td' => 'class="nowraponall"',
155  'textnoformat' => $resultarray[$line][1]
156  );
157  $this->info_box_contents[$line][] = array(
158  'td' => 'class="right"',
159  'textnoformat' => (empty($resultarray[$line][2]) ? '' : $form->textwithpicto(dol_print_date($resultarray[$line][2], "dayhoursec", 'tzserver'), $langs->trans("CurrentTimeZone")))
160  );
161  $this->info_box_contents[$line][] = array(
162  'td' => 'class="center" ',
163  'textnoformat' => $resultarray[$line][4]
164  );
165  $line++;
166  }
167  $this->info_box_contents[$line][] = array(
168  'td' => 'class="tdoverflowmax300" colspan="3"',
169  'text' => $langs->trans("NumberScheduledJobError")
170  );
171  $this->info_box_contents[$line][] = array(
172  'td' => 'class="center"',
173  '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>' : '<a href="'.DOL_URL_ROOT.'/cron/list.php"><div class="center badge-status4">0</div></a>')
174  );
175  } else {
176  $this->info_box_contents[0][0] = array(
177  'td' => '',
178  'maxlength' => 500,
179  'text' => ($this->db->lasterror() . ' sql=' . $sql)
180  );
181  }
182  } else {
183  $this->info_box_contents[0][0] = array(
184  'td' => 'class="nohover opacitymedium left"',
185  'text' => $langs->trans("ReadPermissionNotAllowed")
186  );
187  }
188  }
189 
198  public function showBox($head = null, $contents = null, $nooutput = 0)
199  {
200  return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
201  }
202 }
box_scheduled_jobs\loadBox
loadBox($max=5)
Load data into info_box_contents array to show array later.
Definition: box_scheduled_jobs.php:75
db
$conf db
API class for accounts.
Definition: inc.php:41
Cronjob
Cron Job class.
Definition: cronjob.class.php:31
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
box_scheduled_jobs
Class to manage the box to show last contracted products/services lines.
Definition: box_scheduled_jobs.php:33
box_scheduled_jobs\showBox
showBox($head=null, $contents=null, $nooutput=0)
Method to show box.
Definition: box_scheduled_jobs.php:198
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2500
box_scheduled_jobs\__construct
__construct($db, $param)
Constructor.
Definition: box_scheduled_jobs.php:60
dol_eval
dol_eval($s, $returnvalue=0, $hideerrors=1, $onlysimplestring='1')
Replace eval function to add more security.
Definition: functions.lib.php:8597
ModeleBoxes
Class ModeleBoxes.
Definition: modules_boxes.php:34