Merge pull request #15322 from alifrumin/removePrintIcon
[civicrm-core.git] / CRM / Core / BAO / Dashboard.php
index 1bfd6812d2126136b8dfe7288d0913b3e6b15353..a94154d178f63da4bea9d3aca6b3d5b4df798900 100644 (file)
@@ -35,6 +35,7 @@
  * Class contains Contact dashboard related functions.
  */
 class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard {
+
   /**
    * Add Dashboard.
    *
@@ -108,6 +109,7 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard {
       'contact_id' => $contactID,
       'is_active' => 1,
       'dashboard_id.is_active' => 1,
+      'dashboard_id.domain_id' => CRM_Core_Config::domainID(),
       'options' => ['sort' => 'weight', 'limit' => 0],
       'return' => [
         'id',
@@ -142,7 +144,7 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard {
     // If empty, then initialize default dashlets for this user.
     if (!$results['count']) {
       // They may just have disabled all their dashlets. Check if any records exist for this contact.
-      if (!civicrm_api3('DashboardContact', 'getcount', ['contact_id' => $contactID])) {
+      if (!civicrm_api3('DashboardContact', 'getcount', ['contact_id' => $contactID, 'dashboard_id.domain_id' => CRM_Core_Config::domainID()])) {
         $dashlets = self::initializeDashlets();
       }
     }
@@ -175,15 +177,15 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard {
    * the default dashlets.
    *
    * @return array
-   *    Array of dashboard_id's
+   *   Array of dashboard_id's
    * @throws \CiviCRM_API3_Exception
    */
   public static function initializeDashlets() {
     $dashlets = [];
     $getDashlets = civicrm_api3("Dashboard", "get", [
-        'domain_id' => CRM_Core_Config::domainID(),
-        'option.limit' => 0,
-      ]);
+      'domain_id' => CRM_Core_Config::domainID(),
+      'option.limit' => 0,
+    ]);
     $contactID = CRM_Core_Session::getLoggedInContactID();
     $allDashlets = CRM_Utils_Array::index(['name'], $getDashlets['values']);
     $defaultDashlets = [];
@@ -271,10 +273,8 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard {
         }
 
         // hack to handle case permissions
-        if (!$componentName && in_array($key, [
-            'access my cases and activities',
-            'access all cases and activities',
-          ])
+        if (!$componentName
+          && in_array($key, ['access my cases and activities', 'access all cases and activities'])
         ) {
           $componentName = 'CiviCase';
         }
@@ -357,13 +357,39 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard {
       }
     }
 
-    // Disable inactive widgets
-    $dashletClause = $dashletIDs ? "dashboard_id NOT IN  (" . implode(',', $dashletIDs) . ")" : '(1)';
+    // Find dashlets in this domain.
+    $domainDashlets = civicrm_api3('Dashboard', 'get', [
+      'return' => array('id'),
+      'domain_id' => CRM_Core_Config::domainID(),
+      'options' => ['limit' => 0],
+    ]);
+
+    // Get the array of IDs.
+    $domainDashletIDs = [];
+    if ($domainDashlets['is_error'] == 0) {
+      $domainDashletIDs = CRM_Utils_Array::collect('id', $domainDashlets['values']);
+    }
+
+    // Restrict query to Dashlets in this domain.
+    $domainDashletClause = !empty($domainDashletIDs) ? "dashboard_id IN (" . implode(',', $domainDashletIDs) . ")" : '(1)';
+
+    // Target only those Dashlets which are inactive.
+    $dashletClause = $dashletIDs ? "dashboard_id NOT IN (" . implode(',', $dashletIDs) . ")" : '(1)';
+
+    // Build params.
+    $params = [
+      1 => [$contactID, 'Integer'],
+    ];
+
+    // Build query.
     $updateQuery = "UPDATE civicrm_dashboard_contact
                     SET is_active = 0
-                    WHERE $dashletClause AND contact_id = {$contactID}";
+                    WHERE $domainDashletClause
+                    AND $dashletClause
+                    AND contact_id = %1";
 
-    CRM_Core_DAO::executeQuery($updateQuery);
+    // Disable inactive widgets.
+    CRM_Core_DAO::executeQuery($updateQuery, $params);
   }
 
   /**
@@ -389,7 +415,16 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard {
     $dashlet = new CRM_Core_DAO_Dashboard();
 
     if (!$dashboardID) {
-      // check url is same as exiting entries, if yes just update existing
+
+      // Assign domain before search to allow identical dashlets in different domains.
+      if (empty($params['domain_id'])) {
+        $dashlet->domain_id = CRM_Core_Config::domainID();
+      }
+      else {
+        $dashlet->domain_id = CRM_Utils_Array::value('domain_id', $params);
+      }
+
+      // Try and find an existing dashlet - it will be updated if found.
       if (!empty($params['name'])) {
         $dashlet->name = CRM_Utils_Array::value('name', $params);
         $dashlet->find(TRUE);
@@ -398,9 +433,7 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard {
         $dashlet->url = CRM_Utils_Array::value('url', $params);
         $dashlet->find(TRUE);
       }
-      if (empty($params['domain_id'])) {
-        $dashlet->domain_id = CRM_Core_Config::domainID();
-      }
+
     }
     else {
       $dashlet->id = $dashboardID;