Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * This interface defines methods that need to be implemented | |
30 | * by every scheduled job (cron task) in CiviCRM. | |
31 | * | |
32 | * @package CRM | |
e7112fa7 | 33 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
34 | * $Id$ |
35 | * | |
36 | */ | |
37 | class CRM_Core_ScheduledJob { | |
38 | ||
39 | var $version = 3; | |
40 | ||
41 | var $name = NULL; | |
42 | ||
43 | var $apiParams = array(); | |
44 | ||
45 | var $remarks = array(); | |
46 | ||
a0ee3941 | 47 | /** |
c490a46a | 48 | * @param array $params |
a0ee3941 | 49 | */ |
6a488035 TO |
50 | public function __construct($params) { |
51 | foreach ($params as $name => $param) { | |
52 | $this->$name = $param; | |
53 | } | |
54 | ||
55 | // version is set to 3 by default - if different number | |
56 | // defined in params, it's replaced later on, however, | |
57 | // it's practically useles, since it seems none of api v2 | |
58 | // will work properly in cron job setup. It might become | |
59 | // useful when/if api v4 starts to emerge and will need | |
60 | // testing in the cron job setup. To permanenty require | |
61 | // hardcoded api version, it's enough to move below line | |
62 | // under following if block. | |
63 | $this->apiParams = array('version' => $this->version); | |
64 | ||
65 | if (!empty($this->parameters)) { | |
66 | $lines = explode("\n", $this->parameters); | |
67 | ||
68 | foreach ($lines as $line) { | |
69 | $pair = explode("=", $line); | |
94ca7fdb | 70 | if ($pair === FALSE || count($pair) != 2 || trim($pair[0]) == '' || trim($pair[1]) == '') { |
6a488035 TO |
71 | $this->remarks[] .= 'Malformed parameters!'; |
72 | break; | |
73 | } | |
74 | $this->apiParams[trim($pair[0])] = trim($pair[1]); | |
75 | } | |
76 | } | |
77 | } | |
78 | ||
a0ee3941 EM |
79 | /** |
80 | * @param null $date | |
81 | */ | |
6a488035 | 82 | public function saveLastRun($date = NULL) { |
353ffa53 TO |
83 | $dao = new CRM_Core_DAO_Job(); |
84 | $dao->id = $this->id; | |
6a488035 TO |
85 | $dao->last_run = ($date == NULL) ? CRM_Utils_Date::currentDBDate() : CRM_Utils_Date::currentDBDate($date); |
86 | $dao->save(); | |
87 | } | |
88 | ||
a0ee3941 EM |
89 | /** |
90 | * @return bool | |
91 | */ | |
6a488035 TO |
92 | public function needsRunning() { |
93 | // run if it was never run | |
94 | if (empty($this->last_run)) { | |
95 | return TRUE; | |
96 | } | |
97 | ||
98 | // run_frequency check | |
99 | switch ($this->run_frequency) { | |
100 | case 'Always': | |
101 | return TRUE; | |
102 | ||
103 | case 'Hourly': | |
d0be1535 KW |
104 | $format = 'YmdH'; |
105 | break; | |
6a488035 TO |
106 | |
107 | case 'Daily': | |
d0be1535 KW |
108 | $format = 'Ymd'; |
109 | break; | |
7fce7b7f B |
110 | |
111 | case 'Mondays': | |
112 | $now = CRM_Utils_Date::currentDBDate(); | |
113 | $dayAgo = strtotime('-1 day', strtotime($now)); | |
114 | $lastRun = strtotime($this->last_run); | |
083224dc CW |
115 | $nowDayOfWeek = date('l', strtotime($now)); |
116 | return ($lastRun < $dayAgo && $nowDayOfWeek == 'Monday'); | |
7fce7b7f B |
117 | |
118 | case '1stOfMth': | |
119 | $now = CRM_Utils_Date::currentDBDate(); | |
120 | $dayAgo = strtotime('-1 day', strtotime($now)); | |
121 | $lastRun = strtotime($this->last_run); | |
083224dc CW |
122 | $nowDayOfMonth = date('j', strtotime($now)); |
123 | return ($lastRun < $dayAgo && $nowDayOfMonth == '1'); | |
7fce7b7f B |
124 | |
125 | case '1stOfQtr': | |
126 | $now = CRM_Utils_Date::currentDBDate(); | |
127 | $dayAgo = strtotime('-1 day', strtotime($now)); | |
128 | $lastRun = strtotime($this->last_run); | |
083224dc CW |
129 | $nowDayOfMonth = date('j', strtotime($now)); |
130 | $nowMonth = date('n', strtotime($now)); | |
131 | $qtrMonths = array('1', '4', '7', '10'); | |
132 | return ($lastRun < $dayAgo && $nowDayOfMonth == '13' && in_array($nowMonth, $qtrMonths)); | |
6a488035 TO |
133 | } |
134 | ||
d0be1535 KW |
135 | $now = CRM_Utils_Date::currentDBDate(); |
136 | $lastTime = date($format, strtotime($this->last_run)); | |
137 | $thisTime = date($format, strtotime($now)); | |
138 | ||
139 | return ($lastTime <> $thisTime); | |
6a488035 TO |
140 | } |
141 | ||
2aa397bc TO |
142 | public function __destruct() { |
143 | } | |
96025800 | 144 | |
6a488035 | 145 | } |