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
21 * Create a page for displaying UF Groups.
23 * Heart of this class is the run method which checks
24 * for action type and then displays the appropriate
28 class CRM_UF_Page_Group
extends CRM_Core_Page
{
31 * The action links that we need to display for the browse screen.
35 private static $_actionLinks = NULL;
38 * Get the action links for this page.
44 public static function &actionLinks() {
45 // check if variable _actionsLinks is populated
46 if (!self
::$_actionLinks) {
47 // helper variable for nicer formatting
48 $copyExtra = ts('Are you sure you want to make a copy of this Profile?');
49 self
::$_actionLinks = [
50 CRM_Core_Action
::BROWSE
=> [
51 'name' => ts('Fields'),
52 'url' => 'civicrm/admin/uf/group/field',
53 'qs' => 'reset=1&action=browse&gid=%%id%%',
54 'title' => ts('View and Edit Fields'),
56 CRM_Core_Action
::UPDATE
=> [
57 'name' => ts('Settings'),
58 'url' => 'civicrm/admin/uf/group/update',
59 'qs' => 'action=update&id=%%id%%&context=group',
60 'title' => ts('Edit CiviCRM Profile Group'),
62 CRM_Core_Action
::PREVIEW
=> [
63 'name' => ts('Preview'),
64 'url' => 'civicrm/admin/uf/group',
65 'qs' => 'action=preview&id=%%id%%&field=0&context=group',
66 'title' => ts('Edit CiviCRM Profile Group'),
68 CRM_Core_Action
::ADD
=> [
69 'name' => ts('Use - Create Mode'),
70 'url' => 'civicrm/profile/create',
71 'qs' => 'gid=%%id%%&reset=1',
72 'title' => ts('Use - Create Mode'),
75 CRM_Core_Action
::ADVANCED
=> [
76 'name' => ts('Use - Edit Mode'),
77 'url' => 'civicrm/profile/edit',
78 'qs' => 'gid=%%id%%&reset=1',
79 'title' => ts('Use - Edit Mode'),
82 CRM_Core_Action
::BASIC
=> [
83 'name' => ts('Use - Listings Mode'),
84 'url' => 'civicrm/profile',
85 'qs' => 'gid=%%id%%&reset=1',
86 'title' => ts('Use - Listings Mode'),
89 CRM_Core_Action
::DISABLE
=> [
90 'name' => ts('Disable'),
91 'ref' => 'crm-enable-disable',
92 'title' => ts('Disable CiviCRM Profile Group'),
94 CRM_Core_Action
::ENABLE
=> [
95 'name' => ts('Enable'),
96 'ref' => 'crm-enable-disable',
97 'title' => ts('Enable CiviCRM Profile Group'),
99 CRM_Core_Action
::DELETE
=> [
100 'name' => ts('Delete'),
101 'url' => 'civicrm/admin/uf/group',
102 'qs' => 'action=delete&id=%%id%%',
103 'title' => ts('Delete CiviCRM Profile Group'),
105 CRM_Core_Action
::COPY
=> [
106 'name' => ts('Copy'),
107 'url' => 'civicrm/admin/uf/group',
108 'qs' => 'action=copy&gid=%%id%%',
109 'title' => ts('Make a Copy of CiviCRM Profile Group'),
110 'extra' => 'onclick = "return confirm(\'' . $copyExtra . '\');"',
113 $allowRemoteSubmit = Civi
::settings()->get('remote_profile_submissions');
114 if ($allowRemoteSubmit) {
115 self
::$_actionLinks[CRM_Core_Action
::PROFILE
] = [
116 'name' => ts('HTML Form Snippet'),
117 'url' => 'civicrm/admin/uf/group',
118 'qs' => 'action=profile&gid=%%id%%',
119 'title' => ts('HTML Form Snippet for this Profile'),
123 return self
::$_actionLinks;
129 * This method is called after the page is created. It checks for the
130 * type of action and executes that action.
131 * Finally it calls the parent's run method.
133 public function run() {
134 // get the requested action
135 $action = CRM_Utils_Request
::retrieve('action', 'String',
137 // default to 'browse'
141 // assign vars to templates
142 $this->assign('action', $action);
143 $this->assign('selectedChild', CRM_Utils_Request
::retrieve('selectedChild', 'Alphanumeric', $this));
144 $id = CRM_Utils_Request
::retrieve('id', 'Positive',
148 //set the context and then start w/ action.
149 $this->setContext($id, $action);
151 // what action to take ?
152 if ($action & (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD | CRM_Core_Action
::DELETE | CRM_Core_Action
::DISABLE
)) {
153 $this->edit($id, $action);
156 // if action is enable or disable do the needful.
157 if ($action & CRM_Core_Action
::ENABLE
) {
158 CRM_Core_BAO_UFGroup
::setIsActive($id, 1);
160 // update cms integration with registration / my account
161 CRM_Utils_System
::updateCategories();
163 elseif ($action & CRM_Core_Action
::PROFILE
) {
165 CRM_Utils_System
::setTitle(ts('%1 - HTML Form Snippet', [1 => $this->_title
]));
167 elseif ($action & CRM_Core_Action
::PREVIEW
) {
168 $this->preview($id, $action);
170 elseif ($action & CRM_Core_Action
::COPY
) {
173 // finally browse the uf groups
177 return parent
::run();
181 * make a copy of a profile, including
182 * all the fields in the profile
186 public function copy() {
187 $gid = CRM_Utils_Request
::retrieve('gid', 'Positive',
188 $this, TRUE, 0, 'GET'
191 CRM_Core_BAO_UFGroup
::copy($gid);
192 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/admin/uf/group', 'reset=1'));
196 * for profile mode (standalone html form ) for uf group
200 public function profile() {
201 $config = CRM_Core_Config
::singleton();
203 // reassign resource base to be the full url, CRM-4660
204 $config->resourceBase
= $config->userFrameworkResourceURL
;
205 $config->useFrameworkRelativeBase
= $config->userFrameworkBaseURL
;
207 $gid = CRM_Utils_Request
::retrieve('gid', 'Positive',
208 $this, FALSE, 0, 'GET'
210 $controller = new CRM_Core_Controller_Simple('CRM_Profile_Form_Edit', ts('Create'), CRM_Core_Action
::ADD
,
213 $controller->reset();
214 $controller->process();
215 $controller->set('gid', $gid);
216 $controller->setEmbedded(TRUE);
218 $template = CRM_Core_Smarty
::singleton();
219 $template->assign('gid', $gid);
220 $template->assign('tplFile', 'CRM/Profile/Form/Edit.tpl');
221 $profile = trim($template->fetch('CRM/Form/default.tpl'));
223 // not sure how to circumvent our own navigation system to generate the right form url
224 $urlReplaceWith = 'civicrm/profile/create&gid=' . $gid . '&reset=1';
225 if ($config->userSystem
->is_drupal
&& $config->cleanURL
) {
226 $urlReplaceWith = 'civicrm/profile/create?gid=' . $gid . '&reset=1';
228 $profile = str_replace('civicrm/admin/uf/group', $urlReplaceWith, $profile);
230 // FIXME: (CRM-3587) hack to make standalone profile work
231 // in wordpress and joomla without administrator login
232 if ($config->userFramework
== 'Joomla') {
233 $profile = str_replace('/administrator/', '/index.php', $profile);
235 elseif ($config->userFramework
== 'WordPress') {
236 //@todo remove this part when it is OK to deprecate CIVICRM_UF_WP_BASEPAGE-CRM-15933
237 if (defined('CIVICRM_UF_WP_BASEPAGE')) {
238 $wpbase = CIVICRM_UF_WP_BASEPAGE
;
240 elseif (!empty($config->wpBasePage
)) {
241 $wpbase = $config->wpBasePage
;
244 $wpbase = 'index.php';
246 $profile = str_replace('/wp-admin/admin.php', '/' . $wpbase . '/', $profile);
250 CRM_Core_Resources
::singleton()->addCoreResources('html-header');
251 $profile = CRM_Core_Region
::instance('html-header')->render('', FALSE) . $profile;
253 $this->assign('profile', htmlentities($profile, ENT_NOQUOTES
, 'UTF-8'));
254 //get the title of uf group
256 $title = CRM_Core_BAO_UFGroup
::getTitle($gid);
257 $this->_title
= $title;
260 $title = 'Profile Form';
263 $this->assign('title', $title);
264 $this->assign('action', CRM_Core_Action
::PROFILE
);
265 $this->assign('isForm', 0);
273 * @param string $action
274 * The action to be invoked.
278 public function edit($id, $action) {
279 // create a simple controller for editing uf data
280 $controller = new CRM_Core_Controller_Simple('CRM_UF_Form_Group', ts('CiviCRM Profile Group'), $action);
281 $this->setContext($id, $action);
282 $controller->set('id', $id);
283 $controller->setEmbedded(TRUE);
284 $controller->process();
289 * Browse all uf data groups.
295 public function browse($action = NULL) {
297 $allUFGroups = CRM_Core_BAO_UFGroup
::getModuleUFGroup();
298 if (empty($allUFGroups)) {
302 $ufGroups = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_UFField', 'uf_group_id');
303 CRM_Utils_Hook
::aclGroup(CRM_Core_Permission
::ADMIN
, NULL, 'civicrm_uf_group', $ufGroups, $allUFGroups);
305 foreach ($allUFGroups as $id => $value) {
307 $ufGroup[$id]['id'] = $id;
308 $ufGroup[$id]['title'] = $value['title'];
309 $ufGroup[$id]['frontend_title'] = $value['frontend_title'];
310 $ufGroup[$id]['created_id'] = $value['created_id'];
311 $ufGroup[$id]['created_by'] = CRM_Contact_BAO_Contact
::displayName($value['created_id']);
312 $ufGroup[$id]['description'] = $value['description'];
313 $ufGroup[$id]['is_active'] = $value['is_active'];
314 $ufGroup[$id]['group_type'] = $value['group_type'];
315 $ufGroup[$id]['is_reserved'] = $value['is_reserved'];
317 // form all action links
318 $action = array_sum(array_keys(self
::actionLinks()));
320 // update enable/disable links depending on uf_group properties.
321 if ($value['is_active']) {
322 $action -= CRM_Core_Action
::ENABLE
;
325 $action -= CRM_Core_Action
::DISABLE
;
328 // drop certain actions if the profile is reserved
329 if ($value['is_reserved']) {
330 $action -= CRM_Core_Action
::UPDATE
;
331 $action -= CRM_Core_Action
::DISABLE
;
332 $action -= CRM_Core_Action
::DELETE
;
335 $groupTypes = self
::extractGroupTypes($value['group_type']);
337 // drop Create, Edit and View mode links if profile group_type is one of the following:
338 // Contribution, Membership, Activity, Participant, Case, Grant
339 $isMixedProfile = CRM_Core_BAO_UFField
::checkProfileType($id);
340 if ($isMixedProfile) {
341 $action -= CRM_Core_Action
::ADD
;
342 $action -= CRM_Core_Action
::ADVANCED
;
343 $action -= CRM_Core_Action
::BASIC
;
346 if (array_key_exists(CRM_Core_Action
::PROFILE
, self
::$_actionLinks)) {
347 $action -= CRM_Core_Action
::PROFILE
;
351 $ufGroup[$id]['group_type'] = self
::formatGroupTypes($groupTypes);
353 $ufGroup[$id]['action'] = CRM_Core_Action
::formLink(self
::actionLinks(), $action,
357 'ufGroup.row.actions',
361 //get the "Used For" from uf_join
362 $ufGroup[$id]['module'] = implode(', ', CRM_Core_BAO_UFGroup
::getUFJoinRecord($id, TRUE));
365 $this->assign('rows', $ufGroup);
369 * for preview mode for ufoup.
376 public function preview($id, $action) {
377 $controller = new CRM_Core_Controller_Simple('CRM_UF_Form_Preview', ts('CiviCRM Profile Group Preview'), NULL);
378 $controller->set('id', $id);
379 $controller->setEmbedded(TRUE);
380 $controller->process();
388 public function setContext($id, $action) {
389 $context = CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this);
391 //we need to differentiate context for update and preview profile.
392 if (!$context && !($action & (CRM_Core_Action
::UPDATE | CRM_Core_Action
::PREVIEW
))) {
396 if ($context == 'field') {
397 $url = CRM_Utils_System
::url('civicrm/admin/uf/group/field', "reset=1&action=browse&gid={$id}");
400 $url = CRM_Utils_System
::url('civicrm/admin/uf/group', 'reset=1&action=browse');
403 $session = CRM_Core_Session
::singleton();
404 $session->pushUserContext($url);
412 public static function extractGroupTypes($groupType) {
413 $returnGroupTypes = [];
415 return $returnGroupTypes;
418 $groupTypeParts = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $groupType);
419 foreach (explode(',', $groupTypeParts[0]) as $type) {
420 $returnGroupTypes[$type] = $type;
423 if (!empty($groupTypeParts[1])) {
424 foreach (explode(',', $groupTypeParts[1]) as $typeValue) {
425 $groupTypeValues = $valueLabels = [];
426 $valueParts = explode(':', $typeValue);
428 switch ($valueParts[0]) {
429 case 'ContributionType':
430 $typeName = 'Contribution';
431 $valueLabels = CRM_Contribute_PseudoConstant
::financialType();
434 case 'ParticipantRole':
435 $typeName = 'Participant';
436 $valueLabels = CRM_Event_PseudoConstant
::participantRole();
439 case 'ParticipantEventName':
440 $typeName = 'Participant';
441 $valueLabels = CRM_Event_PseudoConstant
::event();
444 case 'ParticipantEventType':
445 $typeName = 'Participant';
446 $valueLabels = CRM_Event_PseudoConstant
::eventType();
449 case 'MembershipType':
450 $typeName = 'Membership';
451 $valueLabels = CRM_Member_PseudoConstant
::membershipType();
455 $typeName = 'Activity';
456 $valueLabels = CRM_Core_PseudoConstant
::ActivityType(TRUE, TRUE, FALSE, 'label', TRUE);
461 $valueLabels = CRM_Case_PseudoConstant
::caseType();
465 foreach ($valueParts as $val) {
466 if (CRM_Utils_Rule
::integer($val)) {
467 $groupTypeValues[$val] = $valueLabels[$val] ??
NULL;
471 if (!is_array($returnGroupTypes[$typeName])) {
472 $returnGroupTypes[$typeName] = [];
474 $returnGroupTypes[$typeName][$valueParts[0]] = $groupTypeValues;
477 return $returnGroupTypes;
481 * Format 'group_type' field for display
483 * @param array $groupTypes
484 * output from self::extractGroupTypes
487 public static function formatGroupTypes($groupTypes) {
488 $groupTypesString = '';
489 if (!empty($groupTypes)) {
490 $groupTypesStrings = [];
491 foreach ($groupTypes as $groupType => $typeValues) {
492 if (is_array($typeValues)) {
493 if ($groupType == 'Participant') {
494 foreach ($typeValues as $subType => $subTypeValues) {
495 $groupTypesStrings[] = $subType . '::' . implode(': ', $subTypeValues);
499 $groupTypesStrings[] = $groupType . '::' . implode(': ', current($typeValues));
503 $groupTypesStrings[] = $groupType;
506 $groupTypesString = implode(', ', $groupTypesStrings);
508 return $groupTypesString;