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