Ian province abbreviation patch - issue 724
[civicrm-core.git] / CRM / Admin / Form / Job.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
31 * @copyright CiviCRM LLC (c) 2004-2015
32 */
33
34 /**
35 * Class for configuring jobs.
36 */
37 class CRM_Admin_Form_Job extends CRM_Admin_Form {
38 protected $_id = NULL;
39
40 public function preProcess() {
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 /**
63 * Build the form object.
64 *
65 * @param bool $check
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
80 $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', array(
81 'CRM_Core_DAO_Job',
82 $this->_id,
83 ));
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
97 $this->add('select', 'run_frequency', ts('Run frequency'), CRM_Core_SelectValues::getJobFrequency());
98
99 $this->add('textarea', 'parameters', ts('Command parameters'),
100 "cols=50 rows=6"
101 );
102
103 // is this job active ?
104 $this->add('checkbox', 'is_active', ts('Is this Scheduled Job active?'));
105
106 $this->addFormRule(array('CRM_Admin_Form_Job', 'formRule'));
107 }
108
109 /**
110 * @param $fields
111 *
112 * @return array|bool
113 * @throws API_Exception
114 */
115 public static function formRule($fields) {
116
117 $errors = array();
118
119 require_once 'api/api.php';
120
121 /** @var \Civi\API\Kernel $apiKernel */
122 $apiKernel = \Civi\Core\Container::singleton()->get('civi_api_kernel');
123 $apiRequest = \Civi\API\Request::create($fields['api_entity'], $fields['api_action'], array('version' => 3), NULL);
124 try {
125 $apiKernel->resolve($apiRequest);
126 }
127 catch (\Civi\API\Exception\NotImplementedException $e) {
128 $errors['api_action'] = ts('Given API command is not defined.');
129 }
130
131 if (!empty($errors)) {
132 return $errors;
133 }
134
135 return empty($errors) ? TRUE : $errors;
136 }
137
138 /**
139 * @return array
140 */
141 public function setDefaultValues() {
142 $defaults = array();
143
144 if (!$this->_id) {
145 $defaults['is_active'] = $defaults['is_default'] = 1;
146 return $defaults;
147 }
148 $domainID = CRM_Core_Config::domainID();
149
150 $dao = new CRM_Core_DAO_Job();
151 $dao->id = $this->_id;
152 $dao->domain_id = $domainID;
153 if (!$dao->find(TRUE)) {
154 return $defaults;
155 }
156
157 CRM_Core_DAO::storeValues($dao, $defaults);
158
159 // CRM-10708
160 // job entity thats shipped with core is all lower case.
161 // this makes sure camel casing is followed for proper working of default population.
162 if (!empty($defaults['api_entity'])) {
163 $defaults['api_entity'] = ucfirst($defaults['api_entity']);
164 }
165
166 return $defaults;
167 }
168
169 /**
170 * Process the form submission.
171 */
172 public function postProcess() {
173
174 CRM_Utils_System::flushCache('CRM_Core_DAO_Job');
175
176 if ($this->_action & CRM_Core_Action::DELETE) {
177 CRM_Core_BAO_Job::del($this->_id);
178 CRM_Core_Session::setStatus("", ts('Scheduled Job Deleted.'), "success");
179 return;
180 }
181
182 $values = $this->controller->exportValues($this->_name);
183 $domainID = CRM_Core_Config::domainID();
184
185 $dao = new CRM_Core_DAO_Job();
186
187 $dao->id = $this->_id;
188 $dao->domain_id = $domainID;
189 $dao->run_frequency = $values['run_frequency'];
190 $dao->parameters = $values['parameters'];
191 $dao->name = $values['name'];
192 $dao->api_entity = $values['api_entity'];
193 $dao->api_action = $values['api_action'];
194 $dao->description = $values['description'];
195 $dao->is_active = CRM_Utils_Array::value('is_active', $values, 0);
196
197 $dao->save();
198
199 // 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.
200 if ($values['api_action'] == 'update_greeting' && CRM_Utils_Array::value('is_active', $values) == 1) {
201 // pass "wiki" as 6th param to docURL2 if you are linking to a page in wiki.civicrm.org
202 $docLink = CRM_Utils_System::docURL2("Managing Scheduled Jobs", NULL, NULL, NULL, NULL, "wiki");
203 $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));
204 CRM_Core_Session::setStatus($msg, ts('Warning: Update Greeting job enabled'), 'alert');
205 }
206
207 }
208
209 }