Merge pull request #7969 from monishdeb/CRM-18224
[civicrm-core.git] / CRM / UF / Page / Group.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * Create a page for displaying UF Groups.
38 *
39 * Heart of this class is the run method which checks
40 * for action type and then displays the appropriate
41 * page.
42 *
43 */
44class CRM_UF_Page_Group extends CRM_Core_Page {
45
46 /**
fe482240 47 * The action links that we need to display for the browse screen.
6a488035
TO
48 *
49 * @var array
50 */
51 private static $_actionLinks = NULL;
52
53 /**
54 * Get the action links for this page.
55 *
56 * @param
57 *
a6c01b45 58 * @return array
6a488035 59 */
d1424f8f 60 public static function &actionLinks() {
6a488035
TO
61 // check if variable _actionsLinks is populated
62 if (!self::$_actionLinks) {
63 // helper variable for nicer formatting
64 $copyExtra = ts('Are you sure you want to make a copy of this Profile?');
65 self::$_actionLinks = array(
66 CRM_Core_Action::BROWSE => array(
67 'name' => ts('Fields'),
68 'url' => 'civicrm/admin/uf/group/field',
69 'qs' => 'reset=1&action=browse&gid=%%id%%',
70 'title' => ts('View and Edit Fields'),
71 ),
72 CRM_Core_Action::UPDATE => array(
73 'name' => ts('Settings'),
74 'url' => 'civicrm/admin/uf/group/update',
75 'qs' => 'action=update&id=%%id%%&context=group',
76 'title' => ts('Edit CiviCRM Profile Group'),
77 ),
78 CRM_Core_Action::PREVIEW => array(
79 'name' => ts('Preview'),
80 'url' => 'civicrm/admin/uf/group',
81 'qs' => 'action=preview&id=%%id%%&field=0&context=group',
82 'title' => ts('Edit CiviCRM Profile Group'),
83 ),
84 CRM_Core_Action::ADD => array(
3495d8d7 85 'name' => ts('Use - Create Mode'),
6a488035
TO
86 'url' => 'civicrm/profile/create',
87 'qs' => 'gid=%%id%%&reset=1',
3495d8d7
J
88 'title' => ts('Use - Create Mode'),
89 'fe' => TRUE,
90 ),
91 CRM_Core_Action::ADVANCED => array(
92 'name' => ts('Use - Edit Mode'),
93 'url' => 'civicrm/profile/edit',
94 'qs' => 'gid=%%id%%&reset=1',
95 'title' => ts('Use - Edit Mode'),
9147c186 96 'fe' => TRUE,
6a488035 97 ),
9865c956 98 CRM_Core_Action::BASIC => array(
3495d8d7 99 'name' => ts('Use - Listings Mode'),
9865c956
DG
100 'url' => 'civicrm/profile',
101 'qs' => 'gid=%%id%%&reset=1',
3495d8d7 102 'title' => ts('Use - Listings Mode'),
9147c186 103 'fe' => TRUE,
9865c956 104 ),
6a488035
TO
105 CRM_Core_Action::DISABLE => array(
106 'name' => ts('Disable'),
4d17a233 107 'ref' => 'crm-enable-disable',
6a488035
TO
108 'title' => ts('Disable CiviCRM Profile Group'),
109 ),
110 CRM_Core_Action::ENABLE => array(
111 'name' => ts('Enable'),
4d17a233 112 'ref' => 'crm-enable-disable',
6a488035
TO
113 'title' => ts('Enable CiviCRM Profile Group'),
114 ),
115 CRM_Core_Action::DELETE => array(
116 'name' => ts('Delete'),
117 'url' => 'civicrm/admin/uf/group',
118 'qs' => 'action=delete&id=%%id%%',
119 'title' => ts('Delete CiviCRM Profile Group'),
120 ),
121 CRM_Core_Action::PROFILE => array(
122 'name' => ts('HTML Form Snippet'),
123 'url' => 'civicrm/admin/uf/group',
124 'qs' => 'action=profile&gid=%%id%%',
125 'title' => ts('HTML Form Snippet for this Profile'),
126 ),
127 CRM_Core_Action::COPY => array(
3495d8d7 128 'name' => ts('Copy'),
6a488035
TO
129 'url' => 'civicrm/admin/uf/group',
130 'qs' => 'action=copy&gid=%%id%%',
131 'title' => ts('Make a Copy of CiviCRM Profile Group'),
132 'extra' => 'onclick = "return confirm(\'' . $copyExtra . '\');"',
133 ),
134 );
135 }
136 return self::$_actionLinks;
137 }
138
139 /**
140 * Run the page.
141 *
142 * This method is called after the page is created. It checks for the
143 * type of action and executes that action.
144 * Finally it calls the parent's run method.
145 *
146 * @param
147 *
148 * @return void
6a488035 149 */
00be9182 150 public function run() {
6a488035
TO
151 // get the requested action
152 $action = CRM_Utils_Request::retrieve('action', 'String',
153 $this, FALSE,
154 // default to 'browse'
155 'browse'
156 );
157
158 // assign vars to templates
159 $this->assign('action', $action);
edb21379 160 $this->assign('selectedChild', CRM_Utils_Request::retrieve('selectedChild', 'String', $this));
6a488035
TO
161 $id = CRM_Utils_Request::retrieve('id', 'Positive',
162 $this, FALSE, 0
163 );
164
165 //set the context and then start w/ action.
166 $this->setContext($id, $action);
167
168 // what action to take ?
169 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE | CRM_Core_Action::DISABLE)) {
170 $this->edit($id, $action);
171 }
172 else {
173 // if action is enable or disable do the needful.
174 if ($action & CRM_Core_Action::ENABLE) {
175 CRM_Core_BAO_UFGroup::setIsActive($id, 1);
176
177 // update cms integration with registration / my account
178 CRM_Utils_System::updateCategories();
179 }
180 elseif ($action & CRM_Core_Action::PROFILE) {
181 $this->profile();
182 CRM_Utils_System::setTitle(ts('%1 - HTML Form Snippet', array(1 => $this->_title)));
183 }
184 elseif ($action & CRM_Core_Action::PREVIEW) {
185 $this->preview($id, $action);
186 }
187 elseif ($action & CRM_Core_Action::COPY) {
188 $this->copy();
189 }
190 // finally browse the uf groups
191 $this->browse();
192 }
193 // parent run
194 return parent::run();
195 }
196
197 /**
dc195289 198 * make a copy of a profile, including
6a488035
TO
199 * all the fields in the profile
200 *
201 * @return void
6a488035 202 */
00be9182 203 public function copy() {
6a488035
TO
204 $gid = CRM_Utils_Request::retrieve('gid', 'Positive',
205 $this, TRUE, 0, 'GET'
206 );
207
208 CRM_Core_BAO_UFGroup::copy($gid);
209 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/uf/group', 'reset=1'));
210 }
211
212 /**
dc195289 213 * for profile mode (standalone html form ) for uf group
6a488035
TO
214 *
215 * @return void
6a488035 216 */
00be9182 217 public function profile() {
6a488035
TO
218 $config = CRM_Core_Config::singleton();
219
220 // reassign resource base to be the full url, CRM-4660
221 $config->resourceBase = $config->userFrameworkResourceURL;
222 $config->useFrameworkRelativeBase = $config->userFrameworkBaseURL;
223
224 $gid = CRM_Utils_Request::retrieve('gid', 'Positive',
225 $this, FALSE, 0, 'GET'
226 );
227 $controller = new CRM_Core_Controller_Simple('CRM_Profile_Form_Edit', ts('Create'), CRM_Core_Action::ADD,
228 FALSE, FALSE, TRUE
229 );
230 $controller->reset();
231 $controller->process();
232 $controller->set('gid', $gid);
233 $controller->setEmbedded(TRUE);
234 $controller->run();
235 $template = CRM_Core_Smarty::singleton();
236 $template->assign('gid', $gid);
237 $template->assign('tplFile', 'CRM/Profile/Form/Edit.tpl');
880e33cd 238 $profile = trim($template->fetch('CRM/Form/default.tpl'));
6a488035
TO
239
240 // not sure how to circumvent our own navigation system to generate the right form url
241 $urlReplaceWith = 'civicrm/profile/create&amp;gid=' . $gid . '&amp;reset=1';
242 if ($config->userSystem->is_drupal && $config->cleanURL) {
243 $urlReplaceWith = 'civicrm/profile/create?gid=' . $gid . '&amp;reset=1';
244 }
245 $profile = str_replace('civicrm/admin/uf/group', $urlReplaceWith, $profile);
246
8c63e816
CW
247 // FIXME: (CRM-3587) hack to make standalone profile work
248 // in wordpress and joomla without administrator login
6a488035
TO
249 if ($config->userFramework == 'Joomla') {
250 $profile = str_replace('/administrator/', '/index.php', $profile);
251 }
013e44b2 252 elseif ($config->userFramework == 'WordPress') {
c73b1c19 253 //@todo remove this part when it is OK to deprecate CIVICRM_UF_WP_BASEPAGE-CRM-15933
254 if (defined('CIVICRM_UF_WP_BASEPAGE')) {
255 $wpbase = CIVICRM_UF_WP_BASEPAGE;
256 }
257 elseif (!empty($config->wpBasePage)) {
258 $wpbase = $config->wpBasePage;
259 }
260 else {
261 $wpbase = 'index.php';
262 }
81badc57 263 $profile = str_replace('/wp-admin/admin.php', '/' . $wpbase . '/', $profile);
6a488035
TO
264 }
265
880e33cd
CW
266 // add header files
267 CRM_Core_Resources::singleton()->addCoreResources('html-header');
268 $profile = CRM_Core_Region::instance('html-header')->render('', FALSE) . $profile;
6a488035 269
6a488035
TO
270 $this->assign('profile', htmlentities($profile, ENT_NOQUOTES, 'UTF-8'));
271 //get the title of uf group
272 if ($gid) {
273 $title = CRM_Core_BAO_UFGroup::getTitle($gid);
274 $this->_title = $title;
275 }
276 else {
277 $title = 'Profile Form';
278 }
279
280 $this->assign('title', $title);
281 $this->assign('action', CRM_Core_Action::PROFILE);
282 $this->assign('isForm', 0);
283 }
284
285 /**
fe482240 286 * Edit uf group.
6a488035 287 *
5ce1712d
TO
288 * @param int $id
289 * Uf group id.
290 * @param string $action
291 * The action to be invoked.
6a488035
TO
292 *
293 * @return void
6a488035 294 */
00be9182 295 public function edit($id, $action) {
6a488035
TO
296 // create a simple controller for editing uf data
297 $controller = new CRM_Core_Controller_Simple('CRM_UF_Form_Group', ts('CiviCRM Profile Group'), $action);
298 $this->setContext($id, $action);
299 $controller->set('id', $id);
300 $controller->setEmbedded(TRUE);
301 $controller->process();
302 $controller->run();
303 }
304
305 /**
306 * Browse all uf data groups.
307 *
308 * @param
309 *
310 * @return void
6a488035 311 */
00be9182 312 public function browse($action = NULL) {
6a488035 313 $ufGroup = array();
6a488035
TO
314 $allUFGroups = CRM_Core_BAO_UFGroup::getModuleUFGroup();
315 if (empty($allUFGroups)) {
316 return;
317 }
318
ff4f7744 319 $ufGroups = CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFField', 'uf_group_id');
6a488035
TO
320 CRM_Utils_Hook::aclGroup(CRM_Core_Permission::ADMIN, NULL, 'civicrm_uf_group', $ufGroups, $allUFGroups);
321
322 foreach ($allUFGroups as $id => $value) {
323 $ufGroup[$id] = array();
324 $ufGroup[$id]['id'] = $id;
325 $ufGroup[$id]['title'] = $value['title'];
bcbde5ca
AW
326 $ufGroup[$id]['created_id'] = $value['created_id'];
327 $ufGroup[$id]['created_by'] = CRM_Contact_BAO_Contact::displayName($value['created_id']);
328 $ufGroup[$id]['description'] = $value['description'];
6a488035
TO
329 $ufGroup[$id]['is_active'] = $value['is_active'];
330 $ufGroup[$id]['group_type'] = $value['group_type'];
331 $ufGroup[$id]['is_reserved'] = $value['is_reserved'];
332
333 // form all action links
d1424f8f 334 $action = array_sum(array_keys(self::actionLinks()));
6a488035
TO
335
336 // update enable/disable links depending on uf_group properties.
337 if ($value['is_active']) {
338 $action -= CRM_Core_Action::ENABLE;
339 }
340 else {
341 $action -= CRM_Core_Action::DISABLE;
342 }
343
344 // drop certain actions if the profile is reserved
345 if ($value['is_reserved']) {
346 $action -= CRM_Core_Action::UPDATE;
347 $action -= CRM_Core_Action::DISABLE;
348 $action -= CRM_Core_Action::DELETE;
349 }
350
351 $groupTypes = self::extractGroupTypes($value['group_type']);
6a488035 352
b233e1b4 353 // drop Create, Edit and View mode links if profile group_type is one of the following:
4d4daa2d 354 // Contribution, Membership, Activity, Participant, Case, Grant
7e50650d
PN
355 $isMixedProfile = CRM_Core_BAO_UFField::checkProfileType($id);
356 if ($isMixedProfile) {
6a488035 357 $action -= CRM_Core_Action::ADD;
caa0151f
PN
358 $action -= CRM_Core_Action::ADVANCED;
359 $action -= CRM_Core_Action::BASIC;
360 $action -= CRM_Core_Action::PROFILE;
6a488035
TO
361 }
362
b233e1b4 363 $ufGroup[$id]['group_type'] = self::formatGroupTypes($groupTypes);
6a488035
TO
364
365 $ufGroup[$id]['action'] = CRM_Core_Action::formLink(self::actionLinks(), $action,
87dab4a4
AH
366 array('id' => $id),
367 ts('more'),
368 FALSE,
369 'ufGroup.row.actions',
370 'UFGroup',
371 $id
6a488035
TO
372 );
373 //get the "Used For" from uf_join
374 $ufGroup[$id]['module'] = implode(', ', CRM_Core_BAO_UFGroup::getUFJoinRecord($id, TRUE));
375 }
376
377 $this->assign('rows', $ufGroup);
378 }
379
380 /**
fe482240 381 * for preview mode for ufoup.
6a488035 382 *
5ce1712d
TO
383 * @param int $id
384 * Uf group id.
6a488035 385 *
3bdf1f3a 386 * @param int $action
6a488035 387 */
00be9182 388 public function preview($id, $action) {
6a488035
TO
389 $controller = new CRM_Core_Controller_Simple('CRM_UF_Form_Preview', ts('CiviCRM Profile Group Preview'), NULL);
390 $controller->set('id', $id);
391 $controller->setEmbedded(TRUE);
392 $controller->process();
393 $controller->run();
394 }
395
624e56fa 396 /**
100fef9d 397 * @param int $id
624e56fa
EM
398 * @param $action
399 */
00be9182 400 public function setContext($id, $action) {
6a488035
TO
401 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
402
403 //we need to differentiate context for update and preview profile.
404 if (!$context && !($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::PREVIEW))) {
405 $context = 'group';
406 }
407
408 if ($context == 'field') {
409 $url = CRM_Utils_System::url('civicrm/admin/uf/group/field', "reset=1&action=browse&gid={$id}");
410 }
411 else {
412 $url = CRM_Utils_System::url('civicrm/admin/uf/group', 'reset=1&action=browse');
413 }
414
415 $session = CRM_Core_Session::singleton();
416 $session->pushUserContext($url);
417 }
418
624e56fa
EM
419 /**
420 * @param $groupType
421 *
422 * @return array
423 */
00be9182 424 public static function extractGroupTypes($groupType) {
6a488035
TO
425 $returnGroupTypes = array();
426 if (!$groupType) {
427 return $returnGroupTypes;
428 }
429
430 $groupTypeParts = explode(CRM_Core_DAO::VALUE_SEPARATOR, $groupType);
431 foreach (explode(',', $groupTypeParts[0]) as $type) {
432 $returnGroupTypes[$type] = $type;
433 }
434
a7488080 435 if (!empty($groupTypeParts[1])) {
6a488035
TO
436 foreach (explode(',', $groupTypeParts[1]) as $typeValue) {
437 $groupTypeValues = $valueLabels = array();
438 $valueParts = explode(':', $typeValue);
439 $typeName = NULL;
440 switch ($valueParts[0]) {
441 case 'ContributionType':
442 $typeName = 'Contribution';
443 $valueLabels = CRM_Contribute_PseudoConstant::financialType();
444 break;
445
446 case 'ParticipantRole':
447 $typeName = 'Participant';
448 $valueLabels = CRM_Event_PseudoConstant::participantRole();
449 break;
450
451 case 'ParticipantEventName':
452 $typeName = 'Participant';
453 $valueLabels = CRM_Event_PseudoConstant::event();
454 break;
455
456 case 'ParticipantEventType':
457 $typeName = 'Participant';
458 $valueLabels = CRM_Event_PseudoConstant::eventType();
459 break;
460
461 case 'MembershipType':
462 $typeName = 'Membership';
463 $valueLabels = CRM_Member_PseudoConstant::membershipType();
464 break;
465
466 case 'ActivityType':
467 $typeName = 'Activity';
468 $valueLabels = CRM_Core_PseudoConstant::ActivityType(TRUE, TRUE, FALSE, 'label', TRUE);
469 break;
9147c186 470
1cb28d5d 471 case 'CaseType':
472 $typeName = 'Case';
473 $valueLabels = CRM_Case_PseudoConstant::caseType();
474 break;
6a488035
TO
475 }
476
477 foreach ($valueParts as $val) {
478 if (CRM_Utils_Rule::integer($val)) {
479 $groupTypeValues[$val] = CRM_Utils_Array::value($val, $valueLabels);
480 }
481 }
482
483 if (!is_array($returnGroupTypes[$typeName])) {
484 $returnGroupTypes[$typeName] = array();
485 }
486 $returnGroupTypes[$typeName][$valueParts[0]] = $groupTypeValues;
487 }
488 }
489 return $returnGroupTypes;
490 }
96025800 491
b233e1b4
CW
492 /**
493 * Format 'group_type' field for display
494 *
495 * @param array $groupTypes
496 * output from self::extractGroupTypes
497 * @return string
498 */
499 public static function formatGroupTypes($groupTypes) {
500 $groupTypesString = '';
501 if (!empty($groupTypes)) {
502 $groupTypesStrings = array();
503 foreach ($groupTypes as $groupType => $typeValues) {
504 if (is_array($typeValues)) {
505 if ($groupType == 'Participant') {
506 foreach ($typeValues as $subType => $subTypeValues) {
507 $groupTypesStrings[] = $subType . '::' . implode(': ', $subTypeValues);
508 }
509 }
510 else {
511 $groupTypesStrings[] = $groupType . '::' . implode(': ', current($typeValues));
512 }
513 }
514 else {
515 $groupTypesStrings[] = $groupType;
516 }
517 }
518 $groupTypesString = implode(', ', $groupTypesStrings);
519 }
520 return $groupTypesString;
521 }
522
6a488035 523}