Fixes for contact matching
[civicrm-core.git] / CRM / Contact / Page / View.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Main page for viewing contact.
20 */
21 class CRM_Contact_Page_View extends CRM_Core_Page {
22
23 /**
24 * The id of the object being viewed (note/relationship etc)
25 *
26 * @var int
27 */
28 protected $_id;
29
30 /**
31 * The contact id of the contact being viewed
32 *
33 * @var int
34 */
35 protected $_contactId;
36
37 /**
38 * The action that we are performing
39 *
40 * @var string
41 */
42 protected $_action;
43
44 /**
45 * The permission we have on this contact
46 *
47 * @var string
48 */
49 protected $_permission;
50
51 /**
52 * Heart of the viewing process.
53 *
54 * The runner gets all the meta data for the contact and calls the appropriate type of page to view.
55 */
56 public function preProcess() {
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) {
72 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
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
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')
90 );
91 }
92
93 $this->assign('contactId', $this->_contactId);
94
95 // see if we can get prev/next positions from qfKey
96 $navContacts = [
97 'prevContactID' => NULL,
98 'prevContactName' => NULL,
99 'nextContactID' => NULL,
100 'nextContactName' => NULL,
101 'nextPrevError' => 0,
102 ];
103 if ($qfKey) {
104 $pos = Civi::service('prevnext')->getPositions("civicrm search $qfKey",
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
122 $context = $_GET['context'] ?? NULL;
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 }
131 elseif ($context) {
132 $this->assign('context', $context);
133 CRM_Utils_System::appendBreadCrumb([
134 [
135 'title' => ts('Search Results'),
136 'url' => CRM_Utils_System::url("civicrm/contact/search/$context", ['qfKey' => $qfKey]),
137 ],
138 ]);
139 }
140 }
141 $this->assign($navContacts);
142
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]]);
145
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) : '');
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
159 [$displayName, $contactImage, $contactType, $contactSubtype, $contactImageUrl] = self::getContactDetails($this->_contactId);
160 $this->assign('displayName', $displayName);
161
162 $this->set('contactType', $contactType);
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));
166
167 // add to recently viewed block
168 $isDeleted = (bool) CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'is_deleted');
169
170 $recentOther = [
171 'imageUrl' => $contactImageUrl,
172 'is_deleted' => $isDeleted,
173 ];
174
175 if (CRM_Contact_BAO_Contact_Permission::allow($this->_contactId, CRM_Core_Permission::EDIT)) {
176 $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/add', "reset=1&action=update&cid={$this->_contactId}");
177 }
178
179 if (($session->get('userID') != $this->_contactId) && CRM_Core_Permission::check('delete contacts')
180 && !$isDeleted
181 ) {
182 $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/delete', "reset=1&delete=1&cid={$this->_contactId}");
183 }
184
185 CRM_Utils_Recent::add($displayName,
186 CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$this->_contactId}"),
187 $this->_contactId,
188 'Contact',
189 $this->_contactId,
190 $displayName,
191 $recentOther
192 );
193 $this->assign('isDeleted', $isDeleted);
194
195 // set page title
196 $title = self::setTitle($this->_contactId, $isDeleted);
197 $this->assign('title', $title);
198
199 // Check if this is default domain contact CRM-10482
200 if (CRM_Contact_BAO_Contact::checkDomainContact($this->_contactId)) {
201 $this->assign('domainContact', TRUE);
202 }
203 else {
204 $this->assign('domainContact', FALSE);
205 }
206
207 // Add links for actions menu
208 self::addUrls($this, $this->_contactId);
209 $this->assign('groupOrganizationUrl', $this->getGroupOrganizationUrl($contactType));
210
211 // Assign deleteURL variable, used as part of ContactImage.tpl
212 self::$_template->ensureVariablesAreAssigned(['deleteURL']);
213 }
214
215 /**
216 * Get meta details of the contact.
217 *
218 * @param int $contactId
219 *
220 * @return array
221 * contact fields in fixed order
222 */
223 public static function getContactDetails($contactId) {
224 return list($displayName,
225 $contactImage,
226 $contactType,
227 $contactSubtype,
228 $contactImageUrl
229 ) = CRM_Contact_BAO_Contact::getDisplayAndImage($contactId,
230 TRUE,
231 TRUE
232 );
233 }
234
235 /**
236 * @param CRM_Core_Page $page
237 * @param int $contactID
238 */
239 public static function checkUserPermission($page, $contactID = NULL) {
240 // check for permissions
241 $page->_permission = NULL;
242
243 if (!$contactID) {
244 $contactID = $page->_contactId;
245 }
246
247 // automatically grant permission for users on their own record. makes
248 // things easier in dashboard
249 $session = CRM_Core_Session::singleton();
250
251 if ($session->get('userID') == $contactID && CRM_Core_Permission::check('edit my contact')) {
252 $page->assign('permission', 'edit');
253 $page->_permission = CRM_Core_Permission::EDIT;
254 // deleted contacts’ stuff should be (at best) only viewable
255 }
256 elseif (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'is_deleted') and CRM_Core_Permission::check('access deleted contacts')) {
257 $page->assign('permission', 'view');
258 $page->_permission = CRM_Core_Permission::VIEW;
259 }
260 elseif (CRM_Contact_BAO_Contact_Permission::allow($contactID, CRM_Core_Permission::EDIT)) {
261 $page->assign('permission', 'edit');
262 $page->_permission = CRM_Core_Permission::EDIT;
263 }
264 elseif (CRM_Contact_BAO_Contact_Permission::allow($contactID, CRM_Core_Permission::VIEW)) {
265 $page->assign('permission', 'view');
266 $page->_permission = CRM_Core_Permission::VIEW;
267 }
268 else {
269 $session->pushUserContext(CRM_Utils_System::url('civicrm', 'reset=1'));
270 CRM_Core_Error::statusBounce(ts('You do not have the necessary permission to view this contact.'));
271 }
272 }
273
274 /**
275 * @param int $contactId
276 * @param bool $isDeleted
277 *
278 * @return string
279 */
280 public static function setTitle($contactId, $isDeleted = FALSE) {
281 static $contactDetails;
282 $contactImage = NULL;
283 if (!isset($contactDetails[$contactId])) {
284 [$displayName, $contactImage] = self::getContactDetails($contactId);
285 $contactDetails[$contactId] = [
286 'displayName' => $displayName,
287 'contactImage' => $contactImage,
288 'isDeceased' => (bool) CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'is_deceased'),
289 ];
290 }
291 else {
292 $displayName = $contactDetails[$contactId]['displayName'];
293 $contactImage = $contactDetails[$contactId]['contactImage'];
294 }
295
296 // set page title
297 $title = "{$contactImage} {$displayName}";
298 if ($contactDetails[$contactId]['isDeceased']) {
299 $title .= ' <span class="crm-contact-deceased">(' . ts('deceased') . ')</span>';
300 }
301 if ($isDeleted) {
302 $title = "<del>{$title}</del>";
303 try {
304 $mergedTo = civicrm_api3('Contact', 'getmergedto', ['contact_id' => $contactId, 'api.Contact.get' => ['return' => 'display_name']]);
305 }
306 catch (CiviCRM_API3_Exception $e) {
307 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.'));
308 $mergedTo = ['count' => 0];
309 }
310 if ($mergedTo['count']) {
311 $mergedToContactID = $mergedTo['id'];
312 $mergedToDisplayName = $mergedTo['values'][$mergedToContactID]['api.Contact.get']['values'][0]['display_name'];
313 $title .= ' ' . ts('(This contact has been merged to <a href="%1">%2</a>)', [
314 1 => CRM_Utils_System::url('civicrm/contact/view', ['reset' => 1, 'cid' => $mergedToContactID]),
315 2 => $mergedToDisplayName,
316 ]);
317 }
318 }
319
320 // Inline-edit places its own title on the page
321 CRM_Utils_System::setTitle('CiviCRM', '<span id="crm-remove-title" style="display:none">CiviCRM</span>');
322
323 return $title;
324 }
325
326 /**
327 * Add urls for display in the actions menu.
328 * @param CRM_Core_Page $obj
329 * @param int $cid
330 */
331 public static function addUrls(&$obj, $cid) {
332 $uid = CRM_Core_BAO_UFMatch::getUFId($cid);
333 $obj->assign('userRecordId', $uid);
334 $userRecordUrl = '';
335 if ($uid) {
336 $userRecordUrl = CRM_Core_Config::singleton()->userSystem->getUserRecordUrl($cid);
337 }
338 elseif (CRM_Core_Config::singleton()->userSystem->checkPermissionAddUser()) {
339 $userAddUrl = CRM_Utils_System::url('civicrm/contact/view/useradd', 'reset=1&action=add&cid=' . $cid);
340 $obj->assign('userAddUrl', $userAddUrl);
341 }
342 $obj->assign('userRecordUrl', $userRecordUrl);
343
344 if (CRM_Core_Permission::check('access Contact Dashboard')) {
345 $dashboardURL = CRM_Utils_System::url('civicrm/user',
346 "reset=1&id={$cid}"
347 );
348 $obj->assign('dashboardURL', $dashboardURL);
349 }
350
351 // See if other modules want to add links to the activtity bar
352 $hookLinks = [];
353 CRM_Utils_Hook::links('view.contact.activity',
354 'Contact',
355 $cid,
356 $hookLinks
357 );
358 if (is_array($hookLinks)) {
359 $obj->assign('hookLinks', $hookLinks);
360 }
361 }
362
363 /**
364 * @param string $contactType
365 *
366 * @return string
367 */
368 protected function getGroupOrganizationUrl(string $contactType): string {
369 if ($contactType !== 'Organization' || !CRM_Core_Permission::check('administer Multiple Organizations')
370 || !CRM_Contact_BAO_GroupOrganization::hasGroupAssociated($this->_contactId)
371 || !Civi::settings()->get('is_enabled')
372 ) {
373 return '';
374 }
375 return CRM_Utils_System::url('civicrm/group', "reset=1&oid={$this->_contactId}");
376 }
377
378 }