CRM-13380 : fix status and date filters for mailing search pages
authorPratik Joshi <pratik.joshi@webaccess.co.in>
Fri, 13 Sep 2013 11:25:17 +0000 (16:55 +0530)
committerPratik Joshi <pratik.joshi@webaccess.co.in>
Fri, 13 Sep 2013 11:25:17 +0000 (16:55 +0530)
----------------------------------------
* CRM-13380: 'Mailings' pages: search doesn't work correctly.
  http://issues.civicrm.org/jira/browse/CRM-13380

CRM/Mailing/Selector/Browse.php

index 5bd69c1ac0c466949234156b1961af57cc8f03ee..1a197cf48ca64f4bf9afb08eab7014895c9a8c53 100644 (file)
@@ -445,27 +445,57 @@ LEFT JOIN  civicrm_contact scheduledContact ON ( $mailing.scheduled_id = schedul
       }
     }
 
-
     $from = $this->_parent->get('mailing_from');
     if (!CRM_Utils_System::isNull($from)) {
-      $dateClause1[] = 'civicrm_mailing_job.start_date >= %2';
-      $dateClause2[] = 'civicrm_mailing_job.scheduled_date >= %2';
-      $params[2]     = array($from, 'String');
+      if ($this->_parent->get('unscheduled')) {
+        $dateClause1[] = 'civicrm_mailing.created_date >= %2';
+      }
+      else {
+        $dateClause1[] = 'civicrm_mailing_job.start_date >= %2';
+        $dateClause2[] = 'civicrm_mailing_job.scheduled_date >= %2';
+      }
+      $params[2] = array($from, 'String');
     }
 
     $to = $this->_parent->get('mailing_to');
     if (!CRM_Utils_System::isNull($to)) {
-      $dateClause1[] = 'civicrm_mailing_job.start_date <= %3';
-      $dateClause2[] = 'civicrm_mailing_job.scheduled_date <= %3';
+      if ($this->_parent->get('unscheduled')) {
+        $dateClause1[] = ' civicrm_mailing.created_date <= %3 ';
+      }
+      else {
+        $dateClause1[] = 'civicrm_mailing_job.start_date <= %3';
+        $dateClause2[] = 'civicrm_mailing_job.scheduled_date <= %3';
+      }
       $params[3]     = array($to, 'String');
     }
 
+    $addtlStatuses = $this->_parent->get('archived') ? ", 'Canceled'" : '';
     if (!empty($dateClause1)) {
-      $dateClause1[] = "civicrm_mailing_job.status IN ('Complete', 'Running')";
-      $dateClause2[] = "civicrm_mailing_job.status IN ('Scheduled')";
-      $dateClause1   = implode(' AND ', $dateClause1);
-      $dateClause2   = implode(' AND ', $dateClause2);
-      $clauses[]     = "( ({$dateClause1}) OR ({$dateClause2}) )";
+      if (!$this->_parent->get('unscheduled')) {
+        $dateClause1[] = "civicrm_mailing_job.status IN ('Complete', 'Running' $addtlStatuses)";
+        $dateClause2[] = "civicrm_mailing_job.status IN ('Scheduled' $addtlStatuses)";
+      }
+
+      if (!empty($dateClause1)) {
+        $dateClause1   = implode(' AND ', $dateClause1);
+      }
+      if (!empty($dateClause2)) {
+        $dateClause2   = implode(' AND ', $dateClause2);
+      }
+
+      $dateC = NULL;
+      if (!empty($dateClause1)) {
+        $dateC = " ({$dateClause1}) ";
+      }
+      if (!empty($dateClause2)) {
+        if ($dateC) {
+          $dateC .= " OR ({$dateClause2}) ";
+        }
+        else {
+          $dateC = " ({$dateClause2}) ";
+        }
+      }
+      $clauses[] = " ({$dateC}) ";
     }
 
     if ($this->_parent->get('unscheduled')) {
@@ -473,11 +503,6 @@ LEFT JOIN  civicrm_contact scheduledContact ON ( $mailing.scheduled_id = schedul
       $clauses[] = "civicrm_mailing.scheduled_id IS NULL";
     }
 
-    if ($this->_parent->get('archived')) {
-      // CRM-6446: archived view should also show cancelled mailings
-      $clauses[] = "(civicrm_mailing.is_archived = 1 OR civicrm_mailing_job.status = 'Canceled')";
-    }
-
     if ($this->_parent->get('sms')) {
       $clauses[] = "(civicrm_mailing.sms_provider_id IS NOT NULL)";
     }
@@ -490,17 +515,24 @@ LEFT JOIN  civicrm_contact scheduledContact ON ( $mailing.scheduled_id = schedul
     if ($this->_parent->get('scheduled')) {
       $clauses[] = "civicrm_mailing.scheduled_id IS NOT NULL";
       $clauses[] = "( civicrm_mailing.is_archived IS NULL OR civicrm_mailing.is_archived = 0 )";
+    }
+    if (!$this->_parent->get('unscheduled')) {
       $status    = $this->_parent->get('mailing_status');
       if (!empty($status)) {
         $status    = array_keys($status);
         $status    = implode("','", $status);
         $clauses[] = "civicrm_mailing_job.status IN ('$status')";
       }
-      else {
+      elseif ($this->_parent->get('scheduled')) {
         $clauses[] = "civicrm_mailing_job.status IN ('Scheduled', 'Complete', 'Running')";
       }
     }
 
+    if ($this->_parent->get('archived')) {
+      // CRM-6446: archived view should also show cancelled mailings
+      $clauses[] = "(civicrm_mailing.is_archived = 1 OR civicrm_mailing_job.status = 'Canceled')";
+    }
+
     if ($sortBy &&
       $this->_parent->_sortByCharacter !== NULL
     ) {