Merge pull request #4871 from eileenmcnaughton/CRM-15795
[civicrm-core.git] / CRM / Contact / Page / View / CustomData.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 * Page for displaying custom data
38 *
39 */
40 class CRM_Contact_Page_View_CustomData extends CRM_Core_Page {
41
42 /**
43 * The id of the object being viewed (note/relationship etc)
44 *
45 * @int
46 */
47 public $_groupId;
48
49 /**
50 * Class constructor
51 *
52 * @return CRM_Contact_Page_View_CustomData
53 */
54 public function __construct() {
55 parent::__construct();
56 }
57
58 /**
59 * Add a few specific things to view contact
60 *
61 * @return void
62 *
63 */
64 public function preProcess() {
65 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
66 $this->assign('contactId', $this->_contactId);
67
68 // check logged in url permission
69 CRM_Contact_Page_View::checkUserPermission($this);
70
71 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
72 $this->assign('action', $this->_action);
73
74 $this->_groupId = CRM_Utils_Request::retrieve('gid', 'Positive', $this, TRUE);
75 $this->assign('groupId', $this->_groupId);
76
77 $this->_multiRecordDisplay = CRM_Utils_Request::retrieve('multiRecordDisplay', 'String', $this, FALSE);
78 $this->_cgcount = CRM_Utils_Request::retrieve('cgcount', 'Positive', $this, FALSE);
79 $this->_recId = CRM_Utils_Request::retrieve('recId', 'Positive', $this, FALSE);
80 }
81
82 /**
83 * Run the page.
84 *
85 * This method is called after the page is created. It checks for the
86 * type of action and executes that action.
87 *
88 *
89 * @return void
90 * @static
91 */
92 public function run() {
93 $this->preProcess();
94
95 //set the userContext stack
96 $doneURL = 'civicrm/contact/view';
97 $session = CRM_Core_Session::singleton();
98 $session->pushUserContext(CRM_Utils_System::url($doneURL, 'action=browse&selectedChild=custom_' . $this->_groupId), FALSE);
99
100 // get permission detail view or edit
101 // use a comtact id specific function which gives us much better granularity
102 // CRM-12646
103 $editCustomData = CRM_Contact_BAO_Contact_Permission::allow($this->_contactId, CRM_Core_Permission::EDIT);
104 $this->assign('editCustomData', $editCustomData);
105
106 //allow to edit own customdata CRM-5518
107 $editOwnCustomData = FALSE;
108 if ($session->get('userID') == $this->_contactId) {
109 $editOwnCustomData = TRUE;
110 }
111 $this->assign('editOwnCustomData', $editOwnCustomData);
112
113 if ($this->_action == CRM_Core_Action::BROWSE) {
114 //Custom Groups Inline
115 $entityType = CRM_Contact_BAO_Contact::getContactType($this->_contactId);
116 $entitySubType = CRM_Contact_BAO_Contact::getContactSubType($this->_contactId);
117 $groupTree = &CRM_Core_BAO_CustomGroup::getTree($entityType, $this, $this->_contactId,
118 $this->_groupId, $entitySubType
119 );
120
121 $displayStyle = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup',
122 $this->_groupId,
123 'style'
124 );
125
126 if ($this->_multiRecordDisplay != 'single') {
127 $id = "custom_{$this->_groupId}";
128 $this->ajaxResponse['tabCount'] = CRM_Contact_BAO_Contact::getCountComponent($id, $this->_contactId, $groupTree[$this->_groupId]['table_name']);
129 }
130
131 if ($displayStyle === 'Tab with table' && $this->_multiRecordDisplay != 'single') {
132 $ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
133 $this->_contactId,
134 'contact_type'
135 );
136
137 $this->assign('displayStyle', 'tableOriented');
138 // here the multi custom data listing code will go
139 $multiRecordFieldListing = TRUE;
140 $page = new CRM_Profile_Page_MultipleRecordFieldsListing();
141 $page->set('contactId', $this->_contactId);
142 $page->set('customGroupId', $this->_groupId);
143 $page->set('action', CRM_Core_Action::BROWSE);
144 $page->set('multiRecordFieldListing', $multiRecordFieldListing);
145 $page->set('pageViewType', 'customDataView');
146 $page->set('contactType', $ctype);
147 $page->run();
148 }
149 else {
150 $recId = NULL;
151 if ($this->_multiRecordDisplay == 'single') {
152 $groupTitle = CRM_Core_BAO_CustomGroup::getTitle($this->_groupId);
153 CRM_Utils_System::setTitle(ts('View %1 Record', array(1 => $groupTitle)));
154
155 $recId = $this->_recId;
156 $this->assign('multiRecordDisplay', $this->_multiRecordDisplay);
157 $this->assign('skipTitle', 1);
158 }
159 CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree, FALSE, NULL, NULL, $recId);
160 }
161 }
162 else {
163
164 $controller = new CRM_Core_Controller_Simple('CRM_Contact_Form_CustomData',
165 ts('Custom Data'),
166 $this->_action
167 );
168 $controller->setEmbedded(TRUE);
169
170 $controller->set('tableId', $this->_contactId);
171 $controller->set('groupId', $this->_groupId);
172 $controller->set('entityType', CRM_Contact_BAO_Contact::getContactType($this->_contactId));
173 $controller->set('entitySubType', CRM_Contact_BAO_Contact::getContactSubType($this->_contactId, ','));
174 $controller->process();
175 $controller->run();
176 }
177 return parent::run();
178 }
179 }