Merge pull request #20909 from eileenmcnaughton/index
[civicrm-core.git] / CRM / Contact / Page / View / CustomData.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 * Page for displaying custom data.
20 */
21 class CRM_Contact_Page_View_CustomData extends CRM_Core_Page {
22
23 /**
24 * Custom group id.
25 *
26 * @var int
27 */
28 public $_groupId;
29
30 /**
31 * Class constructor.
32 *
33 * @return CRM_Contact_Page_View_CustomData
34 */
35 public function __construct() {
36 parent::__construct();
37 }
38
39 /**
40 * Add a few specific things to view contact.
41 */
42 public function preProcess() {
43 $this->_groupId = CRM_Utils_Request::retrieve('gid', 'Positive', $this, TRUE);
44 $this->assign('groupId', $this->_groupId);
45
46 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
47 $this->_recId = CRM_Utils_Request::retrieve('recId', 'Positive', $this);
48
49 // If no cid supplied, look it up
50 if (!$this->_contactId && $this->_recId) {
51 $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_groupId, 'table_name');
52 if ($tableName) {
53 $this->_contactId = CRM_Core_DAO::singleValueQuery("SELECT entity_id FROM `$tableName` WHERE id = %1", [1 => [$this->_recId, 'Integer']]);
54 }
55 }
56 if (!$this->_contactId) {
57 throw new CRM_Core_Exception(ts('Could not find valid value for %1', [1 => 'cid']));
58 }
59
60 $this->assign('contactId', $this->_contactId);
61
62 // check logged in url permission
63 CRM_Contact_Page_View::checkUserPermission($this);
64
65 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
66 $this->assign('action', $this->_action);
67
68 $this->_multiRecordDisplay = CRM_Utils_Request::retrieve('multiRecordDisplay', 'String', $this, FALSE);
69 $this->_cgcount = CRM_Utils_Request::retrieve('cgcount', 'Positive', $this, FALSE);
70 }
71
72 /**
73 * Run the page.
74 *
75 * This method is called after the page is created. It checks for the
76 * type of action and executes that action.
77 */
78 public function run() {
79 $this->preProcess();
80
81 //set the userContext stack
82 $doneURL = 'civicrm/contact/view';
83 $session = CRM_Core_Session::singleton();
84 $session->pushUserContext(CRM_Utils_System::url($doneURL, 'action=browse&selectedChild=custom_' . $this->_groupId), FALSE);
85
86 // Check permission to edit this contact
87 $editPermission = CRM_Contact_BAO_Contact_Permission::allow($this->_contactId, CRM_Core_Permission::EDIT);
88 $this->assign('editPermission', $editPermission);
89
90 if ($this->_action == CRM_Core_Action::BROWSE) {
91
92 $displayStyle = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup',
93 $this->_groupId,
94 'style'
95 );
96
97 if ($this->_multiRecordDisplay != 'single') {
98 $id = "custom_{$this->_groupId}";
99 $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_groupId, 'table_name');
100 $this->ajaxResponse['tabCount'] = CRM_Contact_BAO_Contact::getCountComponent($id, $this->_contactId, $tableName);
101 }
102
103 if ($displayStyle === 'Tab with table' && $this->_multiRecordDisplay != 'single') {
104 $ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
105 $this->_contactId,
106 'contact_type'
107 );
108
109 $this->assign('displayStyle', 'tableOriented');
110 // here the multi custom data listing code will go
111 $multiRecordFieldListing = TRUE;
112 $page = new CRM_Profile_Page_MultipleRecordFieldsListing();
113 $page->set('contactId', $this->_contactId);
114 $page->set('customGroupId', $this->_groupId);
115 $page->set('action', CRM_Core_Action::BROWSE);
116 $page->set('multiRecordFieldListing', $multiRecordFieldListing);
117 $page->set('pageViewType', 'customDataView');
118 $page->set('contactType', $ctype);
119 $page->_headersOnly = TRUE;
120 $page->run();
121 }
122 else {
123 //Custom Groups Inline
124 $entityType = CRM_Contact_BAO_Contact::getContactType($this->_contactId);
125 $entitySubType = CRM_Contact_BAO_Contact::getContactSubType($this->_contactId);
126 $recId = NULL;
127 if ($this->_multiRecordDisplay == 'single') {
128 $groupTitle = CRM_Core_BAO_CustomGroup::getTitle($this->_groupId);
129 CRM_Utils_System::setTitle(ts('View %1 Record', [1 => $groupTitle]));
130 $groupTree = CRM_Core_BAO_CustomGroup::getTree($entityType, NULL, $this->_contactId,
131 $this->_groupId, $entitySubType, NULL, TRUE, NULL, FALSE, CRM_Core_Permission::VIEW, $this->_cgcount
132 );
133
134 $recId = $this->_recId;
135 $this->assign('multiRecordDisplay', $this->_multiRecordDisplay);
136 $this->assign('skipTitle', 1);
137 }
138 else {
139 $groupTree = CRM_Core_BAO_CustomGroup::getTree($entityType, NULL, $this->_contactId,
140 $this->_groupId, $entitySubType, NULL, TRUE, NULL, FALSE, CRM_Core_Permission::VIEW
141 );
142 }
143 CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree, FALSE, NULL, NULL, $recId, $this->_contactId, TRUE);
144 }
145 }
146 else {
147
148 $controller = new CRM_Core_Controller_Simple('CRM_Contact_Form_CustomData',
149 ts('Custom Data'),
150 $this->_action
151 );
152 $controller->setEmbedded(TRUE);
153
154 $controller->set('tableId', $this->_contactId);
155 $controller->set('groupId', $this->_groupId);
156 $controller->set('entityType', CRM_Contact_BAO_Contact::getContactType($this->_contactId));
157 $controller->set('entitySubType', CRM_Contact_BAO_Contact::getContactSubType($this->_contactId, ','));
158 $controller->process();
159 $controller->run();
160 }
161 return parent::run();
162 }
163
164 }