Update EventFees.tpl
[civicrm-core.git] / CRM / UF / Page / Group.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
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 */
00be9182 60 public 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(
85 'name' => ts('Use Profile-Create Mode'),
86 'url' => 'civicrm/profile/create',
87 'qs' => 'gid=%%id%%&reset=1',
88 'title' => ts('Use Profile-Create Mode'),
9147c186 89 'fe' => TRUE,
6a488035 90 ),
9865c956
DG
91 CRM_Core_Action::BASIC => array(
92 'name' => ts('Use Profile-Listings Mode'),
93 'url' => 'civicrm/profile',
94 'qs' => 'gid=%%id%%&reset=1',
95 'title' => ts('Use Profile-Listings Mode'),
9147c186 96 'fe' => TRUE,
9865c956 97 ),
6a488035
TO
98 CRM_Core_Action::DISABLE => array(
99 'name' => ts('Disable'),
4d17a233 100 'ref' => 'crm-enable-disable',
6a488035
TO
101 'title' => ts('Disable CiviCRM Profile Group'),
102 ),
103 CRM_Core_Action::ENABLE => array(
104 'name' => ts('Enable'),
4d17a233 105 'ref' => 'crm-enable-disable',
6a488035
TO
106 'title' => ts('Enable CiviCRM Profile Group'),
107 ),
108 CRM_Core_Action::DELETE => array(
109 'name' => ts('Delete'),
110 'url' => 'civicrm/admin/uf/group',
111 'qs' => 'action=delete&id=%%id%%',
112 'title' => ts('Delete CiviCRM Profile Group'),
113 ),
114 CRM_Core_Action::PROFILE => array(
115 'name' => ts('HTML Form Snippet'),
116 'url' => 'civicrm/admin/uf/group',
117 'qs' => 'action=profile&gid=%%id%%',
118 'title' => ts('HTML Form Snippet for this Profile'),
119 ),
120 CRM_Core_Action::COPY => array(
121 'name' => ts('Copy Profile'),
122 'url' => 'civicrm/admin/uf/group',
123 'qs' => 'action=copy&gid=%%id%%',
124 'title' => ts('Make a Copy of CiviCRM Profile Group'),
125 'extra' => 'onclick = "return confirm(\'' . $copyExtra . '\');"',
126 ),
127 );
128 }
129 return self::$_actionLinks;
130 }
131
132 /**
133 * Run the page.
134 *
135 * This method is called after the page is created. It checks for the
136 * type of action and executes that action.
137 * Finally it calls the parent's run method.
138 *
139 * @param
140 *
141 * @return void
6a488035 142 */
00be9182 143 public function run() {
6a488035
TO
144 // get the requested action
145 $action = CRM_Utils_Request::retrieve('action', 'String',
146 $this, FALSE,
147 // default to 'browse'
148 'browse'
149 );
150
151 // assign vars to templates
152 $this->assign('action', $action);
153 $id = CRM_Utils_Request::retrieve('id', 'Positive',
154 $this, FALSE, 0
155 );
156
157 //set the context and then start w/ action.
158 $this->setContext($id, $action);
159
160 // what action to take ?
161 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE | CRM_Core_Action::DISABLE)) {
162 $this->edit($id, $action);
163 }
164 else {
165 // if action is enable or disable do the needful.
166 if ($action & CRM_Core_Action::ENABLE) {
167 CRM_Core_BAO_UFGroup::setIsActive($id, 1);
168
169 // update cms integration with registration / my account
170 CRM_Utils_System::updateCategories();
171 }
172 elseif ($action & CRM_Core_Action::PROFILE) {
173 $this->profile();
174 CRM_Utils_System::setTitle(ts('%1 - HTML Form Snippet', array(1 => $this->_title)));
175 }
176 elseif ($action & CRM_Core_Action::PREVIEW) {
177 $this->preview($id, $action);
178 }
179 elseif ($action & CRM_Core_Action::COPY) {
180 $this->copy();
181 }
182 // finally browse the uf groups
183 $this->browse();
184 }
185 // parent run
186 return parent::run();
187 }
188
189 /**
dc195289 190 * make a copy of a profile, including
6a488035
TO
191 * all the fields in the profile
192 *
193 * @return void
6a488035 194 */
00be9182 195 public function copy() {
6a488035
TO
196 $gid = CRM_Utils_Request::retrieve('gid', 'Positive',
197 $this, TRUE, 0, 'GET'
198 );
199
200 CRM_Core_BAO_UFGroup::copy($gid);
201 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/uf/group', 'reset=1'));
202 }
203
204 /**
dc195289 205 * for profile mode (standalone html form ) for uf group
6a488035
TO
206 *
207 * @return void
6a488035 208 */
00be9182 209 public function profile() {
6a488035
TO
210 $config = CRM_Core_Config::singleton();
211
212 // reassign resource base to be the full url, CRM-4660
213 $config->resourceBase = $config->userFrameworkResourceURL;
214 $config->useFrameworkRelativeBase = $config->userFrameworkBaseURL;
215
216 $gid = CRM_Utils_Request::retrieve('gid', 'Positive',
217 $this, FALSE, 0, 'GET'
218 );
219 $controller = new CRM_Core_Controller_Simple('CRM_Profile_Form_Edit', ts('Create'), CRM_Core_Action::ADD,
220 FALSE, FALSE, TRUE
221 );
222 $controller->reset();
223 $controller->process();
224 $controller->set('gid', $gid);
225 $controller->setEmbedded(TRUE);
226 $controller->run();
227 $template = CRM_Core_Smarty::singleton();
228 $template->assign('gid', $gid);
229 $template->assign('tplFile', 'CRM/Profile/Form/Edit.tpl');
880e33cd 230 $profile = trim($template->fetch('CRM/Form/default.tpl'));
6a488035
TO
231
232 // not sure how to circumvent our own navigation system to generate the right form url
233 $urlReplaceWith = 'civicrm/profile/create&amp;gid=' . $gid . '&amp;reset=1';
234 if ($config->userSystem->is_drupal && $config->cleanURL) {
235 $urlReplaceWith = 'civicrm/profile/create?gid=' . $gid . '&amp;reset=1';
236 }
237 $profile = str_replace('civicrm/admin/uf/group', $urlReplaceWith, $profile);
238
8c63e816
CW
239 // FIXME: (CRM-3587) hack to make standalone profile work
240 // in wordpress and joomla without administrator login
6a488035
TO
241 if ($config->userFramework == 'Joomla') {
242 $profile = str_replace('/administrator/', '/index.php', $profile);
243 }
013e44b2
CW
244 elseif ($config->userFramework == 'WordPress') {
245 $profile = str_replace('/wp-admin/admin.php', '/index.php', $profile);
6a488035
TO
246 }
247
880e33cd
CW
248 // add header files
249 CRM_Core_Resources::singleton()->addCoreResources('html-header');
250 $profile = CRM_Core_Region::instance('html-header')->render('', FALSE) . $profile;
6a488035 251
6a488035
TO
252 $this->assign('profile', htmlentities($profile, ENT_NOQUOTES, 'UTF-8'));
253 //get the title of uf group
254 if ($gid) {
255 $title = CRM_Core_BAO_UFGroup::getTitle($gid);
256 $this->_title = $title;
257 }
258 else {
259 $title = 'Profile Form';
260 }
261
262 $this->assign('title', $title);
263 $this->assign('action', CRM_Core_Action::PROFILE);
264 $this->assign('isForm', 0);
265 }
266
267 /**
fe482240 268 * Edit uf group.
6a488035 269 *
5ce1712d
TO
270 * @param int $id
271 * Uf group id.
272 * @param string $action
273 * The action to be invoked.
6a488035
TO
274 *
275 * @return void
6a488035 276 */
00be9182 277 public function edit($id, $action) {
6a488035
TO
278 // create a simple controller for editing uf data
279 $controller = new CRM_Core_Controller_Simple('CRM_UF_Form_Group', ts('CiviCRM Profile Group'), $action);
280 $this->setContext($id, $action);
281 $controller->set('id', $id);
282 $controller->setEmbedded(TRUE);
283 $controller->process();
284 $controller->run();
285 }
286
287 /**
288 * Browse all uf data groups.
289 *
290 * @param
291 *
292 * @return void
6a488035 293 */
00be9182 294 public function browse($action = NULL) {
6a488035
TO
295 $ufGroup = array();
296 $allUFGroups = array();
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) {
306 $ufGroup[$id] = array();
307 $ufGroup[$id]['id'] = $id;
308 $ufGroup[$id]['title'] = $value['title'];
bcbde5ca
AW
309 $ufGroup[$id]['created_id'] = $value['created_id'];
310 $ufGroup[$id]['created_by'] = CRM_Contact_BAO_Contact::displayName($value['created_id']);
311 $ufGroup[$id]['description'] = $value['description'];
6a488035
TO
312 $ufGroup[$id]['is_active'] = $value['is_active'];
313 $ufGroup[$id]['group_type'] = $value['group_type'];
314 $ufGroup[$id]['is_reserved'] = $value['is_reserved'];
315
316 // form all action links
317 $action = array_sum(array_keys($this->actionLinks()));
318
319 // update enable/disable links depending on uf_group properties.
320 if ($value['is_active']) {
321 $action -= CRM_Core_Action::ENABLE;
322 }
323 else {
324 $action -= CRM_Core_Action::DISABLE;
325 }
326
327 // drop certain actions if the profile is reserved
328 if ($value['is_reserved']) {
329 $action -= CRM_Core_Action::UPDATE;
330 $action -= CRM_Core_Action::DISABLE;
331 $action -= CRM_Core_Action::DELETE;
332 }
333
334 $groupTypes = self::extractGroupTypes($value['group_type']);
1cb28d5d 335 $groupComponents = array('Contribution', 'Membership', 'Activity', 'Participant', 'Case');
6a488035
TO
336
337 // drop Create, Edit and View mode links if profile group_type is Contribution, Membership, Activities or Participant
338 $componentFound = array_intersect($groupComponents, array_keys($groupTypes));
339 if (!empty($componentFound)) {
340 $action -= CRM_Core_Action::ADD;
341 }
342
343 $groupTypesString = '';
344 if (!empty($groupTypes)) {
345 $groupTypesStrings = array();
346 foreach ($groupTypes as $groupType => $typeValues) {
347 if (is_array($typeValues)) {
348 if ($groupType == 'Participant') {
349 foreach ($typeValues as $subType => $subTypeValues) {
350 $groupTypesStrings[] = $subType . '::' . implode(': ', $subTypeValues);
351 }
352 }
353 else {
354 $groupTypesStrings[] = $groupType . '::' . implode(': ', current($typeValues));
355 }
356 }
357 else {
358 $groupTypesStrings[] = $groupType;
359 }
360 }
361 $groupTypesString = implode(', ', $groupTypesStrings);
362 }
363 $ufGroup[$id]['group_type'] = $groupTypesString;
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 *
77b97be7
EM
386 * @param $action
387 *
6a488035 388 * @return void
6a488035 389 */
00be9182 390 public function preview($id, $action) {
6a488035
TO
391 $controller = new CRM_Core_Controller_Simple('CRM_UF_Form_Preview', ts('CiviCRM Profile Group Preview'), NULL);
392 $controller->set('id', $id);
393 $controller->setEmbedded(TRUE);
394 $controller->process();
395 $controller->run();
396 }
397
624e56fa 398 /**
100fef9d 399 * @param int $id
624e56fa
EM
400 * @param $action
401 */
00be9182 402 public function setContext($id, $action) {
6a488035
TO
403 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
404
405 //we need to differentiate context for update and preview profile.
406 if (!$context && !($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::PREVIEW))) {
407 $context = 'group';
408 }
409
410 if ($context == 'field') {
411 $url = CRM_Utils_System::url('civicrm/admin/uf/group/field', "reset=1&action=browse&gid={$id}");
412 }
413 else {
414 $url = CRM_Utils_System::url('civicrm/admin/uf/group', 'reset=1&action=browse');
415 }
416
417 $session = CRM_Core_Session::singleton();
418 $session->pushUserContext($url);
419 }
420
624e56fa
EM
421 /**
422 * @param $groupType
423 *
424 * @return array
425 */
00be9182 426 public static function extractGroupTypes($groupType) {
6a488035
TO
427 $returnGroupTypes = array();
428 if (!$groupType) {
429 return $returnGroupTypes;
430 }
431
432 $groupTypeParts = explode(CRM_Core_DAO::VALUE_SEPARATOR, $groupType);
433 foreach (explode(',', $groupTypeParts[0]) as $type) {
434 $returnGroupTypes[$type] = $type;
435 }
436
a7488080 437 if (!empty($groupTypeParts[1])) {
6a488035
TO
438 foreach (explode(',', $groupTypeParts[1]) as $typeValue) {
439 $groupTypeValues = $valueLabels = array();
440 $valueParts = explode(':', $typeValue);
441 $typeName = NULL;
442 switch ($valueParts[0]) {
443 case 'ContributionType':
444 $typeName = 'Contribution';
445 $valueLabels = CRM_Contribute_PseudoConstant::financialType();
446 break;
447
448 case 'ParticipantRole':
449 $typeName = 'Participant';
450 $valueLabels = CRM_Event_PseudoConstant::participantRole();
451 break;
452
453 case 'ParticipantEventName':
454 $typeName = 'Participant';
455 $valueLabels = CRM_Event_PseudoConstant::event();
456 break;
457
458 case 'ParticipantEventType':
459 $typeName = 'Participant';
460 $valueLabels = CRM_Event_PseudoConstant::eventType();
461 break;
462
463 case 'MembershipType':
464 $typeName = 'Membership';
465 $valueLabels = CRM_Member_PseudoConstant::membershipType();
466 break;
467
468 case 'ActivityType':
469 $typeName = 'Activity';
470 $valueLabels = CRM_Core_PseudoConstant::ActivityType(TRUE, TRUE, FALSE, 'label', TRUE);
471 break;
9147c186 472
1cb28d5d 473 case 'CaseType':
474 $typeName = 'Case';
475 $valueLabels = CRM_Case_PseudoConstant::caseType();
476 break;
6a488035
TO
477 }
478
479 foreach ($valueParts as $val) {
480 if (CRM_Utils_Rule::integer($val)) {
481 $groupTypeValues[$val] = CRM_Utils_Array::value($val, $valueLabels);
482 }
483 }
484
485 if (!is_array($returnGroupTypes[$typeName])) {
486 $returnGroupTypes[$typeName] = array();
487 }
488 $returnGroupTypes[$typeName][$valueParts[0]] = $groupTypeValues;
489 }
490 }
491 return $returnGroupTypes;
492 }
96025800 493
6a488035 494}