Merge pull request #13967 from eileenmcnaughton/activity_token
[civicrm-core.git] / CRM / Contact / Page / View.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33
34/**
35 * Main page for viewing contact.
6a488035
TO
36 */
37class CRM_Contact_Page_View extends CRM_Core_Page {
38
39 /**
100fef9d 40 * The id of the object being viewed (note/relationship etc)
6a488035
TO
41 *
42 * @int
6a488035
TO
43 */
44 protected $_id;
45
46 /**
100fef9d 47 * The contact id of the contact being viewed
6a488035
TO
48 *
49 * @int
6a488035
TO
50 */
51 protected $_contactId;
52
53 /**
54 * The action that we are performing
55 *
56 * @string
6a488035
TO
57 */
58 protected $_action;
59
60 /**
61 * The permission we have on this contact
62 *
63 * @string
6a488035
TO
64 */
65 protected $_permission;
66
67 /**
95cdcc0f 68 * Heart of the viewing process.
6a488035 69 *
95cdcc0f 70 * The runner gets all the meta data for the contact and calls the appropriate type of page to view.
6a488035 71 */
00be9182 72 public function preProcess() {
6a488035
TO
73 // process url params
74 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
75 $this->assign('id', $this->_id);
76
77 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $this);
78 //validate the qfKey
79 if (!CRM_Utils_Rule::qfKey($qfKey)) {
80 $qfKey = NULL;
81 }
82 $this->assign('searchKey', $qfKey);
83
84 // retrieve the group contact id, so that we can get contact id
85 $gcid = CRM_Utils_Request::retrieve('gcid', 'Positive', $this);
86
87 if (!$gcid) {
780f5a52 88 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
6a488035
TO
89 }
90 else {
91 $this->_contactId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_GroupContact', $gcid, 'contact_id');
92 }
93
94 if (!$this->_contactId) {
95 CRM_Core_Error::statusBounce(
96 ts('We could not find a contact id.'),
97 CRM_Utils_System::url('civicrm/dashboard', 'reset=1')
98 );
99 }
100
101 // ensure that the id does exist
481a74f4 102 if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'id') != $this->_contactId) {
6a488035
TO
103 CRM_Core_Error::statusBounce(
104 ts('A Contact with that ID does not exist: %1', array(1 => $this->_contactId)),
105 CRM_Utils_System::url('civicrm/dashboard', 'reset=1')
106 );
107 }
108
109 $this->assign('contactId', $this->_contactId);
110
111 // see if we can get prev/next positions from qfKey
112 $navContacts = array(
113 'prevContactID' => NULL,
114 'prevContactName' => NULL,
115 'nextContactID' => NULL,
116 'nextContactName' => NULL,
117 'nextPrevError' => 0,
118 );
119 if ($qfKey) {
d379a6d9 120 $pos = Civi::service('prevnext')->getPositions("civicrm search $qfKey",
6a488035
TO
121 $this->_contactId,
122 $this->_contactId
123 );
124 $found = FALSE;
125
126 if (isset($pos['prev'])) {
127 $navContacts['prevContactID'] = $pos['prev']['id1'];
128 $navContacts['prevContactName'] = $pos['prev']['data'];
129 $found = TRUE;
130 }
131
132 if (isset($pos['next'])) {
133 $navContacts['nextContactID'] = $pos['next']['id1'];
134 $navContacts['nextContactName'] = $pos['next']['data'];
135 $found = TRUE;
136 }
137
72b60e71 138 $context = CRM_Utils_Array::value('context', $_GET);
6a488035
TO
139 if (!$found) {
140 // seems like we did not find any contacts
141 // maybe due to bug CRM-9096
142 // however we should account for 1 contact results (which dont have prev next)
143 if (!$pos['foundEntry']) {
144 $navContacts['nextPrevError'] = 1;
145 }
146 }
72b60e71
CW
147 elseif ($context) {
148 $this->assign('context', $context);
149 CRM_Utils_System::appendBreadCrumb(array(
150 array(
151 'title' => ts('Search Results'),
152 'url' => CRM_Utils_System::url("civicrm/contact/search/$context", array('qfKey' => $qfKey)),
153 ),
154 ));
155 }
6a488035
TO
156 }
157 $this->assign($navContacts);
158
159 $path = CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $this->_contactId);
ce80b209 160 CRM_Utils_System::appendBreadCrumb(array(array('title' => ts('View Contact'), 'url' => $path)));
6a488035
TO
161
162 if ($image_URL = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'image_URL')) {
c3821398 163 $this->assign("imageURL", CRM_Utils_File::getImageURL($image_URL));
6a488035
TO
164 }
165
166 // also store in session for future use
167 $session = CRM_Core_Session::singleton();
168 $session->set('view.id', $this->_contactId);
169
170 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
171 $this->assign('action', $this->_action);
172
173 // check logged in user permission
174 self::checkUserPermission($this);
175
006389de 176 list($displayName, $contactImage, $contactType, $contactSubtype, $contactImageUrl) = self::getContactDetails($this->_contactId);
6a488035
TO
177 $this->assign('displayName', $displayName);
178
179 $this->set('contactType', $contactType);
ae34b946
DS
180
181 // note: there could still be multiple subtypes. We just trimming the outer separator.
182 $this->set('contactSubtype', trim($contactSubtype, CRM_Core_DAO::VALUE_SEPARATOR));
6a488035
TO
183
184 // add to recently viewed block
185 $isDeleted = (bool) CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'is_deleted');
186
187 $recentOther = array(
188 'imageUrl' => $contactImageUrl,
189 'subtype' => $contactSubtype,
190 'isDeleted' => $isDeleted,
191 );
192
1795d03b 193 if (CRM_Contact_BAO_Contact_Permission::allow($this->_contactId, CRM_Core_Permission::EDIT)) {
6a488035
TO
194 $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/add', "reset=1&action=update&cid={$this->_contactId}");
195 }
196
197 if (($session->get('userID') != $this->_contactId) && CRM_Core_Permission::check('delete contacts')
198 && !$isDeleted
199 ) {
200 $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/delete', "reset=1&delete=1&cid={$this->_contactId}");
201 }
202
203 CRM_Utils_Recent::add($displayName,
204 CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$this->_contactId}"),
205 $this->_contactId,
206 $contactType,
207 $this->_contactId,
208 $displayName,
209 $recentOther
210 );
211 $this->assign('isDeleted', $isDeleted);
212
213 // set page title
214 $title = self::setTitle($this->_contactId, $isDeleted);
215 $this->assign('title', $title);
aafd773a 216
6a488035
TO
217 // Check if this is default domain contact CRM-10482
218 if (CRM_Contact_BAO_Contact::checkDomainContact($this->_contactId)) {
219 $this->assign('domainContact', TRUE);
0db6c3e1
TO
220 }
221 else {
88b7498f 222 $this->assign('domainContact', FALSE);
6a488035
TO
223 }
224
225 // Add links for actions menu
226 self::addUrls($this, $this->_contactId);
227
228 if ($contactType == 'Organization' &&
229 CRM_Core_Permission::check('administer Multiple Organizations') &&
aaffa79f 230 Civi::settings()->get('is_enabled')) {
6a488035
TO
231 //check is any relationship between the organization and groups
232 $groupOrg = CRM_Contact_BAO_GroupOrganization::hasGroupAssociated($this->_contactId);
233 if ($groupOrg) {
234 $groupOrganizationUrl = CRM_Utils_System::url('civicrm/group',
235 "reset=1&oid={$this->_contactId}"
236 );
237 $this->assign('groupOrganizationUrl', $groupOrganizationUrl);
238 }
239 }
240 }
241
242 /**
243 * Get meta details of the contact.
244 *
100fef9d 245 * @param int $contactId
fd31fa4c 246 *
a6c01b45
CW
247 * @return array
248 * contact fields in fixed order
6a488035 249 */
00be9182 250 public static function getContactDetails($contactId) {
6a488035
TO
251 return list($displayName,
252 $contactImage,
253 $contactType,
254 $contactSubtype,
255 $contactImageUrl
353ffa53 256 ) = CRM_Contact_BAO_Contact::getDisplayAndImage($contactId,
6a488035
TO
257 TRUE,
258 TRUE
006389de 259 );
6a488035
TO
260 }
261
4319322b
EM
262 /**
263 * @param $page
100fef9d 264 * @param int $contactID
4319322b 265 */
00be9182 266 public static function checkUserPermission($page, $contactID = NULL) {
6a488035
TO
267 // check for permissions
268 $page->_permission = NULL;
269
270 if (!$contactID) {
271 $contactID = $page->_contactId;
272 }
273
274 // automatically grant permissin for users on their own record. makes
275 // things easier in dashboard
276 $session = CRM_Core_Session::singleton();
277
ad623fd4 278 if ($session->get('userID') == $contactID && CRM_Core_Permission::check('edit my contact')) {
6a488035
TO
279 $page->assign('permission', 'edit');
280 $page->_permission = CRM_Core_Permission::EDIT;
281 // deleted contacts’ stuff should be (at best) only viewable
282 }
283 elseif (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'is_deleted') and CRM_Core_Permission::check('access deleted contacts')) {
284 $page->assign('permission', 'view');
285 $page->_permission = CRM_Core_Permission::VIEW;
286 }
287 elseif (CRM_Contact_BAO_Contact_Permission::allow($contactID, CRM_Core_Permission::EDIT)) {
288 $page->assign('permission', 'edit');
289 $page->_permission = CRM_Core_Permission::EDIT;
290 }
291 elseif (CRM_Contact_BAO_Contact_Permission::allow($contactID, CRM_Core_Permission::VIEW)) {
292 $page->assign('permission', 'view');
293 $page->_permission = CRM_Core_Permission::VIEW;
294 }
295 else {
296 $session->pushUserContext(CRM_Utils_System::url('civicrm', 'reset=1'));
297 CRM_Core_Error::statusBounce(ts('You do not have the necessary permission to view this contact.'));
298 }
299 }
300
59f97da6 301 /**
100fef9d 302 * @param int $contactId
59f97da6
EM
303 * @param bool $isDeleted
304 *
305 * @return string
306 */
00be9182 307 public static function setTitle($contactId, $isDeleted = FALSE) {
6a488035 308 static $contactDetails;
93ddaacb 309 $contactImage = NULL;
6a488035
TO
310 if (!isset($contactDetails[$contactId])) {
311 list($displayName, $contactImage) = self::getContactDetails($contactId);
312 $contactDetails[$contactId] = array(
313 'displayName' => $displayName,
314 'contactImage' => $contactImage,
7d3ddae6 315 'isDeceased' => (bool) CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'is_deceased'),
6a488035
TO
316 );
317 }
318 else {
319 $displayName = $contactDetails[$contactId]['displayName'];
320 $contactImage = $contactDetails[$contactId]['contactImage'];
321 }
322
323 // set page title
324 $title = "{$contactImage} {$displayName}";
7d3ddae6 325 if ($contactDetails[$contactId]['isDeceased']) {
da2786f7 326 $title .= ' <span class="crm-contact-deceased">(deceased)</span>';
7d3ddae6 327 }
6a488035
TO
328 if ($isDeleted) {
329 $title = "<del>{$title}</del>";
93ddaacb 330 $mergedTo = civicrm_api3('Contact', 'getmergedto', ['contact_id' => $contactId, 'api.Contact.get' => ['return' => 'display_name']]);
331 if ($mergedTo['count']) {
332 $mergedToContactID = $mergedTo['id'];
333 $mergedToDisplayName = $mergedTo['values'][$mergedToContactID]['api.Contact.get']['values'][0]['display_name'];
334 $title .= ' ' . ts('(This contact has been merged to <a href="%1">%2</a>)', [
335 1 => CRM_Utils_System::url('civicrm/contact/view', ['reset' => 1, 'cid' => $mergedToContactID]),
336 2 => $mergedToDisplayName,
337 ]);
338 }
6a488035
TO
339 }
340
341 // Inline-edit places its own title on the page
342 CRM_Utils_System::setTitle('CiviCRM', '<span id="crm-remove-title" style="display:none">CiviCRM</span>');
343
344 return $title;
345 }
346
347 /**
fe482240 348 * Add urls for display in the actions menu.
59f97da6 349 * @param CRM_Core_Page $obj
77c5b619 350 * @param int $cid
6a488035 351 */
00be9182 352 public static function addUrls(&$obj, $cid) {
ce80b209 353 $uid = CRM_Core_BAO_UFMatch::getUFId($cid);
ccd74e76 354
6a488035 355 if ($uid) {
59f97da6 356 $userRecordUrl = CRM_Core_Config::singleton()->userSystem->getUserRecordUrl($cid);
6a488035
TO
357 $obj->assign('userRecordUrl', $userRecordUrl);
358 $obj->assign('userRecordId', $uid);
359 }
59f97da6
EM
360 elseif (CRM_Core_Config::singleton()->userSystem->checkPermissionAddUser()) {
361 $userAddUrl = CRM_Utils_System::url('civicrm/contact/view/useradd', 'reset=1&action=add&cid=' . $cid);
6a488035
TO
362 $obj->assign('userAddUrl', $userAddUrl);
363 }
364
365 if (CRM_Core_Permission::check('access Contact Dashboard')) {
366 $dashboardURL = CRM_Utils_System::url('civicrm/user',
367 "reset=1&id={$cid}"
368 );
369 $obj->assign('dashboardURL', $dashboardURL);
370 }
371
372 // See if other modules want to add links to the activtity bar
a9b15f31
AH
373 $hookLinks = array();
374 CRM_Utils_Hook::links('view.contact.activity',
6a488035
TO
375 'Contact',
376 $cid,
1273d77c 377 $hookLinks
6a488035
TO
378 );
379 if (is_array($hookLinks)) {
380 $obj->assign('hookLinks', $hookLinks);
381 }
382 }
96025800 383
6a488035 384}