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