8279ba39806e0ab32d4d51200aa8fdb7e761132e
[civicrm-core.git] / CRM / UF / Page / Group.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 ),
91 CRM_Core_Action::DISABLE => array(
92 'name' => ts('Disable'),
93 'ref' => 'crm-enable-disable',
94 'title' => ts('Disable CiviCRM Profile Group'),
95 ),
96 CRM_Core_Action::ENABLE => array(
97 'name' => ts('Enable'),
98 'ref' => 'crm-enable-disable',
99 'title' => ts('Enable CiviCRM Profile Group'),
100 ),
101 CRM_Core_Action::DELETE => array(
102 'name' => ts('Delete'),
103 'url' => 'civicrm/admin/uf/group',
104 'qs' => 'action=delete&id=%%id%%',
105 'title' => ts('Delete CiviCRM Profile Group'),
106 ),
107 CRM_Core_Action::PROFILE => array(
108 'name' => ts('HTML Form Snippet'),
109 'url' => 'civicrm/admin/uf/group',
110 'qs' => 'action=profile&gid=%%id%%',
111 'title' => ts('HTML Form Snippet for this Profile'),
112 ),
113 CRM_Core_Action::COPY => array(
114 'name' => ts('Copy Profile'),
115 'url' => 'civicrm/admin/uf/group',
116 'qs' => 'action=copy&gid=%%id%%',
117 'title' => ts('Make a Copy of CiviCRM Profile Group'),
118 'extra' => 'onclick = "return confirm(\'' . $copyExtra . '\');"',
119 ),
120 );
121 }
122 return self::$_actionLinks;
123 }
124
125 /**
126 * Run the page.
127 *
128 * This method is called after the page is created. It checks for the
129 * type of action and executes that action.
130 * Finally it calls the parent's run method.
131 *
132 * @param
133 *
134 * @return void
135 * @access public
136 */
137 function run() {
138 // get the requested action
139 $action = CRM_Utils_Request::retrieve('action', 'String',
140 $this, FALSE,
141 // default to 'browse'
142 'browse'
143 );
144
145 // assign vars to templates
146 $this->assign('action', $action);
147 $id = CRM_Utils_Request::retrieve('id', 'Positive',
148 $this, FALSE, 0
149 );
150
151 //set the context and then start w/ action.
152 $this->setContext($id, $action);
153
154 // what action to take ?
155 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE | CRM_Core_Action::DISABLE)) {
156 $this->edit($id, $action);
157 }
158 else {
159 // if action is enable or disable do the needful.
160 if ($action & CRM_Core_Action::ENABLE) {
161 CRM_Core_BAO_UFGroup::setIsActive($id, 1);
162
163 // update cms integration with registration / my account
164 CRM_Utils_System::updateCategories();
165 }
166 elseif ($action & CRM_Core_Action::PROFILE) {
167 $this->profile();
168 CRM_Utils_System::setTitle(ts('%1 - HTML Form Snippet', array(1 => $this->_title)));
169 }
170 elseif ($action & CRM_Core_Action::PREVIEW) {
171 $this->preview($id, $action);
172 }
173 elseif ($action & CRM_Core_Action::COPY) {
174 $this->copy();
175 }
176 // finally browse the uf groups
177 $this->browse();
178 }
179 // parent run
180 return parent::run();
181 }
182
183 /**
184 * This function is to make a copy of a profile, including
185 * all the fields in the profile
186 *
187 * @return void
188 * @access public
189 */
190 function copy() {
191 $gid = CRM_Utils_Request::retrieve('gid', 'Positive',
192 $this, TRUE, 0, 'GET'
193 );
194
195 CRM_Core_BAO_UFGroup::copy($gid);
196 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/uf/group', 'reset=1'));
197 }
198
199 /**
200 * This function is for profile mode (standalone html form ) for uf group
201 *
202 * @return void
203 * @access public
204 */
205 function profile() {
206 $config = CRM_Core_Config::singleton();
207
208 // reassign resource base to be the full url, CRM-4660
209 $config->resourceBase = $config->userFrameworkResourceURL;
210 $config->useFrameworkRelativeBase = $config->userFrameworkBaseURL;
211
212 $gid = CRM_Utils_Request::retrieve('gid', 'Positive',
213 $this, FALSE, 0, 'GET'
214 );
215 $controller = new CRM_Core_Controller_Simple('CRM_Profile_Form_Edit', ts('Create'), CRM_Core_Action::ADD,
216 FALSE, FALSE, TRUE
217 );
218 $controller->reset();
219 $controller->process();
220 $controller->set('gid', $gid);
221 $controller->setEmbedded(TRUE);
222 $controller->run();
223 $template = CRM_Core_Smarty::singleton();
224 $template->assign('gid', $gid);
225 $template->assign('tplFile', 'CRM/Profile/Form/Edit.tpl');
226 $profile = trim($template->fetch('CRM/Form/default.tpl'));
227
228 // not sure how to circumvent our own navigation system to generate the right form url
229 $urlReplaceWith = 'civicrm/profile/create&amp;gid=' . $gid . '&amp;reset=1';
230 if ($config->userSystem->is_drupal && $config->cleanURL) {
231 $urlReplaceWith = 'civicrm/profile/create?gid=' . $gid . '&amp;reset=1';
232 }
233 $profile = str_replace('civicrm/admin/uf/group', $urlReplaceWith, $profile);
234
235 // FIXME: (CRM-3587) hack to make standalone profile work
236 // in wordpress and joomla without administrator login
237 if ($config->userFramework == 'Joomla') {
238 $profile = str_replace('/administrator/', '/index.php', $profile);
239 }
240 elseif ($config->userFramework == 'WordPress') {
241 $profile = str_replace('/wp-admin/admin.php', '/index.php', $profile);
242 }
243
244 // add header files
245 CRM_Core_Resources::singleton()->addCoreResources('html-header');
246 $profile = CRM_Core_Region::instance('html-header')->render('', FALSE) . $profile;
247
248 $this->assign('profile', htmlentities($profile, ENT_NOQUOTES, 'UTF-8'));
249 //get the title of uf group
250 if ($gid) {
251 $title = CRM_Core_BAO_UFGroup::getTitle($gid);
252 $this->_title = $title;
253 }
254 else {
255 $title = 'Profile Form';
256 }
257
258 $this->assign('title', $title);
259 $this->assign('action', CRM_Core_Action::PROFILE);
260 $this->assign('isForm', 0);
261 }
262
263 /**
264 * edit uf group
265 *
266 * @param int $id uf group id
267 * @param string $action the action to be invoked
268 *
269 * @return void
270 * @access public
271 */
272 function edit($id, $action) {
273 // create a simple controller for editing uf data
274 $controller = new CRM_Core_Controller_Simple('CRM_UF_Form_Group', ts('CiviCRM Profile Group'), $action);
275 $this->setContext($id, $action);
276 $controller->set('id', $id);
277 $controller->setEmbedded(TRUE);
278 $controller->process();
279 $controller->run();
280 }
281
282 /**
283 * Browse all uf data groups.
284 *
285 * @param
286 *
287 * @return void
288 * @access public
289 * @static
290 */
291 function browse($action = NULL) {
292 $ufGroup = array();
293 $allUFGroups = array();
294 $allUFGroups = CRM_Core_BAO_UFGroup::getModuleUFGroup();
295 if (empty($allUFGroups)) {
296 return;
297 }
298
299 $ufGroups = CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFField', 'uf_group_id');
300 CRM_Utils_Hook::aclGroup(CRM_Core_Permission::ADMIN, NULL, 'civicrm_uf_group', $ufGroups, $allUFGroups);
301
302 foreach ($allUFGroups as $id => $value) {
303 $ufGroup[$id] = array();
304 $ufGroup[$id]['id'] = $id;
305 $ufGroup[$id]['title'] = $value['title'];
306 $ufGroup[$id]['created_id'] = $value['created_id'];
307 $ufGroup[$id]['created_by'] = CRM_Contact_BAO_Contact::displayName($value['created_id']);
308 $ufGroup[$id]['description'] = $value['description'];
309 $ufGroup[$id]['is_active'] = $value['is_active'];
310 $ufGroup[$id]['group_type'] = $value['group_type'];
311 $ufGroup[$id]['is_reserved'] = $value['is_reserved'];
312
313 // form all action links
314 $action = array_sum(array_keys($this->actionLinks()));
315
316 // update enable/disable links depending on uf_group properties.
317 if ($value['is_active']) {
318 $action -= CRM_Core_Action::ENABLE;
319 }
320 else {
321 $action -= CRM_Core_Action::DISABLE;
322 }
323
324 // drop certain actions if the profile is reserved
325 if ($value['is_reserved']) {
326 $action -= CRM_Core_Action::UPDATE;
327 $action -= CRM_Core_Action::DISABLE;
328 $action -= CRM_Core_Action::DELETE;
329 }
330
331 $groupTypes = self::extractGroupTypes($value['group_type']);
332 $groupComponents = array('Contribution', 'Membership', 'Activity', 'Participant');
333
334 // drop Create, Edit and View mode links if profile group_type is Contribution, Membership, Activities or Participant
335 $componentFound = array_intersect($groupComponents, array_keys($groupTypes));
336 if (!empty($componentFound)) {
337 $action -= CRM_Core_Action::ADD;
338 }
339
340 $groupTypesString = '';
341 if (!empty($groupTypes)) {
342 $groupTypesStrings = array();
343 foreach ($groupTypes as $groupType => $typeValues) {
344 if (is_array($typeValues)) {
345 if ($groupType == 'Participant') {
346 foreach ($typeValues as $subType => $subTypeValues) {
347 $groupTypesStrings[] = $subType . '::' . implode(': ', $subTypeValues);
348 }
349 }
350 else {
351 $groupTypesStrings[] = $groupType . '::' . implode(': ', current($typeValues));
352 }
353 }
354 else {
355 $groupTypesStrings[] = $groupType;
356 }
357 }
358 $groupTypesString = implode(', ', $groupTypesStrings);
359 }
360 $ufGroup[$id]['group_type'] = $groupTypesString;
361
362 $ufGroup[$id]['action'] = CRM_Core_Action::formLink(self::actionLinks(), $action,
363 array('id' => $id),
364 ts('more'),
365 FALSE,
366 'ufGroup.row.actions',
367 'UFGroup',
368 $id
369 );
370 //get the "Used For" from uf_join
371 $ufGroup[$id]['module'] = implode(', ', CRM_Core_BAO_UFGroup::getUFJoinRecord($id, TRUE));
372 }
373
374 $this->assign('rows', $ufGroup);
375 }
376
377 /**
378 * this function is for preview mode for ufoup
379 *
380 * @param int $id uf group id
381 *
382 * @return void
383 * @access public
384 */
385 function preview($id, $action) {
386 $controller = new CRM_Core_Controller_Simple('CRM_UF_Form_Preview', ts('CiviCRM Profile Group Preview'), NULL);
387 $controller->set('id', $id);
388 $controller->setEmbedded(TRUE);
389 $controller->process();
390 $controller->run();
391 }
392
393 function setContext($id, $action) {
394 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
395
396 //we need to differentiate context for update and preview profile.
397 if (!$context && !($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::PREVIEW))) {
398 $context = 'group';
399 }
400
401 if ($context == 'field') {
402 $url = CRM_Utils_System::url('civicrm/admin/uf/group/field', "reset=1&action=browse&gid={$id}");
403 }
404 else {
405 $url = CRM_Utils_System::url('civicrm/admin/uf/group', 'reset=1&action=browse');
406 }
407
408 $session = CRM_Core_Session::singleton();
409 $session->pushUserContext($url);
410 }
411
412 static function extractGroupTypes($groupType) {
413 $returnGroupTypes = array();
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 (CRM_Utils_Array::value(1, $groupTypeParts)) {
424 foreach (explode(',', $groupTypeParts[1]) as $typeValue) {
425 $groupTypeValues = $valueLabels = array();
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
460 foreach ($valueParts as $val) {
461 if (CRM_Utils_Rule::integer($val)) {
462 $groupTypeValues[$val] = CRM_Utils_Array::value($val, $valueLabels);
463 }
464 }
465
466 if (!is_array($returnGroupTypes[$typeName])) {
467 $returnGroupTypes[$typeName] = array();
468 }
469 $returnGroupTypes[$typeName][$valueParts[0]] = $groupTypeValues;
470 }
471 }
472 return $returnGroupTypes;
473 }
474 }
475