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