Merge pull request #23173 from braders/escape-on-output-event-links
[civicrm-core.git] / CRM / Contact / Page / View / UserDashBoard.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 * This class is used to build User Dashboard
20 */
21 class CRM_Contact_Page_View_UserDashBoard extends CRM_Core_Page {
22 public $_contactId = NULL;
23
24 /**
25 * Always show public groups.
26 * @var bool
27 */
28 public $_onlyPublicGroups = TRUE;
29
30 public $_edit = TRUE;
31
32 /**
33 * The action links that we need to display for the browse screen.
34 *
35 * @var array
36 */
37 public static $_links = NULL;
38
39 /**
40 * @throws Exception
41 */
42 public function __construct() {
43 parent::__construct();
44
45 if (!CRM_Core_Permission::check('access Contact Dashboard')) {
46 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/dashboard', 'reset=1'));
47 }
48
49 $this->_contactId = CRM_Utils_Request::retrieve('id', 'Positive', $this);
50 $userID = CRM_Core_Session::getLoggedInContactID();
51
52 $userChecksum = $this->getUserChecksum();
53 $validUser = FALSE;
54 if ($userChecksum) {
55 $this->assign('userChecksum', $userChecksum);
56 $validUser = CRM_Contact_BAO_Contact_Utils::validChecksum($this->_contactId, $userChecksum);
57 $this->_isChecksumUser = $validUser;
58 }
59
60 if (!$this->_contactId) {
61 $this->_contactId = $userID;
62 }
63 elseif ($this->_contactId != $userID && !$validUser) {
64 if (!CRM_Contact_BAO_Contact_Permission::allow($this->_contactId, CRM_Core_Permission::VIEW)) {
65 CRM_Core_Error::statusBounce(ts('You do not have permission to access this contact.'));
66 }
67 if (!CRM_Contact_BAO_Contact_Permission::allow($this->_contactId, CRM_Core_Permission::EDIT)) {
68 $this->_edit = FALSE;
69 }
70 }
71 }
72
73 /**
74 * Heart of the viewing process.
75 *
76 * The runner gets all the meta data for the contact and calls the appropriate type of page to view.
77 */
78 public function preProcess() {
79 if (!$this->_contactId) {
80 throw new CRM_Core_Exception(ts('You must be logged in to view this page.'));
81 }
82
83 list($displayName, $contactImage) = CRM_Contact_BAO_Contact::getDisplayAndImage($this->_contactId);
84
85 $this->set('displayName', $displayName);
86 $this->set('contactImage', $contactImage);
87
88 CRM_Utils_System::setTitle(ts('Dashboard - %1', [1 => $displayName]));
89
90 $this->assign('recentlyViewed', FALSE);
91 }
92
93 /**
94 * Build user dashboard.
95 */
96 public function buildUserDashBoard() {
97 //build component selectors
98 $dashboardElements = [];
99
100 $dashboardOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
101 'user_dashboard_options'
102 );
103
104 $components = CRM_Core_Component::getEnabledComponents();
105 $this->assign('contactId', $this->_contactId);
106 foreach ($components as $name => $component) {
107 $elem = $component->getUserDashboardElement();
108 if (!$elem) {
109 continue;
110 }
111
112 if (!empty($dashboardOptions[$name]) &&
113 (CRM_Core_Permission::access($component->name) ||
114 CRM_Core_Permission::check($elem['perm'][0])
115 )
116 ) {
117
118 $userDashboard = $component->getUserDashboardObject();
119 $dashboardElements[] = [
120 'class' => 'crm-dashboard-' . strtolower($component->name),
121 'sectionTitle' => $elem['title'],
122 'templatePath' => $userDashboard->getTemplateFileName(),
123 'weight' => $elem['weight'],
124 ];
125 $userDashboard->run();
126 }
127 }
128
129 // CRM-16512 - Hide related contact table if user lacks permission to view self
130 if (!empty($dashboardOptions['Permissioned Orgs']) && CRM_Core_Permission::check('view my contact')) {
131 $columnHeaders = CRM_Contact_BAO_Relationship::getColumnHeaders();
132 $contactRelationships = $selector = NULL;
133 CRM_Utils_Hook::searchColumns('relationship.columns', $columnHeaders, $contactRelationships, $selector);
134 $this->assign('columnHeaders', $columnHeaders);
135 $this->assign('entityInClassFormat', 'relationship');
136 $dashboardElements[] = [
137 'class' => 'crm-dashboard-permissionedOrgs',
138 'templatePath' => 'CRM/Contact/Page/View/RelationshipSelector.tpl',
139 'sectionTitle' => ts('Your Contacts / Organizations'),
140 'weight' => 40,
141 ];
142
143 }
144
145 if (!empty($dashboardOptions['PCP'])) {
146 $dashboardElements[] = [
147 'class' => 'crm-dashboard-pcp',
148 'templatePath' => 'CRM/Contribute/Page/PcpUserDashboard.tpl',
149 'sectionTitle' => ts('Personal Campaign Pages'),
150 'weight' => 40,
151 ];
152 list($pcpBlock, $pcpInfo) = CRM_PCP_BAO_PCP::getPcpDashboardInfo($this->_contactId);
153 $this->assign('pcpBlock', $pcpBlock);
154 $this->assign('pcpInfo', $pcpInfo);
155 }
156
157 if (!empty($dashboardOptions['Assigned Activities']) && empty($this->_isChecksumUser)) {
158 // Assigned Activities section
159 $dashboardElements[] = [
160 'class' => 'crm-dashboard-assignedActivities',
161 'templatePath' => 'CRM/Activity/Page/UserDashboard.tpl',
162 'sectionTitle' => ts('Your Assigned Activities'),
163 'weight' => 5,
164 ];
165 $userDashboard = new CRM_Activity_Page_UserDashboard();
166 $userDashboard->run();
167 }
168
169 usort($dashboardElements, ['CRM_Utils_Sort', 'cmpFunc']);
170 foreach ($dashboardElements as $index => $dashboardElement) {
171 // Ensure property is set to avoid smarty notices
172 if (!array_key_exists('class', $dashboardElement)) {
173 $dashboardElements[$index]['class'] = NULL;
174 }
175 }
176 $this->assign('dashboardElements', $dashboardElements);
177
178 if (!empty($dashboardOptions['Groups'])) {
179 $this->assign('showGroup', TRUE);
180 //build group selector
181 $gContact = new CRM_Contact_Page_View_UserDashBoard_GroupContact();
182 $gContact->run();
183 }
184 else {
185 $this->assign('showGroup', FALSE);
186 }
187 }
188
189 /**
190 * Perform actions and display for user dashboard.
191 */
192 public function run() {
193 $this->preProcess();
194 $this->buildUserDashBoard();
195 return parent::run();
196 }
197
198 /**
199 * Get action links.
200 *
201 * @return array
202 * (reference) of action links
203 */
204 public static function &links() {
205 if (!(self::$_links)) {
206 $disableExtra = ts('Are you sure you want to disable this relationship?');
207
208 self::$_links = [
209 CRM_Core_Action::UPDATE => [
210 'name' => ts('Edit Contact Information'),
211 'url' => 'civicrm/contact/relatedcontact',
212 'qs' => 'action=update&reset=1&cid=%%cbid%%&rcid=%%cid%%',
213 'title' => ts('Edit Contact Information'),
214 ],
215 CRM_Core_Action::VIEW => [
216 'name' => ts('Dashboard'),
217 'url' => 'civicrm/user',
218 'class' => 'no-popup',
219 'qs' => 'reset=1&id=%%cbid%%',
220 'title' => ts('View Contact Dashboard'),
221 ],
222 ];
223
224 if (CRM_Core_Permission::check('access CiviCRM')) {
225 self::$_links += [
226 CRM_Core_Action::DISABLE => [
227 'name' => ts('Disable'),
228 'url' => 'civicrm/contact/view/rel',
229 'qs' => 'action=disable&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%&selectedChild=rel&context=dashboard',
230 'extra' => 'onclick = "return confirm(\'' . $disableExtra . '\');"',
231 'title' => ts('Disable Relationship'),
232 ],
233 ];
234 }
235 }
236
237 // call the hook so we can modify it
238 CRM_Utils_Hook::links('view.contact.userDashBoard',
239 'Contact',
240 CRM_Core_DAO::$_nullObject,
241 self::$_links
242 );
243 return self::$_links;
244 }
245
246 /**
247 * Get the user checksum from the url to use in links.
248 *
249 * @return string
250 */
251 protected function getUserChecksum() {
252 $userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this);
253 if (empty($userID) && $this->_contactId) {
254 return $userChecksum;
255 }
256 return FALSE;
257 }
258
259 }