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