Cleanup deprecated CRM_Core_BAO_Settings calls CRM-17507
[civicrm-core.git] / CRM / Contact / Page / View / Summary.php
CommitLineData
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 */
33
34/**
35 * Main page for viewing contact.
6a488035
TO
36 */
37class CRM_Contact_Page_View_Summary extends CRM_Contact_Page_View {
38
39 /**
95cdcc0f 40 * Heart of the viewing process.
6a488035 41 *
95cdcc0f 42 * The runner gets all the meta data for the contact and calls the appropriate type of page to view.
6a488035 43 */
00be9182 44 public function preProcess() {
6a488035
TO
45 parent::preProcess();
46
47 // actions buttom contextMenu
48 $menuItems = CRM_Contact_BAO_Contact::contextMenu($this->_contactId);
49
50 $this->assign('actionsMenuList', $menuItems);
51
52 //retrieve inline custom data
53 $entityType = $this->get('contactType');
54 if ($entitySubType = $this->get('contactSubtype')) {
55 $entitySubType = explode(CRM_Core_DAO::VALUE_SEPARATOR,
56 trim($entitySubType, CRM_Core_DAO::VALUE_SEPARATOR)
57 );
58 }
59 $groupTree = &CRM_Core_BAO_CustomGroup::getTree($entityType,
60 $this,
61 $this->_contactId,
62 NULL,
63 $entitySubType
64 );
65
66 CRM_Core_BAO_CustomGroup::buildCustomDataView($this,
67 $groupTree
68 );
69
70 // also create the form element for the activity links box
71 $controller = new CRM_Core_Controller_Simple(
72 'CRM_Activity_Form_ActivityLinks',
73 ts('Activity Links'),
74 NULL,
75 FALSE,
76 FALSE,
77 TRUE
78 );
79 $controller->setEmbedded(TRUE);
80 $controller->run();
81 }
82
83 /**
95cdcc0f 84 * Heart of the viewing process.
6a488035 85 *
95cdcc0f 86 * The runner gets all the meta data for the contact and calls the appropriate type of page to view.
6a488035 87 */
00be9182 88 public function run() {
6a488035
TO
89 $this->preProcess();
90
91 if ($this->_action & CRM_Core_Action::UPDATE) {
92 $this->edit();
93 }
94 else {
95 $this->view();
96 }
97
98 return parent::run();
99 }
100
101 /**
fe482240 102 * Edit name and address of a contact.
6a488035 103 */
00be9182 104 public function edit() {
6a488035
TO
105 // set the userContext stack
106 $session = CRM_Core_Session::singleton();
107 $url = CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $this->_contactId);
108 $session->pushUserContext($url);
109
110 $controller = new CRM_Core_Controller_Simple('CRM_Contact_Form_Contact', ts('Contact Page'), CRM_Core_Action::UPDATE);
111 $controller->setEmbedded(TRUE);
112 $controller->process();
113 return $controller->run();
114 }
115
116 /**
fe482240 117 * View summary details of a contact.
6a488035 118 */
00be9182 119 public function view() {
23223213 120 // Add js for tabs, in-place editing, and jstree for tags
6a488035 121 CRM_Core_Resources::singleton()
96ed17aa 122 ->addScriptFile('civicrm', 'templates/CRM/Contact/Page/View/Summary.js', 2, 'html-header')
0e44568b 123 ->addStyleFile('civicrm', 'css/contactSummary.css', 2, 'html-header')
6a488035 124 ->addScriptFile('civicrm', 'packages/jquery/plugins/jstree/jquery.jstree.js', 0, 'html-header', FALSE)
7d041da3 125 ->addStyleFile('civicrm', 'packages/jquery/plugins/jstree/themes/default/style.css', 0, 'html-header')
96ed17aa 126 ->addScriptFile('civicrm', 'templates/CRM/common/TabHeader.js', 1, 'html-header')
58b65bf6
CW
127 ->addSetting(array(
128 'summaryPrint' => array('mode' => $this->_print),
129 'tabSettings' => array('active' => CRM_Utils_Request::retrieve('selectedChild', 'String', $this, FALSE, 'summary')),
130 ));
46d2738c 131 $this->assign('summaryPrint', $this->_print);
6a488035
TO
132 $session = CRM_Core_Session::singleton();
133 $url = CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $this->_contactId);
134 $session->pushUserContext($url);
135
353ffa53 136 $params = array();
6a488035 137 $defaults = array();
353ffa53 138 $ids = array();
6a488035
TO
139
140 $params['id'] = $params['contact_id'] = $this->_contactId;
141 $params['noRelationships'] = $params['noNotes'] = $params['noGroups'] = TRUE;
142 $contact = CRM_Contact_BAO_Contact::retrieve($params, $defaults, TRUE);
c3f51c13 143 // Let summary page know if outbound mail is disabled so email links can be built conditionally
aaffa79f 144 $mailingBackend = Civi::settings()->get('mailing_backend');
c3f51c13 145 $this->assign('mailingOutboundOption', $mailingBackend['outBound_option']);
6a488035
TO
146
147 $communicationType = array(
148 'phone' => array(
149 'type' => 'phoneType',
150 'id' => 'phone_type',
365cc343
AS
151 'daoName' => 'CRM_Core_DAO_Phone',
152 'fieldName' => 'phone_type_id',
6a488035
TO
153 ),
154 'im' => array(
155 'type' => 'IMProvider',
156 'id' => 'provider',
365cc343
AS
157 'daoName' => 'CRM_Core_DAO_IM',
158 'fieldName' => 'provider_id',
6a488035
TO
159 ),
160 'website' => array(
161 'type' => 'websiteType',
162 'id' => 'website_type',
365cc343
AS
163 'daoName' => 'CRM_Core_DAO_Website',
164 'fieldName' => 'website_type_id',
6a488035
TO
165 ),
166 'address' => array('skip' => TRUE, 'customData' => 1),
167 'email' => array('skip' => TRUE),
168 'openid' => array('skip' => TRUE),
169 );
170
171 foreach ($communicationType as $key => $value) {
a7488080 172 if (!empty($defaults[$key])) {
6a488035 173 foreach ($defaults[$key] as & $val) {
8f785c9e 174 CRM_Utils_Array::lookupValue($val, 'location_type', CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', array('labelColumn' => 'display_name')), FALSE);
a7488080 175 if (empty($value['skip'])) {
365cc343 176 $daoName = $value['daoName'];
a810c9da 177 $pseudoConst = $daoName::buildOptions($value['fieldName'], 'get');
6a488035
TO
178 CRM_Utils_Array::lookupValue($val, $value['id'], $pseudoConst, FALSE);
179 }
180 }
181 if (isset($value['customData'])) {
182 foreach ($defaults[$key] as $blockId => $blockVal) {
183 $idValue = $blockVal['id'];
481a74f4 184 if ($key == 'address') {
a7488080 185 if (!empty($blockVal['master_id'])) {
6a488035
TO
186 $idValue = $blockVal['master_id'];
187 }
188 }
189 $groupTree = CRM_Core_BAO_CustomGroup::getTree(ucfirst($key),
190 $this,
191 $idValue
192 );
193 // we setting the prefix to dnc_ below so that we don't overwrite smarty's grouptree var.
194 $defaults[$key][$blockId]['custom'] = CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree, FALSE, NULL, "dnc_");
195 }
196 // reset template variable since that won't be of any use, and could be misleading
197 $this->assign("dnc_viewCustomData", NULL);
198 }
199 }
200 }
201
a7488080 202 if (!empty($defaults['gender_id'])) {
41b47b8a 203 $defaults['gender_display'] = CRM_Core_PseudoConstant::getLabel('CRM_Contact_DAO_Contact', 'gender_id', $defaults['gender_id']);
6a488035
TO
204 }
205
aa62b355
OB
206 $communicationStyle = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'communication_style_id');
207 if (!empty($communicationStyle)) {
a7488080 208 if (!empty($defaults['communication_style_id'])) {
aa62b355
OB
209 $defaults['communication_style_display'] = $communicationStyle[CRM_Utils_Array::value('communication_style_id', $defaults)];
210 }
211 else {
212 // Make sure the field is displayed as long as it is active, even if it is unset for this contact.
213 $defaults['communication_style_display'] = '';
214 }
215 }
216
6a488035
TO
217 // to make contact type label available in the template -
218 $contactType = array_key_exists('contact_sub_type', $defaults) ? $defaults['contact_sub_type'] : $defaults['contact_type'];
219 $defaults['contact_type_label'] = CRM_Contact_BAO_ContactType::contactTypePairs(TRUE, $contactType, ', ');
220
221 // get contact tags
222 $contactTags = CRM_Core_BAO_EntityTag::getContactTags($this->_contactId);
223
224 if (!empty($contactTags)) {
225 $defaults['contactTag'] = implode(', ', $contactTags);
226 }
227
228 $defaults['privacy_values'] = CRM_Core_SelectValues::privacy();
229
230 //Show blocks only if they are visible in edit form
63cd4fcf
DL
231 $this->_editOptions = CRM_Core_BAO_Setting::valueOptions(
232 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
6a488035
TO
233 'contact_edit_options'
234 );
235
236 foreach ($this->_editOptions as $blockName => $value) {
237 $varName = '_show' . $blockName;
238 $this->$varName = $value;
239 $this->assign(substr($varName, 1), $this->$varName);
240 }
241
242 // get contact name of shared contact names
243 $sharedAddresses = array();
244 $shareAddressContactNames = CRM_Contact_BAO_Contact_Utils::getAddressShareContactNames($defaults['address']);
245 foreach ($defaults['address'] as $key => $addressValue) {
a7488080 246 if (!empty($addressValue['master_id']) &&
6a488035
TO
247 !$shareAddressContactNames[$addressValue['master_id']]['is_deleted']
248 ) {
249 $sharedAddresses[$key]['shared_address_display'] = array(
250 'address' => $addressValue['display'],
251 'name' => $shareAddressContactNames[$addressValue['master_id']]['name'],
252 );
253 }
254 }
255 $this->assign('sharedAddresses', $sharedAddresses);
256
257 //get the current employer name
258 if (CRM_Utils_Array::value('contact_type', $defaults) == 'Individual') {
259 if ($contact->employer_id && $contact->organization_name) {
260 $defaults['current_employer'] = $contact->organization_name;
261 $defaults['current_employer_id'] = $contact->employer_id;
262 }
263
264 //for birthdate format with respect to birth format set
265 $this->assign('birthDateViewFormat', CRM_Utils_Array::value('qfMapping', CRM_Utils_Date::checkBirthDateFormat()));
266 }
59f97da6 267
46adf42c 268 $defaults['external_identifier'] = $contact->external_identifier;
6a488035
TO
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 $allTabs = array();
277 $weight = 10;
278
63cd4fcf
DL
279 $this->_viewOptions = CRM_Core_BAO_Setting::valueOptions(
280 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
281 'contact_view_options',
282 TRUE
6a488035 283 );
63cd4fcf
DL
284
285 // show the tabs only if user has generic access to CiviCRM
286 $accessCiviCRM = CRM_Core_Permission::check('access CiviCRM');
287
6a488035
TO
288 $changeLog = $this->_viewOptions['log'];
289 $this->assign_by_ref('changeLog', $changeLog);
290 $components = CRM_Core_Component::getEnabledComponents();
291
292 foreach ($components as $name => $component) {
a7488080 293 if (!empty($this->_viewOptions[$name]) &&
63cd4fcf 294 CRM_Core_Permission::access($component->name)
6a488035
TO
295 ) {
296 $elem = $component->registerTab();
297
298 // FIXME: not very elegant, probably needs better approach
299 // allow explicit id, if not defined, use keyword instead
300 if (array_key_exists('id', $elem)) {
301 $i = $elem['id'];
302 }
303 else {
304 $i = $component->getKeyword();
305 }
306 $u = $elem['url'];
307
308 //appending isTest to url for test soft credit CRM-3891.
309 //FIXME: hack ajax url.
58b65bf6 310 $q = "reset=1&force=1&cid={$this->_contactId}";
6a488035 311 if (CRM_Utils_Request::retrieve('isTest', 'Positive', $this)) {
58b65bf6 312 $q .= "&isTest=1";
6a488035
TO
313 }
314 $allTabs[] = array(
315 'id' => $i,
316 'url' => CRM_Utils_System::url("civicrm/contact/view/$u", $q),
317 'title' => $elem['title'],
318 'weight' => $elem['weight'],
319 'count' => CRM_Contact_BAO_Contact::getCountComponent($u, $this->_contactId),
6b754ecb 320 'class' => 'livePage',
6a488035
TO
321 );
322 // make sure to get maximum weight, rest of tabs go after
323 // FIXME: not very elegant again
324 if ($weight < $elem['weight']) {
325 $weight = $elem['weight'];
326 }
327 }
328 }
329
2ede60ec 330 $rest = array(
d6539f93
CW
331 'activity' => array(
332 'title' => ts('Activities'),
23223213 333 'class' => 'livePage',
d6539f93
CW
334 ),
335 'rel' => array(
336 'title' => ts('Relationships'),
23223213 337 'class' => 'livePage',
d6539f93
CW
338 ),
339 'group' => array(
340 'title' => ts('Groups'),
341 'class' => 'ajaxForm',
342 ),
343 'note' => array(
344 'title' => ts('Notes'),
4e8065a9 345 'class' => 'livePage',
d6539f93
CW
346 ),
347 'tag' => array(
348 'title' => ts('Tags'),
349 ),
350 'log' => array(
351 'title' => ts('Change Log'),
352 ),
6a488035
TO
353 );
354
355 foreach ($rest as $k => $v) {
8cc574cf 356 if ($accessCiviCRM && !empty($this->_viewOptions[$k])) {
d6539f93 357 $allTabs[] = $v + array(
bed98343 358 'id' => $k,
359 'url' => CRM_Utils_System::url(
360 "civicrm/contact/view/$k",
361 "reset=1&cid={$this->_contactId}"
362 ),
363 'weight' => $weight,
364 'count' => CRM_Contact_BAO_Contact::getCountComponent($k, $this->_contactId),
365 );
6a488035
TO
366 $weight += 10;
367 }
368 }
369
370 // now add all the custom tabs
371 $entityType = $this->get('contactType');
2ede60ec
DL
372 $activeGroups = CRM_Core_BAO_CustomGroup::getActiveGroups(
373 $entityType,
6a488035
TO
374 'civicrm/contact/view/cd',
375 $this->_contactId
376 );
377
378 foreach ($activeGroups as $group) {
379 $id = "custom_{$group['id']}";
380 $allTabs[] = array(
381 'id' => $id,
58b65bf6 382 'url' => CRM_Utils_System::url($group['path'], $group['query'] . "&selectedChild=$id"),
6a488035
TO
383 'title' => $group['title'],
384 'weight' => $weight,
385 'count' => CRM_Contact_BAO_Contact::getCountComponent($id, $this->_contactId, $group['table_name']),
a97040f5 386 'hideCount' => !$group['is_multiple'],
21dfd5f5 387 'class' => 'livePage',
6a488035
TO
388 );
389 $weight += 10;
390 }
391
392 // see if any other modules want to add any tabs
393 CRM_Utils_Hook::tabs($allTabs, $this->_contactId);
394
395 // now sort the tabs based on weight
396 usort($allTabs, array('CRM_Utils_Sort', 'cmpFunc'));
397
398 $this->assign('allTabs', $allTabs);
399
6a488035
TO
400 // hook for contact summary
401 // ignored but needed to prevent warnings
402 $contentPlacement = CRM_Utils_Hook::SUMMARY_BELOW;
403 CRM_Utils_Hook::summary($this->_contactId, $content, $contentPlacement);
404 if ($content) {
405 $this->assign_by_ref('hookContent', $content);
406 $this->assign('hookContentPlacement', $contentPlacement);
407 }
408 }
409
4319322b
EM
410 /**
411 * @return string
412 */
00be9182 413 public function getTemplateFileName() {
6a488035 414 if ($this->_contactId) {
ce80b209 415 $contactSubtypes = $this->get('contactSubtype') ? explode(CRM_Core_DAO::VALUE_SEPARATOR, $this->get('contactSubtype')) : array();
ae34b946
DS
416
417 // there could be multiple subtypes. We check templates for each of the subtype, and return the first one found.
418 foreach ($contactSubtypes as $csType) {
419 if ($csType) {
420 $templateFile = "CRM/Contact/Page/View/SubType/{$csType}.tpl";
421 $template = CRM_Core_Page::getTemplate();
422 if ($template->template_exists($templateFile)) {
423 return $templateFile;
424 }
6a488035
TO
425 }
426 }
427 }
428 return parent::getTemplateFileName();
429 }
96025800 430
6a488035 431}