Merge branch '5.48' to master
[civicrm-core.git] / CRM / Contact / Page / View / Summary.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Main page for viewing contact.
20 */
21 class CRM_Contact_Page_View_Summary extends CRM_Contact_Page_View {
22
23 /**
24 * Heart of the viewing process.
25 *
26 * The runner gets all the meta data for the contact and calls the appropriate type of page to view.
27 */
28 public function preProcess() {
29 parent::preProcess();
30
31 // actions buttom contextMenu
32 $menuItems = CRM_Contact_BAO_Contact::contextMenu($this->_contactId);
33
34 $this->assign('actionsMenuList', $menuItems);
35
36 //retrieve inline custom data
37 $entityType = $this->get('contactType');
38 if ($entitySubType = $this->get('contactSubtype')) {
39 $entitySubType = explode(CRM_Core_DAO::VALUE_SEPARATOR,
40 trim($entitySubType, CRM_Core_DAO::VALUE_SEPARATOR)
41 );
42 }
43 // Custom groups with VIEW permission
44 $visibleGroups = CRM_Core_BAO_CustomGroup::getTree($entityType,
45 NULL,
46 $this->_contactId,
47 NULL,
48 $entitySubType,
49 NULL,
50 TRUE,
51 NULL,
52 FALSE,
53 CRM_Core_Permission::VIEW
54 );
55
56 CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $visibleGroups, FALSE, NULL, NULL, NULL, $this->_contactId, CRM_Core_Permission::EDIT);
57
58 // also create the form element for the activity links box
59 $controller = new CRM_Core_Controller_Simple(
60 'CRM_Activity_Form_ActivityLinks',
61 ts('Activity Links'),
62 NULL,
63 FALSE,
64 FALSE,
65 TRUE
66 );
67 $controller->setEmbedded(TRUE);
68 $controller->run();
69 }
70
71 /**
72 * Heart of the viewing process.
73 *
74 * The runner gets all the meta data for the contact and calls the appropriate type of page to view.
75 */
76 public function run() {
77 $this->preProcess();
78
79 if ($this->_action & CRM_Core_Action::UPDATE) {
80 $this->edit();
81 }
82 else {
83 $this->view();
84 }
85
86 return parent::run();
87 }
88
89 /**
90 * Edit name and address of a contact.
91 */
92 public function edit() {
93 // set the userContext stack
94 $session = CRM_Core_Session::singleton();
95 $url = CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $this->_contactId);
96 $session->pushUserContext($url);
97
98 $controller = new CRM_Core_Controller_Simple('CRM_Contact_Form_Contact', ts('Contact Page'), CRM_Core_Action::UPDATE);
99 $controller->setEmbedded(TRUE);
100 $controller->process();
101 return $controller->run();
102 }
103
104 /**
105 * View summary details of a contact.
106 *
107 * @throws \CRM_Core_Exception
108 */
109 public function view() {
110 // Add js for tabs, in-place editing, and jstree for tags
111 CRM_Core_Resources::singleton()
112 ->addScriptFile('civicrm', 'templates/CRM/Contact/Page/View/Summary.js', 2, 'html-header')
113 ->addStyleFile('civicrm', 'css/contactSummary.css', 2, 'html-header')
114 ->addScriptFile('civicrm', 'templates/CRM/common/TabHeader.js', 1, 'html-header')
115 ->addSetting([
116 'summaryPrint' => ['mode' => $this->_print],
117 'tabSettings' => ['active' => CRM_Utils_Request::retrieve('selectedChild', 'Alphanumeric', $this, FALSE, 'summary')],
118 ]);
119 $this->assign('summaryPrint', $this->_print);
120 $session = CRM_Core_Session::singleton();
121 $url = CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $this->_contactId);
122 $session->pushUserContext($url);
123 $this->assignFieldMetadataToTemplate('Contact');
124
125 $params = [];
126 $defaults = [
127 // Set empty default values for these - they will be overwritten when the contact is
128 // loaded in CRM_Contact_BAO_Contact::retrieve if there are real values
129 // but since we are not using apiV4 they will be left unset if empty.
130 // However, the wind up assigned as smarty variables so we ensure they are set to prevent e-notices
131 // used by ContactInfo.tpl
132 'job_title' => '',
133 'current_employer_id' => '',
134 'nick_name' => '',
135 'legal_name' => '',
136 'source' => '',
137 'sic_code' => '',
138 'external_identifier' => '',
139 // for CommunicationPreferences.tpl
140 'postal_greeting_custom' => '',
141 'email_greeting_custom' => '',
142 'addressee_custom' => '',
143 'communication_style_display' => '',
144 // for Demographics.tpl
145 'age' => ['y' => '', 'm' => ''],
146 'birth_date' => '',
147 // for Website.tpl (the others don't seem to enotice for some reason).
148 'website' => [],
149 ];
150
151 $params['contact_id'] = $this->_contactId;
152
153 CRM_Contact_BAO_Contact::getValues(array_merge(['id' => $this->_contactId], $params), $defaults);
154 $defaults['im'] = CRM_Core_BAO_IM::getValues(['contact_id' => $params['contact_id']]);
155 $defaults['email'] = CRM_Core_BAO_Email::getValues(['contact_id' => $params['contact_id']]);
156 $defaults['openid'] = CRM_Core_BAO_OpenID::getValues(['contact_id' => $params['contact_id']]);
157 $defaults['phone'] = CRM_Core_BAO_Phone::getValues(['contact_id' => $params['contact_id']]);
158 $defaults['address'] = CRM_Core_BAO_Address::getValues(['contact_id' => $params['contact_id']], TRUE);
159 CRM_Core_BAO_Website::getValues($params, $defaults);
160 // Copy employer fields to the current_employer keys.
161 if (($defaults['contact_type'] === 'Individual') && !empty($defaults['employer_id']) && !empty($defaults['organization_name'])) {
162 $defaults['current_employer'] = $defaults['organization_name'];
163 $defaults['current_employer_id'] = $defaults['employer_id'];
164 }
165
166 // Let summary page know if outbound mail is disabled so email links can be built conditionally
167 $mailingBackend = Civi::settings()->get('mailing_backend');
168 $this->assign('mailingOutboundOption', $mailingBackend['outBound_option']);
169
170 $communicationType = [
171 'phone' => [
172 'type' => 'phoneType',
173 'id' => 'phone_type',
174 'daoName' => 'CRM_Core_DAO_Phone',
175 'fieldName' => 'phone_type_id',
176 ],
177 'im' => [
178 'type' => 'IMProvider',
179 'id' => 'provider',
180 'daoName' => 'CRM_Core_DAO_IM',
181 'fieldName' => 'provider_id',
182 ],
183 'website' => [
184 'type' => 'websiteType',
185 'id' => 'website_type',
186 'daoName' => 'CRM_Core_DAO_Website',
187 'fieldName' => 'website_type_id',
188 ],
189 'address' => ['skip' => TRUE, 'customData' => 1],
190 'email' => ['skip' => TRUE],
191 'openid' => ['skip' => TRUE],
192 ];
193
194 foreach ($communicationType as $key => $value) {
195 if (!empty($defaults[$key])) {
196 foreach ($defaults[$key] as & $val) {
197 CRM_Utils_Array::lookupValue($val, 'location_type', CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', ['labelColumn' => 'display_name']), FALSE);
198 if (empty($value['skip'])) {
199 $daoName = $value['daoName'];
200 $pseudoConst = $daoName::buildOptions($value['fieldName'], 'get');
201 CRM_Utils_Array::lookupValue($val, $value['id'], $pseudoConst, FALSE);
202 }
203 }
204 if (isset($value['customData'])) {
205 foreach ($defaults[$key] as $blockId => $blockVal) {
206 $idValue = $blockVal['id'];
207 if ($key == 'address') {
208 if (!empty($blockVal['master_id'])) {
209 $idValue = $blockVal['master_id'];
210 }
211 }
212 $groupTree = CRM_Core_BAO_CustomGroup::getTree(ucfirst($key), NULL, $idValue, NULL, [],
213 NULL, TRUE, NULL, FALSE, CRM_Core_Permission::VIEW);
214 // we setting the prefix to dnc_ below so that we don't overwrite smarty's grouptree var.
215 $defaults[$key][$blockId]['custom'] = CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree, FALSE, NULL, "dnc_");
216 }
217 // reset template variable since that won't be of any use, and could be misleading
218 $this->assign("dnc_viewCustomData", NULL);
219 }
220 }
221 }
222
223 $defaults['gender_display'] = CRM_Core_PseudoConstant::getLabel('CRM_Contact_DAO_Contact', 'gender_id', $defaults['gender_id'] ?? NULL);
224
225 $communicationStyle = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'communication_style_id');
226 if (!empty($communicationStyle)) {
227 if (!empty($defaults['communication_style_id'])) {
228 $defaults['communication_style_display'] = $communicationStyle[$defaults['communication_style_id']];
229 }
230 else {
231 // Make sure the field is displayed as long as it is active, even if it is unset for this contact.
232 $defaults['communication_style_display'] = '';
233 }
234 }
235
236 // to make contact type label available in the template -
237 $contactType = array_key_exists('contact_sub_type', $defaults) ? $defaults['contact_sub_type'] : $defaults['contact_type'];
238 $defaults['contact_type_label'] = CRM_Contact_BAO_ContactType::contactTypePairs(TRUE, $contactType, ', ');
239
240 // get contact tags
241 $defaults['contactTag'] = CRM_Core_BAO_EntityTag::getContactTags($this->_contactId);
242 if (!empty($defaults['contactTag'])) {
243 $defaults['allTags'] = CRM_Core_BAO_Tag::getTagsUsedFor('civicrm_contact', FALSE);
244 }
245
246 $defaults['privacy_values'] = CRM_Core_SelectValues::privacy();
247
248 //Show blocks only if they are visible in edit form
249 $this->_editOptions = CRM_Core_BAO_Setting::valueOptions(
250 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
251 'contact_edit_options'
252 );
253
254 foreach ($this->_editOptions as $blockName => $value) {
255 $varName = '_show' . $blockName;
256 $this->$varName = $value;
257 $this->assign(substr($varName, 1), $this->$varName);
258 }
259
260 // get contact name of shared contact names
261 $sharedAddresses = [];
262 $shareAddressContactNames = CRM_Contact_BAO_Contact_Utils::getAddressShareContactNames($defaults['address']);
263 foreach ($defaults['address'] as $key => $addressValue) {
264 if (!empty($addressValue['master_id']) &&
265 !$shareAddressContactNames[$addressValue['master_id']]['is_deleted']
266 ) {
267 $sharedAddresses[$key]['shared_address_display'] = [
268 'address' => $addressValue['display'],
269 'name' => $shareAddressContactNames[$addressValue['master_id']]['name'],
270 ];
271 }
272 }
273 $this->assign('sharedAddresses', $sharedAddresses);
274
275 $this->assign($defaults);
276
277 // FIXME: when we sort out TZ isssues with DATETIME/TIMESTAMP, we can skip next query
278 // also assign the last modifed details
279 $lastModified = CRM_Core_BAO_Log::lastModified($this->_contactId, 'civicrm_contact');
280 $this->assign_by_ref('lastModified', $lastModified);
281
282 $this->_viewOptions = CRM_Core_BAO_Setting::valueOptions(
283 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
284 'contact_view_options',
285 TRUE
286 );
287
288 $changeLog = $this->_viewOptions['log'];
289 $this->assign_by_ref('changeLog', $changeLog);
290
291 $this->assign('allTabs', $this->getTabs());
292
293 // hook for contact summary
294 // ignored but needed to prevent warnings
295 $contentPlacement = CRM_Utils_Hook::SUMMARY_BELOW;
296 CRM_Utils_Hook::summary($this->_contactId, $content, $contentPlacement);
297 if ($content) {
298 $this->assign_by_ref('hookContent', $content);
299 $this->assign('hookContentPlacement', $contentPlacement);
300 }
301 }
302
303 /**
304 * @return string
305 */
306 public function getTemplateFileName() {
307 if ($this->_contactId) {
308 $contactSubtypes = $this->get('contactSubtype') ? explode(CRM_Core_DAO::VALUE_SEPARATOR, $this->get('contactSubtype')) : [];
309
310 // there could be multiple subtypes. We check templates for each of the subtype, and return the first one found.
311 foreach ($contactSubtypes as $csType) {
312 if ($csType) {
313 $templateFile = "CRM/Contact/Page/View/SubType/{$csType}.tpl";
314 $template = CRM_Core_Page::getTemplate();
315 if ($template->template_exists($templateFile)) {
316 return $templateFile;
317 }
318 }
319 }
320 }
321 return parent::getTemplateFileName();
322 }
323
324 /**
325 * @return array
326 */
327 public static function basicTabs() {
328 return [
329 [
330 'id' => 'summary',
331 'template' => 'CRM/Contact/Page/View/Summary-tab.tpl',
332 'title' => ts('Summary'),
333 'weight' => 0,
334 'icon' => 'crm-i fa-address-card-o',
335 ],
336 [
337 'id' => 'activity',
338 'title' => ts('Activities'),
339 'class' => 'livePage',
340 'weight' => 70,
341 'icon' => 'crm-i fa-tasks',
342 ],
343 [
344 'id' => 'rel',
345 'title' => ts('Relationships'),
346 'class' => 'livePage',
347 'weight' => 80,
348 'icon' => 'crm-i fa-handshake-o',
349 ],
350 [
351 'id' => 'group',
352 'title' => ts('Groups'),
353 'class' => 'ajaxForm',
354 'weight' => 90,
355 'icon' => 'crm-i fa-users',
356 ],
357 [
358 'id' => 'note',
359 'title' => ts('Notes'),
360 'class' => 'livePage',
361 'weight' => 100,
362 'icon' => 'crm-i fa-sticky-note-o',
363 ],
364 [
365 'id' => 'tag',
366 'title' => ts('Tags'),
367 'weight' => 110,
368 'icon' => 'crm-i fa-tags',
369 ],
370 [
371 'id' => 'log',
372 'title' => ts('Change Log'),
373 'weight' => 120,
374 'icon' => 'crm-i fa-history',
375 ],
376 ];
377 }
378
379 /**
380 * @return array
381 * @throws \CRM_Core_Exception
382 */
383 public function getTabs() {
384 $allTabs = [];
385 $getCountParams = [];
386 $weight = 10;
387
388 foreach (CRM_Core_Component::getEnabledComponents() as $name => $component) {
389 if (!empty($this->_viewOptions[$name]) &&
390 CRM_Core_Permission::access($component->name)
391 ) {
392 $elem = $component->registerTab();
393
394 // FIXME: not very elegant, probably needs better approach
395 // allow explicit id, if not defined, use keyword instead
396 $i = $elem['id'] ?? $component->getKeyword();
397 $u = $elem['url'];
398
399 //appending isTest to url for test soft credit CRM-3891.
400 //FIXME: hack ajax url.
401 $q = "reset=1&force=1&cid={$this->_contactId}";
402 if (CRM_Utils_Request::retrieve('isTest', 'Positive', $this)) {
403 $q .= "&isTest=1";
404 }
405 $allTabs[] = [
406 'id' => $i,
407 'url' => CRM_Utils_System::url("civicrm/contact/view/$u", $q),
408 'title' => $elem['title'],
409 'weight' => $elem['weight'],
410 'count' => NULL,
411 'class' => 'livePage',
412 'icon' => $component->getIcon(),
413 ];
414 $getCountParams[$i] = [$u, $this->_contactId];
415 }
416 }
417
418 // show the tabs only if user has generic access to CiviCRM
419 $accessCiviCRM = CRM_Core_Permission::check('access CiviCRM');
420 foreach (self::basicTabs() as $tab) {
421 if ($tab['id'] == 'summary') {
422 $allTabs[] = $tab;
423 }
424 elseif ($accessCiviCRM && !empty($this->_viewOptions[$tab['id']])) {
425 $allTabs[] = $tab + [
426 'url' => CRM_Utils_System::url("civicrm/contact/view/{$tab['id']}", "reset=1&cid={$this->_contactId}"),
427 'count' => NULL,
428 ];
429 $getCountParams[$tab['id']] = [$tab['id'], $this->_contactId];
430 $weight = $tab['weight'] + 10;
431 }
432 }
433
434 // now add all the custom tabs
435 $entityType = $this->get('contactType');
436 $activeGroups = CRM_Core_BAO_CustomGroup::getActiveGroups(
437 $entityType,
438 'civicrm/contact/view/cd',
439 $this->_contactId
440 );
441
442 foreach ($activeGroups as $group) {
443 $id = "custom_{$group['id']}";
444 $allTabs[] = [
445 'id' => $id,
446 'url' => CRM_Utils_System::url($group['path'], $group['query'] . "&selectedChild=$id"),
447 'title' => $group['title'],
448 'weight' => $weight,
449 'count' => NULL,
450 'hideCount' => !$group['is_multiple'],
451 'class' => 'livePage',
452 'icon' => 'crm-i ' . ($group['icon'] ?: 'fa-gear'),
453 ];
454 $getCountParams[$id] = [$id, $this->_contactId, $group['table_name']];
455 $weight += 10;
456 }
457
458 // Allow other modules to add or remove tabs
459 $context = ['contact_id' => $this->_contactId];
460 CRM_Utils_Hook::tabset('civicrm/contact/view', $allTabs, $context);
461
462 $expectedKeys = ['count', 'class', 'template', 'hideCount', 'icon'];
463
464 foreach ($allTabs as &$tab) {
465 // Ensure tab has all expected keys
466 $tab += array_fill_keys($expectedKeys, NULL);
467 // Get tab counts last to avoid wasting time; if a tab was removed by hook, the count isn't needed.
468 if (!isset($tab['count']) && isset($getCountParams[$tab['id']])) {
469 $tab['count'] = call_user_func_array([
470 'CRM_Contact_BAO_Contact',
471 'getCountComponent',
472 ], $getCountParams[$tab['id']]);
473 }
474 }
475
476 // now sort the tabs based on weight
477 usort($allTabs, ['CRM_Utils_Sort', 'cmpFunc']);
478 return $allTabs;
479 }
480
481 }