Merge pull request #10197 from GinkgoFJG/CRM-20458
[civicrm-core.git] / CRM / Contact / Page / View.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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
31 * @copyright CiviCRM LLC (c) 2004-2017
32 */
33
34 /**
35 * Main page for viewing contact.
36 */
37 class CRM_Contact_Page_View extends CRM_Core_Page {
38
39 /**
40 * The id of the object being viewed (note/relationship etc)
41 *
42 * @int
43 */
44 protected $_id;
45
46 /**
47 * The contact id of the contact being viewed
48 *
49 * @int
50 */
51 protected $_contactId;
52
53 /**
54 * The action that we are performing
55 *
56 * @string
57 */
58 protected $_action;
59
60 /**
61 * The permission we have on this contact
62 *
63 * @string
64 */
65 protected $_permission;
66
67 /**
68 * Heart of the viewing process.
69 *
70 * The runner gets all the meta data for the contact and calls the appropriate type of page to view.
71 */
72 public function preProcess() {
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
102 if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'id') != $this->_contactId) {
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 $context = CRM_Utils_Array::value('context', $_GET);
139 if (!$found) {
140 // seems like we did not find any contacts
141 // maybe due to bug CRM-9096
142 // however we should account for 1 contact results (which dont have prev next)
143 if (!$pos['foundEntry']) {
144 $navContacts['nextPrevError'] = 1;
145 }
146 }
147 elseif ($context) {
148 $this->assign('context', $context);
149 CRM_Utils_System::appendBreadCrumb(array(
150 array(
151 'title' => ts('Search Results'),
152 'url' => CRM_Utils_System::url("civicrm/contact/search/$context", array('qfKey' => $qfKey)),
153 ),
154 ));
155 }
156 }
157 $this->assign($navContacts);
158
159 $path = CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $this->_contactId);
160 CRM_Utils_System::appendBreadCrumb(array(array('title' => ts('View Contact'), 'url' => $path)));
161
162 if ($image_URL = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'image_URL')) {
163 $this->assign("imageURL", CRM_Utils_File::getImageURL($image_URL));
164 }
165
166 // also store in session for future use
167 $session = CRM_Core_Session::singleton();
168 $session->set('view.id', $this->_contactId);
169
170 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
171 $this->assign('action', $this->_action);
172
173 // check logged in user permission
174 self::checkUserPermission($this);
175
176 list($displayName, $contactImage, $contactType, $contactSubtype, $contactImageUrl) = self::getContactDetails($this->_contactId);
177 $this->assign('displayName', $displayName);
178
179 $this->set('contactType', $contactType);
180
181 // note: there could still be multiple subtypes. We just trimming the outer separator.
182 $this->set('contactSubtype', trim($contactSubtype, CRM_Core_DAO::VALUE_SEPARATOR));
183
184 // add to recently viewed block
185 $isDeleted = (bool) CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'is_deleted');
186
187 $recentOther = array(
188 'imageUrl' => $contactImageUrl,
189 'subtype' => $contactSubtype,
190 'isDeleted' => $isDeleted,
191 );
192
193 if (CRM_Contact_BAO_Contact_Permission::allow($this->_contactId, CRM_Core_Permission::EDIT)) {
194 $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/add', "reset=1&action=update&cid={$this->_contactId}");
195 }
196
197 if (($session->get('userID') != $this->_contactId) && CRM_Core_Permission::check('delete contacts')
198 && !$isDeleted
199 ) {
200 $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/delete', "reset=1&delete=1&cid={$this->_contactId}");
201 }
202
203 CRM_Utils_Recent::add($displayName,
204 CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$this->_contactId}"),
205 $this->_contactId,
206 $contactType,
207 $this->_contactId,
208 $displayName,
209 $recentOther
210 );
211 $this->assign('isDeleted', $isDeleted);
212
213 // set page title
214 $title = self::setTitle($this->_contactId, $isDeleted);
215 $this->assign('title', $title);
216
217 // Check if this is default domain contact CRM-10482
218 if (CRM_Contact_BAO_Contact::checkDomainContact($this->_contactId)) {
219 $this->assign('domainContact', TRUE);
220 }
221 else {
222 $this->assign('domainContact', FALSE);
223 }
224
225 // Add links for actions menu
226 self::addUrls($this, $this->_contactId);
227
228 if ($contactType == 'Organization' &&
229 CRM_Core_Permission::check('administer Multiple Organizations') &&
230 Civi::settings()->get('is_enabled')) {
231 //check is any relationship between the organization and groups
232 $groupOrg = CRM_Contact_BAO_GroupOrganization::hasGroupAssociated($this->_contactId);
233 if ($groupOrg) {
234 $groupOrganizationUrl = CRM_Utils_System::url('civicrm/group',
235 "reset=1&oid={$this->_contactId}"
236 );
237 $this->assign('groupOrganizationUrl', $groupOrganizationUrl);
238 }
239 }
240 }
241
242 /**
243 * Get meta details of the contact.
244 *
245 * @param int $contactId
246 *
247 * @return array
248 * contact fields in fixed order
249 */
250 public static function getContactDetails($contactId) {
251 return list($displayName,
252 $contactImage,
253 $contactType,
254 $contactSubtype,
255 $contactImageUrl
256 ) = CRM_Contact_BAO_Contact::getDisplayAndImage($contactId,
257 TRUE,
258 TRUE
259 );
260 }
261
262 /**
263 * @param $page
264 * @param int $contactID
265 */
266 public static function checkUserPermission($page, $contactID = NULL) {
267 // check for permissions
268 $page->_permission = NULL;
269
270 if (!$contactID) {
271 $contactID = $page->_contactId;
272 }
273
274 // automatically grant permissin for users on their own record. makes
275 // things easier in dashboard
276 $session = CRM_Core_Session::singleton();
277
278 if ($session->get('userID') == $contactID && CRM_Core_Permission::check('edit my contact')) {
279 $page->assign('permission', 'edit');
280 $page->_permission = CRM_Core_Permission::EDIT;
281 // deleted contacts’ stuff should be (at best) only viewable
282 }
283 elseif (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'is_deleted') and CRM_Core_Permission::check('access deleted contacts')) {
284 $page->assign('permission', 'view');
285 $page->_permission = CRM_Core_Permission::VIEW;
286 }
287 elseif (CRM_Contact_BAO_Contact_Permission::allow($contactID, CRM_Core_Permission::EDIT)) {
288 $page->assign('permission', 'edit');
289 $page->_permission = CRM_Core_Permission::EDIT;
290 }
291 elseif (CRM_Contact_BAO_Contact_Permission::allow($contactID, CRM_Core_Permission::VIEW)) {
292 $page->assign('permission', 'view');
293 $page->_permission = CRM_Core_Permission::VIEW;
294 }
295 else {
296 $session->pushUserContext(CRM_Utils_System::url('civicrm', 'reset=1'));
297 CRM_Core_Error::statusBounce(ts('You do not have the necessary permission to view this contact.'));
298 }
299 }
300
301 /**
302 * @param int $contactId
303 * @param bool $isDeleted
304 *
305 * @return string
306 */
307 public static function setTitle($contactId, $isDeleted = FALSE) {
308 static $contactDetails;
309 $displayName = $contactImage = NULL;
310 if (!isset($contactDetails[$contactId])) {
311 list($displayName, $contactImage) = self::getContactDetails($contactId);
312 $contactDetails[$contactId] = array(
313 'displayName' => $displayName,
314 'contactImage' => $contactImage,
315 'isDeceased' => (bool) CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'is_deceased'),
316 );
317 }
318 else {
319 $displayName = $contactDetails[$contactId]['displayName'];
320 $contactImage = $contactDetails[$contactId]['contactImage'];
321 }
322
323 // set page title
324 $title = "{$contactImage} {$displayName}";
325 if ($contactDetails[$contactId]['isDeceased']) {
326 $title .= ' <span class="crm-contact-deceased">(deceased)</span>';
327 }
328 if ($isDeleted) {
329 $title = "<del>{$title}</del>";
330 }
331
332 // Inline-edit places its own title on the page
333 CRM_Utils_System::setTitle('CiviCRM', '<span id="crm-remove-title" style="display:none">CiviCRM</span>');
334
335 return $title;
336 }
337
338 /**
339 * Add urls for display in the actions menu.
340 * @param CRM_Core_Page $obj
341 * @param int $cid
342 */
343 public static function addUrls(&$obj, $cid) {
344 $uid = CRM_Core_BAO_UFMatch::getUFId($cid);
345
346 if ($uid) {
347 $userRecordUrl = CRM_Core_Config::singleton()->userSystem->getUserRecordUrl($cid);
348 $obj->assign('userRecordUrl', $userRecordUrl);
349 $obj->assign('userRecordId', $uid);
350 }
351 elseif (CRM_Core_Config::singleton()->userSystem->checkPermissionAddUser()) {
352 $userAddUrl = CRM_Utils_System::url('civicrm/contact/view/useradd', 'reset=1&action=add&cid=' . $cid);
353 $obj->assign('userAddUrl', $userAddUrl);
354 }
355
356 if (CRM_Core_Permission::check('access Contact Dashboard')) {
357 $dashboardURL = CRM_Utils_System::url('civicrm/user',
358 "reset=1&id={$cid}"
359 );
360 $obj->assign('dashboardURL', $dashboardURL);
361 }
362
363 // See if other modules want to add links to the activtity bar
364 $hookLinks = array();
365 CRM_Utils_Hook::links('view.contact.activity',
366 'Contact',
367 $cid,
368 $hookLinks
369 );
370 if (is_array($hookLinks)) {
371 $obj->assign('hookLinks', $hookLinks);
372 }
373 }
374
375 }