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