(NFC) (dev/core#878) Simplify copyright header (CRM/*)
[civicrm-core.git] / CRM / Admin / Form / Job.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 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Class for configuring jobs.
20 */
21 class CRM_Admin_Form_Job extends CRM_Admin_Form {
22 protected $_id = NULL;
23
24 public function preProcess() {
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 /**
47 * Build the form object.
48 *
49 * @param bool $check
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
64 $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', [
65 'CRM_Core_DAO_Job',
66 $this->_id,
67 ]);
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
81 $this->add('select', 'run_frequency', ts('Run frequency'), CRM_Core_SelectValues::getJobFrequency());
82
83 // CRM-17686
84 $this->add('datepicker', 'scheduled_run_date', ts('Scheduled Run Date'), NULL, FALSE, ['minDate' => time()]);
85
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
93 $this->addFormRule(['CRM_Admin_Form_Job', 'formRule']);
94 }
95
96 /**
97 * @param $fields
98 *
99 * @return array|bool
100 * @throws API_Exception
101 */
102 public static function formRule($fields) {
103
104 $errors = [];
105
106 require_once 'api/api.php';
107
108 /** @var \Civi\API\Kernel $apiKernel */
109 $apiKernel = \Civi::service('civi_api_kernel');
110 $apiRequest = \Civi\API\Request::create($fields['api_entity'], $fields['api_action'], ['version' => 3], NULL);
111 try {
112 $apiKernel->resolve($apiRequest);
113 }
114 catch (\Civi\API\Exception\NotImplementedException $e) {
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
125 /**
126 * @return array
127 */
128 public function setDefaultValues() {
129 $defaults = [];
130
131 if (!$this->_id) {
132 $defaults['is_active'] = $defaults['is_default'] = 1;
133 return $defaults;
134 }
135 $domainID = CRM_Core_Config::domainID();
136
137 $dao = new CRM_Core_DAO_Job();
138 $dao->id = $this->_id;
139 $dao->domain_id = $domainID;
140 if (!$dao->find(TRUE)) {
141 return $defaults;
142 }
143
144 CRM_Core_DAO::storeValues($dao, $defaults);
145
146 // CRM-17686
147 if (!empty($dao->scheduled_run_date)) {
148 $ts = strtotime($dao->scheduled_run_date);
149 $defaults['scheduled_run_date'] = date("Y-m-d H:i:s", $ts);
150 }
151
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.
155 if (!empty($defaults['api_entity'])) {
156 $defaults['api_entity'] = ucfirst($defaults['api_entity']);
157 }
158
159 return $defaults;
160 }
161
162 /**
163 * Process the form submission.
164 */
165 public function postProcess() {
166
167 CRM_Utils_System::flushCache();
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
180 $dao->id = $this->_id;
181 $dao->domain_id = $domainID;
182 $dao->run_frequency = $values['run_frequency'];
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);
189
190 // CRM-17686
191 $ts = strtotime($values['scheduled_run_date']);
192 // if a date/time is supplied and not in the past, then set the next scheduled run...
193 if ($ts > time()) {
194 $dao->scheduled_run_date = CRM_Utils_Date::currentDBDate($ts);
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.
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.
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.
203 To avoid this issue, please schedule Monthly and Quarterly jobs to run within the first 28 days of the month.'),
204 ts('Warning'), 'info', ['expires' => 0]);
205 }
206 }
207 }
208 // ...otherwise, if this isn't a new scheduled job, clear the next scheduled run
209 elseif ($dao->id) {
210 $job = new CRM_Core_ScheduledJob(['id' => $dao->id]);
211 $job->clearScheduledRunDate();
212 }
213
214 $dao->save();
215
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");
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]);
221 CRM_Core_Session::setStatus($msg, ts('Warning: Update Greeting job enabled'), 'alert');
222 }
223
224 }
225
226 }