ensure recur options are present on backend cc contribution form.
[civicrm-core.git] / CRM / Core / ScheduledJob.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
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
17 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 */
19 class CRM_Core_ScheduledJob {
20
21 public $version = 3;
22
23 public $name = NULL;
24
25 public $apiParams = [];
26
27 public $remarks = [];
28
29 /**
30 * @param array $params
31 */
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.
45 $this->apiParams = ['version' => $this->version];
46
47 if (!empty($this->parameters)) {
48 $lines = explode("\n", $this->parameters);
49
50 foreach ($lines as $line) {
51 $pair = explode("=", $line);
52 if ($pair === FALSE || count($pair) != 2 || trim($pair[0]) == '' || trim($pair[1]) == '') {
53 $this->remarks[] .= 'Malformed parameters!';
54 break;
55 }
56 $this->apiParams[trim($pair[0])] = trim($pair[1]);
57 }
58 }
59 }
60
61 /**
62 * Update the last_run date of this job
63 */
64 public function saveLastRun() {
65 $dao = new CRM_Core_DAO_Job();
66 $dao->id = $this->id;
67 $dao->last_run = CRM_Utils_Date::currentDBDate();
68 $dao->save();
69 }
70
71 /**
72 * Delete the scheduled_run_date from this job
73 */
74 public function clearScheduledRunDate() {
75 CRM_Core_DAO::executeQuery('UPDATE civicrm_job SET scheduled_run_date = NULL WHERE id = %1', [
76 '1' => [$this->id, 'Integer'],
77 ]);
78 }
79
80 /**
81 * @return bool
82 */
83 public function needsRunning() {
84
85 // CRM-17686
86 // check if the job has a specific scheduled date/time
87 if (!empty($this->scheduled_run_date)) {
88 if (strtotime($this->scheduled_run_date) <= time()) {
89 $this->clearScheduledRunDate();
90 return TRUE;
91 }
92 else {
93 return FALSE;
94 }
95 }
96
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
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';
122 break;
123
124 case 'Daily':
125 $offset = '+1 day';
126 break;
127
128 case 'Hourly':
129 $offset = '+1 hour';
130 break;
131 }
132
133 $now = strtotime(CRM_Utils_Date::currentDBDate());
134 $lastTime = strtotime($this->last_run);
135 $nextTime = strtotime($offset, $lastTime);
136
137 return ($now >= $nextTime);
138 }
139
140 public function __destruct() {
141 }
142
143 }