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