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