Merge pull request #15813 from eileenmcnaughton/fee
[civicrm-core.git] / CRM / UF / Form / Group.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class is for UF Group (Profile) configuration.
20 */
21 class CRM_UF_Form_Group extends CRM_Core_Form {
22
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
43 /**
44 * Deletion message to be assigned to the form.
45 *
46 * @var string
47 */
48 protected $deleteMessage;
49
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'],
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 ],
83 ];
84 }
85
86 /**
87 * Explicitly declare the entity api name.
88 */
89 public function getDefaultEntity() {
90 return 'UFGroup';
91 }
92
93 /**
94 * The form id saved to the session for an update.
95 *
96 * @var int
97 */
98 protected $_id;
99
100 /**
101 * The title for group.
102 *
103 * @var int
104 */
105 protected $_title;
106 protected $_groupElement;
107 protected $_group;
108 protected $_allPanes;
109
110 /**
111 * Set variables up before form is built.
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
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
127 // setting title for html page
128 if ($this->_action & CRM_Core_Action::UPDATE) {
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));
133 $status = 0;
134 $status = CRM_Core_BAO_UFGroup::usedByModule($this->_id);
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 /**
159 * Build the form object.
160 *
161 * @return void
162 */
163 public function buildQuickForm() {
164 self::buildQuickEntityForm();
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 }
172 $this->addButtons([
173 [
174 'type' => 'next',
175 'name' => $display,
176 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
177 'isDefault' => TRUE,
178 ],
179 [
180 'type' => 'cancel',
181 'name' => ts('Cancel'),
182 ],
183 ]);
184 return;
185 }
186
187 //add checkboxes
188 $uf_group_type = [];
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'), '&nbsp;');
194
195 // help text
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'));
198
199 // weight
200 $this->add('number', 'weight', ts('Order'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_UFJoin', 'weight'), TRUE);
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
206 $paneNames = ['Advanced Settings' => 'buildAdvanceSetting'];
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
216 $allPanes[$name] = [
217 'url' => CRM_Utils_System::url('civicrm/admin/uf/group/setting', $dataURL),
218 'open' => 'false',
219 'id' => $type,
220 ];
221
222 CRM_UF_Form_AdvanceSetting::$type($this);
223 }
224
225 $this->addButtons([
226 [
227 'type' => 'next',
228 'name' => ts('Save'),
229 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
230 'isDefault' => TRUE,
231 ],
232 [
233 'type' => 'cancel',
234 'name' => ts('Cancel'),
235 ],
236 ]);
237
238 // views are implemented as frozen form
239 if ($this->_action & CRM_Core_Action::VIEW) {
240 $this->freeze();
241 $this->addElement('button', 'done', ts('Done'), ['onclick' => "location.href='civicrm/admin/uf/group?reset=1&action=browse'"]);
242 }
243
244 $this->addFormRule(['CRM_UF_Form_Group', 'formRule'], $this);
245 }
246
247 /**
248 * Set default values for the form. Note that in edit/view mode
249 * the default values are retrieved from the database
250 *
251 *
252 * @return void
253 */
254 public function setDefaultValues() {
255 $defaults = [];
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
272 $params = ['id' => $this->_id];
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
283 $showAdvanced = 0;
284 $advFields = [
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',
295 ];
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 {
305 $defaults['add_cancel_button'] = 1;
306 $defaults['is_active'] = 1;
307 $defaults['is_map'] = 0;
308 $defaults['is_update_dupe'] = 0;
309 $defaults['is_proximity_search'] = 0;
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 /**
320 * Global form rule.
321 *
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.
328 *
329 * @return bool|array
330 * true if no errors, else array of errors
331 */
332 public static function formRule($fields, $files, $self) {
333 $errors = [];
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';
340 $pCnt = CRM_Core_DAO::singleValueQuery($query, [
341 1 => [$name, 'String'],
342 2 => [(int) $self->_id, 'Integer'],
343 ]);
344 if ($pCnt) {
345 $errors['title'] = ts('Profile \'%1\' already exists in Database.', [1 => $title]);
346 }
347
348 return empty($errors) ? TRUE : $errors;
349 }
350
351 /**
352 * Process the form.
353 *
354 * @return void
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);
360 CRM_Core_Session::setStatus(ts("Your CiviCRM Profile '%1' has been deleted.", [1 => $title]), ts('Profile Deleted'), 'success');
361 }
362 elseif ($this->_action & CRM_Core_Action::DISABLE) {
363 $ufJoinParams = ['uf_group_id' => $this->_id];
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.
370 $ids = [];
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
391 if (!empty($params['is_active'])) {
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
397 $ufJoinParams = ['uf_group_id' => $this->_id];
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');
403 CRM_Core_Session::setStatus(ts("Your CiviCRM Profile '%1' has been saved.", [1 => $ufGroup->title]), ts('Profile Saved'), 'success');
404 }
405 else {
406 // Jump directly to adding a field if popups are disabled
407 $action = CRM_Core_Resources::singleton()->ajaxPopupsEnabled ? '' : '/add';
408 $url = CRM_Utils_System::url("civicrm/admin/uf/group/field$action", 'reset=1&new=1&gid=' . $ufGroup->id . '&action=' . ($action ? 'add' : 'browse'));
409 CRM_Core_Session::setStatus(ts('Your CiviCRM Profile \'%1\' has been added. You can add fields to this profile now.',
410 [1 => $ufGroup->title]
411 ), ts('Profile Added'), 'success');
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 }
420
421 /**
422 * Set the delete message.
423 *
424 * We do this from the constructor in order to do a translation.
425 */
426 public function setDeleteMessage() {
427 }
428
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);
436 $otherModuleString = NULL;
437 if (!empty($otherModules)) {
438 foreach ($otherModules as $key) {
439 $otherModuleString .= " [ x ] <label>" . $key . "</label>";
440 }
441 }
442 return $otherModuleString;
443 }
444
445 }