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 * Main page for viewing contact.
21 class CRM_Contact_Page_View
extends CRM_Core_Page
{
24 * The id of the object being viewed (note/relationship etc)
31 * The contact id of the contact being viewed
35 protected $_contactId;
38 * The action that we are performing
45 * The permission we have on this contact
49 protected $_permission;
52 * Heart of the viewing process.
54 * The runner gets all the meta data for the contact and calls the appropriate type of page to view.
56 public function preProcess() {
58 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
59 $this->assign('id', $this->_id
);
61 $qfKey = CRM_Utils_Request
::retrieve('key', 'String', $this);
63 if (!CRM_Utils_Rule
::qfKey($qfKey)) {
66 $this->assign('searchKey', $qfKey);
68 // retrieve the group contact id, so that we can get contact id
69 $gcid = CRM_Utils_Request
::retrieve('gcid', 'Positive', $this);
72 $this->_contactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
75 $this->_contactId
= CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_GroupContact', $gcid, 'contact_id');
78 if (!$this->_contactId
) {
79 CRM_Core_Error
::statusBounce(
80 ts('We could not find a contact id.'),
81 CRM_Utils_System
::url('civicrm/dashboard', 'reset=1')
85 // ensure that the id does exist
86 if (CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId
, 'id') != $this->_contactId
) {
87 CRM_Core_Error
::statusBounce(
88 ts('A Contact with that ID does not exist: %1', [1 => $this->_contactId
]),
89 CRM_Utils_System
::url('civicrm/dashboard', 'reset=1')
93 $this->assign('contactId', $this->_contactId
);
95 // see if we can get prev/next positions from qfKey
97 'prevContactID' => NULL,
98 'prevContactName' => NULL,
99 'nextContactID' => NULL,
100 'nextContactName' => NULL,
101 'nextPrevError' => 0,
104 $pos = Civi
::service('prevnext')->getPositions("civicrm search $qfKey",
110 if (isset($pos['prev'])) {
111 $navContacts['prevContactID'] = $pos['prev']['id1'];
112 $navContacts['prevContactName'] = $pos['prev']['data'];
116 if (isset($pos['next'])) {
117 $navContacts['nextContactID'] = $pos['next']['id1'];
118 $navContacts['nextContactName'] = $pos['next']['data'];
122 $context = CRM_Utils_Array
::value('context', $_GET);
124 // seems like we did not find any contacts
125 // maybe due to bug CRM-9096
126 // however we should account for 1 contact results (which dont have prev next)
127 if (!$pos['foundEntry']) {
128 $navContacts['nextPrevError'] = 1;
132 $this->assign('context', $context);
133 CRM_Utils_System
::appendBreadCrumb([
135 'title' => ts('Search Results'),
136 'url' => CRM_Utils_System
::url("civicrm/contact/search/$context", ['qfKey' => $qfKey]),
141 $this->assign($navContacts);
143 $path = CRM_Utils_System
::url('civicrm/contact/view', 'reset=1&cid=' . $this->_contactId
);
144 CRM_Utils_System
::appendBreadCrumb([['title' => ts('View Contact'), 'url' => $path]]);
146 if ($image_URL = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId
, 'image_URL')) {
147 $this->assign("imageURL", CRM_Utils_File
::getImageURL($image_URL));
150 // also store in session for future use
151 $session = CRM_Core_Session
::singleton();
152 $session->set('view.id', $this->_contactId
);
154 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE, 'browse');
155 $this->assign('action', $this->_action
);
157 // check logged in user permission
158 self
::checkUserPermission($this);
160 list($displayName, $contactImage, $contactType, $contactSubtype, $contactImageUrl) = self
::getContactDetails($this->_contactId
);
161 $this->assign('displayName', $displayName);
163 $this->set('contactType', $contactType);
165 // note: there could still be multiple subtypes. We just trimming the outer separator.
166 $this->set('contactSubtype', trim($contactSubtype, CRM_Core_DAO
::VALUE_SEPARATOR
));
168 // add to recently viewed block
169 $isDeleted = (bool) CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId
, 'is_deleted');
172 'imageUrl' => $contactImageUrl,
173 'subtype' => $contactSubtype,
174 'isDeleted' => $isDeleted,
177 if (CRM_Contact_BAO_Contact_Permission
::allow($this->_contactId
, CRM_Core_Permission
::EDIT
)) {
178 $recentOther['editUrl'] = CRM_Utils_System
::url('civicrm/contact/add', "reset=1&action=update&cid={$this->_contactId}");
181 if (($session->get('userID') != $this->_contactId
) && CRM_Core_Permission
::check('delete contacts')
184 $recentOther['deleteUrl'] = CRM_Utils_System
::url('civicrm/contact/view/delete', "reset=1&delete=1&cid={$this->_contactId}");
187 CRM_Utils_Recent
::add($displayName,
188 CRM_Utils_System
::url('civicrm/contact/view', "reset=1&cid={$this->_contactId}"),
195 $this->assign('isDeleted', $isDeleted);
198 $title = self
::setTitle($this->_contactId
, $isDeleted);
199 $this->assign('title', $title);
201 // Check if this is default domain contact CRM-10482
202 if (CRM_Contact_BAO_Contact
::checkDomainContact($this->_contactId
)) {
203 $this->assign('domainContact', TRUE);
206 $this->assign('domainContact', FALSE);
209 // Add links for actions menu
210 self
::addUrls($this, $this->_contactId
);
212 if ($contactType == 'Organization' &&
213 CRM_Core_Permission
::check('administer Multiple Organizations') &&
214 Civi
::settings()->get('is_enabled')) {
215 //check is any relationship between the organization and groups
216 $groupOrg = CRM_Contact_BAO_GroupOrganization
::hasGroupAssociated($this->_contactId
);
218 $groupOrganizationUrl = CRM_Utils_System
::url('civicrm/group',
219 "reset=1&oid={$this->_contactId}"
221 $this->assign('groupOrganizationUrl', $groupOrganizationUrl);
227 * Get meta details of the contact.
229 * @param int $contactId
232 * contact fields in fixed order
234 public static function getContactDetails($contactId) {
235 return list($displayName,
240 ) = CRM_Contact_BAO_Contact
::getDisplayAndImage($contactId,
248 * @param int $contactID
250 public static function checkUserPermission($page, $contactID = NULL) {
251 // check for permissions
252 $page->_permission
= NULL;
255 $contactID = $page->_contactId
;
258 // automatically grant permissin for users on their own record. makes
259 // things easier in dashboard
260 $session = CRM_Core_Session
::singleton();
262 if ($session->get('userID') == $contactID && CRM_Core_Permission
::check('edit my contact')) {
263 $page->assign('permission', 'edit');
264 $page->_permission
= CRM_Core_Permission
::EDIT
;
265 // deleted contacts’ stuff should be (at best) only viewable
267 elseif (CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'is_deleted') and CRM_Core_Permission
::check('access deleted contacts')) {
268 $page->assign('permission', 'view');
269 $page->_permission
= CRM_Core_Permission
::VIEW
;
271 elseif (CRM_Contact_BAO_Contact_Permission
::allow($contactID, CRM_Core_Permission
::EDIT
)) {
272 $page->assign('permission', 'edit');
273 $page->_permission
= CRM_Core_Permission
::EDIT
;
275 elseif (CRM_Contact_BAO_Contact_Permission
::allow($contactID, CRM_Core_Permission
::VIEW
)) {
276 $page->assign('permission', 'view');
277 $page->_permission
= CRM_Core_Permission
::VIEW
;
280 $session->pushUserContext(CRM_Utils_System
::url('civicrm', 'reset=1'));
281 CRM_Core_Error
::statusBounce(ts('You do not have the necessary permission to view this contact.'));
286 * @param int $contactId
287 * @param bool $isDeleted
291 public static function setTitle($contactId, $isDeleted = FALSE) {
292 static $contactDetails;
293 $contactImage = NULL;
294 if (!isset($contactDetails[$contactId])) {
295 list($displayName, $contactImage) = self
::getContactDetails($contactId);
296 $contactDetails[$contactId] = [
297 'displayName' => $displayName,
298 'contactImage' => $contactImage,
299 'isDeceased' => (bool) CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'is_deceased'),
303 $displayName = $contactDetails[$contactId]['displayName'];
304 $contactImage = $contactDetails[$contactId]['contactImage'];
308 $title = "{$contactImage} {$displayName}";
309 if ($contactDetails[$contactId]['isDeceased']) {
310 $title .= ' <span class="crm-contact-deceased">(' . ts('deceased') . ')</span>';
313 $title = "<del>{$title}</del>";
314 $mergedTo = civicrm_api3('Contact', 'getmergedto', ['contact_id' => $contactId, 'api.Contact.get' => ['return' => 'display_name']]);
315 if ($mergedTo['count']) {
316 $mergedToContactID = $mergedTo['id'];
317 $mergedToDisplayName = $mergedTo['values'][$mergedToContactID]['api.Contact.get']['values'][0]['display_name'];
318 $title .= ' ' . ts('(This contact has been merged to <a href="%1">%2</a>)', [
319 1 => CRM_Utils_System
::url('civicrm/contact/view', ['reset' => 1, 'cid' => $mergedToContactID]),
320 2 => $mergedToDisplayName,
325 // Inline-edit places its own title on the page
326 CRM_Utils_System
::setTitle('CiviCRM', '<span id="crm-remove-title" style="display:none">CiviCRM</span>');
332 * Add urls for display in the actions menu.
333 * @param CRM_Core_Page $obj
336 public static function addUrls(&$obj, $cid) {
337 $uid = CRM_Core_BAO_UFMatch
::getUFId($cid);
340 $userRecordUrl = CRM_Core_Config
::singleton()->userSystem
->getUserRecordUrl($cid);
341 $obj->assign('userRecordUrl', $userRecordUrl);
342 $obj->assign('userRecordId', $uid);
344 elseif (CRM_Core_Config
::singleton()->userSystem
->checkPermissionAddUser()) {
345 $userAddUrl = CRM_Utils_System
::url('civicrm/contact/view/useradd', 'reset=1&action=add&cid=' . $cid);
346 $obj->assign('userAddUrl', $userAddUrl);
349 if (CRM_Core_Permission
::check('access Contact Dashboard')) {
350 $dashboardURL = CRM_Utils_System
::url('civicrm/user',
353 $obj->assign('dashboardURL', $dashboardURL);
356 // See if other modules want to add links to the activtity bar
358 CRM_Utils_Hook
::links('view.contact.activity',
363 if (is_array($hookLinks)) {
364 $obj->assign('hookLinks', $hookLinks);