Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
7f3c8b3d | 19 | * This class is for UF Group (Profile) configuration. |
6a488035 TO |
20 | */ |
21 | class CRM_UF_Form_Group extends CRM_Core_Form { | |
22 | ||
7f3c8b3d | 23 | use CRM_Core_Form_EntityFormTrait; |
24 | ||
25 | /** | |
26 | * Fields for the entity to be assigned to the template. | |
27 | * | |
28 | * Fields may have keys | |
29 | * - name (required to show in tpl from the array) | |
30 | * - description (optional, will appear below the field) | |
31 | * - not-auto-addable - this class will not attempt to add the field using addField. | |
32 | * (this will be automatically set if the field does not have html in it's metadata | |
33 | * or is not a core field on the form's entity). | |
34 | * - help (option) add help to the field - e.g ['id' => 'id-source', 'file' => 'CRM/Contact/Form/Contact']] | |
35 | * - template - use a field specific template to render this field | |
36 | * - required | |
37 | * - is_freeze (field should be frozen). | |
38 | * | |
39 | * @var array | |
40 | */ | |
41 | protected $entityFields = []; | |
42 | ||
154975ba PN |
43 | /** |
44 | * Deletion message to be assigned to the form. | |
45 | * | |
46 | * @var string | |
47 | */ | |
48 | protected $deleteMessage; | |
49 | ||
88aae6d4 A |
50 | /** |
51 | * @var bool | |
52 | */ | |
53 | public $submitOnce = TRUE; | |
54 | ||
7f3c8b3d | 55 | /** |
56 | * Set entity fields to be assigned to the form. | |
57 | */ | |
58 | protected function setEntityFields() { | |
59 | $this->entityFields = [ | |
60 | 'title' => ['name' => 'title'], | |
61 | 'frontend_title' => ['name' => 'frontend_title'], | |
927f5956 | 62 | 'description' => [ |
63 | 'name' => 'description', | |
64 | 'help' => ['id' => 'id-description', 'file' => 'CRM/UF/Form/Group.hlp'], | |
65 | ], | |
66 | 'uf_group_type' => [ | |
67 | 'name' => 'uf_group_type', | |
68 | 'not-auto-addable' => TRUE, | |
69 | 'help' => ['id' => 'id-used_for', 'file' => 'CRM/UF/Form/Group.hlp'], | |
70 | 'post_html_text' => ' ' . $this->getOtherModuleString(), | |
71 | ], | |
72 | 'cancel_button_text' => [ | |
73 | 'name' => 'cancel_button_text', | |
74 | 'help' => [ | |
75 | 'id' => 'id-cancel_button_text', | |
76 | 'file' => 'CRM/UF/Form/Group.hlp', | |
77 | ], | |
78 | 'class' => 'cancel_button_section', | |
79 | ], | |
80 | 'submit_button_text' => [ | |
81 | 'name' => 'submit_button_text', | |
82 | 'help' => [ | |
83 | 'id' => 'id-submit_button_text', | |
84 | 'file' => 'CRM/UF/Form/Group.hlp', | |
85 | ], | |
86 | 'class' => '', | |
87 | ], | |
7f3c8b3d | 88 | ]; |
89 | } | |
90 | ||
91 | /** | |
92 | * Explicitly declare the entity api name. | |
93 | */ | |
94 | public function getDefaultEntity() { | |
95 | return 'UFGroup'; | |
96 | } | |
97 | ||
6a488035 | 98 | /** |
fe482240 | 99 | * The title for group. |
6a488035 TO |
100 | * |
101 | * @var int | |
6a488035 TO |
102 | */ |
103 | protected $_title; | |
104 | protected $_groupElement; | |
105 | protected $_group; | |
106 | protected $_allPanes; | |
107 | ||
108 | /** | |
fe482240 | 109 | * Set variables up before form is built. |
6a488035 TO |
110 | */ |
111 | public function preProcess() { | |
112 | // current form id | |
113 | $this->_id = $this->get('id'); | |
114 | if (!$this->_id) { | |
115 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, 0); | |
116 | } | |
117 | $this->assign('gid', $this->_id); | |
118 | $this->_group = CRM_Core_PseudoConstant::group(); | |
119 | ||
7df37017 RN |
120 | if ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::DELETE)) { |
121 | $title = CRM_Core_BAO_UFGroup::getTitle($this->_id); | |
122 | $this->assign('profileTitle', $title); | |
123 | } | |
124 | ||
6a488035 TO |
125 | // setting title for html page |
126 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
203b4734 | 127 | $this->setTitle(ts('Profile Settings') . " - $title"); |
6a488035 TO |
128 | } |
129 | elseif ($this->_action & (CRM_Core_Action::DISABLE | CRM_Core_Action::DELETE)) { | |
130 | $ufGroup['module'] = implode(' , ', CRM_Core_BAO_UFGroup::getUFJoinRecord($this->_id, TRUE)); | |
353ffa53 TO |
131 | $status = 0; |
132 | $status = CRM_Core_BAO_UFGroup::usedByModule($this->_id); | |
6a488035 TO |
133 | if ($this->_action & (CRM_Core_Action::DISABLE)) { |
134 | if ($status) { | |
135 | $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?'; | |
136 | } | |
137 | else { | |
138 | $message = 'Are you sure you want to disable this Profile?'; | |
139 | } | |
140 | } | |
141 | else { | |
142 | if ($status) { | |
143 | $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?'; | |
144 | } | |
145 | else { | |
146 | $message = 'Are you sure you want to delete this Profile? This action cannot be undone.'; | |
147 | } | |
148 | } | |
149 | $this->assign('message', $message); | |
150 | } | |
151 | else { | |
203b4734 | 152 | $this->setTitle(ts('New CiviCRM Profile')); |
6a488035 TO |
153 | } |
154 | } | |
155 | ||
156 | /** | |
fe482240 | 157 | * Build the form object. |
6a488035 TO |
158 | * |
159 | * @return void | |
6a488035 TO |
160 | */ |
161 | public function buildQuickForm() { | |
7f3c8b3d | 162 | self::buildQuickEntityForm(); |
6a488035 TO |
163 | if ($this->_action & (CRM_Core_Action::DISABLE | CRM_Core_Action::DELETE)) { |
164 | if ($this->_action & (CRM_Core_Action::DISABLE)) { | |
165 | $display = 'Disable Profile'; | |
166 | } | |
167 | else { | |
168 | $display = 'Delete Profile'; | |
169 | } | |
927f5956 | 170 | $this->addButtons([ |
171 | [ | |
c5c263ca AH |
172 | 'type' => 'next', |
173 | 'name' => $display, | |
174 | 'spacing' => ' ', | |
175 | 'isDefault' => TRUE, | |
927f5956 | 176 | ], |
177 | [ | |
c5c263ca AH |
178 | 'type' => 'cancel', |
179 | 'name' => ts('Cancel'), | |
927f5956 | 180 | ], |
181 | ]); | |
6a488035 TO |
182 | return; |
183 | } | |
6a488035 | 184 | |
6a488035 | 185 | //add checkboxes |
927f5956 | 186 | $uf_group_type = []; |
6a488035 TO |
187 | $UFGroupType = CRM_Core_SelectValues::ufGroupTypes(); |
188 | foreach ($UFGroupType as $key => $value) { | |
189 | $uf_group_type[] = $this->createElement('checkbox', $key, NULL, $value); | |
190 | } | |
191 | $this->addGroup($uf_group_type, 'uf_group_type', ts('Used For'), ' '); | |
192 | ||
193 | // help text | |
5d51a2f9 CW |
194 | $this->add('wysiwyg', 'help_pre', ts('Pre-form Help'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_UFGroup', 'help_post')); |
195 | $this->add('wysiwyg', 'help_post', ts('Post-form Help'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_UFGroup', 'help_post')); | |
6a488035 TO |
196 | |
197 | // weight | |
f20d2b0d | 198 | $this->add('number', 'weight', ts('Order'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_UFJoin', 'weight'), TRUE); |
6a488035 TO |
199 | $this->addRule('weight', ts('is a numeric field'), 'numeric'); |
200 | ||
201 | // is this group active ? | |
202 | $this->addElement('checkbox', 'is_active', ts('Is this CiviCRM Profile active?')); | |
203 | ||
c995124e BT |
204 | $paneNames = [ |
205 | ts('Advanced Settings') => 'buildAdvanceSetting', | |
206 | ]; | |
6a488035 TO |
207 | |
208 | foreach ($paneNames as $name => $type) { | |
209 | if ($this->_id) { | |
210 | $dataURL = "&reset=1&action=update&id={$this->_id}&snippet=4&formType={$type}"; | |
211 | } | |
212 | else { | |
213 | $dataURL = "&reset=1&action=add&snippet=4&formType={$type}"; | |
214 | } | |
215 | ||
927f5956 | 216 | $allPanes[$name] = [ |
217 | 'url' => CRM_Utils_System::url('civicrm/admin/uf/group/setting', $dataURL), | |
6a488035 TO |
218 | 'open' => 'false', |
219 | 'id' => $type, | |
927f5956 | 220 | ]; |
6a488035 | 221 | |
0e6e8724 | 222 | CRM_UF_Form_AdvanceSetting::$type($this); |
6a488035 TO |
223 | } |
224 | ||
927f5956 | 225 | $this->addButtons([ |
226 | [ | |
c5c263ca AH |
227 | 'type' => 'next', |
228 | 'name' => ts('Save'), | |
229 | 'spacing' => ' ', | |
230 | 'isDefault' => TRUE, | |
927f5956 | 231 | ], |
232 | [ | |
c5c263ca AH |
233 | 'type' => 'cancel', |
234 | 'name' => ts('Cancel'), | |
927f5956 | 235 | ], |
236 | ]); | |
6a488035 TO |
237 | |
238 | // views are implemented as frozen form | |
239 | if ($this->_action & CRM_Core_Action::VIEW) { | |
240 | $this->freeze(); | |
cbd83dde AH |
241 | $this->addElement('xbutton', 'done', ts('Done'), [ |
242 | 'type' => 'button', | |
243 | 'onclick' => "location.href='civicrm/admin/uf/group?reset=1&action=browse'", | |
244 | ]); | |
6a488035 TO |
245 | } |
246 | ||
927f5956 | 247 | $this->addFormRule(['CRM_UF_Form_Group', 'formRule'], $this); |
6a488035 TO |
248 | } |
249 | ||
250 | /** | |
c490a46a | 251 | * Set default values for the form. Note that in edit/view mode |
6a488035 TO |
252 | * the default values are retrieved from the database |
253 | * | |
6a488035 TO |
254 | * |
255 | * @return void | |
256 | */ | |
00be9182 | 257 | public function setDefaultValues() { |
927f5956 | 258 | $defaults = []; |
6a488035 TO |
259 | $showHide = new CRM_Core_ShowHideBlocks(); |
260 | ||
261 | if ($this->_action == CRM_Core_Action::ADD) { | |
262 | $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_UFJoin'); | |
263 | } | |
264 | ||
265 | //id fetched for Dojo Pane | |
266 | $pId = CRM_Utils_Request::retrieve('id', 'Positive', $this); | |
267 | if (isset($pId)) { | |
268 | $this->_id = $pId; | |
269 | } | |
270 | ||
271 | if ((isset($this->_id))) { | |
272 | ||
273 | $defaults['weight'] = CRM_Core_BAO_UFGroup::getWeight($this->_id); | |
274 | ||
927f5956 | 275 | $params = ['id' => $this->_id]; |
6a488035 | 276 | CRM_Core_BAO_UFGroup::retrieve($params, $defaults); |
9c1bc317 CW |
277 | $defaults['group'] = $defaults['limit_listings_group_id'] ?? NULL; |
278 | $defaults['add_contact_to_group'] = $defaults['add_to_group_id'] ?? NULL; | |
6a488035 TO |
279 | //get the uf join records for current uf group |
280 | $ufJoinRecords = CRM_Core_BAO_UFGroup::getUFJoinRecord($this->_id); | |
281 | foreach ($ufJoinRecords as $key => $value) { | |
282 | $checked[$value] = 1; | |
283 | } | |
2e1f50d6 | 284 | $defaults['uf_group_type'] = $checked ?? ""; |
6a488035 | 285 | |
6a488035 | 286 | $showAdvanced = 0; |
927f5956 | 287 | $advFields = [ |
353ffa53 TO |
288 | 'group', |
289 | 'post_URL', | |
290 | 'cancel_URL', | |
291 | 'add_captcha', | |
292 | 'is_map', | |
293 | 'is_uf_link', | |
294 | 'is_edit_link', | |
295 | 'is_update_dupe', | |
296 | 'is_cms_user', | |
297 | 'is_proximity_search', | |
927f5956 | 298 | ]; |
6a488035 TO |
299 | foreach ($advFields as $key) { |
300 | if (!empty($defaults[$key])) { | |
301 | $showAdvanced = 1; | |
302 | $this->_allPanes['Advanced Settings']['open'] = 'true'; | |
303 | break; | |
304 | } | |
305 | } | |
306 | } | |
307 | else { | |
b3f1028c | 308 | $defaults['add_cancel_button'] = 1; |
6a488035 TO |
309 | $defaults['is_active'] = 1; |
310 | $defaults['is_map'] = 0; | |
311 | $defaults['is_update_dupe'] = 0; | |
312 | $defaults['is_proximity_search'] = 0; | |
6a488035 TO |
313 | } |
314 | // Don't assign showHide elements to template in DELETE mode (fields to be shown and hidden don't exist) | |
315 | if (!($this->_action & CRM_Core_Action::DELETE) && !($this->_action & CRM_Core_Action::DISABLE)) { | |
316 | $showHide->addToTemplate(); | |
317 | } | |
318 | $this->assign('allPanes', $this->_allPanes); | |
319 | return $defaults; | |
320 | } | |
321 | ||
322 | /** | |
fe482240 | 323 | * Global form rule. |
6a488035 | 324 | * |
5ce1712d TO |
325 | * @param array $fields |
326 | * The input form values. | |
327 | * @param array $files | |
328 | * The uploaded files if any. | |
329 | * @param array $self | |
330 | * Current form object. | |
6a488035 | 331 | * |
72b3a70c CW |
332 | * @return bool|array |
333 | * true if no errors, else array of errors | |
6a488035 | 334 | */ |
00be9182 | 335 | public static function formRule($fields, $files, $self) { |
927f5956 | 336 | $errors = []; |
6a488035 TO |
337 | |
338 | //validate profile title as well as name. | |
339 | $title = $fields['title']; | |
340 | $name = CRM_Utils_String::munge($title, '_', 56); | |
341 | $name .= $self->_id ? '_' . $self->_id : ''; | |
342 | $query = 'select count(*) from civicrm_uf_group where ( name like %1 ) and id != %2'; | |
927f5956 | 343 | $pCnt = CRM_Core_DAO::singleValueQuery($query, [ |
344 | 1 => [$name, 'String'], | |
345 | 2 => [(int) $self->_id, 'Integer'], | |
346 | ]); | |
6a488035 | 347 | if ($pCnt) { |
927f5956 | 348 | $errors['title'] = ts('Profile \'%1\' already exists in Database.', [1 => $title]); |
6a488035 TO |
349 | } |
350 | ||
351 | return empty($errors) ? TRUE : $errors; | |
352 | } | |
353 | ||
354 | /** | |
fe482240 | 355 | * Process the form. |
6a488035 TO |
356 | * |
357 | * @return void | |
6a488035 TO |
358 | */ |
359 | public function postProcess() { | |
360 | if ($this->_action & CRM_Core_Action::DELETE) { | |
361 | $title = CRM_Core_BAO_UFGroup::getTitle($this->_id); | |
362 | CRM_Core_BAO_UFGroup::del($this->_id); | |
927f5956 | 363 | CRM_Core_Session::setStatus(ts("Your CiviCRM Profile '%1' has been deleted.", [1 => $title]), ts('Profile Deleted'), 'success'); |
6a488035 TO |
364 | } |
365 | elseif ($this->_action & CRM_Core_Action::DISABLE) { | |
927f5956 | 366 | $ufJoinParams = ['uf_group_id' => $this->_id]; |
6a488035 TO |
367 | CRM_Core_BAO_UFGroup::delUFJoin($ufJoinParams); |
368 | ||
369 | CRM_Core_BAO_UFGroup::setIsActive($this->_id, 0); | |
370 | } | |
371 | else { | |
372 | // get the submitted form values. | |
6a488035 TO |
373 | $params = $this->controller->exportValues($this->_name); |
374 | ||
375 | if (!array_key_exists('is_active', $params)) { | |
376 | $params['is_active'] = 0; | |
377 | } | |
378 | ||
379 | if ($this->_action & (CRM_Core_Action::UPDATE)) { | |
e7de4124 | 380 | $params['id'] = $this->_id; |
6a488035 TO |
381 | // CRM-5284 |
382 | // lets skip trying to mess around with profile weights and allow the user to do as needed. | |
383 | } | |
384 | elseif ($this->_action & CRM_Core_Action::ADD) { | |
385 | $session = CRM_Core_Session::singleton(); | |
386 | $params['created_id'] = $session->get('userID'); | |
387 | $params['created_date'] = date('YmdHis'); | |
388 | } | |
389 | ||
390 | // create uf group | |
e7de4124 | 391 | $ufGroup = CRM_Core_BAO_UFGroup::add($params); |
6a488035 | 392 | |
a7488080 | 393 | if (!empty($params['is_active'])) { |
6a488035 | 394 | //make entry in uf join table |
259ab031 | 395 | CRM_Core_BAO_UFGroup::createUFJoin($params['weight'], $params['uf_group_type'] ?? [], $ufGroup->id); |
6a488035 TO |
396 | } |
397 | elseif ($this->_id) { | |
398 | // this profile has been set to inactive, delete all corresponding UF Join's | |
927f5956 | 399 | $ufJoinParams = ['uf_group_id' => $this->_id]; |
6a488035 TO |
400 | CRM_Core_BAO_UFGroup::delUFJoin($ufJoinParams); |
401 | } | |
402 | ||
403 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
404 | $url = CRM_Utils_System::url('civicrm/admin/uf/group', 'reset=1&action=browse'); | |
927f5956 | 405 | CRM_Core_Session::setStatus(ts("Your CiviCRM Profile '%1' has been saved.", [1 => $ufGroup->title]), ts('Profile Saved'), 'success'); |
6a488035 TO |
406 | } |
407 | else { | |
704f21c0 CW |
408 | // Jump directly to adding a field if popups are disabled |
409 | $action = CRM_Core_Resources::singleton()->ajaxPopupsEnabled ? '' : '/add'; | |
6f231148 | 410 | $url = CRM_Utils_System::url("civicrm/admin/uf/group/field$action", 'reset=1&new=1&gid=' . $ufGroup->id . '&action=' . ($action ? 'add' : 'browse')); |
6a488035 | 411 | CRM_Core_Session::setStatus(ts('Your CiviCRM Profile \'%1\' has been added. You can add fields to this profile now.', |
927f5956 | 412 | [1 => $ufGroup->title] |
353ffa53 | 413 | ), ts('Profile Added'), 'success'); |
6a488035 TO |
414 | } |
415 | $session = CRM_Core_Session::singleton(); | |
416 | $session->replaceUserContext($url); | |
417 | } | |
418 | ||
419 | // update cms integration with registration / my account | |
420 | CRM_Utils_System::updateCategories(); | |
421 | } | |
96025800 | 422 | |
7f3c8b3d | 423 | /** |
424 | * Set the delete message. | |
425 | * | |
426 | * We do this from the constructor in order to do a translation. | |
427 | */ | |
927f5956 | 428 | public function setDeleteMessage() { |
429 | } | |
7f3c8b3d | 430 | |
cbd71f73 | 431 | /** |
432 | * Get the string to display next to the used for field indicating unchangeable uses. | |
433 | * | |
434 | * @return string | |
435 | */ | |
436 | protected function getOtherModuleString() { | |
437 | $otherModules = CRM_Core_BAO_UFGroup::getUFJoinRecord($this->_id, TRUE, TRUE); | |
16a2d251 | 438 | $otherModuleString = NULL; |
cbd71f73 | 439 | if (!empty($otherModules)) { |
cbd71f73 | 440 | foreach ($otherModules as $key) { |
441 | $otherModuleString .= " [ x ] <label>" . $key . "</label>"; | |
442 | } | |
443 | } | |
444 | return $otherModuleString; | |
445 | } | |
446 | ||
6a488035 | 447 | } |