3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 * Create a page for displaying CiviCRM Profile Fields.
39 * Heart of this class is the run method which checks
40 * for action type and then displays the appropriate
44 class CRM_Profile_Page_Dynamic
extends CRM_Core_Page
{
47 * The contact id of the person we are viewing
55 * the profile group are are interested in
63 * The profile types we restrict this page to display
71 * Should we bypass permissions
76 protected $_skipPermission;
79 * Store profile ids if multiple profile ids are passed using comma separated.
80 * Currently lets implement this functionality only for dialog mode
82 protected $_profileIds = array();
85 * Contact profile having activity fields?
89 protected $_isContactActivityProfile = FALSE;
92 * Activity Id connected to the profile
96 protected $_activityId = NULL;
98 protected $_multiRecord = NULL;
100 protected $_recordId = NULL;
103 * fetch multirecord as well as non-multirecord fields
105 protected $_allFields = NULL;
110 * @param int $id the contact id
111 * @param int $gid the group id
114 * @param bool $skipPermission
115 * @param null $profileIds
117 * @return \CRM_Profile_Page_Dynamic
120 function __construct($id, $gid, $restrict, $skipPermission = FALSE, $profileIds = NULL) {
121 parent
::__construct();
125 $this->_restrict
= $restrict;
126 $this->_skipPermission
= $skipPermission;
128 if (!array_key_exists('multiRecord', $_GET)) {
129 $this->set('multiRecord', NULL);
131 if (!array_key_exists('recordId', $_GET)) {
132 $this->set('recordId', NULL);
134 if (!array_key_exists('allFields', $_GET)) {
135 $this->set('allFields', NULL);
138 //specifies the action being done on a multi record field
139 $multiRecordAction = CRM_Utils_Request
::retrieve('multiRecord', 'String', $this);
141 $this->_multiRecord
= (!is_numeric($multiRecordAction)) ?
142 CRM_Core_Action
::resolve($multiRecordAction) : $multiRecordAction;
143 if ($this->_multiRecord
) {
144 $this->set('multiRecord', $this->_multiRecord
);
147 if ($this->_multiRecord
& CRM_Core_Action
::VIEW
) {
148 $this->_recordId
= CRM_Utils_Request
::retrieve('recordId', 'Positive', $this);
149 $this->_allFields
= CRM_Utils_Request
::retrieve('allFields', 'Integer', $this);
153 $this->_profileIds
= $profileIds;
156 $this->_profileIds
= array($gid);
159 $this->_activityId
= CRM_Utils_Request
::retrieve('aid', 'Positive', $this, FALSE, 0, 'GET');
160 if (is_numeric($this->_activityId
)) {
161 $latestRevisionId = CRM_Activity_BAO_Activity
::getLatestActivityId($this->_activityId
);
162 if ($latestRevisionId) {
163 $this->_activityId
= $latestRevisionId;
166 $this->_isContactActivityProfile
= CRM_Core_BAO_UFField
::checkContactActivityProfileType($this->_gid
);
170 * Get the action links for this page.
172 * @return array $_actionLinks
175 function &actionLinks() {
182 * This method is called after the page is created. It checks for the
183 * type of action and executes that action.
190 $template = CRM_Core_Smarty
::singleton();
191 if ($this->_id
&& $this->_gid
) {
193 // first check that id is part of the limit group id, CRM-4822
194 $limitListingsGroupsID = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup',
196 'limit_listings_group_id'
198 $config = CRM_Core_Config
::singleton();
199 if ($limitListingsGroupsID) {
201 if (!CRM_Contact_BAO_GroupContact
::isContactInGroup($this->_id
,
202 $limitListingsGroupsID
204 CRM_Utils_System
::setTitle(ts('Profile View - Permission Denied'));
205 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');
209 $session = CRM_Core_Session
::singleton();
210 $userID = $session->get('userID');
212 $this->_isPermissionedChecksum
= $allowPermission = FALSE;
213 $permissionType = CRM_Core_Permission
::VIEW
;
214 if (CRM_Core_Permission
::check('administer users') || CRM_Core_Permission
::check('view all contacts') || CRM_Contact_BAO_Contact_Permission
::allow($this->_id
)) {
215 $allowPermission = TRUE;
217 if ($this->_id
!= $userID) {
218 // do not allow edit for anon users in joomla frontend, CRM-4668, unless u have checksum CRM-5228
219 if ($config->userFrameworkFrontend
) {
220 $this->_isPermissionedChecksum
= CRM_Contact_BAO_Contact_Permission
::validateOnlyChecksum($this->_id
, $this, FALSE);
221 if (!$this->_isPermissionedChecksum
) {
222 $this->_isPermissionedChecksum
= $allowPermission;
226 $this->_isPermissionedChecksum
= CRM_Contact_BAO_Contact_Permission
::validateChecksumContact($this->_id
, $this, FALSE);
230 // Users with create or edit permission should be allowed to view their own profile
231 if ($this->_id
== $userID ||
$this->_isPermissionedChecksum
) {
232 if (!CRM_Core_Permission
::check('profile view')) {
233 if (CRM_Core_Permission
::check('profile create') || CRM_Core_Permission
::check('profile edit')) {
234 $this->_skipPermission
= TRUE;
239 // make sure we dont expose all fields based on permission
241 if ((!$config->userFrameworkFrontend
&& $allowPermission) ||
242 $this->_id
== $userID ||
243 $this->_isPermissionedChecksum
249 $fields = CRM_Core_BAO_UFGroup
::getFields($this->_profileIds
, FALSE, CRM_Core_Action
::VIEW
,
250 NULL, NULL, FALSE, $this->_restrict
,
251 $this->_skipPermission
, NULL,
255 if ($this->_multiRecord
& CRM_Core_Action
::VIEW
&& $this->_recordId
&& !$this->_allFields
) {
256 CRM_Core_BAO_UFGroup
::shiftMultiRecordFields($fields, $multiRecordFields);
257 $fields = $multiRecordFields;
259 if ($this->_isContactActivityProfile
&& $this->_gid
) {
260 $errors = CRM_Profile_Form
::validateContactActivityProfile($this->_activityId
, $this->_id
, $this->_gid
);
261 if (!empty($errors)) {
262 CRM_Core_Error
::fatal(array_pop($errors));
266 //reformat fields array
267 foreach ($fields as $name => $field) {
268 // also eliminate all formatting fields
269 if (CRM_Utils_Array
::value('field_type', $field) == 'Formatting') {
270 unset($fields[$name]);
273 // make sure that there is enough permission to expose this field
274 if (!$admin && $field['visibility'] == 'User and User Admin Only') {
275 unset($fields[$name]);
279 if ($this->_isContactActivityProfile
) {
280 $contactFields = $activityFields = array();
282 foreach ($fields as $fieldName => $field) {
283 if (CRM_Utils_Array
::value('field_type', $field) == 'Activity') {
284 $activityFields[$fieldName] = $field;
287 $contactFields[$fieldName] = $field;
291 CRM_Core_BAO_UFGroup
::getValues($this->_id
, $contactFields, $values);
292 if ($this->_activityId
) {
293 CRM_Core_BAO_UFGroup
::getValues(
298 array(array('activity_id', '=', $this->_activityId
, 0, 0))
303 $customWhereClause = NULL;
304 if ($this->_multiRecord
& CRM_Core_Action
::VIEW
&& $this->_recordId
) {
305 if ($this->_allFields
) {
306 $copyFields = $fields;
307 CRM_Core_BAO_UFGroup
::shiftMultiRecordFields($copyFields, $multiRecordFields);
308 $fieldKey = key($multiRecordFields);
310 $fieldKey = key($fields);
312 if ($fieldID = CRM_Core_BAO_CustomField
::getKeyID($fieldKey)) {
313 $tableColumnGroup = CRM_Core_BAO_CustomField
::getTableColumnGroup($fieldID);
314 $columnName = "{$tableColumnGroup[0]}.id";
315 $customWhereClause = $columnName . ' = ' . $this->_recordId
;
318 CRM_Core_BAO_UFGroup
::getValues($this->_id
, $fields, $values, TRUE, NULL, FALSE, $customWhereClause);
321 // $profileFields array can be used for customized display of field labels and values in Profile/View.tpl
322 $profileFields = array();
325 foreach ($fields as $name => $field) {
327 // Create a unique, non-empty index for each field.
328 $index = $field['title'];
329 if ($index === '') $index = ' ';
330 while (array_key_exists($index, $labels))
333 $labels[$index] = preg_replace('/\s+|\W+/', '_', $name);
336 foreach ($values as $title => $value) {
337 $profileFields[$labels[$title]] = array(
343 $template->assign_by_ref('row', $values);
344 $template->assign_by_ref('profileFields', $profileFields);
347 $name = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid
, 'name');
348 $this->assign('ufGroupName', $name);
349 CRM_Utils_Hook
::viewProfile($name);
351 if (strtolower($name) == 'summary_overlay') {
352 $template->assign('overlayProfile', TRUE);
355 if (($this->_multiRecord
& CRM_Core_Action
::VIEW
) && $this->_recordId
&& !$this->_allFields
) {
356 $fieldDetail = reset($fields);
357 $fieldId = CRM_Core_BAO_CustomField
::getKeyID($fieldDetail['name']);
358 $customGroupDetails = CRM_Core_BAO_CustomGroup
::getGroupTitles(array($fieldId));
359 $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', array(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 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
424 function getTemplateFileName() {
425 $fileName = $this->checkTemplateFileExists();
426 return $fileName ?
$fileName : parent
::getTemplateFileName();
430 * Default extra tpl file basically just replaces .tpl with .extra.tpl
431 * i.e. we dont override
439 function overrideExtraTemplateFileName() {
440 $fileName = $this->checkTemplateFileExists('extra.');
441 return $fileName ?
$fileName : parent
::overrideExtraTemplateFileName();