Merge pull request #17470 from colemanw/array
[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 * $Id$
19 *
20 */
21 class CRM_Core_ScheduledJob {
22
23 public $version = 3;
24
25 public $name = NULL;
26
27 public $apiParams = [];
28
29 public $remarks = [];
30
31 /**
32 * @param array $params
33 */
34 public function __construct($params) {
35 foreach ($params as $name => $param) {
36 $this->$name = $param;
37 }
38
39 // version is set to 3 by default - if different number
40 // defined in params, it's replaced later on, however,
41 // it's practically useles, since it seems none of api v2
42 // will work properly in cron job setup. It might become
43 // useful when/if api v4 starts to emerge and will need
44 // testing in the cron job setup. To permanenty require
45 // hardcoded api version, it's enough to move below line
46 // under following if block.
47 $this->apiParams = ['version' => $this->version];
48
49 if (!empty($this->parameters)) {
50 $lines = explode("\n", $this->parameters);
51
52 foreach ($lines as $line) {
53 $pair = explode("=", $line);
54 if ($pair === FALSE || count($pair) != 2 || trim($pair[0]) == '' || trim($pair[1]) == '') {
55 $this->remarks[] .= 'Malformed parameters!';
56 break;
57 }
58 $this->apiParams[trim($pair[0])] = trim($pair[1]);
59 }
60 }
61 }
62
63 /**
64 * @param null $date
65 */
66 public function saveLastRun($date = NULL) {
67 $dao = new CRM_Core_DAO_Job();
68 $dao->id = $this->id;
69 $dao->last_run = ($date == NULL) ? CRM_Utils_Date::currentDBDate() : CRM_Utils_Date::currentDBDate($date);
70 $dao->save();
71 }
72
73 /**
74 * @return void
75 */
76 public function clearScheduledRunDate() {
77 CRM_Core_DAO::executeQuery('UPDATE civicrm_job SET scheduled_run_date = NULL WHERE id = %1',
78 [
79 '1' => [$this->id, 'Integer'],
80 ]);
81 }
82
83 /**
84 * @return bool
85 */
86 public function needsRunning() {
87
88 // CRM-17686
89 // check if the job has a specific scheduled date/time
90 if (!empty($this->scheduled_run_date)) {
91 if (strtotime($this->scheduled_run_date) <= time()) {
92 $this->clearScheduledRunDate();
93 return TRUE;
94 }
95 else {
96 return FALSE;
97 }
98 }
99
100 // run if it was never run
101 if (empty($this->last_run)) {
102 return TRUE;
103 }
104
105 // run_frequency check
106 switch ($this->run_frequency) {
107 case 'Always':
108 return TRUE;
109
110 // CRM-17669
111 case 'Yearly':
112 $offset = '+1 year';
113 break;
114
115 case 'Quarter':
116 $offset = '+3 months';
117 break;
118
119 case 'Monthly':
120 $offset = '+1 month';
121 break;
122
123 case 'Weekly':
124 $offset = '+1 week';
125 break;
126
127 case 'Daily':
128 $offset = '+1 day';
129 break;
130
131 case 'Hourly':
132 $offset = '+1 hour';
133 break;
134 }
135
136 $now = strtotime(CRM_Utils_Date::currentDBDate());
137 $lastTime = strtotime($this->last_run);
138 $nextTime = strtotime($offset, $lastTime);
139
140 return ($now >= $nextTime);
141 }
142
143 public function __destruct() {
144 }
145
146 }