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