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