CRM-12743 - goodbye eval
[civicrm-core.git] / CRM / UF / Form / Group.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 * This class is for UF Group
38 */
39 class CRM_UF_Form_Group extends CRM_Core_Form {
40
41 /**
42 * the form id saved to the session for an update
43 *
44 * @var int
45 * @access protected
46 */
47 protected $_id;
48
49 /**
50 * the title for group
51 *
52 * @var int
53 * @access protected
54 */
55 protected $_title;
56 protected $_groupElement;
57 protected $_group;
58 protected $_allPanes;
59
60 /**
61 * Function to set variables up before form is built
62 *
63 * @return void
64 * @access public
65 */
66 public function preProcess() {
67 // current form id
68 $this->_id = $this->get('id');
69 if (!$this->_id) {
70 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, 0);
71 }
72 $this->assign('gid', $this->_id);
73 $this->_group = CRM_Core_PseudoConstant::group();
74
75 if ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::DELETE)) {
76 $title = CRM_Core_BAO_UFGroup::getTitle($this->_id);
77 $this->assign('profileTitle', $title);
78 }
79
80 // setting title for html page
81 if ($this->_action & CRM_Core_Action::UPDATE) {
82 CRM_Utils_System::setTitle(ts('Profile Settings') . " - $title");
83 }
84 elseif ($this->_action & (CRM_Core_Action::DISABLE | CRM_Core_Action::DELETE)) {
85 $ufGroup['module'] = implode(' , ', CRM_Core_BAO_UFGroup::getUFJoinRecord($this->_id, TRUE));
86 $status = 0;
87 $status = CRM_Core_BAO_UFGroup::usedByModule($this->_id);
88 if ($this->_action & (CRM_Core_Action::DISABLE)) {
89 if ($status) {
90 $message = 'This profile is currently used for ' . $ufGroup['module'] . '. If you disable the profile - it will be removed from these forms and/or modules. Do you want to continue?';
91 }
92 else {
93 $message = 'Are you sure you want to disable this Profile?';
94 }
95 }
96 else {
97 if ($status) {
98 $message = 'This profile is currently used for ' . $ufGroup['module'] . '. If you delete the profile - it will be removed from these forms and/or modules. This action cannot be undone. Do you want to continue?';
99 }
100 else {
101 $message = 'Are you sure you want to delete this Profile? This action cannot be undone.';
102 }
103 }
104 $this->assign('message', $message);
105 }
106 else {
107 CRM_Utils_System::setTitle(ts('New CiviCRM Profile'));
108 }
109 }
110
111 /**
112 * Function to actually build the form
113 *
114 * @return void
115 * @access public
116 */
117 public function buildQuickForm() {
118 if ($this->_action & (CRM_Core_Action::DISABLE | CRM_Core_Action::DELETE)) {
119 if ($this->_action & (CRM_Core_Action::DISABLE)) {
120 $display = 'Disable Profile';
121 }
122 else {
123 $display = 'Delete Profile';
124 }
125 $this->addButtons(array(
126 array(
127 'type' => 'next',
128 'name' => $display,
129 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
130 'isDefault' => TRUE,
131 ),
132 array(
133 'type' => 'cancel',
134 'name' => ts('Cancel'),
135 ),
136 )
137 );
138 return;
139 }
140 $this->applyFilter('__ALL__', 'trim');
141
142 // title
143 $this->add('text', 'title', ts('Profile Name'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_UFGroup', 'title'), TRUE);
144
145
146 //add checkboxes
147 $uf_group_type = array();
148 $UFGroupType = CRM_Core_SelectValues::ufGroupTypes();
149 foreach ($UFGroupType as $key => $value) {
150 $uf_group_type[] = $this->createElement('checkbox', $key, NULL, $value);
151 }
152 $this->addGroup($uf_group_type, 'uf_group_type', ts('Used For'), '&nbsp;');
153
154 // help text
155 $this->addWysiwyg('help_pre', ts('Pre-form Help'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_UFGroup', 'help_post'));
156 $this->addWysiwyg('help_post', ts('Post-form Help'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_UFGroup', 'help_post'));
157
158 // weight
159 $this->add('text', 'weight', ts('Order'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_UFJoin', 'weight'), TRUE);
160 $this->addRule('weight', ts('is a numeric field'), 'numeric');
161
162 // is this group active ?
163 $this->addElement('checkbox', 'is_active', ts('Is this CiviCRM Profile active?'));
164
165 $paneNames = array('Advanced Settings' => 'buildAdvanceSetting');
166
167 foreach ($paneNames as $name => $type) {
168 if ($this->_id) {
169 $dataURL = "&reset=1&action=update&id={$this->_id}&snippet=4&formType={$type}";
170 }
171 else {
172 $dataURL = "&reset=1&action=add&snippet=4&formType={$type}";
173 }
174
175 $allPanes[$name] = array(
176 'url' => CRM_Utils_System::url('civicrm/admin/uf/group/setting',
177 $dataURL
178 ),
179 'open' => 'false',
180 'id' => $type,
181 );
182
183 CRM_UF_Form_AdvanceSetting::$type($this);
184 }
185
186 $this->addButtons(array(
187 array(
188 'type' => 'next',
189 'name' => ts('Save'),
190 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
191 'isDefault' => TRUE,
192 ),
193 array(
194 'type' => 'cancel',
195 'name' => ts('Cancel'),
196 ),
197 )
198 );
199
200 // views are implemented as frozen form
201 if ($this->_action & CRM_Core_Action::VIEW) {
202 $this->freeze();
203 $this->addElement('button', 'done', ts('Done'), array('onclick' => "location.href='civicrm/admin/uf/group?reset=1&action=browse'"));
204 }
205
206 $this->addFormRule(array('CRM_UF_Form_Group', 'formRule'), $this);
207 }
208
209 /**
210 * This function sets the default values for the form. Note that in edit/view mode
211 * the default values are retrieved from the database
212 *
213 * @access public
214 *
215 * @return void
216 */
217 function setDefaultValues() {
218 $defaults = array();
219 $showHide = new CRM_Core_ShowHideBlocks();
220
221 if ($this->_action == CRM_Core_Action::ADD) {
222 $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_UFJoin');
223 }
224
225 //id fetched for Dojo Pane
226 $pId = CRM_Utils_Request::retrieve('id', 'Positive', $this);
227 if (isset($pId)) {
228 $this->_id = $pId;
229 }
230
231 if ((isset($this->_id))) {
232
233 $defaults['weight'] = CRM_Core_BAO_UFGroup::getWeight($this->_id);
234
235 $params = array('id' => $this->_id);
236 CRM_Core_BAO_UFGroup::retrieve($params, $defaults);
237 $defaults['group'] = CRM_Utils_Array::value('limit_listings_group_id', $defaults);
238 $defaults['add_contact_to_group'] = CRM_Utils_Array::value('add_to_group_id', $defaults);
239 //get the uf join records for current uf group
240 $ufJoinRecords = CRM_Core_BAO_UFGroup::getUFJoinRecord($this->_id);
241 foreach ($ufJoinRecords as $key => $value) {
242 $checked[$value] = 1;
243 }
244 $defaults['uf_group_type'] = isset($checked) ? $checked : "";
245
246 //get the uf join records for current uf group other than default modules
247 $otherModules = array();
248 $otherModules = CRM_Core_BAO_UFGroup::getUFJoinRecord($this->_id, TRUE, TRUE);
249 if (!empty($otherModules)) {
250 $otherModuleString = NULL;
251 foreach ($otherModules as $key) {
252 $otherModuleString .= " [ x ] <label>" . $key . "</label>";
253 }
254 $this->assign('otherModuleString', $otherModuleString);
255 }
256
257 $showAdvanced = 0;
258 $advFields = array(
259 'group', 'post_URL', 'cancel_URL',
260 'add_captcha', 'is_map', 'is_uf_link', 'is_edit_link',
261 'is_update_dupe', 'is_cms_user', 'is_proximity_search',
262 );
263 foreach ($advFields as $key) {
264 if (!empty($defaults[$key])) {
265 $showAdvanced = 1;
266 $this->_allPanes['Advanced Settings']['open'] = 'true';
267 break;
268 }
269 }
270 }
271 else {
272 $defaults['is_active'] = 1;
273 $defaults['is_map'] = 0;
274 $defaults['is_update_dupe'] = 0;
275 $defaults['is_proximity_search'] = 0;
276 $defaults['uf_group_type[Profile]'] = 1;
277 }
278 // Don't assign showHide elements to template in DELETE mode (fields to be shown and hidden don't exist)
279 if (!($this->_action & CRM_Core_Action::DELETE) && !($this->_action & CRM_Core_Action::DISABLE)) {
280 $showHide->addToTemplate();
281 }
282 $this->assign('allPanes', $this->_allPanes);
283 return $defaults;
284 }
285
286 /**
287 * global form rule
288 *
289 * @param array $fields the input form values
290 * @param array $files the uploaded files if any
291 * @param array $self current form object.
292 *
293 * @return true if no errors, else array of errors
294 * @access public
295 * @static
296 */
297 static function formRule($fields, $files, $self) {
298 $errors = array();
299
300 //validate profile title as well as name.
301 $title = $fields['title'];
302 $name = CRM_Utils_String::munge($title, '_', 56);
303 $name .= $self->_id ? '_' . $self->_id : '';
304 $query = 'select count(*) from civicrm_uf_group where ( name like %1 ) and id != %2';
305 $pCnt = CRM_Core_DAO::singleValueQuery($query, array(1 => array($name, 'String'),
306 2 => array((int)$self->_id, 'Integer'),
307 ));
308 if ($pCnt) {
309 $errors['title'] = ts('Profile \'%1\' already exists in Database.', array(1 => $title));
310 }
311
312 return empty($errors) ? TRUE : $errors;
313 }
314
315 /**
316 * Process the form
317 *
318 * @return void
319 * @access public
320 */
321 public function postProcess() {
322 if ($this->_action & CRM_Core_Action::DELETE) {
323 $title = CRM_Core_BAO_UFGroup::getTitle($this->_id);
324 CRM_Core_BAO_UFGroup::del($this->_id);
325 CRM_Core_Session::setStatus(ts("Your CiviCRM Profile '%1' has been deleted.", array(1 => $title)), ts('Profile Deleted'), 'success');
326 }
327 elseif ($this->_action & CRM_Core_Action::DISABLE) {
328 $ufJoinParams = array('uf_group_id' => $this->_id);
329 CRM_Core_BAO_UFGroup::delUFJoin($ufJoinParams);
330
331 CRM_Core_BAO_UFGroup::setIsActive($this->_id, 0);
332 }
333 else {
334 // get the submitted form values.
335 $params = $ids = array();
336 $params = $this->controller->exportValues($this->_name);
337
338 if (!array_key_exists('is_active', $params)) {
339 $params['is_active'] = 0;
340 }
341
342 if ($this->_action & (CRM_Core_Action::UPDATE)) {
343 $ids['ufgroup'] = $this->_id;
344 // CRM-5284
345 // lets skip trying to mess around with profile weights and allow the user to do as needed.
346 }
347 elseif ($this->_action & CRM_Core_Action::ADD) {
348 $session = CRM_Core_Session::singleton();
349 $params['created_id'] = $session->get('userID');
350 $params['created_date'] = date('YmdHis');
351 }
352
353 // create uf group
354 $ufGroup = CRM_Core_BAO_UFGroup::add($params, $ids);
355
356 if (CRM_Utils_Array::value('is_active', $params)) {
357 //make entry in uf join table
358 CRM_Core_BAO_UFGroup::createUFJoin($params, $ufGroup->id);
359 }
360 elseif ($this->_id) {
361 // this profile has been set to inactive, delete all corresponding UF Join's
362 $ufJoinParams = array('uf_group_id' => $this->_id);
363 CRM_Core_BAO_UFGroup::delUFJoin($ufJoinParams);
364 }
365
366 if ($this->_action & CRM_Core_Action::UPDATE) {
367 $url = CRM_Utils_System::url('civicrm/admin/uf/group', 'reset=1&action=browse');
368 CRM_Core_Session::setStatus(ts("Your CiviCRM Profile '%1' has been saved.", array(1 => $ufGroup->title)), ts('Profile Saved'), 'success');
369 }
370 else {
371 $url = CRM_Utils_System::url('civicrm/admin/uf/group/field/add', 'reset=1&action=add&gid=' . $ufGroup->id);
372 CRM_Core_Session::setStatus(ts('Your CiviCRM Profile \'%1\' has been added. You can add fields to this profile now.',
373 array(1 => $ufGroup->title)
374 ), ts('Profile Added'), 'success');
375 }
376 $session = CRM_Core_Session::singleton();
377 $session->replaceUserContext($url);
378 }
379
380 // update cms integration with registration / my account
381 CRM_Utils_System::updateCategories();
382 }
383 }
384