Merge pull request #8494 from kcristiano/CRM-18221
[civicrm-core.git] / CRM / Core / BAO / Dashboard.php
index e42bf80bfab52c9a769a5f50f2c78ba5416c05e4..51caf401440f8ca26ea7587d02a3aeeb3bd9db2b 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.7                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2015                                |
+ | Copyright CiviCRM LLC (c) 2004-2016                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2015
+ * @copyright CiviCRM LLC (c) 2004-2016
  */
 
 /**
@@ -107,29 +107,30 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard {
   public static function getContactDashlets($flatFormat = FALSE, $contactID = NULL) {
     $dashlets = array();
 
-    if (!$contactID) {
-      $contactID = CRM_Core_Session::singleton()->get('userID');
-    }
-
     // Get contact dashboard dashlets.
     $hasDashlets = FALSE;
     $dao = new CRM_Contact_DAO_DashboardContact();
-    $dao->contact_id = $contactID;
+    $dao->contact_id = $contactID ? $contactID : CRM_Core_Session::singleton()->getLoggedInContactID();
     $dao->orderBy('column_no asc, weight asc');
     $dao->find();
+
+    // The available list will only include those which are valid for the domain.
+    $availableDashlets = self::getDashlets();
     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) {
-          // append weight so that order is preserved.
-          $dashlets[$dao->column_no]["{$dao->weight}-{$dao->dashboard_id}"] = $dao->is_minimized;
+      if ((!empty($availableDashlets[$dao->dashboard_id]) && $availableDashlets[$dao->dashboard_id]['is_active'])) {
+        $hasDashlets = TRUE;
+        if (!$flatFormat) {
+          if ($dao->is_active) {
+            // append weight so that order is preserved.
+            $dashlets[$dao->column_no]["{$dao->weight}-{$dao->dashboard_id}"] = $dao->is_minimized;
+          }
+        }
+        else {
+          $dashlets[$dao->dashboard_id] = $dao->dashboard_id;
         }
-      }
-      else {
-        $dashlets[$dao->dashboard_id] = $dao->dashboard_id;
       }
     }
 
@@ -162,12 +163,12 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard {
         'domain_id' => CRM_Core_Config::domainID(),
         'option.limit' => 0,
       ));
-    $contactID = CRM_Core_Session::singleton()->get('userID');
+    $contactID = CRM_Core_Session::singleton()->getLoggedInContactID();
     $allDashlets = CRM_Utils_Array::index(array('name'), $getDashlets['values']);
     $defaultDashlets = array();
     $defaults = array('blog' => 1, 'getting-started' => '0');
     foreach ($defaults as $name => $column) {
-      if (!empty($allDashlets[$name])) {
+      if (!empty($allDashlets[$name]) && !empty($allDashlets[$name]['id'])) {
         $defaultDashlets[$name] = array(
           'dashboard_id' => $allDashlets[$name]['id'],
           'is_active' => 1,