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