3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * Class contains Contact dashboard related functions
39 class CRM_Core_BAO_Dashboard
extends CRM_Core_DAO_Dashboard
{
41 * function to add Dashboard
43 * @param array $params values
50 static function create($params) {
51 $hook = empty($params['id']) ?
'create' : 'edit';
52 CRM_Utils_Hook
::pre($hook, 'Dashboard', CRM_Utils_Array
::value('id', $params), $params);
53 $dao = self
::addDashlet($params);
54 CRM_Utils_Hook
::post($hook, 'Dashboard', $dao->id
, $dao);
59 * Get the list of dashlets enabled by admin
61 * @param boolean $all all or only active
62 * @param boolean $checkPermission all or only authorized for the current user
64 * @return array $widgets array of dashlets
68 static function getDashlets($all = TRUE, $checkPermission = TRUE) {
70 $dao = new CRM_Core_DAO_Dashboard();
76 $dao->domain_id
= CRM_Core_Config
::domainID();
79 while ($dao->fetch()) {
80 if ($checkPermission && !self
::checkPermission($dao->permission
, $dao->permission_operator
)) {
85 CRM_Core_DAO
::storeValues($dao, $values);
86 $dashlets[$dao->id
] = $values;
93 * Get the list of dashlets for the current user or the specified user.
95 * Additionlly, initializes the dashboard with defaults if this is the
96 * user's first visit to their dashboard.
98 * @param boolean $flatFormat this is true if you want simple associated
99 * array of all the contact's dashlets whether or not they are enabled.
101 * @param integer $contactID provide the dashlets for the contact id
102 * passed rather than the current user.
104 * @return array $dashlets array of dashlets
108 static function getContactDashlets($flatFormat = FALSE, $contactID = NULL) {
112 $contactID = CRM_Core_Session
::singleton()->get('userID');
115 // Get contact dashboard dashlets.
116 $hasDashlets = FALSE;
117 $dao = new CRM_Contact_DAO_DashboardContact();
118 $dao->contact_id
= $contactID;
119 $dao->orderBy('column_no asc, weight asc');
121 while ($dao->fetch()) {
122 // When a dashlet is removed, it stays in the table with status disabled,
123 // so even if a user decides not to have any dashlets show, they will still
124 // have records in the table to indicate that we are not newly initializing.
127 if ($dao->is_active
) {
128 // append weight so that order is preserved.
129 $dashlets[$dao->column_no
]["{$dao->weight}-{$dao->dashboard_id}"] = $dao->is_minimized
;
133 $dashlets[$dao->dashboard_id
] = $dao->dashboard_id
;
141 // If empty, then initialize contact dashboard for this user.
143 return self
::initializeDashlets();
149 * Setup default dashlets for new users
151 * When a user accesses their dashboard for the first time, set up
152 * the default dashlets.
154 * @return array of dashboard_id's
156 static function initializeDashlets() {
158 $getDashlets = civicrm_api3("Dashboard", "get", array('domain_id' => CRM_Core_Config
::domainID(), 'option.limit' => 0));
159 $contactID = CRM_Core_Session
::singleton()->get('userID');
160 $allDashlets = CRM_Utils_Array
::index(array('name'), $getDashlets['values']);
161 $defaultDashlets = array();
162 if (CRM_Utils_Array
::value('blog', $allDashlets)) {
163 $defaultDashlets['blog'] = array(
164 'dashboard_id' => $allDashlets['blog']['id'],
167 'contact_id' => $contactID,
170 CRM_Utils_Hook
::dashboard_defaults($allDashlets, $defaultDashlets);
171 if (is_array($defaultDashlets) && !empty($defaultDashlets)) {
172 foreach ($defaultDashlets as $id => $defaultDashlet) {
173 $dashboard_id = $defaultDashlet['dashboard_id'];
174 if (!self
::checkPermission($getDashlets['values'][$dashboard_id]['permission'],
175 $getDashlets['values'][$dashboard_id]['permission_operator'])) {
179 $assignDashlets = civicrm_api3("dashboard_contact", "create", $defaultDashlet);
180 $dashlets[$dashboard_id] = $defaultDashlet['dashboard_id'];
189 * Function to check dashlet permission for current user
194 * @internal param \permission $string string
196 * @return boolean true if use has permission else false
198 static function checkPermission($permission, $operator) {
200 $permissions = explode(',', $permission);
201 $config = CRM_Core_Config
::singleton();
203 static $allComponents;
204 if (!$allComponents) {
205 $allComponents = CRM_Core_Component
::getNames();
208 $hasPermission = FALSE;
209 foreach ($permissions as $key) {
212 $componentName = NULL;
213 if (strpos($key, 'access') === 0) {
214 $componentName = trim(substr($key, 6));
215 if (!in_array($componentName, $allComponents)) {
216 $componentName = NULL;
220 // hack to handle case permissions
221 if (!$componentName && in_array($key, array(
222 'access my cases and activities', 'access all cases and activities'))) {
223 $componentName = 'CiviCase';
226 //hack to determine if it's a component related permission
227 if ($componentName) {
228 if (!in_array($componentName, $config->enableComponents
) ||
229 !CRM_Core_Permission
::check($key)
231 $showDashlet = FALSE;
232 if ($operator == 'AND') {
237 $hasPermission = TRUE;
240 elseif (!CRM_Core_Permission
::check($key)) {
241 $showDashlet = FALSE;
242 if ($operator == 'AND') {
247 $hasPermission = TRUE;
251 if (!$showDashlet && !$hasPermission) {
259 // if permission is not set consider everyone has permission to access it.
265 * Function to get details of each dashlets
267 * @param int $dashletID widget ID
269 * @return array associted array title and content
273 static function getDashletInfo($dashletID) {
274 $dashletInfo = array();
276 $params = array(1 => array($dashletID, 'Integer'));
277 $query = "SELECT name, label, url, fullscreen_url, is_fullscreen FROM civicrm_dashboard WHERE id = %1";
278 $dashboadDAO = CRM_Core_DAO
::executeQuery($query, $params);
279 $dashboadDAO->fetch();
282 $dao = new CRM_Contact_DAO_DashboardContact();
284 $session = CRM_Core_Session
::singleton();
285 $dao->contact_id
= $session->get('userID');
286 $dao->dashboard_id
= $dashletID;
289 //reset content based on the cache time set in config
290 $createdDate = strtotime($dao->created_date
);
291 $dateDiff = round(abs(time() - $createdDate) / 60);
293 $config = CRM_Core_Config
::singleton();
294 if ($config->dashboardCacheTimeout
<= $dateDiff) {
295 $dao->content
= NULL;
298 // if content is empty and url is set, retrieve it from url
299 if (!$dao->content
&& $dashboadDAO->url
) {
300 $url = $dashboadDAO->url
;
303 // -lets use relative url for internal use.
304 // -make sure relative url should not be htmlize.
305 if (substr($dashboadDAO->url
, 0, 4) != 'http') {
306 $urlParam = explode('?', $dashboadDAO->url
);
307 $url = CRM_Utils_System
::url($urlParam[0], $urlParam[1], TRUE, NULL, FALSE);
310 //get content from url
311 $dao->content
= CRM_Utils_System
::getServerResponse($url);
312 $dao->created_date
= date("YmdHis");
316 $dashletInfo = array(
317 'title' => $dashboadDAO->label
,
318 'name' => $dashboadDAO->name
,
319 'content' => $dao->content
,
322 if ($dashboadDAO->is_fullscreen
) {
323 $fullscreenUrl = $dashboadDAO->fullscreen_url
;
324 if (substr($fullscreenUrl, 0, 4) != 'http') {
325 $urlParam = explode('?', $dashboadDAO->fullscreen_url
);
326 $fullscreenUrl = CRM_Utils_System
::url($urlParam[0], $urlParam[1], TRUE, NULL, FALSE);
328 $dashletInfo['fullscreenUrl'] = $fullscreenUrl;
334 * Function to save changes made by use to the Dashlet
336 * @param array $columns associated array
338 * @param null $contactID
340 * @throws RuntimeException
345 static function saveDashletChanges($columns, $contactID=NULL) {
346 $session = CRM_Core_Session
::singleton();
348 $contactID = $session->get('userID');
351 if (empty($contactID)) {
352 throw new RuntimeException("Failed to determine contact ID");
355 //we need to get existing dashlets, so we know when to update or insert
356 $contactDashlets = self
::getContactDashlets(TRUE, $contactID);
358 $dashletIDs = array();
359 if (is_array($columns)) {
360 foreach ($columns as $colNo => $dashlets) {
361 if (!is_integer($colNo)) {
365 foreach ($dashlets as $dashletID => $isMinimized) {
366 $isMinimized = (int) $isMinimized;
367 if (in_array($dashletID, $contactDashlets)) {
368 $query = " UPDATE civicrm_dashboard_contact
369 SET weight = {$weight}, is_minimized = {$isMinimized}, column_no = {$colNo}, is_active = 1
370 WHERE dashboard_id = {$dashletID} AND contact_id = {$contactID} ";
373 $query = " INSERT INTO civicrm_dashboard_contact
374 ( weight, is_minimized, column_no, is_active, dashboard_id, contact_id )
375 VALUES( {$weight}, {$isMinimized}, {$colNo}, 1, {$dashletID}, {$contactID} )";
377 // fire update query for each column
378 $dao = CRM_Core_DAO
::executeQuery($query);
380 $dashletIDs[] = $dashletID;
386 if (!empty($dashletIDs)) {
387 // we need to disable widget that removed
388 $updateQuery = " UPDATE civicrm_dashboard_contact
390 WHERE dashboard_id NOT IN ( " . implode(',', $dashletIDs) . ") AND contact_id = {$contactID}";
393 // this means all widgets are disabled
394 $updateQuery = " UPDATE civicrm_dashboard_contact
396 WHERE contact_id = {$contactID}";
399 CRM_Core_DAO
::executeQuery($updateQuery);
403 * Function to add dashlets
405 * @param array $params associated array
407 * @return object $dashlet returns dashlet object
411 static function addDashlet(&$params) {
413 // special case to handle duplicate entries for report instances
414 $dashboardID = CRM_Utils_Array
::value('id', $params);
416 if (!empty($params['instanceURL'])) {
418 FROM `civicrm_dashboard`
419 WHERE url LIKE '" . CRM_Utils_Array
::value('instanceURL', $params) . "&%'";
420 $dashboardID = CRM_Core_DAO
::singleValueQuery($query);
423 $dashlet = new CRM_Core_DAO_Dashboard();
426 // check url is same as exiting entries, if yes just update existing
427 if (!empty($params['name'])) {
428 $dashlet->name
= CRM_Utils_Array
::value('name', $params);
429 $dashlet->find(TRUE);
432 $dashlet->url
= CRM_Utils_Array
::value('url', $params);
433 $dashlet->find(TRUE);
435 if (empty($params['domain_id'])) {
436 $dashlet->domain_id
= CRM_Core_Config
::domainID();
440 $dashlet->id
= $dashboardID;
443 if (is_array(CRM_Utils_Array
::value('permission', $params))) {
444 $params['permission'] = implode(',', $params['permission']);
446 $dashlet->copyValues($params);
449 // now we need to make dashlet entries for each contact
450 self
::addContactDashlet($dashlet);
460 static function getDashletName($url) {
461 $urlElements = explode('/', $url);
462 if ($urlElements[1] == 'dashlet') {
463 return $urlElements[2];
465 elseif ($urlElements[1] == 'report') {
466 return 'report/' . $urlElements[3];
471 * Update contact dashboard with new dashlet
473 * @param object: $dashlet
478 static function addContactDashlet($dashlet) {
479 $admin = CRM_Core_Permission
::check('administer CiviCRM');
481 // if dashlet is created by admin then you need to add it all contacts.
482 // else just add to contact who is creating this dashlet
483 $contactIDs = array();
485 $query = "SELECT distinct( contact_id )
486 FROM civicrm_dashboard_contact
487 WHERE contact_id NOT IN (
488 SELECT distinct( contact_id )
489 FROM civicrm_dashboard_contact WHERE dashboard_id = {$dashlet->id}
492 $dao = CRM_Core_DAO
::executeQuery($query);
493 while ($dao->fetch()) {
494 $contactIDs[] = $dao->contact_id
;
498 //Get the id of Logged in User
499 $session = CRM_Core_Session
::singleton();
500 $contactID = $session->get('userID');
501 if(!empty($contactID)) {
502 $contactIDs[] = $session->get('userID');
506 if (!empty($contactIDs)) {
507 foreach ($contactIDs as $contactID) {
508 $valuesArray[] = " ( {$dashlet->id}, {$contactID} )";
511 $valuesString = implode(',', $valuesArray);
513 INSERT INTO civicrm_dashboard_contact ( dashboard_id, contact_id )
514 VALUES {$valuesString}";
516 CRM_Core_DAO
::executeQuery($query);
521 * @param array $params each item is a spec for a dashlet on the contact's dashboard
524 static function addContactDashletToDashboard(&$params) {
525 $valuesString = NULL;
527 foreach ($params as $dashboardIDs) {
528 $contactID = CRM_Utils_Array
::value('contact_id', $dashboardIDs);
529 $dashboardID = CRM_Utils_Array
::value('dashboard_id', $dashboardIDs);
530 $column = CRM_Utils_Array
::value('column_no', $dashboardIDs, 0);
531 $columns[$column][$dashboardID] = 0;
533 self
::saveDashletChanges($columns, $contactID);
538 * Function to reset dashlet cache
540 * @param int $contactID reset cache only for specific contact
545 static function resetDashletCache($contactID = null) {
549 $whereClause = "WHERE contact_id = %1";
550 $params[1] = array($contactID, 'Integer');
552 $query = "UPDATE civicrm_dashboard_contact SET content = NULL $whereClause";
553 $dao = CRM_Core_DAO
::executeQuery($query, $params);
564 static function deleteDashlet($dashletID) {
565 $dashlet = new CRM_Core_DAO_Dashboard();
566 $dashlet->id
= $dashletID;