Merge pull request #6680 from colemanw/cleanup
[civicrm-core.git] / CRM / Contact / Page / View.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
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) {
88 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
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) {
120 $pos = CRM_Core_BAO_PrevNextCache::getPositions("civicrm search $qfKey",
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
138 if (!$found) {
139 // seems like we did not find any contacts
140 // maybe due to bug CRM-9096
141 // however we should account for 1 contact results (which dont have prev next)
142 if (!$pos['foundEntry']) {
143 $navContacts['nextPrevError'] = 1;
144 }
145 }
146 }
147 $this->assign($navContacts);
148
149 $path = CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $this->_contactId);
ce80b209 150 CRM_Utils_System::appendBreadCrumb(array(array('title' => ts('View Contact'), 'url' => $path)));
6a488035
TO
151
152 if ($image_URL = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'image_URL')) {
6a488035
TO
153 //CRM-7265 --time being fix.
154 $config = CRM_Core_Config::singleton();
155 $image_URL = str_replace('https://', 'http://', $image_URL);
156 if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'enableSSL')) {
157 $image_URL = str_replace('http://', 'https://', $image_URL);
158 }
159
77d45291 160 list($imageWidth, $imageHeight) = getimagesize(CRM_Utils_String::unstupifyUrl($image_URL));
6a488035
TO
161 list($imageThumbWidth, $imageThumbHeight) = CRM_Contact_BAO_Contact::getThumbSize($imageWidth, $imageHeight);
162 $this->assign("imageWidth", $imageWidth);
163 $this->assign("imageHeight", $imageHeight);
164 $this->assign("imageThumbWidth", $imageThumbWidth);
165 $this->assign("imageThumbHeight", $imageThumbHeight);
166 $this->assign("imageURL", $image_URL);
167 }
168
169 // also store in session for future use
170 $session = CRM_Core_Session::singleton();
171 $session->set('view.id', $this->_contactId);
172
173 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
174 $this->assign('action', $this->_action);
175
176 // check logged in user permission
177 self::checkUserPermission($this);
178
006389de 179 list($displayName, $contactImage, $contactType, $contactSubtype, $contactImageUrl) = self::getContactDetails($this->_contactId);
6a488035
TO
180 $this->assign('displayName', $displayName);
181
182 $this->set('contactType', $contactType);
ae34b946
DS
183
184 // note: there could still be multiple subtypes. We just trimming the outer separator.
185 $this->set('contactSubtype', trim($contactSubtype, CRM_Core_DAO::VALUE_SEPARATOR));
6a488035
TO
186
187 // add to recently viewed block
188 $isDeleted = (bool) CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'is_deleted');
189
190 $recentOther = array(
191 'imageUrl' => $contactImageUrl,
192 'subtype' => $contactSubtype,
193 'isDeleted' => $isDeleted,
194 );
195
1795d03b 196 if (CRM_Contact_BAO_Contact_Permission::allow($this->_contactId, CRM_Core_Permission::EDIT)) {
6a488035
TO
197 $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/add', "reset=1&action=update&cid={$this->_contactId}");
198 }
199
200 if (($session->get('userID') != $this->_contactId) && CRM_Core_Permission::check('delete contacts')
201 && !$isDeleted
202 ) {
203 $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/delete', "reset=1&delete=1&cid={$this->_contactId}");
204 }
205
206 CRM_Utils_Recent::add($displayName,
207 CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$this->_contactId}"),
208 $this->_contactId,
209 $contactType,
210 $this->_contactId,
211 $displayName,
212 $recentOther
213 );
214 $this->assign('isDeleted', $isDeleted);
215
216 // set page title
217 $title = self::setTitle($this->_contactId, $isDeleted);
218 $this->assign('title', $title);
aafd773a 219
6a488035
TO
220 // Check if this is default domain contact CRM-10482
221 if (CRM_Contact_BAO_Contact::checkDomainContact($this->_contactId)) {
222 $this->assign('domainContact', TRUE);
0db6c3e1
TO
223 }
224 else {
88b7498f 225 $this->assign('domainContact', FALSE);
6a488035
TO
226 }
227
228 // Add links for actions menu
229 self::addUrls($this, $this->_contactId);
230
231 if ($contactType == 'Organization' &&
232 CRM_Core_Permission::check('administer Multiple Organizations') &&
233 CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME,
234 'is_enabled'
235 )
236 ) {
237 //check is any relationship between the organization and groups
238 $groupOrg = CRM_Contact_BAO_GroupOrganization::hasGroupAssociated($this->_contactId);
239 if ($groupOrg) {
240 $groupOrganizationUrl = CRM_Utils_System::url('civicrm/group',
241 "reset=1&oid={$this->_contactId}"
242 );
243 $this->assign('groupOrganizationUrl', $groupOrganizationUrl);
244 }
245 }
246 }
247
248 /**
249 * Get meta details of the contact.
250 *
100fef9d 251 * @param int $contactId
fd31fa4c 252 *
a6c01b45
CW
253 * @return array
254 * contact fields in fixed order
6a488035 255 */
00be9182 256 public static function getContactDetails($contactId) {
6a488035
TO
257 return list($displayName,
258 $contactImage,
259 $contactType,
260 $contactSubtype,
261 $contactImageUrl
353ffa53 262 ) = CRM_Contact_BAO_Contact::getDisplayAndImage($contactId,
6a488035
TO
263 TRUE,
264 TRUE
006389de 265 );
6a488035
TO
266 }
267
4319322b
EM
268 /**
269 * @param $page
100fef9d 270 * @param int $contactID
4319322b 271 */
00be9182 272 public static function checkUserPermission($page, $contactID = NULL) {
6a488035
TO
273 // check for permissions
274 $page->_permission = NULL;
275
276 if (!$contactID) {
277 $contactID = $page->_contactId;
278 }
279
280 // automatically grant permissin for users on their own record. makes
281 // things easier in dashboard
282 $session = CRM_Core_Session::singleton();
283
ad623fd4 284 if ($session->get('userID') == $contactID && CRM_Core_Permission::check('edit my contact')) {
6a488035
TO
285 $page->assign('permission', 'edit');
286 $page->_permission = CRM_Core_Permission::EDIT;
287 // deleted contacts’ stuff should be (at best) only viewable
288 }
289 elseif (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'is_deleted') and CRM_Core_Permission::check('access deleted contacts')) {
290 $page->assign('permission', 'view');
291 $page->_permission = CRM_Core_Permission::VIEW;
292 }
293 elseif (CRM_Contact_BAO_Contact_Permission::allow($contactID, CRM_Core_Permission::EDIT)) {
294 $page->assign('permission', 'edit');
295 $page->_permission = CRM_Core_Permission::EDIT;
296 }
297 elseif (CRM_Contact_BAO_Contact_Permission::allow($contactID, CRM_Core_Permission::VIEW)) {
298 $page->assign('permission', 'view');
299 $page->_permission = CRM_Core_Permission::VIEW;
300 }
301 else {
302 $session->pushUserContext(CRM_Utils_System::url('civicrm', 'reset=1'));
303 CRM_Core_Error::statusBounce(ts('You do not have the necessary permission to view this contact.'));
304 }
305 }
306
59f97da6 307 /**
100fef9d 308 * @param int $contactId
59f97da6
EM
309 * @param bool $isDeleted
310 *
311 * @return string
312 */
00be9182 313 public static function setTitle($contactId, $isDeleted = FALSE) {
6a488035
TO
314 static $contactDetails;
315 $displayName = $contactImage = NULL;
316 if (!isset($contactDetails[$contactId])) {
317 list($displayName, $contactImage) = self::getContactDetails($contactId);
318 $contactDetails[$contactId] = array(
319 'displayName' => $displayName,
320 'contactImage' => $contactImage,
7d3ddae6 321 'isDeceased' => (bool) CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'is_deceased'),
6a488035
TO
322 );
323 }
324 else {
325 $displayName = $contactDetails[$contactId]['displayName'];
326 $contactImage = $contactDetails[$contactId]['contactImage'];
327 }
328
329 // set page title
330 $title = "{$contactImage} {$displayName}";
7d3ddae6 331 if ($contactDetails[$contactId]['isDeceased']) {
da2786f7 332 $title .= ' <span class="crm-contact-deceased">(deceased)</span>';
7d3ddae6 333 }
6a488035
TO
334 if ($isDeleted) {
335 $title = "<del>{$title}</del>";
336 }
337
338 // Inline-edit places its own title on the page
339 CRM_Utils_System::setTitle('CiviCRM', '<span id="crm-remove-title" style="display:none">CiviCRM</span>');
340
341 return $title;
342 }
343
344 /**
fe482240 345 * Add urls for display in the actions menu.
59f97da6 346 * @param CRM_Core_Page $obj
77c5b619 347 * @param int $cid
6a488035 348 */
00be9182 349 public static function addUrls(&$obj, $cid) {
ce80b209 350 $uid = CRM_Core_BAO_UFMatch::getUFId($cid);
ccd74e76 351
6a488035 352 if ($uid) {
59f97da6 353 $userRecordUrl = CRM_Core_Config::singleton()->userSystem->getUserRecordUrl($cid);
6a488035
TO
354 $obj->assign('userRecordUrl', $userRecordUrl);
355 $obj->assign('userRecordId', $uid);
356 }
59f97da6
EM
357 elseif (CRM_Core_Config::singleton()->userSystem->checkPermissionAddUser()) {
358 $userAddUrl = CRM_Utils_System::url('civicrm/contact/view/useradd', 'reset=1&action=add&cid=' . $cid);
6a488035
TO
359 $obj->assign('userAddUrl', $userAddUrl);
360 }
361
362 if (CRM_Core_Permission::check('access Contact Dashboard')) {
363 $dashboardURL = CRM_Utils_System::url('civicrm/user',
364 "reset=1&id={$cid}"
365 );
366 $obj->assign('dashboardURL', $dashboardURL);
367 }
368
369 // See if other modules want to add links to the activtity bar
a9b15f31
AH
370 $hookLinks = array();
371 CRM_Utils_Hook::links('view.contact.activity',
6a488035
TO
372 'Contact',
373 $cid,
a9b15f31 374 $hookLinks,
6a488035
TO
375 CRM_Core_DAO::$_nullObject,
376 CRM_Core_DAO::$_nullObject
377 );
378 if (is_array($hookLinks)) {
379 $obj->assign('hookLinks', $hookLinks);
380 }
381 }
96025800 382
6a488035 383}