From 5aa910d4a3713014aaf5e0dc34b2a7bc583389e3 Mon Sep 17 00:00:00 2001 From: Jamie McClelland Date: Wed, 8 Oct 2014 14:23:01 -0400 Subject: [PATCH] CRM-15430 - ensure default dashlets properly initialized for new users. ---------------------------------------- * CRM-15430: clarify code and functionality for hook_civicrm_dashboard_defaults https://issues.civicrm.org/jira/browse/CRM-15430 --- CRM/Core/BAO/Dashboard.php | 56 ++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/CRM/Core/BAO/Dashboard.php b/CRM/Core/BAO/Dashboard.php index de6b98c8f0..638c8848f4 100644 --- a/CRM/Core/BAO/Dashboard.php +++ b/CRM/Core/BAO/Dashboard.php @@ -90,13 +90,16 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard { } /** - * Function to get the list of dashlets for a contact + * Get the list of dashlets for the current user or the specified user. * - * Initializes the dashboard with defaults if this is the user's first visit to their dashboard + * Additionlly, initializes the dashboard with defaults if this is the + * user's first visit to their dashboard. * - * @param boolean $flatFormat this is true if you want simple associated array of contact dashlets + * @param boolean $flatFormat this is true if you want simple associated + * array of all the contact's dashlets whether or not they are enabled. * - * @param null $contactID + * @param integer $contactID provide the dashlets for the contact id + * passed rather than the current user. * * @return array $dashlets array of dashlets * @access public @@ -109,13 +112,16 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard { $contactID = CRM_Core_Session::singleton()->get('userID'); } - // get contact dashboard dashlets + // Get contact dashboard dashlets. $hasDashlets = FALSE; $dao = new CRM_Contact_DAO_DashboardContact(); $dao->contact_id = $contactID; $dao->orderBy('column_no asc, weight asc'); $dao->find(); while ($dao->fetch()) { + // When a dashlet is removed, it stays in the table with status disabled, + // so even if a user decides not to have any dashlets show, they will still + // have records in the table to indicate that we are not newly initializing. $hasDashlets = TRUE; if (!$flatFormat) { if ($dao->is_active) { @@ -132,50 +138,52 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard { return $dashlets; } - // If empty then initialize contact dashboard for this user - $defaultDashlet = self::initializeDashlets($hasDashlets); - return $defaultDashlet ? $defaultDashlet : $dashlets; + // If empty, then initialize contact dashboard for this user. + if(!$hasDashlets) { + return self::initializeDashlets(); + } + return $dashlets; } /** - * @param $hasDashlets + * Setup default dashlets for new users * - * @return bool - * @throws CiviCRM_API3_Exception + * When a user accesses their dashboard for the first time, set up + * the default dashlets. + * + * @return array of dashboard_id's */ - static function initializeDashlets($hasDashlets) { - $getDashlets = civicrm_api3("Dashboard", "get", array('domain_id' => CRM_Core_Config::domainID())); + static function initializeDashlets() { + $dashlets = array(); + $getDashlets = civicrm_api3("Dashboard", "get", array('domain_id' => CRM_Core_Config::domainID(), 'option.limit' => 0)); $contactID = CRM_Core_Session::singleton()->get('userID'); - $allDashlets = CRM_Utils_Array::index(array('name'), $getDashlets['values']); $defaultDashlets = array(); - if (!$hasDashlets && !empty($allDashlets['blog'])) { + if (CRM_Utils_Array::value('blog', $allDashlets)) { $defaultDashlets['blog'] = array( 'dashboard_id' => $allDashlets['blog']['id'], 'is_active' => 1, 'column_no' => 1, 'contact_id' => $contactID, - 'domain_id' => CRM_Core_Config::domainID(), ); } CRM_Utils_Hook::dashboard_defaults($allDashlets, $defaultDashlets); if (is_array($defaultDashlets) && !empty($defaultDashlets)) { - foreach ($defaultDashlets as $defaultDashlet) { - if (!self::checkPermission($getDashlets['values'][$defaultDashlet['dashboard_id']]['permission'], - $getDashlets['values'][$defaultDashlet['dashboard_id']]['permission_operator'])) { - unset($defaultDashlets[$defaultDashlet]); + foreach ($defaultDashlets as $id => $defaultDashlet) { + $dashboard_id = $defaultDashlet['dashboard_id']; + if (!self::checkPermission($getDashlets['values'][$dashboard_id]['permission'], + $getDashlets['values'][$dashboard_id]['permission_operator'])) { continue; } else { $assignDashlets = civicrm_api3("dashboard_contact", "create", $defaultDashlet); - $dashlets[$defaultDashlet['dashboard_id']] = $defaultDashlet['dashboard_id']; + $dashlets[$dashboard_id] = $defaultDashlet['dashboard_id']; } } - return $dashlets; } - - return FALSE; + return $dashlets; } + /** * Function to check dashlet permission for current user -- 2.25.1