Merge pull request #15875 from civicrm/5.20
[civicrm-core.git] / CRM / UF / Page / 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 * $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 */
28class CRM_UF_Page_Group extends CRM_Core_Page {
29
30 /**
fe482240 31 * The action links that we need to display for the browse screen.
6a488035
TO
32 *
33 * @var array
34 */
35 private static $_actionLinks = NULL;
36
37 /**
38 * Get the action links for this page.
39 *
40 * @param
41 *
a6c01b45 42 * @return array
6a488035 43 */
d1424f8f 44 public static function &actionLinks() {
6a488035
TO
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?');
be2fb01f
CW
49 self::$_actionLinks = [
50 CRM_Core_Action::BROWSE => [
6a488035
TO
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'),
be2fb01f
CW
55 ],
56 CRM_Core_Action::UPDATE => [
6a488035
TO
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'),
be2fb01f
CW
61 ],
62 CRM_Core_Action::PREVIEW => [
6a488035
TO
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'),
be2fb01f
CW
67 ],
68 CRM_Core_Action::ADD => [
3495d8d7 69 'name' => ts('Use - Create Mode'),
6a488035
TO
70 'url' => 'civicrm/profile/create',
71 'qs' => 'gid=%%id%%&reset=1',
3495d8d7
J
72 'title' => ts('Use - Create Mode'),
73 'fe' => TRUE,
be2fb01f
CW
74 ],
75 CRM_Core_Action::ADVANCED => [
3495d8d7
J
76 'name' => ts('Use - Edit Mode'),
77 'url' => 'civicrm/profile/edit',
78 'qs' => 'gid=%%id%%&reset=1',
79 'title' => ts('Use - Edit Mode'),
9147c186 80 'fe' => TRUE,
be2fb01f
CW
81 ],
82 CRM_Core_Action::BASIC => [
3495d8d7 83 'name' => ts('Use - Listings Mode'),
9865c956
DG
84 'url' => 'civicrm/profile',
85 'qs' => 'gid=%%id%%&reset=1',
3495d8d7 86 'title' => ts('Use - Listings Mode'),
9147c186 87 'fe' => TRUE,
be2fb01f
CW
88 ],
89 CRM_Core_Action::DISABLE => [
6a488035 90 'name' => ts('Disable'),
4d17a233 91 'ref' => 'crm-enable-disable',
6a488035 92 'title' => ts('Disable CiviCRM Profile Group'),
be2fb01f
CW
93 ],
94 CRM_Core_Action::ENABLE => [
6a488035 95 'name' => ts('Enable'),
4d17a233 96 'ref' => 'crm-enable-disable',
6a488035 97 'title' => ts('Enable CiviCRM Profile Group'),
be2fb01f
CW
98 ],
99 CRM_Core_Action::DELETE => [
6a488035
TO
100 'name' => ts('Delete'),
101 'url' => 'civicrm/admin/uf/group',
102 'qs' => 'action=delete&id=%%id%%',
103 'title' => ts('Delete CiviCRM Profile Group'),
be2fb01f
CW
104 ],
105 CRM_Core_Action::COPY => [
3495d8d7 106 'name' => ts('Copy'),
6a488035
TO
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 . '\');"',
be2fb01f
CW
111 ],
112 ];
e8a64b98 113 $allowRemoteSubmit = Civi::settings()->get('remote_profile_submissions');
1bfe4abf 114 if ($allowRemoteSubmit) {
be2fb01f 115 self::$_actionLinks[CRM_Core_Action::PROFILE] = [
61f53f0d
PN
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'),
be2fb01f 120 ];
61f53f0d 121 }
6a488035
TO
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.
6a488035 132 */
00be9182 133 public function run() {
6a488035
TO
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);
c1bd8375 143 $this->assign('selectedChild', CRM_Utils_Request::retrieve('selectedChild', 'Alphanumeric', $this));
6a488035
TO
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();
be2fb01f 165 CRM_Utils_System::setTitle(ts('%1 - HTML Form Snippet', [1 => $this->_title]));
6a488035
TO
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 /**
dc195289 181 * make a copy of a profile, including
6a488035
TO
182 * all the fields in the profile
183 *
184 * @return void
6a488035 185 */
00be9182 186 public function copy() {
6a488035
TO
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 /**
dc195289 196 * for profile mode (standalone html form ) for uf group
6a488035
TO
197 *
198 * @return void
6a488035 199 */
00be9182 200 public function profile() {
6a488035
TO
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');
880e33cd 221 $profile = trim($template->fetch('CRM/Form/default.tpl'));
6a488035
TO
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
8c63e816
CW
230 // FIXME: (CRM-3587) hack to make standalone profile work
231 // in wordpress and joomla without administrator login
6a488035
TO
232 if ($config->userFramework == 'Joomla') {
233 $profile = str_replace('/administrator/', '/index.php', $profile);
234 }
013e44b2 235 elseif ($config->userFramework == 'WordPress') {
c73b1c19 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 }
81badc57 246 $profile = str_replace('/wp-admin/admin.php', '/' . $wpbase . '/', $profile);
6a488035
TO
247 }
248
880e33cd
CW
249 // add header files
250 CRM_Core_Resources::singleton()->addCoreResources('html-header');
251 $profile = CRM_Core_Region::instance('html-header')->render('', FALSE) . $profile;
6a488035 252
6a488035
TO
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 /**
fe482240 269 * Edit uf group.
6a488035 270 *
5ce1712d
TO
271 * @param int $id
272 * Uf group id.
273 * @param string $action
274 * The action to be invoked.
6a488035
TO
275 *
276 * @return void
6a488035 277 */
00be9182 278 public function edit($id, $action) {
6a488035
TO
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
6a488035 294 */
00be9182 295 public function browse($action = NULL) {
be2fb01f 296 $ufGroup = [];
6a488035
TO
297 $allUFGroups = CRM_Core_BAO_UFGroup::getModuleUFGroup();
298 if (empty($allUFGroups)) {
299 return;
300 }
301
ff4f7744 302 $ufGroups = CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFField', 'uf_group_id');
6a488035
TO
303 CRM_Utils_Hook::aclGroup(CRM_Core_Permission::ADMIN, NULL, 'civicrm_uf_group', $ufGroups, $allUFGroups);
304
305 foreach ($allUFGroups as $id => $value) {
be2fb01f 306 $ufGroup[$id] = [];
6a488035
TO
307 $ufGroup[$id]['id'] = $id;
308 $ufGroup[$id]['title'] = $value['title'];
ce1a9db4 309 $ufGroup[$id]['frontend_title'] = $value['frontend_title'];
bcbde5ca
AW
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'];
6a488035
TO
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
d1424f8f 318 $action = array_sum(array_keys(self::actionLinks()));
6a488035
TO
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']);
6a488035 336
b233e1b4 337 // drop Create, Edit and View mode links if profile group_type is one of the following:
4d4daa2d 338 // Contribution, Membership, Activity, Participant, Case, Grant
7e50650d
PN
339 $isMixedProfile = CRM_Core_BAO_UFField::checkProfileType($id);
340 if ($isMixedProfile) {
6a488035 341 $action -= CRM_Core_Action::ADD;
caa0151f
PN
342 $action -= CRM_Core_Action::ADVANCED;
343 $action -= CRM_Core_Action::BASIC;
9664eee1
BS
344
345 //CRM-21004
b841e03f 346 if (array_key_exists(CRM_Core_Action::PROFILE, self::$_actionLinks)) {
9664eee1
BS
347 $action -= CRM_Core_Action::PROFILE;
348 }
6a488035
TO
349 }
350
b233e1b4 351 $ufGroup[$id]['group_type'] = self::formatGroupTypes($groupTypes);
6a488035
TO
352
353 $ufGroup[$id]['action'] = CRM_Core_Action::formLink(self::actionLinks(), $action,
be2fb01f 354 ['id' => $id],
87dab4a4
AH
355 ts('more'),
356 FALSE,
357 'ufGroup.row.actions',
358 'UFGroup',
359 $id
6a488035
TO
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 /**
fe482240 369 * for preview mode for ufoup.
6a488035 370 *
5ce1712d
TO
371 * @param int $id
372 * Uf group id.
6a488035 373 *
3bdf1f3a 374 * @param int $action
6a488035 375 */
00be9182 376 public function preview($id, $action) {
6a488035
TO
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
624e56fa 384 /**
100fef9d 385 * @param int $id
624e56fa
EM
386 * @param $action
387 */
00be9182 388 public function setContext($id, $action) {
edc80cda 389 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
6a488035
TO
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
624e56fa
EM
407 /**
408 * @param $groupType
409 *
410 * @return array
411 */
00be9182 412 public static function extractGroupTypes($groupType) {
be2fb01f 413 $returnGroupTypes = [];
6a488035
TO
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
a7488080 423 if (!empty($groupTypeParts[1])) {
6a488035 424 foreach (explode(',', $groupTypeParts[1]) as $typeValue) {
be2fb01f 425 $groupTypeValues = $valueLabels = [];
6a488035
TO
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;
9147c186 458
1cb28d5d 459 case 'CaseType':
460 $typeName = 'Case';
461 $valueLabels = CRM_Case_PseudoConstant::caseType();
462 break;
6a488035
TO
463 }
464
465 foreach ($valueParts as $val) {
466 if (CRM_Utils_Rule::integer($val)) {
467 $groupTypeValues[$val] = CRM_Utils_Array::value($val, $valueLabels);
468 }
469 }
470
471 if (!is_array($returnGroupTypes[$typeName])) {
be2fb01f 472 $returnGroupTypes[$typeName] = [];
6a488035
TO
473 }
474 $returnGroupTypes[$typeName][$valueParts[0]] = $groupTypeValues;
475 }
476 }
477 return $returnGroupTypes;
478 }
96025800 479
b233e1b4
CW
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)) {
be2fb01f 490 $groupTypesStrings = [];
b233e1b4
CW
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
6a488035 511}