Merge pull request #24162 from colemanw/savedSearchLabel
[civicrm-core.git] / CRM / Core / ScheduledJob.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 * This interface defines methods that need to be implemented
14 * by every scheduled job (cron task) in CiviCRM.
15 *
16 * @package CRM
ca5cec67 17 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
18 */
19class CRM_Core_ScheduledJob {
20
518fa0ee 21 public $version = 3;
6a488035 22
518fa0ee 23 public $name = NULL;
6a488035 24
518fa0ee 25 public $apiParams = [];
6a488035 26
518fa0ee 27 public $remarks = [];
6a488035 28
a0ee3941 29 /**
c490a46a 30 * @param array $params
a0ee3941 31 */
6a488035
TO
32 public function __construct($params) {
33 foreach ($params as $name => $param) {
34 $this->$name = $param;
35 }
36
37 // version is set to 3 by default - if different number
38 // defined in params, it's replaced later on, however,
39 // it's practically useles, since it seems none of api v2
40 // will work properly in cron job setup. It might become
41 // useful when/if api v4 starts to emerge and will need
42 // testing in the cron job setup. To permanenty require
43 // hardcoded api version, it's enough to move below line
44 // under following if block.
be2fb01f 45 $this->apiParams = ['version' => $this->version];
6a488035
TO
46
47 if (!empty($this->parameters)) {
48 $lines = explode("\n", $this->parameters);
49
50 foreach ($lines as $line) {
51 $pair = explode("=", $line);
94ca7fdb 52 if ($pair === FALSE || count($pair) != 2 || trim($pair[0]) == '' || trim($pair[1]) == '') {
6a488035
TO
53 $this->remarks[] .= 'Malformed parameters!';
54 break;
55 }
56 $this->apiParams[trim($pair[0])] = trim($pair[1]);
57 }
58 }
59 }
60
a0ee3941 61 /**
14a09e97 62 * Update the last_run date of this job
a0ee3941 63 */
14a09e97 64 public function saveLastRun() {
353ffa53
TO
65 $dao = new CRM_Core_DAO_Job();
66 $dao->id = $this->id;
14a09e97 67 $dao->last_run = CRM_Utils_Date::currentDBDate();
6a488035
TO
68 $dao->save();
69 }
70
bda41fcb 71 /**
14a09e97 72 * Delete the scheduled_run_date from this job
bda41fcb 73 */
d7bace22 74 public function clearScheduledRunDate() {
14a09e97
CW
75 CRM_Core_DAO::executeQuery('UPDATE civicrm_job SET scheduled_run_date = NULL WHERE id = %1', [
76 '1' => [$this->id, 'Integer'],
77 ]);
bda41fcb
DRJ
78 }
79
a0ee3941
EM
80 /**
81 * @return bool
82 */
6a488035 83 public function needsRunning() {
bda41fcb
DRJ
84
85 // CRM-17686
86 // check if the job has a specific scheduled date/time
d7bace22
DRJ
87 if (!empty($this->scheduled_run_date)) {
88 if (strtotime($this->scheduled_run_date) <= time()) {
89 $this->clearScheduledRunDate();
bda41fcb
DRJ
90 return TRUE;
91 }
92 else {
93 return FALSE;
94 }
95 }
96
6a488035
TO
97 // run if it was never run
98 if (empty($this->last_run)) {
99 return TRUE;
100 }
101
102 // run_frequency check
103 switch ($this->run_frequency) {
104 case 'Always':
105 return TRUE;
106
bda41fcb
DRJ
107 // CRM-17669
108 case 'Yearly':
109 $offset = '+1 year';
110 break;
111
112 case 'Quarter':
113 $offset = '+3 months';
114 break;
115
116 case 'Monthly':
117 $offset = '+1 month';
118 break;
119
120 case 'Weekly':
121 $offset = '+1 week';
d0be1535 122 break;
6a488035
TO
123
124 case 'Daily':
bda41fcb 125 $offset = '+1 day';
d0be1535 126 break;
7fce7b7f 127
bda41fcb
DRJ
128 case 'Hourly':
129 $offset = '+1 hour';
130 break;
6a488035
TO
131 }
132
bda41fcb
DRJ
133 $now = strtotime(CRM_Utils_Date::currentDBDate());
134 $lastTime = strtotime($this->last_run);
135 $nextTime = strtotime($offset, $lastTime);
d0be1535 136
bda41fcb 137 return ($now >= $nextTime);
6a488035
TO
138 }
139
2aa397bc
TO
140 public function __destruct() {
141 }
96025800 142
6a488035 143}