Merge pull request #2853 from colemanw/js
[civicrm-core.git] / CRM / Contribute / Form / Task.php
index acb80d794ba29f9b4d9d2f686a3e563d021e1bad..0b547b8b688ca82aba91109ca9cc34c27b694173 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.4                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
@@ -74,6 +74,20 @@ class CRM_Contribute_Form_Task extends CRM_Core_Form {
    */
   public $_contactIds;
 
+  /**
+   * The array that holds all the mapping contribution and contact ids
+   *
+   * @var array
+   */
+  protected $_contributionContactIds = array();
+
+  /**
+   * The flag to tell if there are soft credits included
+   *
+   * @var boolean
+   */
+  public $_includesSoftCredits = FALSE;
+
   /**
    * build all the data structures needed to build the form
    *
@@ -110,15 +124,25 @@ class CRM_Contribute_Form_Task extends CRM_Core_Form {
         $sortOrder = $form->get( CRM_Utils_Sort::SORT_ORDER );
       }
 
+      $form->_includesSoftCredits = CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($queryParams);
       $query = new CRM_Contact_BAO_Query($queryParams, NULL, NULL, FALSE, FALSE,
         CRM_Contact_BAO_Query::MODE_CONTRIBUTE
       );
-      $query->_distinctComponentClause = ' civicrm_contribution.id';
-      $query->_groupByComponentClause = ' GROUP BY civicrm_contribution.id ';
-
+      if ($form->_includesSoftCredits) {
+        $contactIds = $contributionContactIds = array();
+        $query->_rowCountClause = " count(civicrm_contribution.id)";
+        $query->_groupByComponentClause = " GROUP BY contribution_search_scredit_combined.id, contribution_search_scredit_combined.contact_id, contribution_search_scredit_combined.scredit_id ";
+      } else {
+        $query->_distinctComponentClause = ' civicrm_contribution.id';
+        $query->_groupByComponentClause = ' GROUP BY civicrm_contribution.id ';
+      }
       $result = $query->searchQuery(0, 0, $sortOrder);
       while ($result->fetch()) {
         $ids[] = $result->contribution_id;
+        if ($form->_includesSoftCredits) {
+          $contactIds[$result->contact_id] = $result->contact_id;
+          $contributionContactIds["{$result->contact_id}-{$result->contribution_id}"] = $result->contribution_id;
+        }
       }
       $form->assign('totalSelectedContributions', $form->get('rowCount'));
     }
@@ -128,6 +152,10 @@ class CRM_Contribute_Form_Task extends CRM_Core_Form {
 
       $form->assign('totalSelectedContributions', count($ids));
     }
+    if ($form->_includesSoftCredits && !empty($contactIds)) {
+      $form->_contactIds = $contactIds;
+      $form->_contributionContactIds = $contributionContactIds;
+    }
 
     $form->_contributionIds = $form->_componentIds = $ids;
 
@@ -156,9 +184,12 @@ class CRM_Contribute_Form_Task extends CRM_Core_Form {
    * since its used for things like send email
    */
   public function setContactIDs() {
-    $this->_contactIds = &CRM_Core_DAO::getContactIDsFromComponent($this->_contributionIds,
-      'civicrm_contribution'
-    );
+    if (!$this->_includesSoftCredits) {
+      $this->_contactIds = &CRM_Core_DAO::getContactIDsFromComponent(
+        $this->_contributionIds,
+        'civicrm_contribution'
+      );
+    }
   }
 
   /**