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