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 ? | |
8c9c5742 | 202 | $this->addElement('advcheckbox', 'is_active', ts('Is this CiviCRM Profile active?')); |
6a488035 | 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 | 237 | |
927f5956 | 238 | $this->addFormRule(['CRM_UF_Form_Group', 'formRule'], $this); |
6a488035 TO |
239 | } |
240 | ||
241 | /** | |
c490a46a | 242 | * Set default values for the form. Note that in edit/view mode |
6a488035 TO |
243 | * the default values are retrieved from the database |
244 | * | |
6a488035 TO |
245 | * |
246 | * @return void | |
247 | */ | |
00be9182 | 248 | public function setDefaultValues() { |
927f5956 | 249 | $defaults = []; |
6a488035 TO |
250 | $showHide = new CRM_Core_ShowHideBlocks(); |
251 | ||
252 | if ($this->_action == CRM_Core_Action::ADD) { | |
253 | $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_UFJoin'); | |
254 | } | |
255 | ||
256 | //id fetched for Dojo Pane | |
257 | $pId = CRM_Utils_Request::retrieve('id', 'Positive', $this); | |
258 | if (isset($pId)) { | |
259 | $this->_id = $pId; | |
260 | } | |
261 | ||
262 | if ((isset($this->_id))) { | |
263 | ||
264 | $defaults['weight'] = CRM_Core_BAO_UFGroup::getWeight($this->_id); | |
265 | ||
927f5956 | 266 | $params = ['id' => $this->_id]; |
6a488035 | 267 | CRM_Core_BAO_UFGroup::retrieve($params, $defaults); |
9c1bc317 CW |
268 | $defaults['group'] = $defaults['limit_listings_group_id'] ?? NULL; |
269 | $defaults['add_contact_to_group'] = $defaults['add_to_group_id'] ?? NULL; | |
6a488035 TO |
270 | //get the uf join records for current uf group |
271 | $ufJoinRecords = CRM_Core_BAO_UFGroup::getUFJoinRecord($this->_id); | |
272 | foreach ($ufJoinRecords as $key => $value) { | |
273 | $checked[$value] = 1; | |
274 | } | |
2e1f50d6 | 275 | $defaults['uf_group_type'] = $checked ?? ""; |
6a488035 | 276 | |
6a488035 | 277 | $showAdvanced = 0; |
927f5956 | 278 | $advFields = [ |
353ffa53 TO |
279 | 'group', |
280 | 'post_URL', | |
281 | 'cancel_URL', | |
282 | 'add_captcha', | |
283 | 'is_map', | |
284 | 'is_uf_link', | |
285 | 'is_edit_link', | |
286 | 'is_update_dupe', | |
287 | 'is_cms_user', | |
288 | 'is_proximity_search', | |
927f5956 | 289 | ]; |
6a488035 TO |
290 | foreach ($advFields as $key) { |
291 | if (!empty($defaults[$key])) { | |
292 | $showAdvanced = 1; | |
293 | $this->_allPanes['Advanced Settings']['open'] = 'true'; | |
294 | break; | |
295 | } | |
296 | } | |
297 | } | |
298 | else { | |
b3f1028c | 299 | $defaults['add_cancel_button'] = 1; |
6a488035 TO |
300 | $defaults['is_active'] = 1; |
301 | $defaults['is_map'] = 0; | |
302 | $defaults['is_update_dupe'] = 0; | |
303 | $defaults['is_proximity_search'] = 0; | |
6a488035 TO |
304 | } |
305 | // Don't assign showHide elements to template in DELETE mode (fields to be shown and hidden don't exist) | |
306 | if (!($this->_action & CRM_Core_Action::DELETE) && !($this->_action & CRM_Core_Action::DISABLE)) { | |
307 | $showHide->addToTemplate(); | |
308 | } | |
309 | $this->assign('allPanes', $this->_allPanes); | |
310 | return $defaults; | |
311 | } | |
312 | ||
313 | /** | |
fe482240 | 314 | * Global form rule. |
6a488035 | 315 | * |
5ce1712d TO |
316 | * @param array $fields |
317 | * The input form values. | |
318 | * @param array $files | |
319 | * The uploaded files if any. | |
e0f5b841 | 320 | * @param self $self |
5ce1712d | 321 | * Current form object. |
6a488035 | 322 | * |
72b3a70c CW |
323 | * @return bool|array |
324 | * true if no errors, else array of errors | |
6a488035 | 325 | */ |
00be9182 | 326 | public static function formRule($fields, $files, $self) { |
927f5956 | 327 | $errors = []; |
6a488035 TO |
328 | |
329 | //validate profile title as well as name. | |
330 | $title = $fields['title']; | |
331 | $name = CRM_Utils_String::munge($title, '_', 56); | |
332 | $name .= $self->_id ? '_' . $self->_id : ''; | |
333 | $query = 'select count(*) from civicrm_uf_group where ( name like %1 ) and id != %2'; | |
927f5956 | 334 | $pCnt = CRM_Core_DAO::singleValueQuery($query, [ |
335 | 1 => [$name, 'String'], | |
336 | 2 => [(int) $self->_id, 'Integer'], | |
337 | ]); | |
6a488035 | 338 | if ($pCnt) { |
927f5956 | 339 | $errors['title'] = ts('Profile \'%1\' already exists in Database.', [1 => $title]); |
6a488035 TO |
340 | } |
341 | ||
342 | return empty($errors) ? TRUE : $errors; | |
343 | } | |
344 | ||
345 | /** | |
fe482240 | 346 | * Process the form. |
6a488035 TO |
347 | * |
348 | * @return void | |
6a488035 TO |
349 | */ |
350 | public function postProcess() { | |
351 | if ($this->_action & CRM_Core_Action::DELETE) { | |
352 | $title = CRM_Core_BAO_UFGroup::getTitle($this->_id); | |
353 | CRM_Core_BAO_UFGroup::del($this->_id); | |
927f5956 | 354 | CRM_Core_Session::setStatus(ts("Your CiviCRM Profile '%1' has been deleted.", [1 => $title]), ts('Profile Deleted'), 'success'); |
6a488035 TO |
355 | } |
356 | elseif ($this->_action & CRM_Core_Action::DISABLE) { | |
927f5956 | 357 | $ufJoinParams = ['uf_group_id' => $this->_id]; |
6a488035 TO |
358 | CRM_Core_BAO_UFGroup::delUFJoin($ufJoinParams); |
359 | ||
360 | CRM_Core_BAO_UFGroup::setIsActive($this->_id, 0); | |
361 | } | |
362 | else { | |
363 | // get the submitted form values. | |
6a488035 | 364 | $params = $this->controller->exportValues($this->_name); |
6a488035 | 365 | if ($this->_action & (CRM_Core_Action::UPDATE)) { |
e7de4124 | 366 | $params['id'] = $this->_id; |
6a488035 TO |
367 | // CRM-5284 |
368 | // lets skip trying to mess around with profile weights and allow the user to do as needed. | |
369 | } | |
370 | elseif ($this->_action & CRM_Core_Action::ADD) { | |
371 | $session = CRM_Core_Session::singleton(); | |
372 | $params['created_id'] = $session->get('userID'); | |
373 | $params['created_date'] = date('YmdHis'); | |
374 | } | |
375 | ||
376 | // create uf group | |
e7de4124 | 377 | $ufGroup = CRM_Core_BAO_UFGroup::add($params); |
6a488035 | 378 | |
a7488080 | 379 | if (!empty($params['is_active'])) { |
6a488035 | 380 | //make entry in uf join table |
259ab031 | 381 | CRM_Core_BAO_UFGroup::createUFJoin($params['weight'], $params['uf_group_type'] ?? [], $ufGroup->id); |
6a488035 TO |
382 | } |
383 | elseif ($this->_id) { | |
384 | // this profile has been set to inactive, delete all corresponding UF Join's | |
927f5956 | 385 | $ufJoinParams = ['uf_group_id' => $this->_id]; |
6a488035 TO |
386 | CRM_Core_BAO_UFGroup::delUFJoin($ufJoinParams); |
387 | } | |
388 | ||
389 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
390 | $url = CRM_Utils_System::url('civicrm/admin/uf/group', 'reset=1&action=browse'); | |
927f5956 | 391 | CRM_Core_Session::setStatus(ts("Your CiviCRM Profile '%1' has been saved.", [1 => $ufGroup->title]), ts('Profile Saved'), 'success'); |
6a488035 TO |
392 | } |
393 | else { | |
704f21c0 CW |
394 | // Jump directly to adding a field if popups are disabled |
395 | $action = CRM_Core_Resources::singleton()->ajaxPopupsEnabled ? '' : '/add'; | |
6f231148 | 396 | $url = CRM_Utils_System::url("civicrm/admin/uf/group/field$action", 'reset=1&new=1&gid=' . $ufGroup->id . '&action=' . ($action ? 'add' : 'browse')); |
6a488035 | 397 | CRM_Core_Session::setStatus(ts('Your CiviCRM Profile \'%1\' has been added. You can add fields to this profile now.', |
927f5956 | 398 | [1 => $ufGroup->title] |
353ffa53 | 399 | ), ts('Profile Added'), 'success'); |
6a488035 TO |
400 | } |
401 | $session = CRM_Core_Session::singleton(); | |
402 | $session->replaceUserContext($url); | |
403 | } | |
404 | ||
405 | // update cms integration with registration / my account | |
406 | CRM_Utils_System::updateCategories(); | |
407 | } | |
96025800 | 408 | |
7f3c8b3d | 409 | /** |
410 | * Set the delete message. | |
411 | * | |
412 | * We do this from the constructor in order to do a translation. | |
413 | */ | |
927f5956 | 414 | public function setDeleteMessage() { |
415 | } | |
7f3c8b3d | 416 | |
cbd71f73 | 417 | /** |
418 | * Get the string to display next to the used for field indicating unchangeable uses. | |
419 | * | |
420 | * @return string | |
421 | */ | |
422 | protected function getOtherModuleString() { | |
423 | $otherModules = CRM_Core_BAO_UFGroup::getUFJoinRecord($this->_id, TRUE, TRUE); | |
16a2d251 | 424 | $otherModuleString = NULL; |
cbd71f73 | 425 | if (!empty($otherModules)) { |
cbd71f73 | 426 | foreach ($otherModules as $key) { |
427 | $otherModuleString .= " [ x ] <label>" . $key . "</label>"; | |
428 | } | |
429 | } | |
430 | return $otherModuleString; | |
431 | } | |
432 | ||
6a488035 | 433 | } |