3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Class for configuring jobs.
21 class CRM_Admin_Form_Job
extends CRM_Admin_Form
{
22 protected $_id = NULL;
24 public function preProcess() {
28 CRM_Utils_System
::setTitle(ts('Manage - Scheduled Jobs'));
31 $refreshURL = CRM_Utils_System
::url('civicrm/admin/job',
32 "reset=1&action=update&id={$this->_id}",
37 $refreshURL = CRM_Utils_System
::url('civicrm/admin/job',
43 $this->assign('refreshURL', $refreshURL);
47 * Build the form object.
51 public function buildQuickForm($check = FALSE) {
52 parent
::buildQuickForm();
54 if ($this->_action
& CRM_Core_Action
::DELETE
) {
58 $attributes = CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Job');
60 $this->add('text', 'name', ts('Name'),
61 $attributes['name'], TRUE
64 $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', [
69 $this->add('text', 'description', ts('Description'),
70 $attributes['description']
73 $this->add('text', 'api_entity', ts('API Call Entity'),
74 $attributes['api_entity'], TRUE
77 $this->add('text', 'api_action', ts('API Call Action'),
78 $attributes['api_action'], TRUE
81 $this->add('select', 'run_frequency', ts('Run frequency'), CRM_Core_SelectValues
::getJobFrequency());
84 $this->add('datepicker', 'scheduled_run_date', ts('Scheduled Run Date'), NULL, FALSE, ['minDate' => time()]);
86 $this->add('textarea', 'parameters', ts('Command parameters'),
90 // is this job active ?
91 $this->add('checkbox', 'is_active', ts('Is this Scheduled Job active?'));
93 $this->addFormRule(['CRM_Admin_Form_Job', 'formRule']);
100 * @throws API_Exception
102 public static function formRule($fields) {
106 require_once 'api/api.php';
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);
112 $apiKernel->resolve($apiRequest);
114 catch (\Civi\API\Exception\NotImplementedException
$e) {
115 $errors['api_action'] = ts('Given API command is not defined.');
118 if (!empty($errors)) {
122 return empty($errors) ?
TRUE : $errors;
128 public function setDefaultValues() {
132 $defaults['is_active'] = $defaults['is_default'] = 1;
135 $domainID = CRM_Core_Config
::domainID();
137 $dao = new CRM_Core_DAO_Job();
138 $dao->id
= $this->_id
;
139 $dao->domain_id
= $domainID;
140 if (!$dao->find(TRUE)) {
144 CRM_Core_DAO
::storeValues($dao, $defaults);
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);
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']);
163 * Process the form submission.
165 public function postProcess() {
167 CRM_Utils_System
::flushCache();
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");
175 $values = $this->controller
->exportValues($this->_name
);
176 $domainID = CRM_Core_Config
::domainID();
178 $dao = new CRM_Core_DAO_Job();
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);
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...
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]);
208 // ...otherwise, if this isn't a new scheduled job, clear the next scheduled run
210 $job = new CRM_Core_ScheduledJob(['id' => $dao->id
]);
211 $job->clearScheduledRunDate();
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');