Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
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 | |
06b69b18 | 33 | * @copyright CiviCRM LLC (c) 2004-2014 |
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 | ||
47 | /* | |
48 | * Class constructor | |
8ef12e64 | 49 | * |
6a488035 TO |
50 | * @param string $namespace namespace prefix for component's files |
51 | * @access public | |
8ef12e64 | 52 | * |
6a488035 TO |
53 | */ |
54 | ||
a0ee3941 EM |
55 | /** |
56 | * @param $params | |
57 | */ | |
6a488035 TO |
58 | public function __construct($params) { |
59 | foreach ($params as $name => $param) { | |
60 | $this->$name = $param; | |
61 | } | |
62 | ||
63 | // version is set to 3 by default - if different number | |
64 | // defined in params, it's replaced later on, however, | |
65 | // it's practically useles, since it seems none of api v2 | |
66 | // will work properly in cron job setup. It might become | |
67 | // useful when/if api v4 starts to emerge and will need | |
68 | // testing in the cron job setup. To permanenty require | |
69 | // hardcoded api version, it's enough to move below line | |
70 | // under following if block. | |
71 | $this->apiParams = array('version' => $this->version); | |
72 | ||
73 | if (!empty($this->parameters)) { | |
74 | $lines = explode("\n", $this->parameters); | |
75 | ||
76 | foreach ($lines as $line) { | |
77 | $pair = explode("=", $line); | |
78 | if (empty($pair[0]) || empty($pair[1])) { | |
79 | $this->remarks[] .= 'Malformed parameters!'; | |
80 | break; | |
81 | } | |
82 | $this->apiParams[trim($pair[0])] = trim($pair[1]); | |
83 | } | |
84 | } | |
85 | } | |
86 | ||
a0ee3941 EM |
87 | /** |
88 | * @param null $date | |
89 | */ | |
6a488035 TO |
90 | public function saveLastRun($date = NULL) { |
91 | $dao = new CRM_Core_DAO_Job(); | |
92 | $dao->id = $this->id; | |
93 | $dao->last_run = ($date == NULL) ? CRM_Utils_Date::currentDBDate() : CRM_Utils_Date::currentDBDate($date); | |
94 | $dao->save(); | |
95 | } | |
96 | ||
a0ee3941 EM |
97 | /** |
98 | * @return bool | |
99 | */ | |
6a488035 TO |
100 | public function needsRunning() { |
101 | // run if it was never run | |
102 | if (empty($this->last_run)) { | |
103 | return TRUE; | |
104 | } | |
105 | ||
106 | // run_frequency check | |
107 | switch ($this->run_frequency) { | |
108 | case 'Always': | |
109 | return TRUE; | |
110 | ||
111 | case 'Hourly': | |
112 | $now = CRM_Utils_Date::currentDBDate(); | |
113 | $hourAgo = strtotime('-1 hour', strtotime($now)); | |
114 | $lastRun = strtotime($this->last_run); | |
115 | if ($lastRun < $hourAgo) { | |
116 | return TRUE; | |
117 | } | |
118 | ||
119 | case 'Daily': | |
120 | $now = CRM_Utils_Date::currentDBDate(); | |
121 | $dayAgo = strtotime('-1 day', strtotime($now)); | |
122 | $lastRun = strtotime($this->last_run); | |
123 | if ($lastRun < $dayAgo) { | |
124 | return TRUE; | |
125 | } | |
126 | } | |
127 | ||
128 | return FALSE; | |
129 | } | |
130 | ||
131 | public function __destruct() {} | |
132 | } | |
133 |