3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Create a page for displaying CiviCRM Profile Fields.
21 * Heart of this class is the run method which checks
22 * for action type and then displays the appropriate
26 class CRM_Profile_Page_Dynamic
extends CRM_Core_Page
{
29 * The contact id of the person we are viewing.
36 * The profile group are are interested in.
43 * The profile types we restrict this page to display.
50 * Should we bypass permissions.
54 protected $_skipPermission;
57 * Store profile ids if multiple profile ids are passed using comma separated.
58 * Currently lets implement this functionality only for dialog mode
61 protected $_profileIds = [];
64 * Contact profile having activity fields?
68 protected $_isContactActivityProfile = FALSE;
71 * Activity Id connected to the profile.
75 protected $_activityId = NULL;
77 protected $_multiRecord = NULL;
79 protected $_recordId = NULL;
83 * fetch multirecord as well as non-multirecord fields
86 protected $_allFields = NULL;
97 * @param bool $skipPermission
98 * @param null $profileIds
100 * @return \CRM_Profile_Page_Dynamic
102 public function __construct($id, $gid, $restrict, $skipPermission = FALSE, $profileIds = NULL) {
103 parent
::__construct();
107 $this->_restrict
= $restrict;
108 $this->_skipPermission
= $skipPermission;
110 if (!array_key_exists('multiRecord', $_GET)) {
111 $this->set('multiRecord', NULL);
113 if (!array_key_exists('recordId', $_GET)) {
114 $this->set('recordId', NULL);
116 if (!array_key_exists('allFields', $_GET)) {
117 $this->set('allFields', NULL);
120 //specifies the action being done on a multi record field
121 $multiRecordAction = CRM_Utils_Request
::retrieve('multiRecord', 'String', $this);
123 $this->_multiRecord
= (!is_numeric($multiRecordAction)) ? CRM_Core_Action
::resolve($multiRecordAction) : $multiRecordAction;
124 if ($this->_multiRecord
) {
125 $this->set('multiRecord', $this->_multiRecord
);
128 if ($this->_multiRecord
& CRM_Core_Action
::VIEW
) {
129 $this->_recordId
= CRM_Utils_Request
::retrieve('recordId', 'Positive', $this);
130 $this->_allFields
= CRM_Utils_Request
::retrieve('allFields', 'Integer', $this);
134 $this->_profileIds
= $profileIds;
137 $this->_profileIds
= [$gid];
140 $this->_activityId
= CRM_Utils_Request
::retrieve('aid', 'Positive', $this, FALSE, 0, 'GET');
141 if (is_numeric($this->_activityId
)) {
142 $latestRevisionId = CRM_Activity_BAO_Activity
::getLatestActivityId($this->_activityId
);
143 if ($latestRevisionId) {
144 $this->_activityId
= $latestRevisionId;
147 $this->_isContactActivityProfile
= CRM_Core_BAO_UFField
::checkContactActivityProfileType($this->_gid
);
151 * Get the action links for this page.
155 public function &actionLinks() {
162 * This method is called after the page is created. It checks for the
163 * type of action and executes that action.
166 public function run() {
167 $template = CRM_Core_Smarty
::singleton();
168 if ($this->_id
&& $this->_gid
) {
170 // first check that id is part of the limit group id, CRM-4822
171 $limitListingsGroupsID = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup',
173 'limit_listings_group_id'
175 $config = CRM_Core_Config
::singleton();
176 if ($limitListingsGroupsID) {
178 if (!CRM_Contact_BAO_GroupContact
::isContactInGroup($this->_id
,
179 $limitListingsGroupsID
182 CRM_Utils_System
::setTitle(ts('Profile View - Permission Denied'));
183 return CRM_Core_Session
::setStatus(ts('You do not have permission to view this contact record. Contact the site administrator if you need assistance.'), ts('Permission Denied'), 'error');
187 $session = CRM_Core_Session
::singleton();
188 $userID = $session->get('userID');
190 $this->_isPermissionedChecksum
= $allowPermission = FALSE;
191 $permissionType = CRM_Core_Permission
::VIEW
;
192 if (CRM_Core_Permission
::check('administer users') || CRM_Core_Permission
::check('view all contacts') || CRM_Contact_BAO_Contact_Permission
::allow($this->_id
)) {
193 $allowPermission = TRUE;
195 if ($this->_id
!= $userID) {
196 // do not allow edit for anon users in joomla frontend, CRM-4668, unless u have checksum CRM-5228
197 if ($config->userFrameworkFrontend
) {
198 $this->_isPermissionedChecksum
= CRM_Contact_BAO_Contact_Permission
::validateOnlyChecksum($this->_id
, $this, FALSE);
199 if (!$this->_isPermissionedChecksum
) {
200 $this->_isPermissionedChecksum
= $allowPermission;
204 $this->_isPermissionedChecksum
= CRM_Contact_BAO_Contact_Permission
::validateChecksumContact($this->_id
, $this, FALSE);
208 // Users with create or edit permission should be allowed to view their own profile
209 if ($this->_id
== $userID ||
$this->_isPermissionedChecksum
) {
210 if (!CRM_Core_Permission
::check('profile view')) {
211 if (CRM_Core_Permission
::check('profile create') || CRM_Core_Permission
::check('profile edit')) {
212 $this->_skipPermission
= TRUE;
217 // make sure we dont expose all fields based on permission
219 if ((!$config->userFrameworkFrontend
&& $allowPermission) ||
220 $this->_id
== $userID ||
221 $this->_isPermissionedChecksum
227 $fields = CRM_Core_BAO_UFGroup
::getFields($this->_profileIds
, FALSE, CRM_Core_Action
::VIEW
,
228 NULL, NULL, FALSE, $this->_restrict
,
229 $this->_skipPermission
, NULL,
233 if ($this->_multiRecord
& CRM_Core_Action
::VIEW
&& $this->_recordId
&& !$this->_allFields
) {
234 CRM_Core_BAO_UFGroup
::shiftMultiRecordFields($fields, $multiRecordFields);
235 $fields = $multiRecordFields;
237 if ($this->_isContactActivityProfile
&& $this->_gid
) {
238 $errors = CRM_Profile_Form
::validateContactActivityProfile($this->_activityId
, $this->_id
, $this->_gid
);
239 if (!empty($errors)) {
240 CRM_Core_Error
::fatal(array_pop($errors));
244 //reformat fields array
245 foreach ($fields as $name => $field) {
246 // also eliminate all formatting fields
247 if (CRM_Utils_Array
::value('field_type', $field) == 'Formatting') {
248 unset($fields[$name]);
251 // make sure that there is enough permission to expose this field
252 if (!$admin && $field['visibility'] == 'User and User Admin Only') {
253 unset($fields[$name]);
257 if ($this->_isContactActivityProfile
) {
258 $contactFields = $activityFields = [];
260 foreach ($fields as $fieldName => $field) {
261 if (CRM_Utils_Array
::value('field_type', $field) == 'Activity') {
262 $activityFields[$fieldName] = $field;
265 $contactFields[$fieldName] = $field;
269 CRM_Core_BAO_UFGroup
::getValues($this->_id
, $contactFields, $values);
270 if ($this->_activityId
) {
271 CRM_Core_BAO_UFGroup
::getValues(
276 [['activity_id', '=', $this->_activityId
, 0, 0]]
281 $customWhereClause = NULL;
282 if ($this->_multiRecord
& CRM_Core_Action
::VIEW
&& $this->_recordId
) {
283 if ($this->_allFields
) {
284 $copyFields = $fields;
285 CRM_Core_BAO_UFGroup
::shiftMultiRecordFields($copyFields, $multiRecordFields);
286 $fieldKey = key($multiRecordFields);
289 $fieldKey = key($fields);
291 if ($fieldID = CRM_Core_BAO_CustomField
::getKeyID($fieldKey)) {
292 $tableColumnGroup = CRM_Core_BAO_CustomField
::getTableColumnGroup($fieldID);
293 $columnName = "{$tableColumnGroup[0]}.id";
294 $customWhereClause = $columnName . ' = ' . $this->_recordId
;
297 CRM_Core_BAO_UFGroup
::getValues($this->_id
, $fields, $values, TRUE, NULL, FALSE, $customWhereClause);
300 // $profileFields array can be used for customized display of field labels and values in Profile/View.tpl
304 foreach ($fields as $name => $field) {
306 // Create a unique, non-empty index for each field.
307 $index = $field['title'];
311 while (array_key_exists($index, $labels)) {
315 $labels[$index] = preg_replace('/\s+|\W+/', '_', $name);
318 foreach ($values as $title => $value) {
319 $profileFields[$labels[$title]] = [
325 $template->assign_by_ref('row', $values);
326 $template->assign_by_ref('profileFields', $profileFields);
329 $name = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid
, 'name');
330 $this->assign('ufGroupName', $name);
331 CRM_Utils_Hook
::viewProfile($name);
333 if (strtolower($name) == 'summary_overlay') {
334 $template->assign('overlayProfile', TRUE);
337 if (($this->_multiRecord
& CRM_Core_Action
::VIEW
) && $this->_recordId
&& !$this->_allFields
) {
338 $fieldDetail = reset($fields);
339 $fieldId = CRM_Core_BAO_CustomField
::getKeyID($fieldDetail['name']);
340 $customGroupDetails = CRM_Core_BAO_CustomGroup
::getGroupTitles([$fieldId]);
341 $multiRecTitle = $customGroupDetails[$fieldId]['groupTitle'];
344 $title = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid
, 'title');
348 $displayName = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_id
, 'display_name');
350 $session = CRM_Core_Session
::singleton();
351 $config = CRM_Core_Config
::singleton();
352 if ($session->get('userID') &&
353 CRM_Core_Permission
::check('access CiviCRM') &&
354 CRM_Contact_BAO_Contact_Permission
::allow($session->get('userID'), CRM_Core_Permission
::VIEW
) &&
355 !$config->userFrameworkFrontend
357 $contactViewUrl = CRM_Utils_System
::url('civicrm/contact/view', "action=view&reset=1&cid={$this->_id}", TRUE);
358 $this->assign('displayName', $displayName);
359 $displayName = "<a href=\"$contactViewUrl\">{$displayName}</a>";
361 $title .= ' - ' . $displayName;
364 $title = isset($multiRecTitle) ?
ts('View %1 Record', [1 => $multiRecTitle]) : $title;
365 CRM_Utils_System
::setTitle($title);
367 // invoke the pagRun hook, CRM-3906
368 CRM_Utils_Hook
::pageRun($this);
370 return trim($template->fetch($this->getHookedTemplateFileName()));
374 * @param string $suffix
376 * @return null|string
378 public function checkTemplateFileExists($suffix = '') {
380 $templateFile = "CRM/Profile/Page/{$this->_gid}/Dynamic.{$suffix}tpl";
381 $template = CRM_Core_Page
::getTemplate();
382 if ($template->template_exists($templateFile)) {
383 return $templateFile;
386 // lets see if we have customized by name
387 $ufGroupName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid
, 'name');
389 $templateFile = "CRM/Profile/Page/{$ufGroupName}/Dynamic.{$suffix}tpl";
390 if ($template->template_exists($templateFile)) {
391 return $templateFile;
399 * Use the form name to create the tpl file name.
403 public function getTemplateFileName() {
404 $fileName = $this->checkTemplateFileExists();
405 return $fileName ?
$fileName : parent
::getTemplateFileName();
409 * Default extra tpl file basically just replaces .tpl with .extra.tpl
410 * i.e. we dont override
414 public function overrideExtraTemplateFileName() {
415 $fileName = $this->checkTemplateFileExists('extra.');
416 return $fileName ?
$fileName : parent
::overrideExtraTemplateFileName();