3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * Create a page for displaying CiviCRM Profile Fields.
37 * Heart of this class is the run method which checks
38 * for action type and then displays the appropriate
42 class CRM_Profile_Page_Dynamic
extends CRM_Core_Page
{
45 * The contact id of the person we are viewing.
52 * The profile group are are interested in.
59 * The profile types we restrict this page to display.
66 * Should we bypass permissions.
70 protected $_skipPermission;
73 * Store profile ids if multiple profile ids are passed using comma separated.
74 * Currently lets implement this functionality only for dialog mode
77 protected $_profileIds = [];
80 * Contact profile having activity fields?
84 protected $_isContactActivityProfile = FALSE;
87 * Activity Id connected to the profile.
91 protected $_activityId = NULL;
93 protected $_multiRecord = NULL;
95 protected $_recordId = NULL;
99 * fetch multirecord as well as non-multirecord fields
102 protected $_allFields = NULL;
113 * @param bool $skipPermission
114 * @param null $profileIds
116 * @return \CRM_Profile_Page_Dynamic
118 public function __construct($id, $gid, $restrict, $skipPermission = FALSE, $profileIds = NULL) {
119 parent
::__construct();
123 $this->_restrict
= $restrict;
124 $this->_skipPermission
= $skipPermission;
126 if (!array_key_exists('multiRecord', $_GET)) {
127 $this->set('multiRecord', NULL);
129 if (!array_key_exists('recordId', $_GET)) {
130 $this->set('recordId', NULL);
132 if (!array_key_exists('allFields', $_GET)) {
133 $this->set('allFields', NULL);
136 //specifies the action being done on a multi record field
137 $multiRecordAction = CRM_Utils_Request
::retrieve('multiRecord', 'String', $this);
139 $this->_multiRecord
= (!is_numeric($multiRecordAction)) ? CRM_Core_Action
::resolve($multiRecordAction) : $multiRecordAction;
140 if ($this->_multiRecord
) {
141 $this->set('multiRecord', $this->_multiRecord
);
144 if ($this->_multiRecord
& CRM_Core_Action
::VIEW
) {
145 $this->_recordId
= CRM_Utils_Request
::retrieve('recordId', 'Positive', $this);
146 $this->_allFields
= CRM_Utils_Request
::retrieve('allFields', 'Integer', $this);
150 $this->_profileIds
= $profileIds;
153 $this->_profileIds
= [$gid];
156 $this->_activityId
= CRM_Utils_Request
::retrieve('aid', 'Positive', $this, FALSE, 0, 'GET');
157 if (is_numeric($this->_activityId
)) {
158 $latestRevisionId = CRM_Activity_BAO_Activity
::getLatestActivityId($this->_activityId
);
159 if ($latestRevisionId) {
160 $this->_activityId
= $latestRevisionId;
163 $this->_isContactActivityProfile
= CRM_Core_BAO_UFField
::checkContactActivityProfileType($this->_gid
);
167 * Get the action links for this page.
171 public function &actionLinks() {
178 * This method is called after the page is created. It checks for the
179 * type of action and executes that action.
182 public function run() {
183 $template = CRM_Core_Smarty
::singleton();
184 if ($this->_id
&& $this->_gid
) {
186 // first check that id is part of the limit group id, CRM-4822
187 $limitListingsGroupsID = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup',
189 'limit_listings_group_id'
191 $config = CRM_Core_Config
::singleton();
192 if ($limitListingsGroupsID) {
194 if (!CRM_Contact_BAO_GroupContact
::isContactInGroup($this->_id
,
195 $limitListingsGroupsID
198 CRM_Utils_System
::setTitle(ts('Profile View - Permission Denied'));
199 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');
203 $session = CRM_Core_Session
::singleton();
204 $userID = $session->get('userID');
206 $this->_isPermissionedChecksum
= $allowPermission = FALSE;
207 $permissionType = CRM_Core_Permission
::VIEW
;
208 if (CRM_Core_Permission
::check('administer users') || CRM_Core_Permission
::check('view all contacts') || CRM_Contact_BAO_Contact_Permission
::allow($this->_id
)) {
209 $allowPermission = TRUE;
211 if ($this->_id
!= $userID) {
212 // do not allow edit for anon users in joomla frontend, CRM-4668, unless u have checksum CRM-5228
213 if ($config->userFrameworkFrontend
) {
214 $this->_isPermissionedChecksum
= CRM_Contact_BAO_Contact_Permission
::validateOnlyChecksum($this->_id
, $this, FALSE);
215 if (!$this->_isPermissionedChecksum
) {
216 $this->_isPermissionedChecksum
= $allowPermission;
220 $this->_isPermissionedChecksum
= CRM_Contact_BAO_Contact_Permission
::validateChecksumContact($this->_id
, $this, FALSE);
224 // Users with create or edit permission should be allowed to view their own profile
225 if ($this->_id
== $userID ||
$this->_isPermissionedChecksum
) {
226 if (!CRM_Core_Permission
::check('profile view')) {
227 if (CRM_Core_Permission
::check('profile create') || CRM_Core_Permission
::check('profile edit')) {
228 $this->_skipPermission
= TRUE;
233 // make sure we dont expose all fields based on permission
235 if ((!$config->userFrameworkFrontend
&& $allowPermission) ||
236 $this->_id
== $userID ||
237 $this->_isPermissionedChecksum
243 $fields = CRM_Core_BAO_UFGroup
::getFields($this->_profileIds
, FALSE, CRM_Core_Action
::VIEW
,
244 NULL, NULL, FALSE, $this->_restrict
,
245 $this->_skipPermission
, NULL,
249 if ($this->_multiRecord
& CRM_Core_Action
::VIEW
&& $this->_recordId
&& !$this->_allFields
) {
250 CRM_Core_BAO_UFGroup
::shiftMultiRecordFields($fields, $multiRecordFields);
251 $fields = $multiRecordFields;
253 if ($this->_isContactActivityProfile
&& $this->_gid
) {
254 $errors = CRM_Profile_Form
::validateContactActivityProfile($this->_activityId
, $this->_id
, $this->_gid
);
255 if (!empty($errors)) {
256 CRM_Core_Error
::fatal(array_pop($errors));
260 //reformat fields array
261 foreach ($fields as $name => $field) {
262 // also eliminate all formatting fields
263 if (CRM_Utils_Array
::value('field_type', $field) == 'Formatting') {
264 unset($fields[$name]);
267 // make sure that there is enough permission to expose this field
268 if (!$admin && $field['visibility'] == 'User and User Admin Only') {
269 unset($fields[$name]);
273 if ($this->_isContactActivityProfile
) {
274 $contactFields = $activityFields = [];
276 foreach ($fields as $fieldName => $field) {
277 if (CRM_Utils_Array
::value('field_type', $field) == 'Activity') {
278 $activityFields[$fieldName] = $field;
281 $contactFields[$fieldName] = $field;
285 CRM_Core_BAO_UFGroup
::getValues($this->_id
, $contactFields, $values);
286 if ($this->_activityId
) {
287 CRM_Core_BAO_UFGroup
::getValues(
292 [['activity_id', '=', $this->_activityId
, 0, 0]]
297 $customWhereClause = NULL;
298 if ($this->_multiRecord
& CRM_Core_Action
::VIEW
&& $this->_recordId
) {
299 if ($this->_allFields
) {
300 $copyFields = $fields;
301 CRM_Core_BAO_UFGroup
::shiftMultiRecordFields($copyFields, $multiRecordFields);
302 $fieldKey = key($multiRecordFields);
305 $fieldKey = key($fields);
307 if ($fieldID = CRM_Core_BAO_CustomField
::getKeyID($fieldKey)) {
308 $tableColumnGroup = CRM_Core_BAO_CustomField
::getTableColumnGroup($fieldID);
309 $columnName = "{$tableColumnGroup[0]}.id";
310 $customWhereClause = $columnName . ' = ' . $this->_recordId
;
313 CRM_Core_BAO_UFGroup
::getValues($this->_id
, $fields, $values, TRUE, NULL, FALSE, $customWhereClause);
316 // $profileFields array can be used for customized display of field labels and values in Profile/View.tpl
320 foreach ($fields as $name => $field) {
322 // Create a unique, non-empty index for each field.
323 $index = $field['title'];
327 while (array_key_exists($index, $labels)) {
331 $labels[$index] = preg_replace('/\s+|\W+/', '_', $name);
334 foreach ($values as $title => $value) {
335 $profileFields[$labels[$title]] = [
341 $template->assign_by_ref('row', $values);
342 $template->assign_by_ref('profileFields', $profileFields);
345 $name = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid
, 'name');
346 $this->assign('ufGroupName', $name);
347 CRM_Utils_Hook
::viewProfile($name);
349 if (strtolower($name) == 'summary_overlay') {
350 $template->assign('overlayProfile', TRUE);
353 if (($this->_multiRecord
& CRM_Core_Action
::VIEW
) && $this->_recordId
&& !$this->_allFields
) {
354 $fieldDetail = reset($fields);
355 $fieldId = CRM_Core_BAO_CustomField
::getKeyID($fieldDetail['name']);
356 $customGroupDetails = CRM_Core_BAO_CustomGroup
::getGroupTitles([$fieldId]);
357 $multiRecTitle = $customGroupDetails[$fieldId]['groupTitle'];
360 $title = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid
, 'title');
364 $displayName = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_id
, 'display_name');
366 $session = CRM_Core_Session
::singleton();
367 $config = CRM_Core_Config
::singleton();
368 if ($session->get('userID') &&
369 CRM_Core_Permission
::check('access CiviCRM') &&
370 CRM_Contact_BAO_Contact_Permission
::allow($session->get('userID'), CRM_Core_Permission
::VIEW
) &&
371 !$config->userFrameworkFrontend
373 $contactViewUrl = CRM_Utils_System
::url('civicrm/contact/view', "action=view&reset=1&cid={$this->_id}", TRUE);
374 $this->assign('displayName', $displayName);
375 $displayName = "<a href=\"$contactViewUrl\">{$displayName}</a>";
377 $title .= ' - ' . $displayName;
380 $title = isset($multiRecTitle) ?
ts('View %1 Record', [1 => $multiRecTitle]) : $title;
381 CRM_Utils_System
::setTitle($title);
383 // invoke the pagRun hook, CRM-3906
384 CRM_Utils_Hook
::pageRun($this);
386 return trim($template->fetch($this->getHookedTemplateFileName()));
390 * @param string $suffix
392 * @return null|string
394 public function checkTemplateFileExists($suffix = '') {
396 $templateFile = "CRM/Profile/Page/{$this->_gid}/Dynamic.{$suffix}tpl";
397 $template = CRM_Core_Page
::getTemplate();
398 if ($template->template_exists($templateFile)) {
399 return $templateFile;
402 // lets see if we have customized by name
403 $ufGroupName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid
, 'name');
405 $templateFile = "CRM/Profile/Page/{$ufGroupName}/Dynamic.{$suffix}tpl";
406 if ($template->template_exists($templateFile)) {
407 return $templateFile;
415 * Use the form name to create the tpl file name.
419 public function getTemplateFileName() {
420 $fileName = $this->checkTemplateFileExists();
421 return $fileName ?
$fileName : parent
::getTemplateFileName();
425 * Default extra tpl file basically just replaces .tpl with .extra.tpl
426 * i.e. we dont override
430 public function overrideExtraTemplateFileName() {
431 $fileName = $this->checkTemplateFileExists('extra.');
432 return $fileName ?
$fileName : parent
::overrideExtraTemplateFileName();