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