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