Merge pull request #14023 from seamuslee001/core_dedupe_custom
[civicrm-core.git] / CRM / Contact / Page / View / UserDashBoard.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
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 */
430ae6dd
TO
53 static $_links = NULL;
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')) {
be2fb01f 147 $dashboardElements[] = [
e81ccbf6 148 'class' => 'crm-dashboard-permissionedOrgs',
d0592c3d 149 'templatePath' => 'CRM/Contact/Page/View/RelationshipSelector.tpl',
6a488035
TO
150 'sectionTitle' => ts('Your Contacts / Organizations'),
151 'weight' => 40,
be2fb01f 152 ];
6a488035 153
6a488035
TO
154 }
155
a50321d2 156 if (!empty($dashboardOptions['PCP'])) {
be2fb01f 157 $dashboardElements[] = [
e81ccbf6 158 'class' => 'crm-dashboard-pcp',
6a488035
TO
159 'templatePath' => 'CRM/Contribute/Page/PcpUserDashboard.tpl',
160 'sectionTitle' => ts('Personal Campaign Pages'),
161 'weight' => 40,
be2fb01f 162 ];
6a488035
TO
163 list($pcpBlock, $pcpInfo) = CRM_PCP_BAO_PCP::getPcpDashboardInfo($this->_contactId);
164 $this->assign('pcpBlock', $pcpBlock);
165 $this->assign('pcpInfo', $pcpInfo);
166 }
167
a50321d2 168 if (!empty($dashboardOptions['Assigned Activities']) && empty($this->_isChecksumUser)) {
6a488035 169 // Assigned Activities section
be2fb01f 170 $dashboardElements[] = [
e81ccbf6 171 'class' => 'crm-dashboard-assignedActivities',
6a488035
TO
172 'templatePath' => 'CRM/Activity/Page/UserDashboard.tpl',
173 'sectionTitle' => ts('Your Assigned Activities'),
174 'weight' => 5,
be2fb01f 175 ];
acb1052e 176 $userDashboard = new CRM_Activity_Page_UserDashboard();
6a488035
TO
177 $userDashboard->run();
178 }
179
be2fb01f 180 usort($dashboardElements, ['CRM_Utils_Sort', 'cmpFunc']);
6a488035
TO
181 $this->assign('dashboardElements', $dashboardElements);
182
56ed0b01 183 // return true when 'Invoices / Credit Notes' checkbox is checked
a50321d2 184 $this->assign('invoices', $dashboardOptions['Invoices / Credit Notes']);
56ed0b01 185
a50321d2 186 if (!empty($dashboardOptions['Groups'])) {
6a488035
TO
187 $this->assign('showGroup', TRUE);
188 //build group selector
189 $gContact = new CRM_Contact_Page_View_UserDashBoard_GroupContact();
190 $gContact->run();
191 }
192 else {
193 $this->assign('showGroup', FALSE);
194 }
195 }
196
197 /**
fe482240 198 * Perform actions and display for user dashboard.
6a488035 199 */
00be9182 200 public function run() {
6a488035
TO
201 $this->preProcess();
202 $this->buildUserDashBoard();
203 return parent::run();
204 }
205
206 /**
fe482240 207 * Get action links.
6a488035 208 *
a6c01b45
CW
209 * @return array
210 * (reference) of action links
6a488035 211 */
74607358 212 public static function &links() {
6a488035
TO
213 if (!(self::$_links)) {
214 $disableExtra = ts('Are you sure you want to disable this relationship?');
215
be2fb01f
CW
216 self::$_links = [
217 CRM_Core_Action::UPDATE => [
6a488035
TO
218 'name' => ts('Edit Contact Information'),
219 'url' => 'civicrm/contact/relatedcontact',
220 'qs' => 'action=update&reset=1&cid=%%cbid%%&rcid=%%cid%%',
c99ad28e 221 'title' => ts('Edit Contact Information'),
be2fb01f
CW
222 ],
223 CRM_Core_Action::VIEW => [
6a488035
TO
224 'name' => ts('Dashboard'),
225 'url' => 'civicrm/user',
5940abe4 226 'class' => 'no-popup',
6a488035 227 'qs' => 'reset=1&id=%%cbid%%',
bc13177a 228 'title' => ts('View Contact Dashboard'),
be2fb01f
CW
229 ],
230 ];
6a488035 231
6a488035 232 if (CRM_Core_Permission::check('access CiviCRM')) {
be2fb01f
CW
233 self::$_links += [
234 CRM_Core_Action::DISABLE => [
353ffa53
TO
235 'name' => ts('Disable'),
236 'url' => 'civicrm/contact/view/rel',
237 'qs' => 'action=disable&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%&selectedChild=rel&context=dashboard',
238 'extra' => 'onclick = "return confirm(\'' . $disableExtra . '\');"',
239 'title' => ts('Disable Relationship'),
be2fb01f
CW
240 ],
241 ];
6a488035
TO
242 }
243 }
244
245 // call the hook so we can modify it
246 CRM_Utils_Hook::links('view.contact.userDashBoard',
247 'Contact',
248 CRM_Core_DAO::$_nullObject,
1273d77c 249 self::$_links
6a488035
TO
250 );
251 return self::$_links;
252 }
96025800 253
2d97420b 254 /**
255 * Get the user checksum from the url to use in links.
256 *
257 * @return string
258 */
259 protected function getUserChecksum() {
260 $userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this);
261 if (empty($userID) && $this->_contactId) {
262 return $userChecksum;
263 }
264 return FALSE;
265 }
266
6a488035 267}