3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class is for UF Group (Profile) configuration.
21 class CRM_UF_Form_Group
extends CRM_Core_Form
{
23 use CRM_Core_Form_EntityFormTrait
;
26 * Fields for the entity to be assigned to the template.
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
37 * - is_freeze (field should be frozen).
41 protected $entityFields = [];
44 * Deletion message to be assigned to the form.
48 protected $deleteMessage;
51 * Set entity fields to be assigned to the form.
53 protected function setEntityFields() {
54 $this->entityFields
= [
55 'title' => ['name' => 'title'],
56 'frontend_title' => ['name' => 'frontend_title'],
58 'name' => 'description',
59 'help' => ['id' => 'id-description', 'file' => 'CRM/UF/Form/Group.hlp'],
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(),
67 'cancel_button_text' => [
68 'name' => 'cancel_button_text',
70 'id' => 'id-cancel_button_text',
71 'file' => 'CRM/UF/Form/Group.hlp',
73 'class' => 'cancel_button_section',
75 'submit_button_text' => [
76 'name' => 'submit_button_text',
78 'id' => 'id-submit_button_text',
79 'file' => 'CRM/UF/Form/Group.hlp',
87 * Explicitly declare the entity api name.
89 public function getDefaultEntity() {
94 * The title for group.
99 protected $_groupElement;
101 protected $_allPanes;
104 * Set variables up before form is built.
106 public function preProcess() {
108 $this->_id
= $this->get('id');
110 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this, FALSE, 0);
112 $this->assign('gid', $this->_id
);
113 $this->_group
= CRM_Core_PseudoConstant
::group();
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);
120 // setting title for html page
121 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
122 CRM_Utils_System
::setTitle(ts('Profile Settings') . " - $title");
124 elseif ($this->_action
& (CRM_Core_Action
::DISABLE | CRM_Core_Action
::DELETE
)) {
125 $ufGroup['module'] = implode(' , ', CRM_Core_BAO_UFGroup
::getUFJoinRecord($this->_id
, TRUE));
127 $status = CRM_Core_BAO_UFGroup
::usedByModule($this->_id
);
128 if ($this->_action
& (CRM_Core_Action
::DISABLE
)) {
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?';
133 $message = 'Are you sure you want to disable this Profile?';
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?';
141 $message = 'Are you sure you want to delete this Profile? This action cannot be undone.';
144 $this->assign('message', $message);
147 CRM_Utils_System
::setTitle(ts('New CiviCRM Profile'));
152 * Build the form object.
156 public function buildQuickForm() {
157 self
::buildQuickEntityForm();
158 if ($this->_action
& (CRM_Core_Action
::DISABLE | CRM_Core_Action
::DELETE
)) {
159 if ($this->_action
& (CRM_Core_Action
::DISABLE
)) {
160 $display = 'Disable Profile';
163 $display = 'Delete Profile';
169 'spacing' => ' ',
174 'name' => ts('Cancel'),
182 $UFGroupType = CRM_Core_SelectValues
::ufGroupTypes();
183 foreach ($UFGroupType as $key => $value) {
184 $uf_group_type[] = $this->createElement('checkbox', $key, NULL, $value);
186 $this->addGroup($uf_group_type, 'uf_group_type', ts('Used For'), ' ');
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'));
193 $this->add('number', 'weight', ts('Order'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_UFJoin', 'weight'), TRUE);
194 $this->addRule('weight', ts('is a numeric field'), 'numeric');
196 // is this group active ?
197 $this->addElement('checkbox', 'is_active', ts('Is this CiviCRM Profile active?'));
199 $paneNames = ['Advanced Settings' => 'buildAdvanceSetting'];
201 foreach ($paneNames as $name => $type) {
203 $dataURL = "&reset=1&action=update&id={$this->_id}&snippet=4&formType={$type}";
206 $dataURL = "&reset=1&action=add&snippet=4&formType={$type}";
210 'url' => CRM_Utils_System
::url('civicrm/admin/uf/group/setting', $dataURL),
215 CRM_UF_Form_AdvanceSetting
::$type($this);
221 'name' => ts('Save'),
222 'spacing' => ' ',
227 'name' => ts('Cancel'),
231 // views are implemented as frozen form
232 if ($this->_action
& CRM_Core_Action
::VIEW
) {
234 $this->addElement('button', 'done', ts('Done'), ['onclick' => "location.href='civicrm/admin/uf/group?reset=1&action=browse'"]);
237 $this->addFormRule(['CRM_UF_Form_Group', 'formRule'], $this);
241 * Set default values for the form. Note that in edit/view mode
242 * the default values are retrieved from the database
247 public function setDefaultValues() {
249 $showHide = new CRM_Core_ShowHideBlocks();
251 if ($this->_action
== CRM_Core_Action
::ADD
) {
252 $defaults['weight'] = CRM_Utils_Weight
::getDefaultWeight('CRM_Core_DAO_UFJoin');
255 //id fetched for Dojo Pane
256 $pId = CRM_Utils_Request
::retrieve('id', 'Positive', $this);
261 if ((isset($this->_id
))) {
263 $defaults['weight'] = CRM_Core_BAO_UFGroup
::getWeight($this->_id
);
265 $params = ['id' => $this->_id
];
266 CRM_Core_BAO_UFGroup
::retrieve($params, $defaults);
267 $defaults['group'] = $defaults['limit_listings_group_id'] ??
NULL;
268 $defaults['add_contact_to_group'] = $defaults['add_to_group_id'] ??
NULL;
269 //get the uf join records for current uf group
270 $ufJoinRecords = CRM_Core_BAO_UFGroup
::getUFJoinRecord($this->_id
);
271 foreach ($ufJoinRecords as $key => $value) {
272 $checked[$value] = 1;
274 $defaults['uf_group_type'] = $checked ??
"";
287 'is_proximity_search',
289 foreach ($advFields as $key) {
290 if (!empty($defaults[$key])) {
292 $this->_allPanes
['Advanced Settings']['open'] = 'true';
298 $defaults['add_cancel_button'] = 1;
299 $defaults['is_active'] = 1;
300 $defaults['is_map'] = 0;
301 $defaults['is_update_dupe'] = 0;
302 $defaults['is_proximity_search'] = 0;
304 // Don't assign showHide elements to template in DELETE mode (fields to be shown and hidden don't exist)
305 if (!($this->_action
& CRM_Core_Action
::DELETE
) && !($this->_action
& CRM_Core_Action
::DISABLE
)) {
306 $showHide->addToTemplate();
308 $this->assign('allPanes', $this->_allPanes
);
315 * @param array $fields
316 * The input form values.
317 * @param array $files
318 * The uploaded files if any.
320 * Current form object.
323 * true if no errors, else array of errors
325 public static function formRule($fields, $files, $self) {
328 //validate profile title as well as name.
329 $title = $fields['title'];
330 $name = CRM_Utils_String
::munge($title, '_', 56);
331 $name .= $self->_id ?
'_' . $self->_id
: '';
332 $query = 'select count(*) from civicrm_uf_group where ( name like %1 ) and id != %2';
333 $pCnt = CRM_Core_DAO
::singleValueQuery($query, [
334 1 => [$name, 'String'],
335 2 => [(int) $self->_id
, 'Integer'],
338 $errors['title'] = ts('Profile \'%1\' already exists in Database.', [1 => $title]);
341 return empty($errors) ?
TRUE : $errors;
349 public function postProcess() {
350 if ($this->_action
& CRM_Core_Action
::DELETE
) {
351 $title = CRM_Core_BAO_UFGroup
::getTitle($this->_id
);
352 CRM_Core_BAO_UFGroup
::del($this->_id
);
353 CRM_Core_Session
::setStatus(ts("Your CiviCRM Profile '%1' has been deleted.", [1 => $title]), ts('Profile Deleted'), 'success');
355 elseif ($this->_action
& CRM_Core_Action
::DISABLE
) {
356 $ufJoinParams = ['uf_group_id' => $this->_id
];
357 CRM_Core_BAO_UFGroup
::delUFJoin($ufJoinParams);
359 CRM_Core_BAO_UFGroup
::setIsActive($this->_id
, 0);
362 // get the submitted form values.
364 $params = $this->controller
->exportValues($this->_name
);
366 if (!array_key_exists('is_active', $params)) {
367 $params['is_active'] = 0;
370 if ($this->_action
& (CRM_Core_Action
::UPDATE
)) {
371 $ids['ufgroup'] = $this->_id
;
373 // lets skip trying to mess around with profile weights and allow the user to do as needed.
375 elseif ($this->_action
& CRM_Core_Action
::ADD
) {
376 $session = CRM_Core_Session
::singleton();
377 $params['created_id'] = $session->get('userID');
378 $params['created_date'] = date('YmdHis');
382 $ufGroup = CRM_Core_BAO_UFGroup
::add($params, $ids);
384 if (!empty($params['is_active'])) {
385 //make entry in uf join table
386 CRM_Core_BAO_UFGroup
::createUFJoin($params['weight'], $params['uf_group_type'] ??
[], $ufGroup->id
);
388 elseif ($this->_id
) {
389 // this profile has been set to inactive, delete all corresponding UF Join's
390 $ufJoinParams = ['uf_group_id' => $this->_id
];
391 CRM_Core_BAO_UFGroup
::delUFJoin($ufJoinParams);
394 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
395 $url = CRM_Utils_System
::url('civicrm/admin/uf/group', 'reset=1&action=browse');
396 CRM_Core_Session
::setStatus(ts("Your CiviCRM Profile '%1' has been saved.", [1 => $ufGroup->title
]), ts('Profile Saved'), 'success');
399 // Jump directly to adding a field if popups are disabled
400 $action = CRM_Core_Resources
::singleton()->ajaxPopupsEnabled ?
'' : '/add';
401 $url = CRM_Utils_System
::url("civicrm/admin/uf/group/field$action", 'reset=1&new=1&gid=' . $ufGroup->id
. '&action=' . ($action ?
'add' : 'browse'));
402 CRM_Core_Session
::setStatus(ts('Your CiviCRM Profile \'%1\' has been added. You can add fields to this profile now.',
403 [1 => $ufGroup->title
]
404 ), ts('Profile Added'), 'success');
406 $session = CRM_Core_Session
::singleton();
407 $session->replaceUserContext($url);
410 // update cms integration with registration / my account
411 CRM_Utils_System
::updateCategories();
415 * Set the delete message.
417 * We do this from the constructor in order to do a translation.
419 public function setDeleteMessage() {
423 * Get the string to display next to the used for field indicating unchangeable uses.
427 protected function getOtherModuleString() {
428 $otherModules = CRM_Core_BAO_UFGroup
::getUFJoinRecord($this->_id
, TRUE, TRUE);
429 $otherModuleString = NULL;
430 if (!empty($otherModules)) {
431 foreach ($otherModules as $key) {
432 $otherModuleString .= " [ x ] <label>" . $key . "</label>";
435 return $otherModuleString;