Merge pull request #230 from dlobo/CRM-12185
[civicrm-core.git] / CRM / UF / Page / Group.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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 'extra' => 'onclick = "enableDisable( %%id%%,\'' . 'CRM_Core_BAO_UFGroup' . '\',\'' . 'enable-disable\',0,\'UFGroup' . '\' );"',
94 'ref' => 'disable-action',
95 'title' => ts('Disable CiviCRM Profile Group'),
96 ),
97 CRM_Core_Action::ENABLE => array(
98 'name' => ts('Enable'),
99 'extra' => 'onclick = "enableDisable( %%id%%,\'' . 'CRM_Core_BAO_UFGroup' . '\',\'' . 'disable-enable\',0,\'UFGroup' . '\' );"',
100 'ref' => 'enable-action',
101 'title' => ts('Enable CiviCRM Profile Group'),
102 ),
103 CRM_Core_Action::DELETE => array(
104 'name' => ts('Delete'),
105 'url' => 'civicrm/admin/uf/group',
106 'qs' => 'action=delete&id=%%id%%',
107 'title' => ts('Delete CiviCRM Profile Group'),
108 ),
109 CRM_Core_Action::PROFILE => array(
110 'name' => ts('HTML Form Snippet'),
111 'url' => 'civicrm/admin/uf/group',
112 'qs' => 'action=profile&gid=%%id%%',
113 'title' => ts('HTML Form Snippet for this Profile'),
114 ),
115 CRM_Core_Action::COPY => array(
116 'name' => ts('Copy Profile'),
117 'url' => 'civicrm/admin/uf/group',
118 'qs' => 'action=copy&gid=%%id%%',
119 'title' => ts('Make a Copy of CiviCRM Profile Group'),
120 'extra' => 'onclick = "return confirm(\'' . $copyExtra . '\');"',
121 ),
122 );
123 }
124 return self::$_actionLinks;
125 }
126
127 /**
128 * Run the page.
129 *
130 * This method is called after the page is created. It checks for the
131 * type of action and executes that action.
132 * Finally it calls the parent's run method.
133 *
134 * @param
135 *
136 * @return void
137 * @access public
138 */
139 function run() {
140 // get the requested action
141 $action = CRM_Utils_Request::retrieve('action', 'String',
142 $this, FALSE,
143 // default to 'browse'
144 'browse'
145 );
146
147 // assign vars to templates
148 $this->assign('action', $action);
149 $id = CRM_Utils_Request::retrieve('id', 'Positive',
150 $this, FALSE, 0
151 );
152
153 //set the context and then start w/ action.
154 $this->setContext($id, $action);
155
156 // what action to take ?
157 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE | CRM_Core_Action::DISABLE)) {
158 $this->edit($id, $action);
159 }
160 else {
161 // if action is enable or disable do the needful.
162 if ($action & CRM_Core_Action::ENABLE) {
163 CRM_Core_BAO_UFGroup::setIsActive($id, 1);
164
165 // update cms integration with registration / my account
166 CRM_Utils_System::updateCategories();
167 }
168 elseif ($action & CRM_Core_Action::PROFILE) {
169 $this->profile();
170 CRM_Utils_System::setTitle(ts('%1 - HTML Form Snippet', array(1 => $this->_title)));
171 }
172 elseif ($action & CRM_Core_Action::PREVIEW) {
173 $this->preview($id, $action);
174 }
175 elseif ($action & CRM_Core_Action::COPY) {
176 $this->copy();
177 }
178 // finally browse the uf groups
179 $this->browse();
180 }
181 // parent run
182 return parent::run();
183 }
184
185 /**
186 * This function is to make a copy of a profile, including
187 * all the fields in the profile
188 *
189 * @return void
190 * @access public
191 */
192 function copy() {
193 $gid = CRM_Utils_Request::retrieve('gid', 'Positive',
194 $this, TRUE, 0, 'GET'
195 );
196
197 CRM_Core_BAO_UFGroup::copy($gid);
198 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/uf/group', 'reset=1'));
199 }
200
201 /**
202 * This function is for profile mode (standalone html form ) for uf group
203 *
204 * @return void
205 * @access public
206 */
207 function profile() {
208 $config = CRM_Core_Config::singleton();
209
210 // reassign resource base to be the full url, CRM-4660
211 $config->resourceBase = $config->userFrameworkResourceURL;
212 $config->useFrameworkRelativeBase = $config->userFrameworkBaseURL;
213
214 $gid = CRM_Utils_Request::retrieve('gid', 'Positive',
215 $this, FALSE, 0, 'GET'
216 );
217 $controller = new CRM_Core_Controller_Simple('CRM_Profile_Form_Edit', ts('Create'), CRM_Core_Action::ADD,
218 FALSE, FALSE, TRUE
219 );
220 $controller->reset();
221 $controller->process();
222 $controller->set('gid', $gid);
223 $controller->setEmbedded(TRUE);
224 $controller->run();
225 $template = CRM_Core_Smarty::singleton();
226 $template->assign('gid', $gid);
227 $template->assign('tplFile', 'CRM/Profile/Form/Edit.tpl');
228 $profile = trim($template->fetch('CRM/common/commonCSS.tpl'));
229 $profile .= trim($template->fetch('CRM/Form/default.tpl'));
230
231 // not sure how to circumvent our own navigation system to generate the right form url
232 $urlReplaceWith = 'civicrm/profile/create&amp;gid=' . $gid . '&amp;reset=1';
233 if ($config->userSystem->is_drupal && $config->cleanURL) {
234 $urlReplaceWith = 'civicrm/profile/create?gid=' . $gid . '&amp;reset=1';
235 }
236 $profile = str_replace('civicrm/admin/uf/group', $urlReplaceWith, $profile);
237
238 // FIXME: (CRM-3587) hack to make standalone profile in joomla work
239 // without administrator login
240 if ($config->userFramework == 'Joomla') {
241 $profile = str_replace('/administrator/', '/index.php', $profile);
242 }
243 else {
244 if ($config->userFramework == 'WordPress') {
245 $profile = str_replace('/wp-admin/admin.php', '/index.php', $profile);
246 }
247 }
248
249 // add jquery files
250 $profile = CRM_Utils_String::addJqueryFiles($profile);
251
252 // prevent jquery conflict
253 $profile .= '<script type="text/javascript">jQuery.noConflict(true);</script>';
254 $this->assign('profile', htmlentities($profile, ENT_NOQUOTES, 'UTF-8'));
255 //get the title of uf group
256 if ($gid) {
257 $title = CRM_Core_BAO_UFGroup::getTitle($gid);
258 $this->_title = $title;
259 }
260 else {
261 $title = 'Profile Form';
262 }
263
264 $this->assign('title', $title);
265 $this->assign('action', CRM_Core_Action::PROFILE);
266 $this->assign('isForm', 0);
267 }
268
269 /**
270 * edit uf group
271 *
272 * @param int $id uf group id
273 * @param string $action the action to be invoked
274 *
275 * @return void
276 * @access public
277 */
278 function edit($id, $action) {
279 // create a simple controller for editing uf data
280 $controller = new CRM_Core_Controller_Simple('CRM_UF_Form_Group', ts('CiviCRM Profile Group'), $action);
281 $this->setContext($id, $action);
282 $controller->set('id', $id);
283 $controller->setEmbedded(TRUE);
284 $controller->process();
285 $controller->run();
286 }
287
288 /**
289 * Browse all uf data groups.
290 *
291 * @param
292 *
293 * @return void
294 * @access public
295 * @static
296 */
297 function browse($action = NULL) {
298 $ufGroup = array();
299 $allUFGroups = array();
300 $allUFGroups = CRM_Core_BAO_UFGroup::getModuleUFGroup();
301 if (empty($allUFGroups)) {
302 return;
303 }
304
305 $ufGroups = CRM_Core_PseudoConstant::ufGroup();
306 CRM_Utils_Hook::aclGroup(CRM_Core_Permission::ADMIN, NULL, 'civicrm_uf_group', $ufGroups, $allUFGroups);
307
308 foreach ($allUFGroups as $id => $value) {
309 $ufGroup[$id] = array();
310 $ufGroup[$id]['id'] = $id;
311 $ufGroup[$id]['title'] = $value['title'];
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');
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 );
368 //get the "Used For" from uf_join
369 $ufGroup[$id]['module'] = implode(', ', CRM_Core_BAO_UFGroup::getUFJoinRecord($id, TRUE));
370 }
371
372 $this->assign('rows', $ufGroup);
373 }
374
375 /**
376 * this function is for preview mode for ufoup
377 *
378 * @param int $id uf group id
379 *
380 * @return void
381 * @access public
382 */
383 function preview($id, $action) {
384 $controller = new CRM_Core_Controller_Simple('CRM_UF_Form_Preview', ts('CiviCRM Profile Group Preview'), NULL);
385 $controller->set('id', $id);
386 $controller->setEmbedded(TRUE);
387 $controller->process();
388 $controller->run();
389 }
390
391 function setContext($id, $action) {
392 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
393
394 //we need to differentiate context for update and preview profile.
395 if (!$context && !($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::PREVIEW))) {
396 $context = 'group';
397 }
398
399 if ($context == 'field') {
400 $url = CRM_Utils_System::url('civicrm/admin/uf/group/field', "reset=1&action=browse&gid={$id}");
401 }
402 else {
403 $url = CRM_Utils_System::url('civicrm/admin/uf/group', 'reset=1&action=browse');
404 }
405
406 $session = CRM_Core_Session::singleton();
407 $session->pushUserContext($url);
408 }
409
410 static function extractGroupTypes($groupType) {
411 $returnGroupTypes = array();
412 if (!$groupType) {
413 return $returnGroupTypes;
414 }
415
416 $groupTypeParts = explode(CRM_Core_DAO::VALUE_SEPARATOR, $groupType);
417 foreach (explode(',', $groupTypeParts[0]) as $type) {
418 $returnGroupTypes[$type] = $type;
419 }
420
421 if (CRM_Utils_Array::value(1, $groupTypeParts)) {
422 foreach (explode(',', $groupTypeParts[1]) as $typeValue) {
423 $groupTypeValues = $valueLabels = array();
424 $valueParts = explode(':', $typeValue);
425 $typeName = NULL;
426 switch ($valueParts[0]) {
427 case 'ContributionType':
428 $typeName = 'Contribution';
429 $valueLabels = CRM_Contribute_PseudoConstant::financialType();
430 break;
431
432 case 'ParticipantRole':
433 $typeName = 'Participant';
434 $valueLabels = CRM_Event_PseudoConstant::participantRole();
435 break;
436
437 case 'ParticipantEventName':
438 $typeName = 'Participant';
439 $valueLabels = CRM_Event_PseudoConstant::event();
440 break;
441
442 case 'ParticipantEventType':
443 $typeName = 'Participant';
444 $valueLabels = CRM_Event_PseudoConstant::eventType();
445 break;
446
447 case 'MembershipType':
448 $typeName = 'Membership';
449 $valueLabels = CRM_Member_PseudoConstant::membershipType();
450 break;
451
452 case 'ActivityType':
453 $typeName = 'Activity';
454 $valueLabels = CRM_Core_PseudoConstant::ActivityType(TRUE, TRUE, FALSE, 'label', TRUE);
455 break;
456 }
457
458 foreach ($valueParts as $val) {
459 if (CRM_Utils_Rule::integer($val)) {
460 $groupTypeValues[$val] = CRM_Utils_Array::value($val, $valueLabels);
461 }
462 }
463
464 if (!is_array($returnGroupTypes[$typeName])) {
465 $returnGroupTypes[$typeName] = array();
466 }
467 $returnGroupTypes[$typeName][$valueParts[0]] = $groupTypeValues;
468 }
469 }
470 return $returnGroupTypes;
471 }
472 }
473