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