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