Merge pull request #17878 from civicrm/5.28
[civicrm-core.git] / CRM / UF / Page / 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 * $Id$
17 *
18 */
19
20 /**
21 * Create a page for displaying UF Groups.
22 *
23 * Heart of this class is the run method which checks
24 * for action type and then displays the appropriate
25 * page.
26 *
27 */
28 class CRM_UF_Page_Group extends CRM_Core_Page {
29
30 /**
31 * The action links that we need to display for the browse screen.
32 *
33 * @var array
34 */
35 private static $_actionLinks = NULL;
36
37 /**
38 * Get the action links for this page.
39 *
40 * @param
41 *
42 * @return array
43 */
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'),
55 ],
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'),
61 ],
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'),
67 ],
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'),
73 'fe' => TRUE,
74 ],
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'),
80 'fe' => TRUE,
81 ],
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'),
87 'fe' => TRUE,
88 ],
89 CRM_Core_Action::DISABLE => [
90 'name' => ts('Disable'),
91 'ref' => 'crm-enable-disable',
92 'title' => ts('Disable CiviCRM Profile Group'),
93 ],
94 CRM_Core_Action::ENABLE => [
95 'name' => ts('Enable'),
96 'ref' => 'crm-enable-disable',
97 'title' => ts('Enable CiviCRM Profile Group'),
98 ],
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'),
104 ],
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 . '\');"',
111 ],
112 ];
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'),
120 ];
121 }
122 }
123 return self::$_actionLinks;
124 }
125
126 /**
127 * Run the page.
128 *
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.
132 */
133 public function run() {
134 // get the requested action
135 $action = CRM_Utils_Request::retrieve('action', 'String',
136 $this, FALSE,
137 // default to 'browse'
138 'browse'
139 );
140
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',
145 $this, FALSE, 0
146 );
147
148 //set the context and then start w/ action.
149 $this->setContext($id, $action);
150
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);
154 }
155 else {
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);
159
160 // update cms integration with registration / my account
161 CRM_Utils_System::updateCategories();
162 }
163 elseif ($action & CRM_Core_Action::PROFILE) {
164 $this->profile();
165 CRM_Utils_System::setTitle(ts('%1 - HTML Form Snippet', [1 => $this->_title]));
166 }
167 elseif ($action & CRM_Core_Action::PREVIEW) {
168 $this->preview($id, $action);
169 }
170 elseif ($action & CRM_Core_Action::COPY) {
171 $this->copy();
172 }
173 // finally browse the uf groups
174 $this->browse();
175 }
176 // parent run
177 return parent::run();
178 }
179
180 /**
181 * make a copy of a profile, including
182 * all the fields in the profile
183 *
184 * @return void
185 */
186 public function copy() {
187 $gid = CRM_Utils_Request::retrieve('gid', 'Positive',
188 $this, TRUE, 0, 'GET'
189 );
190
191 CRM_Core_BAO_UFGroup::copy($gid);
192 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/uf/group', 'reset=1'));
193 }
194
195 /**
196 * for profile mode (standalone html form ) for uf group
197 *
198 * @return void
199 */
200 public function profile() {
201 $config = CRM_Core_Config::singleton();
202
203 // reassign resource base to be the full url, CRM-4660
204 $config->resourceBase = $config->userFrameworkResourceURL;
205 $config->useFrameworkRelativeBase = $config->userFrameworkBaseURL;
206
207 $gid = CRM_Utils_Request::retrieve('gid', 'Positive',
208 $this, FALSE, 0, 'GET'
209 );
210 $controller = new CRM_Core_Controller_Simple('CRM_Profile_Form_Edit', ts('Create'), CRM_Core_Action::ADD,
211 FALSE, FALSE, TRUE
212 );
213 $controller->reset();
214 $controller->process();
215 $controller->set('gid', $gid);
216 $controller->setEmbedded(TRUE);
217 $controller->run();
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'));
222
223 // not sure how to circumvent our own navigation system to generate the right form url
224 $urlReplaceWith = 'civicrm/profile/create&amp;gid=' . $gid . '&amp;reset=1';
225 if ($config->userSystem->is_drupal && $config->cleanURL) {
226 $urlReplaceWith = 'civicrm/profile/create?gid=' . $gid . '&amp;reset=1';
227 }
228 $profile = str_replace('civicrm/admin/uf/group', $urlReplaceWith, $profile);
229
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);
234 }
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;
239 }
240 elseif (!empty($config->wpBasePage)) {
241 $wpbase = $config->wpBasePage;
242 }
243 else {
244 $wpbase = 'index.php';
245 }
246 $profile = str_replace('/wp-admin/admin.php', '/' . $wpbase . '/', $profile);
247 }
248
249 // add header files
250 CRM_Core_Resources::singleton()->addCoreResources('html-header');
251 $profile = CRM_Core_Region::instance('html-header')->render('', FALSE) . $profile;
252
253 $this->assign('profile', htmlentities($profile, ENT_NOQUOTES, 'UTF-8'));
254 //get the title of uf group
255 if ($gid) {
256 $title = CRM_Core_BAO_UFGroup::getTitle($gid);
257 $this->_title = $title;
258 }
259 else {
260 $title = 'Profile Form';
261 }
262
263 $this->assign('title', $title);
264 $this->assign('action', CRM_Core_Action::PROFILE);
265 $this->assign('isForm', 0);
266 }
267
268 /**
269 * Edit uf group.
270 *
271 * @param int $id
272 * Uf group id.
273 * @param string $action
274 * The action to be invoked.
275 *
276 * @return void
277 */
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();
285 $controller->run();
286 }
287
288 /**
289 * Browse all uf data groups.
290 *
291 * @param
292 *
293 * @return void
294 */
295 public function browse($action = NULL) {
296 $ufGroup = [];
297 $allUFGroups = CRM_Core_BAO_UFGroup::getModuleUFGroup();
298 if (empty($allUFGroups)) {
299 return;
300 }
301
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);
304
305 foreach ($allUFGroups as $id => $value) {
306 $ufGroup[$id] = [];
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'];
316
317 // form all action links
318 $action = array_sum(array_keys(self::actionLinks()));
319
320 // update enable/disable links depending on uf_group properties.
321 if ($value['is_active']) {
322 $action -= CRM_Core_Action::ENABLE;
323 }
324 else {
325 $action -= CRM_Core_Action::DISABLE;
326 }
327
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;
333 }
334
335 $groupTypes = self::extractGroupTypes($value['group_type']);
336
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;
344
345 //CRM-21004
346 if (array_key_exists(CRM_Core_Action::PROFILE, self::$_actionLinks)) {
347 $action -= CRM_Core_Action::PROFILE;
348 }
349 }
350
351 $ufGroup[$id]['group_type'] = self::formatGroupTypes($groupTypes);
352
353 $ufGroup[$id]['action'] = CRM_Core_Action::formLink(self::actionLinks(), $action,
354 ['id' => $id],
355 ts('more'),
356 FALSE,
357 'ufGroup.row.actions',
358 'UFGroup',
359 $id
360 );
361 //get the "Used For" from uf_join
362 $ufGroup[$id]['module'] = implode(', ', CRM_Core_BAO_UFGroup::getUFJoinRecord($id, TRUE));
363 }
364
365 $this->assign('rows', $ufGroup);
366 }
367
368 /**
369 * for preview mode for ufoup.
370 *
371 * @param int $id
372 * Uf group id.
373 *
374 * @param int $action
375 */
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();
381 $controller->run();
382 }
383
384 /**
385 * @param int $id
386 * @param $action
387 */
388 public function setContext($id, $action) {
389 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
390
391 //we need to differentiate context for update and preview profile.
392 if (!$context && !($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::PREVIEW))) {
393 $context = 'group';
394 }
395
396 if ($context == 'field') {
397 $url = CRM_Utils_System::url('civicrm/admin/uf/group/field', "reset=1&action=browse&gid={$id}");
398 }
399 else {
400 $url = CRM_Utils_System::url('civicrm/admin/uf/group', 'reset=1&action=browse');
401 }
402
403 $session = CRM_Core_Session::singleton();
404 $session->pushUserContext($url);
405 }
406
407 /**
408 * @param $groupType
409 *
410 * @return array
411 */
412 public static function extractGroupTypes($groupType) {
413 $returnGroupTypes = [];
414 if (!$groupType) {
415 return $returnGroupTypes;
416 }
417
418 $groupTypeParts = explode(CRM_Core_DAO::VALUE_SEPARATOR, $groupType);
419 foreach (explode(',', $groupTypeParts[0]) as $type) {
420 $returnGroupTypes[$type] = $type;
421 }
422
423 if (!empty($groupTypeParts[1])) {
424 foreach (explode(',', $groupTypeParts[1]) as $typeValue) {
425 $groupTypeValues = $valueLabels = [];
426 $valueParts = explode(':', $typeValue);
427 $typeName = NULL;
428 switch ($valueParts[0]) {
429 case 'ContributionType':
430 $typeName = 'Contribution';
431 $valueLabels = CRM_Contribute_PseudoConstant::financialType();
432 break;
433
434 case 'ParticipantRole':
435 $typeName = 'Participant';
436 $valueLabels = CRM_Event_PseudoConstant::participantRole();
437 break;
438
439 case 'ParticipantEventName':
440 $typeName = 'Participant';
441 $valueLabels = CRM_Event_PseudoConstant::event();
442 break;
443
444 case 'ParticipantEventType':
445 $typeName = 'Participant';
446 $valueLabels = CRM_Event_PseudoConstant::eventType();
447 break;
448
449 case 'MembershipType':
450 $typeName = 'Membership';
451 $valueLabels = CRM_Member_PseudoConstant::membershipType();
452 break;
453
454 case 'ActivityType':
455 $typeName = 'Activity';
456 $valueLabels = CRM_Core_PseudoConstant::ActivityType(TRUE, TRUE, FALSE, 'label', TRUE);
457 break;
458
459 case 'CaseType':
460 $typeName = 'Case';
461 $valueLabels = CRM_Case_PseudoConstant::caseType();
462 break;
463 }
464
465 foreach ($valueParts as $val) {
466 if (CRM_Utils_Rule::integer($val)) {
467 $groupTypeValues[$val] = $valueLabels[$val] ?? NULL;
468 }
469 }
470
471 if (!is_array($returnGroupTypes[$typeName])) {
472 $returnGroupTypes[$typeName] = [];
473 }
474 $returnGroupTypes[$typeName][$valueParts[0]] = $groupTypeValues;
475 }
476 }
477 return $returnGroupTypes;
478 }
479
480 /**
481 * Format 'group_type' field for display
482 *
483 * @param array $groupTypes
484 * output from self::extractGroupTypes
485 * @return string
486 */
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);
496 }
497 }
498 else {
499 $groupTypesStrings[] = $groupType . '::' . implode(': ', current($typeValues));
500 }
501 }
502 else {
503 $groupTypesStrings[] = $groupType;
504 }
505 }
506 $groupTypesString = implode(', ', $groupTypesStrings);
507 }
508 return $groupTypesString;
509 }
510
511 }