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