bulk comment fix
[civicrm-core.git] / CRM / Contribute / Selector / Search.php
index 5836363533b67e016068ac5f35031a7fcf36b38d..95adc2f4ea95dcab39a2a1561f566606377f2835 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$
  *
  */
@@ -83,6 +83,10 @@ class CRM_Contribute_Selector_Search extends CRM_Core_Selector_Base implements C
     'membership_id',
     'currency',
     'contribution_campaign_id',
+    'contribution_soft_credit_name',
+    'contribution_soft_credit_contact_id',
+    'contribution_soft_credit_amount',
+    'contribution_soft_credit_type',
   );
 
   /**
@@ -148,17 +152,22 @@ class CRM_Contribute_Selector_Search extends CRM_Core_Selector_Base implements C
    */
   protected $_query;
 
+  protected $_includeSoftCredits = FALSE;
+
   /**
    * Class constructor
    *
    * @param array $queryParams array of parameters for query
-   * @param int   $action - action of search basic or advanced.
-   * @param string   $contributionClause if the caller wants to further restrict the search (used in contributions)
+   * @param \const|int $action - action of search basic or advanced.
+   * @param string $contributionClause if the caller wants to further restrict the search (used in contributions)
    * @param boolean $single are we dealing only with one contact?
-   * @param int     $limit  how many contributions do we want returned
+   * @param int $limit how many contributions do we want returned
    *
-   * @return CRM_Contact_Selector
-   * @access public
+   * @param string $context
+   * @param null $compContext
+   *
+   * @return \CRM_Contribute_Selector_Search
+  @access public
    */
   function __construct(&$queryParams,
     $action             = CRM_Core_Action::NONE,
@@ -182,15 +191,23 @@ class CRM_Contribute_Selector_Search extends CRM_Core_Selector_Base implements C
     // type of selector
     $this->_action = $action;
 
-    $this->_query = new CRM_Contact_BAO_Query($this->_queryParams,
-      CRM_Contribute_BAO_Query::defaultReturnProperties(CRM_Contact_BAO_Query::MODE_CONTRIBUTE,
+    $this->_includeSoftCredits = CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($this->_queryParams);
+    $this->_query = new CRM_Contact_BAO_Query(
+      $this->_queryParams,
+      CRM_Contribute_BAO_Query::defaultReturnProperties(
+        CRM_Contact_BAO_Query::MODE_CONTRIBUTE,
         FALSE
       ),
       NULL, FALSE, FALSE,
       CRM_Contact_BAO_Query::MODE_CONTRIBUTE
     );
-    $this->_query->_distinctComponentClause = " civicrm_contribution.id";
-    $this->_query->_groupByComponentClause = " GROUP BY civicrm_contribution.id ";
+    if ($this->_includeSoftCredits) {
+      $this->_query->_rowCountClause = " count(civicrm_contribution.id)";
+      $this->_query->_groupByComponentClause = " GROUP BY contribution_search_scredit_combined.id, contribution_search_scredit_combined.contact_id, contribution_search_scredit_combined.scredit_id ";
+    } else {
+      $this->_query->_distinctComponentClause = " civicrm_contribution.id";
+      $this->_query->_groupByComponentClause = " GROUP BY civicrm_contribution.id ";
+    }
   }
   //end of constructor
 
@@ -201,9 +218,13 @@ class CRM_Contribute_Selector_Search extends CRM_Core_Selector_Base implements C
    * - View
    * - Edit
    *
+   * @param null $componentId
+   * @param null $componentAction
+   * @param null $key
+   * @param null $compContext
+   *
    * @return array
    * @access public
-   *
    */
   static function &links($componentId = NULL, $componentAction = NULL, $key = NULL, $compContext = NULL) {
     $extraParams = NULL;
@@ -246,7 +267,9 @@ class CRM_Contribute_Selector_Search extends CRM_Core_Selector_Base implements C
   /**
    * getter for array of the parameters required for creating pager.
    *
-   * @param
+   * @param $action
+   * @param $params
+   * @internal param $
    * @access public
    */
   function getPagerParams($action, &$params) {
@@ -293,6 +316,10 @@ class CRM_Contribute_Selector_Search extends CRM_Core_Selector_Base implements C
    * @return int   the total number of rows for this action
    */
   function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
+    if ($this->_includeSoftCredits) {
+      // especial sort order when rows include soft credits
+      $sort = "civicrm_contribution.receive_date DESC, civicrm_contribution.id, civicrm_contribution_soft.id";
+    }
     $result = $this->_query->searchQuery($offset, $rowCount, $sort,
       FALSE, FALSE,
       FALSE, FALSE,
@@ -424,57 +451,97 @@ class CRM_Contribute_Selector_Search extends CRM_Core_Selector_Base implements C
    * @access public
    */
   public function &getColumnHeaders($action = NULL, $output = NULL) {
-    if (!isset(self::$_columnHeaders)) {
-      self::$_columnHeaders = array(
-        array(
-          'name' => ts('Amount'),
-          'sort' => 'total_amount',
-          'direction' => CRM_Utils_Sort::DONTCARE,
-        ),
-        array('name' => ts('Type'),
-          'sort'      => 'financial_type_id',
-          'direction' => CRM_Utils_Sort::DONTCARE,
-        ),
-        array(
-          'name' => ts('Source'),
-          'sort' => 'contribution_source',
-          'direction' => CRM_Utils_Sort::DONTCARE,
-        ),
-        array(
-          'name' => ts('Received'),
-          'sort' => 'receive_date',
-          'direction' => CRM_Utils_Sort::DESCENDING,
-        ),
-        array(
-          'name' => ts('Thank-you Sent'),
-          'sort' => 'thankyou_date',
-          'direction' => CRM_Utils_Sort::DONTCARE,
-        ),
+    self::$_columnHeaders = array(
+      array(
+        'name' => $this->_includeSoftCredits ? ts('Contribution Amount') : ts('Amount'),
+        'sort' => 'total_amount',
+        'direction' => CRM_Utils_Sort::DONTCARE,
+      ),
+    );
+    if ($this->_includeSoftCredits) {
+      self::$_columnHeaders =
+        array_merge(
+          self::$_columnHeaders,
+          array(
+            array(
+              'name' => ts('Soft Credit Amount'),
+              'sort' => 'contribution_soft_credit_amount',
+              'direction' => CRM_Utils_Sort::DONTCARE,
+            )
+          )
+        );
+    }
+    self::$_columnHeaders =
+      array_merge(
+        self::$_columnHeaders,
         array(
-          'name' => ts('Status'),
-          'sort' => 'contribution_status_id',
-          'direction' => CRM_Utils_Sort::DONTCARE,
-        ),
+          array(
+            'name' => ts('Type'),
+            'sort'      => 'financial_type_id',
+            'direction' => CRM_Utils_Sort::DONTCARE,
+          ),
+          array(
+            'name' => ts('Source'),
+            'sort' => 'contribution_source',
+            'direction' => CRM_Utils_Sort::DONTCARE,
+          ),
+          array(
+            'name' => ts('Received'),
+            'sort' => 'receive_date',
+            'direction' => CRM_Utils_Sort::DESCENDING,
+          ),
+          array(
+            'name' => ts('Thank-you Sent'),
+            'sort' => 'thankyou_date',
+            'direction' => CRM_Utils_Sort::DONTCARE,
+          ),
+          array(
+            'name' => ts('Status'),
+            'sort' => 'contribution_status_id',
+            'direction' => CRM_Utils_Sort::DONTCARE,
+          ),
+          array(
+            'name' => ts('Premium'),
+            'sort' => 'product_name',
+            'direction' => CRM_Utils_Sort::DONTCARE,
+          ),
+        )
+      );
+    if (!$this->_single) {
+      $pre = array(
+        array('desc' => ts('Contact Type')),
         array(
-          'name' => ts('Premium'),
-          'sort' => 'product_name',
+          'name' => ts('Name'),
+          'sort' => 'sort_name',
           'direction' => CRM_Utils_Sort::DONTCARE,
         ),
-        array('desc' => ts('Actions')),
       );
-
-      if (!$this->_single) {
-        $pre = array(
-          array('desc' => ts('Contact Type')),
+      self::$_columnHeaders = array_merge($pre, self::$_columnHeaders);
+    }
+    if ($this->_includeSoftCredits) {
+      self::$_columnHeaders =
+        array_merge(
+          self::$_columnHeaders,
           array(
-            'name' => ts('Name'),
-            'sort' => 'sort_name',
-            'direction' => CRM_Utils_Sort::DONTCARE,
-          ),
+            array(
+              'name' => ts('Soft Credit For'),
+              'sort' => 'contribution_soft_credit_name',
+              'direction' => CRM_Utils_Sort::DONTCARE,
+            ),
+            array(
+              'name' => ts('Soft Credit Type'),
+              'sort' => 'contribution_soft_credit_type',
+              'direction' => CRM_Utils_Sort::ASCENDING,
+            ),
+          )
         );
-        self::$_columnHeaders = array_merge($pre, self::$_columnHeaders);
-      }
     }
+    self::$_columnHeaders =
+      array_merge(
+        self::$_columnHeaders, array(
+          array('desc' => ts('Actions'))
+        )
+      );
     return self::$_columnHeaders;
   }