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