Merge in 5.48
[civicrm-core.git] / CRM / Contact / Page / View / UserDashBoard.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
6a488035 19 * This class is used to build User Dashboard
6a488035
TO
20 */
21class CRM_Contact_Page_View_UserDashBoard extends CRM_Core_Page {
22 public $_contactId = NULL;
23
c490a46a 24 /**
fe482240 25 * Always show public groups.
c490a46a
CW
26 * @var bool
27 */
6a488035
TO
28 public $_onlyPublicGroups = TRUE;
29
30 public $_edit = TRUE;
31
32 /**
fe482240 33 * The action links that we need to display for the browse screen.
6a488035
TO
34 *
35 * @var array
6a488035 36 */
9cd3992e 37 public static $_links = NULL;
430ae6dd 38
4319322b
EM
39 /**
40 * @throws Exception
41 */
00be9182 42 public function __construct() {
6a488035
TO
43 parent::__construct();
44
83564f94 45 if (!CRM_Core_Permission::check('access Contact Dashboard')) {
6a488035 46 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/dashboard', 'reset=1'));
6a488035
TO
47 }
48
49 $this->_contactId = CRM_Utils_Request::retrieve('id', 'Positive', $this);
2dbdb9b9 50 $userID = CRM_Core_Session::getLoggedInContactID();
6a488035 51
2d97420b 52 $userChecksum = $this->getUserChecksum();
2b329653 53 $validUser = FALSE;
2d97420b 54 if ($userChecksum) {
e8401861 55 $this->assign('userChecksum', $userChecksum);
2b329653 56 $validUser = CRM_Contact_BAO_Contact_Utils::validChecksum($this->_contactId, $userChecksum);
4ded03d2 57 $this->_isChecksumUser = $validUser;
2b329653
JP
58 }
59
6a488035
TO
60 if (!$this->_contactId) {
61 $this->_contactId = $userID;
62 }
2b329653 63 elseif ($this->_contactId != $userID && !$validUser) {
6a488035 64 if (!CRM_Contact_BAO_Contact_Permission::allow($this->_contactId, CRM_Core_Permission::VIEW)) {
beb414cc 65 CRM_Core_Error::statusBounce(ts('You do not have permission to access this contact.'));
6a488035
TO
66 }
67 if (!CRM_Contact_BAO_Contact_Permission::allow($this->_contactId, CRM_Core_Permission::EDIT)) {
68 $this->_edit = FALSE;
69 }
70 }
71 }
72
c490a46a 73 /**
95cdcc0f 74 * Heart of the viewing process.
c490a46a 75 *
95cdcc0f 76 * The runner gets all the meta data for the contact and calls the appropriate type of page to view.
c490a46a 77 */
00be9182 78 public function preProcess() {
6a488035 79 if (!$this->_contactId) {
a39a65ee 80 throw new CRM_Core_Exception(ts('You must be logged in to view this page.'));
6a488035
TO
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
be2fb01f 88 CRM_Utils_System::setTitle(ts('Dashboard - %1', [1 => $displayName]));
6a488035
TO
89
90 $this->assign('recentlyViewed', FALSE);
91 }
92
93 /**
fe482240 94 * Build user dashboard.
6a488035 95 */
00be9182 96 public function buildUserDashBoard() {
6a488035 97 //build component selectors
a50321d2 98 $dashboardElements = [];
6a488035 99
a50321d2 100 $dashboardOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
6a488035
TO
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
a50321d2 112 if (!empty($dashboardOptions[$name]) &&
6a488035
TO
113 (CRM_Core_Permission::access($component->name) ||
114 CRM_Core_Permission::check($elem['perm'][0])
115 )
116 ) {
117
118 $userDashboard = $component->getUserDashboardObject();
be2fb01f 119 $dashboardElements[] = [
e81ccbf6 120 'class' => 'crm-dashboard-' . strtolower($component->name),
6a488035 121 'sectionTitle' => $elem['title'],
e81ccbf6 122 'templatePath' => $userDashboard->getTemplateFileName(),
6a488035 123 'weight' => $elem['weight'],
be2fb01f 124 ];
6a488035
TO
125 $userDashboard->run();
126 }
127 }
128
bb36d17a 129 // CRM-16512 - Hide related contact table if user lacks permission to view self
a50321d2 130 if (!empty($dashboardOptions['Permissioned Orgs']) && CRM_Core_Permission::check('view my contact')) {
16b745b0
MWMC
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);
78e2927c 135 $this->assign('entityInClassFormat', 'relationship');
be2fb01f 136 $dashboardElements[] = [
e81ccbf6 137 'class' => 'crm-dashboard-permissionedOrgs',
d0592c3d 138 'templatePath' => 'CRM/Contact/Page/View/RelationshipSelector.tpl',
6a488035
TO
139 'sectionTitle' => ts('Your Contacts / Organizations'),
140 'weight' => 40,
be2fb01f 141 ];
6a488035 142
6a488035
TO
143 }
144
a50321d2 145 if (!empty($dashboardOptions['PCP'])) {
be2fb01f 146 $dashboardElements[] = [
e81ccbf6 147 'class' => 'crm-dashboard-pcp',
6a488035
TO
148 'templatePath' => 'CRM/Contribute/Page/PcpUserDashboard.tpl',
149 'sectionTitle' => ts('Personal Campaign Pages'),
150 'weight' => 40,
be2fb01f 151 ];
6a488035
TO
152 list($pcpBlock, $pcpInfo) = CRM_PCP_BAO_PCP::getPcpDashboardInfo($this->_contactId);
153 $this->assign('pcpBlock', $pcpBlock);
154 $this->assign('pcpInfo', $pcpInfo);
155 }
156
a50321d2 157 if (!empty($dashboardOptions['Assigned Activities']) && empty($this->_isChecksumUser)) {
6a488035 158 // Assigned Activities section
be2fb01f 159 $dashboardElements[] = [
e81ccbf6 160 'class' => 'crm-dashboard-assignedActivities',
6a488035
TO
161 'templatePath' => 'CRM/Activity/Page/UserDashboard.tpl',
162 'sectionTitle' => ts('Your Assigned Activities'),
163 'weight' => 5,
be2fb01f 164 ];
acb1052e 165 $userDashboard = new CRM_Activity_Page_UserDashboard();
6a488035
TO
166 $userDashboard->run();
167 }
168
be2fb01f 169 usort($dashboardElements, ['CRM_Utils_Sort', 'cmpFunc']);
ed85c1ce
EM
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 }
6a488035
TO
176 $this->assign('dashboardElements', $dashboardElements);
177
a50321d2 178 if (!empty($dashboardOptions['Groups'])) {
6a488035
TO
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 /**
fe482240 190 * Perform actions and display for user dashboard.
6a488035 191 */
00be9182 192 public function run() {
6a488035
TO
193 $this->preProcess();
194 $this->buildUserDashBoard();
195 return parent::run();
196 }
197
198 /**
fe482240 199 * Get action links.
6a488035 200 *
a6c01b45
CW
201 * @return array
202 * (reference) of action links
6a488035 203 */
74607358 204 public static function &links() {
6a488035
TO
205 if (!(self::$_links)) {
206 $disableExtra = ts('Are you sure you want to disable this relationship?');
207
be2fb01f
CW
208 self::$_links = [
209 CRM_Core_Action::UPDATE => [
6a488035
TO
210 'name' => ts('Edit Contact Information'),
211 'url' => 'civicrm/contact/relatedcontact',
212 'qs' => 'action=update&reset=1&cid=%%cbid%%&rcid=%%cid%%',
c99ad28e 213 'title' => ts('Edit Contact Information'),
be2fb01f
CW
214 ],
215 CRM_Core_Action::VIEW => [
6a488035
TO
216 'name' => ts('Dashboard'),
217 'url' => 'civicrm/user',
5940abe4 218 'class' => 'no-popup',
6a488035 219 'qs' => 'reset=1&id=%%cbid%%',
bc13177a 220 'title' => ts('View Contact Dashboard'),
be2fb01f
CW
221 ],
222 ];
6a488035 223
6a488035 224 if (CRM_Core_Permission::check('access CiviCRM')) {
be2fb01f
CW
225 self::$_links += [
226 CRM_Core_Action::DISABLE => [
353ffa53
TO
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'),
be2fb01f
CW
232 ],
233 ];
6a488035
TO
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,
1273d77c 241 self::$_links
6a488035
TO
242 );
243 return self::$_links;
244 }
96025800 245
2d97420b 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
6a488035 259}