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