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