CRM-13738 related improvisation and notice fix
authormonishdeb <monish.deb@webaccess.co.in>
Wed, 20 Nov 2013 15:43:15 +0000 (21:13 +0530)
committermonishdeb <monish.deb@webaccess.co.in>
Wed, 20 Nov 2013 15:43:15 +0000 (21:13 +0530)
----------------------------------------
* CRM-13738: Activity Report fails if filtering on any contact
  http://issues.civicrm.org/jira/browse/CRM-13738

CRM/Report/Form/Activity.php

index 961e1b274dfb273819d9f9c6501095c516057c52..863fbad652c0fa9126ce0826df4da15d7bcbda8a 100644 (file)
@@ -437,7 +437,7 @@ class CRM_Report_Form_Activity extends CRM_Report_Form {
     $this->addAddressFromClause();
   }
 
-  function where($recordType) {
+  function where($recordType = NULL) {
     $this->_where = " WHERE {$this->_aliases['civicrm_activity']}.is_test = 0 AND
                                 {$this->_aliases['civicrm_activity']}.is_deleted = 0 AND
                                 {$this->_aliases['civicrm_activity']}.is_current_revision = 1";
@@ -454,18 +454,6 @@ class CRM_Report_Form_Activity extends CRM_Report_Form {
               strstr($fieldName, '_source')
             )
           ) {
-            if ($recordType == 'final') {
-              if (CRM_Utils_Array::value("{$fieldName}_value", $this->_params)) {
-                $field['dbAlias'] = "civicrm_contact_{$fieldName}";
-                $clauses[] = $this->whereClause($field,
-                  CRM_Utils_Array::value("{$fieldName}_op", $this->_params),
-                  CRM_Utils_Array::value("{$fieldName}_value", $this->_params), NULL, NULL
-                );
-              }
-            }
-            continue;
-          }
-          if ($recordType == 'final') {
             continue;
           }
           if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
@@ -510,20 +498,10 @@ class CRM_Report_Form_Activity extends CRM_Report_Form {
     }
 
     if (empty($clauses)) {
-      if ($recordType == 'final') {
-        $this->_where = "";
-      }
-      else {
-        $this->_where .= " ";
-      }
+      $this->_where .= " ";
     }
     else {
-      if ($recordType == 'final') {
-        $this->_where = "WHERE " . implode(' AND ', $clauses);
-      }
-      else {
-        $this->_where .= " AND " . implode(' AND ', $clauses);
-      }
+      $this->_where .= " AND " . implode(' AND ', $clauses);
     }
 
     if ($this->_aclWhere) {
@@ -565,11 +543,28 @@ class CRM_Report_Form_Activity extends CRM_Report_Form {
     $this->buildACLClause(array('civicrm_contact_source', 'civicrm_contact_target', 'civicrm_contact_assignee'));
     $this->beginPostProcess();
 
+    //Assign those recordtype to array which have filter value or operator as 'Is not empty'
+    //which will be further used for where clause buildup of temp tables
+    $sortnameFilters = array();
+    foreach (array('target', 'source', 'assignee') as $type) {
+      if (CRM_Utils_Array::value("contact_{$type}_value", $this->_params) ||
+        CRM_Utils_Array::value("contact_{$type}_op", $this->_params) == 'nnll'
+      ) {
+        $sortnameFilters[$type] = 1;
+      }
+    }
+
     // 1. fill temp table with target results
     $this->select('target');
     $this->from('target');
     $this->customDataFrom();
-    $this->where('target');
+    if (!empty($sortnameFilters) && !array_key_exists('target', $sortnameFilters)
+    ) {
+      $this->_where = "WHERE FALSE";
+    }
+    else {
+      $this->where('target');
+    }
     $insertCols = implode(',', $this->_selectAliases);
     $tempQuery  = "CREATE TEMPORARY TABLE civireport_activity_temp_target CHARACTER SET utf8 COLLATE utf8_unicode_ci AS
 {$this->_select} {$this->_from} {$this->_where} ";
@@ -594,7 +589,13 @@ class CRM_Report_Form_Activity extends CRM_Report_Form {
       $this->select('assignee');
       $this->from('assignee');
       $this->customDataFrom();
-      $this->where('assignee');
+      if (!empty($sortnameFilters) && !array_key_exists('assignee', $sortnameFilters)
+      ) {
+        $this->_where = "WHERE FALSE";
+      }
+      else {
+        $this->where('assignee');
+      }
       $insertCols = implode(',', $this->_selectAliases);
       $tempQuery  = "INSERT INTO civireport_activity_temp_target ({$insertCols})
 {$this->_select}
@@ -609,7 +610,13 @@ class CRM_Report_Form_Activity extends CRM_Report_Form {
       $this->select('source');
       $this->from('source');
       $this->customDataFrom();
-      $this->where('source');
+      if (!empty($sortnameFilters) && !array_key_exists('source', $sortnameFilters)
+      ) {
+        $this->_where = "WHERE FALSE";
+      }
+      else {
+        $this->where('source');
+      }
       $insertCols = implode(',', $this->_selectAliases);
       $tempQuery  = "INSERT INTO civireport_activity_temp_target ({$insertCols})
 {$this->_select}
@@ -620,12 +627,10 @@ class CRM_Report_Form_Activity extends CRM_Report_Form {
     // 5. show final result set from temp table
     $rows = array();
     $this->select('final');
-    $this->where('final');
     $this->orderBy();
     $this->limit();
     $sql = "{$this->_select}
 FROM civireport_activity_temp_target tar
-{$this->_where}
 GROUP BY civicrm_activity_id {$this->_orderBy} {$this->_limit}";
     $this->buildRows($sql, $rows);