CRM-18439 - Report Fixes to include Full Group by clause
[civicrm-core.git] / CRM / Report / Form / Mailing / Bounce.php
index cf7f8ce1bf27cc3286344edcca242af227df7561..6aa4b8dd555ef99bc8a3b3177ccb4f5e79d19ab4 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
  */
 class CRM_Report_Form_Mailing_Bounce extends CRM_Report_Form {
 
@@ -137,6 +137,7 @@ class CRM_Report_Form_Mailing_Bounce extends CRM_Report_Form {
       'filters' => array(
         'bounce_reason' => array(
           'title' => ts('Bounce Reason'),
+          'type' => CRM_Utils_Type::T_STRING,
         ),
         'time_stamp' => array(
           'title' => ts('Bounce Date'),
@@ -188,7 +189,6 @@ class CRM_Report_Form_Mailing_Bounce extends CRM_Report_Form {
         'email' => array(
           'title' => ts('Email'),
           'no_repeat' => TRUE,
-          'required' => TRUE,
         ),
         'on_hold' => array(
           'title' => ts('On hold'),
@@ -268,6 +268,7 @@ class CRM_Report_Form_Mailing_Bounce extends CRM_Report_Form {
       $this->_columnHeaders["civicrm_mailing_bounce_count"]['title'] = ts('Bounce Count');
     }
 
+    $this->_selectClauses = $select;
     $this->_select = "SELECT " . implode(', ', $select) . " ";
   }
 
@@ -293,7 +294,7 @@ class CRM_Report_Form_Mailing_Bounce extends CRM_Report_Form {
     $this->_from .= "
         INNER JOIN civicrm_mailing_event_queue
           ON civicrm_mailing_event_queue.contact_id = {$this->_aliases['civicrm_contact']}.id
-        INNER JOIN civicrm_email {$this->_aliases['civicrm_email']}
+        LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
           ON civicrm_mailing_event_queue.email_id = {$this->_aliases['civicrm_email']}.id
         INNER JOIN civicrm_mailing_event_bounce {$this->_aliases['civicrm_mailing_event_bounce']}
           ON {$this->_aliases['civicrm_mailing_event_bounce']}.event_queue_id = civicrm_mailing_event_queue.id
@@ -358,11 +359,13 @@ class CRM_Report_Form_Mailing_Bounce extends CRM_Report_Form {
 
   public function groupBy() {
     if (!empty($this->_params['charts'])) {
-      $this->_groupBy = " GROUP BY {$this->_aliases['civicrm_mailing']}.id";
+      $groupBy = "{$this->_aliases['civicrm_mailing']}.id";
     }
     else {
-      $this->_groupBy = " GROUP BY {$this->_aliases['civicrm_mailing_event_bounce']}.id";
+      $groupBy = "{$this->_aliases['civicrm_mailing_event_bounce']}.id";
     }
+    $this->_groupBy = " GROUP BY {$groupBy}";
+    $this->_groupBy .= CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $groupBy);
   }
 
   public function postProcess() {
@@ -438,6 +441,15 @@ class CRM_Report_Form_Mailing_Bounce extends CRM_Report_Form {
 
     $entryFound = FALSE;
     foreach ($rows as $rowNum => $row) {
+
+      // If the email address has been deleted
+      if (array_key_exists('civicrm_email_email', $row)) {
+        if (empty($rows[$rowNum]['civicrm_email_email'])) {
+          $rows[$rowNum]['civicrm_email_email'] = '<del>Email address deleted</del>';
+        }
+        $entryFound = TRUE;
+      }
+
       // make count columns point to detail report
       // convert display name to links
       if (array_key_exists('civicrm_contact_sort_name', $row) &&