b6dbcd6331f335d9dc1d900adaf0fe5f3b3ee31b
[civicrm-core.git] / CRM / UF / Page / Group.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 public 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 */
144 public function run() {
145 // get the requested action
146 $action = CRM_Utils_Request::retrieve('action', 'String',
147 $this, FALSE,
148 // default to 'browse'
149 'browse'
150 );
151
152 // assign vars to templates
153 $this->assign('action', $action);
154 $id = CRM_Utils_Request::retrieve('id', 'Positive',
155 $this, FALSE, 0
156 );
157
158 //set the context and then start w/ action.
159 $this->setContext($id, $action);
160
161 // what action to take ?
162 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE | CRM_Core_Action::DISABLE)) {
163 $this->edit($id, $action);
164 }
165 else {
166 // if action is enable or disable do the needful.
167 if ($action & CRM_Core_Action::ENABLE) {
168 CRM_Core_BAO_UFGroup::setIsActive($id, 1);
169
170 // update cms integration with registration / my account
171 CRM_Utils_System::updateCategories();
172 }
173 elseif ($action & CRM_Core_Action::PROFILE) {
174 $this->profile();
175 CRM_Utils_System::setTitle(ts('%1 - HTML Form Snippet', array(1 => $this->_title)));
176 }
177 elseif ($action & CRM_Core_Action::PREVIEW) {
178 $this->preview($id, $action);
179 }
180 elseif ($action & CRM_Core_Action::COPY) {
181 $this->copy();
182 }
183 // finally browse the uf groups
184 $this->browse();
185 }
186 // parent run
187 return parent::run();
188 }
189
190 /**
191 * This function is to make a copy of a profile, including
192 * all the fields in the profile
193 *
194 * @return void
195 */
196 public function copy() {
197 $gid = CRM_Utils_Request::retrieve('gid', 'Positive',
198 $this, TRUE, 0, 'GET'
199 );
200
201 CRM_Core_BAO_UFGroup::copy($gid);
202 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/uf/group', 'reset=1'));
203 }
204
205 /**
206 * This function is for profile mode (standalone html form ) for uf group
207 *
208 * @return void
209 */
210 public function profile() {
211 $config = CRM_Core_Config::singleton();
212
213 // reassign resource base to be the full url, CRM-4660
214 $config->resourceBase = $config->userFrameworkResourceURL;
215 $config->useFrameworkRelativeBase = $config->userFrameworkBaseURL;
216
217 $gid = CRM_Utils_Request::retrieve('gid', 'Positive',
218 $this, FALSE, 0, 'GET'
219 );
220 $controller = new CRM_Core_Controller_Simple('CRM_Profile_Form_Edit', ts('Create'), CRM_Core_Action::ADD,
221 FALSE, FALSE, TRUE
222 );
223 $controller->reset();
224 $controller->process();
225 $controller->set('gid', $gid);
226 $controller->setEmbedded(TRUE);
227 $controller->run();
228 $template = CRM_Core_Smarty::singleton();
229 $template->assign('gid', $gid);
230 $template->assign('tplFile', 'CRM/Profile/Form/Edit.tpl');
231 $profile = trim($template->fetch('CRM/Form/default.tpl'));
232
233 // not sure how to circumvent our own navigation system to generate the right form url
234 $urlReplaceWith = 'civicrm/profile/create&amp;gid=' . $gid . '&amp;reset=1';
235 if ($config->userSystem->is_drupal && $config->cleanURL) {
236 $urlReplaceWith = 'civicrm/profile/create?gid=' . $gid . '&amp;reset=1';
237 }
238 $profile = str_replace('civicrm/admin/uf/group', $urlReplaceWith, $profile);
239
240 // FIXME: (CRM-3587) hack to make standalone profile work
241 // in wordpress and joomla without administrator login
242 if ($config->userFramework == 'Joomla') {
243 $profile = str_replace('/administrator/', '/index.php', $profile);
244 }
245 elseif ($config->userFramework == 'WordPress') {
246 $profile = str_replace('/wp-admin/admin.php', '/index.php', $profile);
247 }
248
249 // add header files
250 CRM_Core_Resources::singleton()->addCoreResources('html-header');
251 $profile = CRM_Core_Region::instance('html-header')->render('', FALSE) . $profile;
252
253 $this->assign('profile', htmlentities($profile, ENT_NOQUOTES, 'UTF-8'));
254 //get the title of uf group
255 if ($gid) {
256 $title = CRM_Core_BAO_UFGroup::getTitle($gid);
257 $this->_title = $title;
258 }
259 else {
260 $title = 'Profile Form';
261 }
262
263 $this->assign('title', $title);
264 $this->assign('action', CRM_Core_Action::PROFILE);
265 $this->assign('isForm', 0);
266 }
267
268 /**
269 * Edit uf group
270 *
271 * @param int $id uf group id
272 * @param string $action the action to be invoked
273 *
274 * @return void
275 */
276 public function edit($id, $action) {
277 // create a simple controller for editing uf data
278 $controller = new CRM_Core_Controller_Simple('CRM_UF_Form_Group', ts('CiviCRM Profile Group'), $action);
279 $this->setContext($id, $action);
280 $controller->set('id', $id);
281 $controller->setEmbedded(TRUE);
282 $controller->process();
283 $controller->run();
284 }
285
286 /**
287 * Browse all uf data groups.
288 *
289 * @param
290 *
291 * @return void
292 * @static
293 */
294 public function browse($action = NULL) {
295 $ufGroup = array();
296 $allUFGroups = array();
297 $allUFGroups = CRM_Core_BAO_UFGroup::getModuleUFGroup();
298 if (empty($allUFGroups)) {
299 return;
300 }
301
302 $ufGroups = CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFField', 'uf_group_id');
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'];
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'];
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']);
335 $groupComponents = array('Contribution', 'Membership', 'Activity', 'Participant', 'Case');
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,
366 array('id' => $id),
367 ts('more'),
368 FALSE,
369 'ufGroup.row.actions',
370 'UFGroup',
371 $id
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 /**
381 * This function is for preview mode for ufoup
382 *
383 * @param int $id uf group id
384 *
385 * @param $action
386 *
387 * @return void
388 */
389 public function preview($id, $action) {
390 $controller = new CRM_Core_Controller_Simple('CRM_UF_Form_Preview', ts('CiviCRM Profile Group Preview'), NULL);
391 $controller->set('id', $id);
392 $controller->setEmbedded(TRUE);
393 $controller->process();
394 $controller->run();
395 }
396
397 /**
398 * @param int $id
399 * @param $action
400 */
401 public function setContext($id, $action) {
402 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
403
404 //we need to differentiate context for update and preview profile.
405 if (!$context && !($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::PREVIEW))) {
406 $context = 'group';
407 }
408
409 if ($context == 'field') {
410 $url = CRM_Utils_System::url('civicrm/admin/uf/group/field', "reset=1&action=browse&gid={$id}");
411 }
412 else {
413 $url = CRM_Utils_System::url('civicrm/admin/uf/group', 'reset=1&action=browse');
414 }
415
416 $session = CRM_Core_Session::singleton();
417 $session->pushUserContext($url);
418 }
419
420 /**
421 * @param $groupType
422 *
423 * @return array
424 */
425 public static function extractGroupTypes($groupType) {
426 $returnGroupTypes = array();
427 if (!$groupType) {
428 return $returnGroupTypes;
429 }
430
431 $groupTypeParts = explode(CRM_Core_DAO::VALUE_SEPARATOR, $groupType);
432 foreach (explode(',', $groupTypeParts[0]) as $type) {
433 $returnGroupTypes[$type] = $type;
434 }
435
436 if (!empty($groupTypeParts[1])) {
437 foreach (explode(',', $groupTypeParts[1]) as $typeValue) {
438 $groupTypeValues = $valueLabels = array();
439 $valueParts = explode(':', $typeValue);
440 $typeName = NULL;
441 switch ($valueParts[0]) {
442 case 'ContributionType':
443 $typeName = 'Contribution';
444 $valueLabels = CRM_Contribute_PseudoConstant::financialType();
445 break;
446
447 case 'ParticipantRole':
448 $typeName = 'Participant';
449 $valueLabels = CRM_Event_PseudoConstant::participantRole();
450 break;
451
452 case 'ParticipantEventName':
453 $typeName = 'Participant';
454 $valueLabels = CRM_Event_PseudoConstant::event();
455 break;
456
457 case 'ParticipantEventType':
458 $typeName = 'Participant';
459 $valueLabels = CRM_Event_PseudoConstant::eventType();
460 break;
461
462 case 'MembershipType':
463 $typeName = 'Membership';
464 $valueLabels = CRM_Member_PseudoConstant::membershipType();
465 break;
466
467 case 'ActivityType':
468 $typeName = 'Activity';
469 $valueLabels = CRM_Core_PseudoConstant::ActivityType(TRUE, TRUE, FALSE, 'label', TRUE);
470 break;
471 case 'CaseType':
472 $typeName = 'Case';
473 $valueLabels = CRM_Case_PseudoConstant::caseType();
474 break;
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 }
491 }