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