Commit | Line | Data |
---|---|---|
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 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
b6c94f42 | 19 | * Class for configuring jobs. |
6a488035 TO |
20 | */ |
21 | class CRM_Admin_Form_Job extends CRM_Admin_Form { | |
430ae6dd TO |
22 | protected $_id = NULL; |
23 | ||
00be9182 | 24 | public function preProcess() { |
6a488035 TO |
25 | |
26 | parent::preProcess(); | |
27 | ||
28 | CRM_Utils_System::setTitle(ts('Manage - Scheduled Jobs')); | |
29 | ||
30 | if ($this->_id) { | |
31 | $refreshURL = CRM_Utils_System::url('civicrm/admin/job', | |
32 | "reset=1&action=update&id={$this->_id}", | |
33 | FALSE, NULL, FALSE | |
34 | ); | |
35 | } | |
36 | else { | |
37 | $refreshURL = CRM_Utils_System::url('civicrm/admin/job', | |
38 | "reset=1&action=add", | |
39 | FALSE, NULL, FALSE | |
40 | ); | |
41 | } | |
42 | ||
43 | $this->assign('refreshURL', $refreshURL); | |
44 | } | |
45 | ||
46 | /** | |
eceb18cc | 47 | * Build the form object. |
6a488035 | 48 | * |
77b97be7 | 49 | * @param bool $check |
6a488035 TO |
50 | */ |
51 | public function buildQuickForm($check = FALSE) { | |
52 | parent::buildQuickForm(); | |
53 | ||
54 | if ($this->_action & CRM_Core_Action::DELETE) { | |
55 | return; | |
56 | } | |
57 | ||
58 | $attributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Job'); | |
59 | ||
60 | $this->add('text', 'name', ts('Name'), | |
61 | $attributes['name'], TRUE | |
62 | ); | |
63 | ||
be2fb01f | 64 | $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', [ |
0d48f1cc TO |
65 | 'CRM_Core_DAO_Job', |
66 | $this->_id, | |
67 | ]); | |
6a488035 TO |
68 | |
69 | $this->add('text', 'description', ts('Description'), | |
70 | $attributes['description'] | |
71 | ); | |
72 | ||
73 | $this->add('text', 'api_entity', ts('API Call Entity'), | |
74 | $attributes['api_entity'], TRUE | |
75 | ); | |
76 | ||
77 | $this->add('text', 'api_action', ts('API Call Action'), | |
78 | $attributes['api_action'], TRUE | |
79 | ); | |
80 | ||
56251ea7 | 81 | $this->add('select', 'run_frequency', ts('Run frequency'), CRM_Core_SelectValues::getJobFrequency()); |
6a488035 | 82 | |
e8f61813 | 83 | // CRM-17686 |
be2fb01f | 84 | $this->add('datepicker', 'scheduled_run_date', ts('Scheduled Run Date'), NULL, FALSE, ['minDate' => time()]); |
e8f61813 | 85 | |
6a488035 TO |
86 | $this->add('textarea', 'parameters', ts('Command parameters'), |
87 | "cols=50 rows=6" | |
88 | ); | |
89 | ||
90 | // is this job active ? | |
91 | $this->add('checkbox', 'is_active', ts('Is this Scheduled Job active?')); | |
92 | ||
be2fb01f | 93 | $this->addFormRule(['CRM_Admin_Form_Job', 'formRule']); |
6a488035 TO |
94 | } |
95 | ||
e0ef6999 EM |
96 | /** |
97 | * @param $fields | |
98 | * | |
99 | * @return array|bool | |
100 | * @throws API_Exception | |
101 | */ | |
00be9182 | 102 | public static function formRule($fields) { |
6a488035 | 103 | |
be2fb01f | 104 | $errors = []; |
6a488035 TO |
105 | |
106 | require_once 'api/api.php'; | |
107 | ||
c65db512 | 108 | /** @var \Civi\API\Kernel $apiKernel */ |
048222df | 109 | $apiKernel = \Civi::service('civi_api_kernel'); |
be2fb01f | 110 | $apiRequest = \Civi\API\Request::create($fields['api_entity'], $fields['api_action'], ['version' => 3], NULL); |
c65db512 TO |
111 | try { |
112 | $apiKernel->resolve($apiRequest); | |
0db6c3e1 TO |
113 | } |
114 | catch (\Civi\API\Exception\NotImplementedException $e) { | |
6a488035 TO |
115 | $errors['api_action'] = ts('Given API command is not defined.'); |
116 | } | |
117 | ||
118 | if (!empty($errors)) { | |
119 | return $errors; | |
120 | } | |
121 | ||
122 | return empty($errors) ? TRUE : $errors; | |
123 | } | |
124 | ||
e0ef6999 EM |
125 | /** |
126 | * @return array | |
127 | */ | |
00be9182 | 128 | public function setDefaultValues() { |
be2fb01f | 129 | $defaults = []; |
6a488035 TO |
130 | |
131 | if (!$this->_id) { | |
132 | $defaults['is_active'] = $defaults['is_default'] = 1; | |
133 | return $defaults; | |
134 | } | |
135 | $domainID = CRM_Core_Config::domainID(); | |
136 | ||
353ffa53 TO |
137 | $dao = new CRM_Core_DAO_Job(); |
138 | $dao->id = $this->_id; | |
6a488035 TO |
139 | $dao->domain_id = $domainID; |
140 | if (!$dao->find(TRUE)) { | |
141 | return $defaults; | |
142 | } | |
143 | ||
144 | CRM_Core_DAO::storeValues($dao, $defaults); | |
145 | ||
e8f61813 | 146 | // CRM-17686 |
d7bace22 DRJ |
147 | if (!empty($dao->scheduled_run_date)) { |
148 | $ts = strtotime($dao->scheduled_run_date); | |
9ee4786b | 149 | $defaults['scheduled_run_date'] = date("Y-m-d H:i:s", $ts); |
e8f61813 DRJ |
150 | } |
151 | ||
6a488035 TO |
152 | // CRM-10708 |
153 | // job entity thats shipped with core is all lower case. | |
154 | // this makes sure camel casing is followed for proper working of default population. | |
a7488080 | 155 | if (!empty($defaults['api_entity'])) { |
6a488035 TO |
156 | $defaults['api_entity'] = ucfirst($defaults['api_entity']); |
157 | } | |
158 | ||
159 | return $defaults; | |
160 | } | |
161 | ||
162 | /** | |
eceb18cc | 163 | * Process the form submission. |
6a488035 TO |
164 | */ |
165 | public function postProcess() { | |
166 | ||
d7d6c461 | 167 | CRM_Utils_System::flushCache(); |
6a488035 TO |
168 | |
169 | if ($this->_action & CRM_Core_Action::DELETE) { | |
170 | CRM_Core_BAO_Job::del($this->_id); | |
171 | CRM_Core_Session::setStatus("", ts('Scheduled Job Deleted.'), "success"); | |
172 | return; | |
173 | } | |
174 | ||
175 | $values = $this->controller->exportValues($this->_name); | |
176 | $domainID = CRM_Core_Config::domainID(); | |
177 | ||
178 | $dao = new CRM_Core_DAO_Job(); | |
179 | ||
353ffa53 TO |
180 | $dao->id = $this->_id; |
181 | $dao->domain_id = $domainID; | |
6a488035 | 182 | $dao->run_frequency = $values['run_frequency']; |
353ffa53 TO |
183 | $dao->parameters = $values['parameters']; |
184 | $dao->name = $values['name']; | |
185 | $dao->api_entity = $values['api_entity']; | |
186 | $dao->api_action = $values['api_action']; | |
187 | $dao->description = $values['description']; | |
188 | $dao->is_active = CRM_Utils_Array::value('is_active', $values, 0); | |
6a488035 | 189 | |
e8f61813 | 190 | // CRM-17686 |
9ee4786b | 191 | $ts = strtotime($values['scheduled_run_date']); |
e8f61813 DRJ |
192 | // if a date/time is supplied and not in the past, then set the next scheduled run... |
193 | if ($ts > time()) { | |
d7bace22 | 194 | $dao->scheduled_run_date = CRM_Utils_Date::currentDBDate($ts); |
e8f61813 DRJ |
195 | // warn about monthly/quarterly scheduling, if applicable |
196 | if (($dao->run_frequency == 'Monthly') || ($dao->run_frequency == 'Quarter')) { | |
197 | $info = getdate($ts); | |
198 | if ($info['mday'] > 28) { | |
199 | CRM_Core_Session::setStatus( | |
200 | ts('Relative month values are calculated based on the length of month(s) that they pass through. | |
9ee4786b | 201 | The result will land on the same day of the month except for days 29-31 when the target month contains fewer days than the previous month. |
d7bace22 | 202 | For example, if a job is scheduled to run on August 31st, the following invocation will occur on October 1st, and then the 1st of every month thereafter. |
e8f61813 | 203 | To avoid this issue, please schedule Monthly and Quarterly jobs to run within the first 28 days of the month.'), |
be2fb01f | 204 | ts('Warning'), 'info', ['expires' => 0]); |
e8f61813 DRJ |
205 | } |
206 | } | |
207 | } | |
208 | // ...otherwise, if this isn't a new scheduled job, clear the next scheduled run | |
9ee4786b | 209 | elseif ($dao->id) { |
be2fb01f | 210 | $job = new CRM_Core_ScheduledJob(['id' => $dao->id]); |
d7bace22 | 211 | $job->clearScheduledRunDate(); |
e8f61813 DRJ |
212 | } |
213 | ||
6a488035 | 214 | $dao->save(); |
f813f78e | 215 | |
6a488035 TO |
216 | // CRM-11143 - Give warning message if update_greetings is Enabled (is_active) since it generally should not be run automatically via execute action or runjobs url. |
217 | if ($values['api_action'] == 'update_greeting' && CRM_Utils_Array::value('is_active', $values) == 1) { | |
218 | // pass "wiki" as 6th param to docURL2 if you are linking to a page in wiki.civicrm.org | |
219 | $docLink = CRM_Utils_System::docURL2("Managing Scheduled Jobs", NULL, NULL, NULL, NULL, "wiki"); | |
be2fb01f | 220 | $msg = ts('The update greeting job can be very resource intensive and is typically not necessary to run on a regular basis. If you do choose to enable the job, we recommend you do not run it with the force=1 option, which would rebuild greetings on all records. Leaving that option absent, or setting it to force=0, will only rebuild greetings for contacts that do not currently have a value stored. %1', [1 => $docLink]); |
6a488035 TO |
221 | CRM_Core_Session::setStatus($msg, ts('Warning: Update Greeting job enabled'), 'alert'); |
222 | } | |
223 | ||
6a488035 | 224 | } |
96025800 | 225 | |
6a488035 | 226 | } |