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