18use Luracast\Restler\RestException;
20require_once DOL_DOCUMENT_ROOT.
'/cron/class/cronjob.class.php';
33 public static $FIELDS = array(
50 $this->cronjob =
new Cronjob($this->db);
63 public function get(
$id)
65 if (!DolibarrApiAccess::$user->hasRight(
'cron',
'read')) {
66 throw new RestException(403);
69 $result = $this->cronjob->fetch(
$id);
71 throw new RestException(404,
'Cron job not found');
75 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
100 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $status = -1, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
102 if (!DolibarrApiAccess::$user->hasRight(
'cron',
'read')) {
103 throw new RestException(403);
111 $offset = $limit * $page;
113 $result = $this->cronjob->fetchAll($sortorder, $sortfield, $limit, $offset, $status, $sqlfilters);
115 throw new RestException(503,
'Error when retrieving cron job list: '.$this->cronjob->errorsToString());
118 foreach ($this->cronjob->lines as $cronjob_static) {
122 if ($pagination_data) {
126 $obj_ret[
'data'] = $tmp;
127 $obj_ret[
'pagination'] = [
128 'total' => count($tmp),
130 'page_count' => ceil(count($tmp) / $limit),
148 public function post($request_data =
null)
150 if (!DolibarrApiAccess::$user->hasRight(
'cron',
'write') || !DolibarrApiAccess::$user->admin) {
151 throw new RestException(403,
"Insufficient rights - creating a cron job requires an admin user");
154 $result = $this->
_validate($request_data);
156 foreach ($request_data as $field => $value) {
157 $this->cronjob->$field = $this->
_checkValForAPI($field, $value, $this->cronjob);
160 if ($this->cronjob->create(DolibarrApiAccess::$user) < 0) {
161 throw new RestException(500,
"Error creating cron job", array_merge(array($this->cronjob->error), $this->cronjob->errors));
164 return $this->cronjob->id;
178 public function put(
$id, $request_data =
null)
180 if (!DolibarrApiAccess::$user->hasRight(
'cron',
'write') || !DolibarrApiAccess::$user->admin) {
181 throw new RestException(403,
"Insufficient rights - updating a cron job requires an admin user");
184 $result = $this->cronjob->fetch(
$id);
186 throw new RestException(404,
'Cron job not found');
190 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
193 foreach ($request_data as $field => $value) {
194 if ($field ==
'id') {
197 $this->cronjob->$field = $this->
_checkValForAPI($field, $value, $this->cronjob);
200 if ($this->cronjob->update(DolibarrApiAccess::$user) > 0) {
201 return $this->
get(
$id);
203 throw new RestException(500, $this->cronjob->errorsToString());
217 public function delete(
$id)
219 if (!DolibarrApiAccess::$user->hasRight(
'cron',
'delete')) {
220 throw new RestException(403);
223 $result = $this->cronjob->fetch(
$id);
225 throw new RestException(404,
'Cron job not found');
229 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
232 if (!$this->cronjob->delete(DolibarrApiAccess::$user)) {
233 throw new RestException(500,
'Error when deleting cron job: '.$this->cronjob->errorsToString());
239 'message' =>
'Cron job deleted'
261 public function run(
$id, $securitykey =
'')
263 if (!DolibarrApiAccess::$user->hasRight(
'cron',
'write') || !DolibarrApiAccess::$user->admin) {
264 throw new RestException(403,
"Insufficient rights - running a cron job requires an admin user");
268 throw new RestException(403,
'Security key is wrong');
271 $result = $this->cronjob->fetch(
$id);
273 throw new RestException(404,
'Cron job not found');
277 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
282 $result = $this->cronjob->run_jobs(DolibarrApiAccess::$user->login);
284 throw new RestException(500,
'Error when running cron job: '.$this->cronjob->errorsToString());
287 $result = $this->cronjob->reprogram_jobs(DolibarrApiAccess::$user->login, $now);
289 throw new RestException(500,
'Job ran but failed to reprogram next run: '.$this->cronjob->errorsToString());
324 if ($data ===
null) {
328 foreach (Cronjobs::$FIELDS as $field) {
329 if (!isset($data[$field])) {
330 throw new RestException(400, $field.
" field missing");
332 $cronjob[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
run($id, $securitykey='')
Run a cron job now.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $status=-1, $sqlfilters='', $properties='', $pagination_data=false)
List cron jobs.
__construct()
Constructor.
put($id, $request_data=null)
Update cron job fields.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
post($request_data=null)
Create cron job object.
_validate($data)
Validate fields before create or update object.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
Check access by user to a given resource.
dol_now($mode='gmt')
Return date for now.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.