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