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