Drop Table before returning from function
[civicrm-core.git] / CRM / Report / Form.php
index 1ecc52d633ddb7a1344aa59441182d51dca32e9c..9149554ba9617e343bc87a34b98058deca83e349 100644 (file)
@@ -361,6 +361,12 @@ class CRM_Report_Form extends CRM_Core_Form {
   public $_orderBy = NULL;
   public $_orderByFields = array();
   public $_orderByArray = array();
+  /**
+   * Array of clauses to group by.
+   *
+   * @var array
+   */
+  protected $_groupByArray = array();
   public $_groupBy = NULL;
   public $_whereClauses = array();
   public $_havingClauses = array();
@@ -690,16 +696,7 @@ class CRM_Report_Form extends CRM_Core_Form {
     }
 
     foreach ($this->_columns as $tableName => $table) {
-      // set alias
-      if (!isset($table['alias'])) {
-        $this->_columns[$tableName]['alias'] = substr($tableName, 8) .
-          '_civireport';
-      }
-      else {
-        $this->_columns[$tableName]['alias'] = $table['alias'] . '_civireport';
-      }
-
-      $this->_aliases[$tableName] = $this->_columns[$tableName]['alias'];
+      $this->setTableAlias($table, $tableName);
 
       $expFields = array();
       // higher preference to bao object
@@ -714,7 +711,7 @@ class CRM_Report_Form extends CRM_Core_Form {
         }
       }
 
-      $doNotCopy = array('required');
+      $doNotCopy = array('required', 'default');
 
       $fieldGroups = array('fields', 'filters', 'group_bys', 'order_bys');
       foreach ($fieldGroups as $fieldGrp) {
@@ -2646,7 +2643,6 @@ WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends) . "') AND
    * Build group by clause.
    */
   public function groupBy() {
-    $groupBys = array();
     if (!empty($this->_params['group_bys']) &&
       is_array($this->_params['group_bys'])
     ) {
@@ -2654,15 +2650,15 @@ WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends) . "') AND
         if (array_key_exists('group_bys', $table)) {
           foreach ($table['group_bys'] as $fieldName => $field) {
             if (!empty($this->_params['group_bys'][$fieldName])) {
-              $groupBys[] = $field['dbAlias'];
+              $this->_groupByArray[] = $field['dbAlias'];
             }
           }
         }
       }
     }
 
-    if (!empty($groupBys)) {
-      $this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $groupBys);
+    if (!empty($this->_groupByArray)) {
+      $this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $this->_groupByArray);
     }
   }
 
@@ -3451,7 +3447,7 @@ WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends) . "') AND
     $smartGroups = array_keys($groups['values']);
 
     $query = "
-       SELECT group_contact.contact_id as id
+       SELECT DISTINCT group_contact.contact_id as id
        FROM civicrm_group_contact group_contact
        WHERE group_contact.group_id IN (" . implode(', ', $filteredGroups) . ")
        AND group_contact.status = 'Added' ";
@@ -4961,4 +4957,26 @@ LEFT JOIN civicrm_contact {$field['alias']} ON {$field['alias']}.id = {$this->_a
     return $select;
   }
 
+  /**
+   * Set table alias.
+   *
+   * @param array $table
+   * @param string $tableName
+   *
+   * @return string
+   *   Alias for table.
+   */
+  protected function setTableAlias($table, $tableName) {
+    if (!isset($table['alias'])) {
+      $this->_columns[$tableName]['alias'] = substr($tableName, 8) .
+        '_civireport';
+    }
+    else {
+      $this->_columns[$tableName]['alias'] = $table['alias'] . '_civireport';
+    }
+
+    $this->_aliases[$tableName] = $this->_columns[$tableName]['alias'];
+    return $this->_aliases[$tableName];
+  }
+
 }