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 +--------------------------------------------------------------------+
17 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 * Create a page for displaying CiviCRM Profile Fields.
23 * Heart of this class is the run method which checks
24 * for action type and then displays the appropriate
28 class CRM_Profile_Page_Dynamic
extends CRM_Core_Page
{
31 * The contact id of the person we are viewing.
38 * The profile group are are interested in.
45 * The profile types we restrict this page to display.
52 * Should we bypass permissions.
56 protected $_skipPermission;
59 * Store profile ids if multiple profile ids are passed using comma separated.
60 * Currently lets implement this functionality only for dialog mode
63 protected $_profileIds = [];
66 * Contact profile having activity fields?
70 protected $_isContactActivityProfile = FALSE;
73 * Activity Id connected to the profile.
77 protected $_activityId = NULL;
79 protected $_multiRecord = NULL;
81 protected $_recordId = NULL;
84 * Should the primary email be converted into a link, if emailabe.
88 protected $isShowEmailTaskLink = FALSE;
92 * fetch multirecord as well as non-multirecord fields
95 protected $_allFields = NULL;
106 * @param bool $skipPermission
107 * @param null $profileIds
109 * @param bool $isShowEmailTaskLink
111 * @throws \CRM_Core_Exception
113 public function __construct($id, $gid, $restrict, $skipPermission = FALSE, $profileIds = NULL, $isShowEmailTaskLink = FALSE) {
114 parent
::__construct();
118 $this->_restrict
= $restrict;
119 $this->_skipPermission
= $skipPermission;
120 $this->isShowEmailTaskLink
= $isShowEmailTaskLink;
122 if (!array_key_exists('multiRecord', $_GET)) {
123 $this->set('multiRecord', NULL);
125 if (!array_key_exists('recordId', $_GET)) {
126 $this->set('recordId', NULL);
128 if (!array_key_exists('allFields', $_GET)) {
129 $this->set('allFields', NULL);
132 //specifies the action being done on a multi record field
133 $multiRecordAction = CRM_Utils_Request
::retrieve('multiRecord', 'String', $this);
135 $this->_multiRecord
= (!is_numeric($multiRecordAction)) ? CRM_Core_Action
::resolve($multiRecordAction) : $multiRecordAction;
136 if ($this->_multiRecord
) {
137 $this->set('multiRecord', $this->_multiRecord
);
140 if ($this->_multiRecord
& CRM_Core_Action
::VIEW
) {
141 $this->_recordId
= CRM_Utils_Request
::retrieve('recordId', 'Positive', $this);
142 $this->_allFields
= CRM_Utils_Request
::retrieve('allFields', 'Integer', $this);
146 $this->_profileIds
= $profileIds;
149 $this->_profileIds
= [$gid];
152 $this->_activityId
= CRM_Utils_Request
::retrieve('aid', 'Positive', $this, FALSE, 0, 'GET');
153 if (is_numeric($this->_activityId
)) {
154 $latestRevisionId = CRM_Activity_BAO_Activity
::getLatestActivityId($this->_activityId
);
155 if ($latestRevisionId) {
156 $this->_activityId
= $latestRevisionId;
159 $this->_isContactActivityProfile
= CRM_Core_BAO_UFField
::checkContactActivityProfileType($this->_gid
);
163 * Get the action links for this page.
167 public function &actionLinks() {
174 * This method is called after the page is created. It checks for the
175 * type of action and executes that action.
178 public function run() {
179 $template = CRM_Core_Smarty
::singleton();
180 if ($this->_id
&& $this->_gid
) {
182 // first check that id is part of the limit group id, CRM-4822
183 $limitListingsGroupsID = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup',
185 'limit_listings_group_id'
187 $config = CRM_Core_Config
::singleton();
188 if ($limitListingsGroupsID) {
190 if (!CRM_Contact_BAO_GroupContact
::isContactInGroup($this->_id
,
191 $limitListingsGroupsID
194 CRM_Utils_System
::setTitle(ts('Profile View - Permission Denied'));
195 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');
199 $session = CRM_Core_Session
::singleton();
200 $userID = $session->get('userID');
202 $this->_isPermissionedChecksum
= $allowPermission = FALSE;
203 $permissionType = CRM_Core_Permission
::VIEW
;
204 if (CRM_Core_Permission
::check('administer users') || CRM_Core_Permission
::check('view all contacts') || CRM_Contact_BAO_Contact_Permission
::allow($this->_id
)) {
205 $allowPermission = TRUE;
207 if ($this->_id
!= $userID) {
208 // do not allow edit for anon users in joomla frontend, CRM-4668, unless u have checksum CRM-5228
209 if ($config->userFrameworkFrontend
) {
210 $this->_isPermissionedChecksum
= CRM_Contact_BAO_Contact_Permission
::validateOnlyChecksum($this->_id
, $this, FALSE);
211 if (!$this->_isPermissionedChecksum
) {
212 $this->_isPermissionedChecksum
= $allowPermission;
216 $this->_isPermissionedChecksum
= CRM_Contact_BAO_Contact_Permission
::validateChecksumContact($this->_id
, $this, FALSE);
220 // Users with create or edit permission should be allowed to view their own profile
221 if ($this->_id
== $userID ||
$this->_isPermissionedChecksum
) {
222 if (!CRM_Core_Permission
::check('profile view')) {
223 if (CRM_Core_Permission
::check('profile create') || CRM_Core_Permission
::check('profile edit')) {
224 $this->_skipPermission
= TRUE;
229 // make sure we dont expose all fields based on permission
231 if ((!$config->userFrameworkFrontend
&& $allowPermission) ||
232 $this->_id
== $userID ||
233 $this->_isPermissionedChecksum
239 $fields = CRM_Core_BAO_UFGroup
::getFields($this->_profileIds
, FALSE, CRM_Core_Action
::VIEW
,
240 NULL, NULL, FALSE, $this->_restrict
,
241 $this->_skipPermission
, NULL,
245 if ($this->_multiRecord
& CRM_Core_Action
::VIEW
&& $this->_recordId
&& !$this->_allFields
) {
246 CRM_Core_BAO_UFGroup
::shiftMultiRecordFields($fields, $multiRecordFields);
247 $fields = $multiRecordFields;
249 if ($this->_isContactActivityProfile
&& $this->_gid
) {
250 $errors = CRM_Profile_Form
::validateContactActivityProfile($this->_activityId
, $this->_id
, $this->_gid
);
251 if (!empty($errors)) {
252 CRM_Core_Error
::statusBounce(array_pop($errors));
256 //reformat fields array
257 foreach ($fields as $name => $field) {
258 // also eliminate all formatting fields
259 if (CRM_Utils_Array
::value('field_type', $field) == 'Formatting') {
260 unset($fields[$name]);
263 // make sure that there is enough permission to expose this field
264 if (!$admin && $field['visibility'] == 'User and User Admin Only') {
265 unset($fields[$name]);
269 if ($this->_isContactActivityProfile
) {
270 $contactFields = $activityFields = [];
272 foreach ($fields as $fieldName => $field) {
273 if (CRM_Utils_Array
::value('field_type', $field) == 'Activity') {
274 $activityFields[$fieldName] = $field;
277 $contactFields[$fieldName] = $field;
281 CRM_Core_BAO_UFGroup
::getValues($this->_id
, $contactFields, $values);
282 if ($this->_activityId
) {
283 CRM_Core_BAO_UFGroup
::getValues(
288 [['activity_id', '=', $this->_activityId
, 0, 0]]
293 $customWhereClause = NULL;
294 if ($this->_multiRecord
& CRM_Core_Action
::VIEW
&& $this->_recordId
) {
295 if ($this->_allFields
) {
296 $copyFields = $fields;
297 CRM_Core_BAO_UFGroup
::shiftMultiRecordFields($copyFields, $multiRecordFields);
298 $fieldKey = key($multiRecordFields);
301 $fieldKey = key($fields);
303 if ($fieldID = CRM_Core_BAO_CustomField
::getKeyID($fieldKey)) {
304 $tableColumnGroup = CRM_Core_BAO_CustomField
::getTableColumnGroup($fieldID);
305 $columnName = "{$tableColumnGroup[0]}.id";
306 $customWhereClause = $columnName . ' = ' . $this->_recordId
;
309 CRM_Core_BAO_UFGroup
::getValues($this->_id
, $fields, $values, TRUE, NULL, FALSE, $customWhereClause);
312 // $profileFields array can be used for customized display of field labels and values in Profile/View.tpl
316 foreach ($fields as $name => $field) {
318 // Create a unique, non-empty index for each field.
319 $index = $field['title'];
323 while (array_key_exists($index, $labels)) {
327 $labels[$index] = preg_replace('/\s+|\W+/', '_', $name);
330 if ($this->isShowEmailTaskLink
) {
331 foreach ($this->getEmailFields($fields) as $fieldName) {
332 $values[$fields[$fieldName]['title']] = $this->getLinkedEmail($values[$fields[$fieldName]['title']]);
335 foreach ($values as $title => $value) {
336 $profileFields[$labels[$title]] = [
342 $template->assign_by_ref('row', $values);
343 $template->assign_by_ref('profileFields', $profileFields);
346 $name = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid
, 'name');
347 $this->assign('ufGroupName', $name);
348 CRM_Utils_Hook
::viewProfile($name);
350 if (strtolower($name) == 'summary_overlay') {
351 $template->assign('overlayProfile', TRUE);
354 if (($this->_multiRecord
& CRM_Core_Action
::VIEW
) && $this->_recordId
&& !$this->_allFields
) {
355 $fieldDetail = reset($fields);
356 $fieldId = CRM_Core_BAO_CustomField
::getKeyID($fieldDetail['name']);
357 $customGroupDetails = CRM_Core_BAO_CustomGroup
::getGroupTitles([$fieldId]);
358 $multiRecTitle = $customGroupDetails[$fieldId]['groupTitle'];
361 $title = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid
, 'title');
365 $displayName = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_id
, 'display_name');
367 $session = CRM_Core_Session
::singleton();
368 $config = CRM_Core_Config
::singleton();
369 if ($session->get('userID') &&
370 CRM_Core_Permission
::check('access CiviCRM') &&
371 CRM_Contact_BAO_Contact_Permission
::allow($session->get('userID'), CRM_Core_Permission
::VIEW
) &&
372 !$config->userFrameworkFrontend
374 $contactViewUrl = CRM_Utils_System
::url('civicrm/contact/view', "action=view&reset=1&cid={$this->_id}", TRUE);
375 $this->assign('displayName', $displayName);
376 $displayName = "<a href=\"$contactViewUrl\">{$displayName}</a>";
378 $title .= ' - ' . $displayName;
381 $title = isset($multiRecTitle) ?
ts('View %1 Record', [1 => $multiRecTitle]) : $title;
382 CRM_Utils_System
::setTitle($title);
384 // invoke the pagRun hook, CRM-3906
385 CRM_Utils_Hook
::pageRun($this);
387 return trim($template->fetch($this->getHookedTemplateFileName()));
391 * @param string $suffix
393 * @return null|string
395 public function checkTemplateFileExists($suffix = '') {
397 $templateFile = "CRM/Profile/Page/{$this->_gid}/Dynamic.{$suffix}tpl";
398 $template = CRM_Core_Page
::getTemplate();
399 if ($template->template_exists($templateFile)) {
400 return $templateFile;
403 // lets see if we have customized by name
404 $ufGroupName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid
, 'name');
406 $templateFile = "CRM/Profile/Page/{$ufGroupName}/Dynamic.{$suffix}tpl";
407 if ($template->template_exists($templateFile)) {
408 return $templateFile;
416 * Use the form name to create the tpl file name.
420 public function getTemplateFileName() {
421 $fileName = $this->checkTemplateFileExists();
422 return $fileName ?
$fileName : parent
::getTemplateFileName();
426 * Default extra tpl file basically just replaces .tpl with .extra.tpl
427 * i.e. we dont override
431 public function overrideExtraTemplateFileName() {
432 $fileName = $this->checkTemplateFileExists('extra.');
433 return $fileName ?
$fileName : parent
::overrideExtraTemplateFileName();
437 * Get the email field as a task link, if not on hold or set to do_not_email.
439 * @param string $email
442 * @throws \API_Exception
443 * @throws \Civi\API\Exception\UnauthorizedException
445 protected function getLinkedEmail($email): string {
449 $emailID = Email
::get()->setOrderBy(['is_primary' => 'DESC'])->setWhere([['contact_id', '=', $this->_id
], ['email', '=', $email], ['on_hold', '=', FALSE], ['contact_id.is_deceased', '=', FALSE], ['contact_id.is_deleted', '=', FALSE], ['contact_id.do_not_email', '=', FALSE]])->execute()->first()['id'];
453 $emailPopupUrl = CRM_Utils_System
::url('civicrm/activity/email/add', [
456 'email_id' => $emailID,
459 return '<a class="crm-popup" href="' . $emailPopupUrl . '">' . $email . '</a>';
463 * Get the email fields from within the fields array.
465 * @param array $fields
467 protected function getEmailFields(array $fields): array {
469 foreach (array_keys($fields) as $fieldName) {
470 if (substr($fieldName, 0, 6) === 'email-'
471 && (is_numeric(substr($fieldName, 6)) ||
substr($fieldName, 6) ===
473 $emailFields[] = $fieldName;