dev/core#2372 Enable $submitOnce for some forms
[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
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) {
6a488035
TO
127 CRM_Utils_System::setTitle(ts('Profile Settings') . " - $title");
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 {
152 CRM_Utils_System::setTitle(ts('New CiviCRM Profile'));
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' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
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'), '&nbsp;');
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
927f5956 204 $paneNames = ['Advanced Settings' => 'buildAdvanceSetting'];
6a488035
TO
205
206 foreach ($paneNames as $name => $type) {
207 if ($this->_id) {
208 $dataURL = "&reset=1&action=update&id={$this->_id}&snippet=4&formType={$type}";
209 }
210 else {
211 $dataURL = "&reset=1&action=add&snippet=4&formType={$type}";
212 }
213
927f5956 214 $allPanes[$name] = [
215 'url' => CRM_Utils_System::url('civicrm/admin/uf/group/setting', $dataURL),
6a488035
TO
216 'open' => 'false',
217 'id' => $type,
927f5956 218 ];
6a488035 219
0e6e8724 220 CRM_UF_Form_AdvanceSetting::$type($this);
6a488035
TO
221 }
222
927f5956 223 $this->addButtons([
224 [
c5c263ca
AH
225 'type' => 'next',
226 'name' => ts('Save'),
227 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
228 'isDefault' => TRUE,
927f5956 229 ],
230 [
c5c263ca
AH
231 'type' => 'cancel',
232 'name' => ts('Cancel'),
927f5956 233 ],
234 ]);
6a488035
TO
235
236 // views are implemented as frozen form
237 if ($this->_action & CRM_Core_Action::VIEW) {
238 $this->freeze();
cbd83dde
AH
239 $this->addElement('xbutton', 'done', ts('Done'), [
240 'type' => 'button',
241 'onclick' => "location.href='civicrm/admin/uf/group?reset=1&action=browse'",
242 ]);
6a488035
TO
243 }
244
927f5956 245 $this->addFormRule(['CRM_UF_Form_Group', 'formRule'], $this);
6a488035
TO
246 }
247
248 /**
c490a46a 249 * Set default values for the form. Note that in edit/view mode
6a488035
TO
250 * the default values are retrieved from the database
251 *
6a488035
TO
252 *
253 * @return void
254 */
00be9182 255 public function setDefaultValues() {
927f5956 256 $defaults = [];
6a488035
TO
257 $showHide = new CRM_Core_ShowHideBlocks();
258
259 if ($this->_action == CRM_Core_Action::ADD) {
260 $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_UFJoin');
261 }
262
263 //id fetched for Dojo Pane
264 $pId = CRM_Utils_Request::retrieve('id', 'Positive', $this);
265 if (isset($pId)) {
266 $this->_id = $pId;
267 }
268
269 if ((isset($this->_id))) {
270
271 $defaults['weight'] = CRM_Core_BAO_UFGroup::getWeight($this->_id);
272
927f5956 273 $params = ['id' => $this->_id];
6a488035 274 CRM_Core_BAO_UFGroup::retrieve($params, $defaults);
9c1bc317
CW
275 $defaults['group'] = $defaults['limit_listings_group_id'] ?? NULL;
276 $defaults['add_contact_to_group'] = $defaults['add_to_group_id'] ?? NULL;
6a488035
TO
277 //get the uf join records for current uf group
278 $ufJoinRecords = CRM_Core_BAO_UFGroup::getUFJoinRecord($this->_id);
279 foreach ($ufJoinRecords as $key => $value) {
280 $checked[$value] = 1;
281 }
2e1f50d6 282 $defaults['uf_group_type'] = $checked ?? "";
6a488035 283
6a488035 284 $showAdvanced = 0;
927f5956 285 $advFields = [
353ffa53
TO
286 'group',
287 'post_URL',
288 'cancel_URL',
289 'add_captcha',
290 'is_map',
291 'is_uf_link',
292 'is_edit_link',
293 'is_update_dupe',
294 'is_cms_user',
295 'is_proximity_search',
927f5956 296 ];
6a488035
TO
297 foreach ($advFields as $key) {
298 if (!empty($defaults[$key])) {
299 $showAdvanced = 1;
300 $this->_allPanes['Advanced Settings']['open'] = 'true';
301 break;
302 }
303 }
304 }
305 else {
b3f1028c 306 $defaults['add_cancel_button'] = 1;
6a488035
TO
307 $defaults['is_active'] = 1;
308 $defaults['is_map'] = 0;
309 $defaults['is_update_dupe'] = 0;
310 $defaults['is_proximity_search'] = 0;
6a488035
TO
311 }
312 // Don't assign showHide elements to template in DELETE mode (fields to be shown and hidden don't exist)
313 if (!($this->_action & CRM_Core_Action::DELETE) && !($this->_action & CRM_Core_Action::DISABLE)) {
314 $showHide->addToTemplate();
315 }
316 $this->assign('allPanes', $this->_allPanes);
317 return $defaults;
318 }
319
320 /**
fe482240 321 * Global form rule.
6a488035 322 *
5ce1712d
TO
323 * @param array $fields
324 * The input form values.
325 * @param array $files
326 * The uploaded files if any.
327 * @param array $self
328 * Current form object.
6a488035 329 *
72b3a70c
CW
330 * @return bool|array
331 * true if no errors, else array of errors
6a488035 332 */
00be9182 333 public static function formRule($fields, $files, $self) {
927f5956 334 $errors = [];
6a488035
TO
335
336 //validate profile title as well as name.
337 $title = $fields['title'];
338 $name = CRM_Utils_String::munge($title, '_', 56);
339 $name .= $self->_id ? '_' . $self->_id : '';
340 $query = 'select count(*) from civicrm_uf_group where ( name like %1 ) and id != %2';
927f5956 341 $pCnt = CRM_Core_DAO::singleValueQuery($query, [
342 1 => [$name, 'String'],
343 2 => [(int) $self->_id, 'Integer'],
344 ]);
6a488035 345 if ($pCnt) {
927f5956 346 $errors['title'] = ts('Profile \'%1\' already exists in Database.', [1 => $title]);
6a488035
TO
347 }
348
349 return empty($errors) ? TRUE : $errors;
350 }
351
352 /**
fe482240 353 * Process the form.
6a488035
TO
354 *
355 * @return void
6a488035
TO
356 */
357 public function postProcess() {
358 if ($this->_action & CRM_Core_Action::DELETE) {
359 $title = CRM_Core_BAO_UFGroup::getTitle($this->_id);
360 CRM_Core_BAO_UFGroup::del($this->_id);
927f5956 361 CRM_Core_Session::setStatus(ts("Your CiviCRM Profile '%1' has been deleted.", [1 => $title]), ts('Profile Deleted'), 'success');
6a488035
TO
362 }
363 elseif ($this->_action & CRM_Core_Action::DISABLE) {
927f5956 364 $ufJoinParams = ['uf_group_id' => $this->_id];
6a488035
TO
365 CRM_Core_BAO_UFGroup::delUFJoin($ufJoinParams);
366
367 CRM_Core_BAO_UFGroup::setIsActive($this->_id, 0);
368 }
369 else {
370 // get the submitted form values.
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)) {
e7de4124 378 $params['id'] = $this->_id;
6a488035
TO
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
e7de4124 389 $ufGroup = CRM_Core_BAO_UFGroup::add($params);
6a488035 390
a7488080 391 if (!empty($params['is_active'])) {
6a488035 392 //make entry in uf join table
259ab031 393 CRM_Core_BAO_UFGroup::createUFJoin($params['weight'], $params['uf_group_type'] ?? [], $ufGroup->id);
6a488035
TO
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}