Merge pull request #4875 from civicrm/minor-fix
[civicrm-core.git] / CRM / Admin / Form / Job.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
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 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id: $
33 *
34 */
35
36/**
37 *
38 */
39class CRM_Admin_Form_Job extends CRM_Admin_Form {
430ae6dd
TO
40 protected $_id = NULL;
41
00be9182 42 public function preProcess() {
6a488035
TO
43
44 parent::preProcess();
45
46 CRM_Utils_System::setTitle(ts('Manage - Scheduled Jobs'));
47
48 if ($this->_id) {
49 $refreshURL = CRM_Utils_System::url('civicrm/admin/job',
50 "reset=1&action=update&id={$this->_id}",
51 FALSE, NULL, FALSE
52 );
53 }
54 else {
55 $refreshURL = CRM_Utils_System::url('civicrm/admin/job',
56 "reset=1&action=add",
57 FALSE, NULL, FALSE
58 );
59 }
60
61 $this->assign('refreshURL', $refreshURL);
62 }
63
64 /**
c490a46a 65 * Build the form object
6a488035 66 *
77b97be7
EM
67 * @param bool $check
68 *
355ba699 69 * @return void
6a488035
TO
70 */
71 public function buildQuickForm($check = FALSE) {
72 parent::buildQuickForm();
73
74 if ($this->_action & CRM_Core_Action::DELETE) {
75 return;
76 }
77
78 $attributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Job');
79
80 $this->add('text', 'name', ts('Name'),
81 $attributes['name'], TRUE
82 );
83
353ffa53
TO
84 $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', array(
85 'CRM_Core_DAO_Job',
86 $this->_id
87 ));
6a488035
TO
88
89 $this->add('text', 'description', ts('Description'),
90 $attributes['description']
91 );
92
93 $this->add('text', 'api_entity', ts('API Call Entity'),
94 $attributes['api_entity'], TRUE
95 );
96
97 $this->add('text', 'api_action', ts('API Call Action'),
98 $attributes['api_action'], TRUE
99 );
100
56251ea7 101 $this->add('select', 'run_frequency', ts('Run frequency'), CRM_Core_SelectValues::getJobFrequency());
6a488035
TO
102
103 $this->add('textarea', 'parameters', ts('Command parameters'),
104 "cols=50 rows=6"
105 );
106
107 // is this job active ?
108 $this->add('checkbox', 'is_active', ts('Is this Scheduled Job active?'));
109
110 $this->addFormRule(array('CRM_Admin_Form_Job', 'formRule'));
111 }
112
e0ef6999
EM
113 /**
114 * @param $fields
115 *
116 * @return array|bool
117 * @throws API_Exception
118 */
00be9182 119 public static function formRule($fields) {
6a488035
TO
120
121 $errors = array();
122
123 require_once 'api/api.php';
124
c65db512
TO
125 /** @var \Civi\API\Kernel $apiKernel */
126 $apiKernel = \Civi\Core\Container::singleton()->get('civi_api_kernel');
c8c9ce59 127 $apiRequest = \Civi\API\Request::create($fields['api_entity'], $fields['api_action'], array('version' => 3), NULL);
c65db512
TO
128 try {
129 $apiKernel->resolve($apiRequest);
0db6c3e1
TO
130 }
131 catch (\Civi\API\Exception\NotImplementedException $e) {
6a488035
TO
132 $errors['api_action'] = ts('Given API command is not defined.');
133 }
134
135 if (!empty($errors)) {
136 return $errors;
137 }
138
139 return empty($errors) ? TRUE : $errors;
140 }
141
e0ef6999
EM
142 /**
143 * @return array
144 */
00be9182 145 public function setDefaultValues() {
6a488035
TO
146 $defaults = array();
147
148 if (!$this->_id) {
149 $defaults['is_active'] = $defaults['is_default'] = 1;
150 return $defaults;
151 }
152 $domainID = CRM_Core_Config::domainID();
153
353ffa53
TO
154 $dao = new CRM_Core_DAO_Job();
155 $dao->id = $this->_id;
6a488035
TO
156 $dao->domain_id = $domainID;
157 if (!$dao->find(TRUE)) {
158 return $defaults;
159 }
160
161 CRM_Core_DAO::storeValues($dao, $defaults);
162
163 // CRM-10708
164 // job entity thats shipped with core is all lower case.
165 // this makes sure camel casing is followed for proper working of default population.
a7488080 166 if (!empty($defaults['api_entity'])) {
6a488035
TO
167 $defaults['api_entity'] = ucfirst($defaults['api_entity']);
168 }
169
170 return $defaults;
171 }
172
173 /**
c490a46a 174 * Process the form submission
6a488035 175 *
6a488035 176 *
355ba699 177 * @return void
6a488035
TO
178 */
179 public function postProcess() {
180
181 CRM_Utils_System::flushCache('CRM_Core_DAO_Job');
182
183 if ($this->_action & CRM_Core_Action::DELETE) {
184 CRM_Core_BAO_Job::del($this->_id);
185 CRM_Core_Session::setStatus("", ts('Scheduled Job Deleted.'), "success");
186 return;
187 }
188
189 $values = $this->controller->exportValues($this->_name);
190 $domainID = CRM_Core_Config::domainID();
191
192 $dao = new CRM_Core_DAO_Job();
193
353ffa53
TO
194 $dao->id = $this->_id;
195 $dao->domain_id = $domainID;
6a488035 196 $dao->run_frequency = $values['run_frequency'];
353ffa53
TO
197 $dao->parameters = $values['parameters'];
198 $dao->name = $values['name'];
199 $dao->api_entity = $values['api_entity'];
200 $dao->api_action = $values['api_action'];
201 $dao->description = $values['description'];
202 $dao->is_active = CRM_Utils_Array::value('is_active', $values, 0);
6a488035
TO
203
204 $dao->save();
f813f78e 205
6a488035
TO
206 // 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.
207 if ($values['api_action'] == 'update_greeting' && CRM_Utils_Array::value('is_active', $values) == 1) {
208 // pass "wiki" as 6th param to docURL2 if you are linking to a page in wiki.civicrm.org
209 $docLink = CRM_Utils_System::docURL2("Managing Scheduled Jobs", NULL, NULL, NULL, NULL, "wiki");
210 $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));
211 CRM_Core_Session::setStatus($msg, ts('Warning: Update Greeting job enabled'), 'alert');
212 }
213
6a488035 214 }
6a488035 215}