[REF] Minor function extraction
authoreileen <emcnaughton@wikimedia.org>
Fri, 19 Mar 2021 01:32:32 +0000 (14:32 +1300)
committereileen <emcnaughton@wikimedia.org>
Fri, 19 Mar 2021 02:31:20 +0000 (15:31 +1300)
CRM/Contribute/Form/Task.php

index 60cc441184cf6533cd8ee7a2e26da8f600a520fa..79fea13c6c71787c22c9cc20a445e9d49a3dfa79 100644 (file)
@@ -42,6 +42,60 @@ class CRM_Contribute_Form_Task extends CRM_Core_Form_Task {
    */
   public $_includesSoftCredits = FALSE;
 
+  /**
+   * Get the results from the BAO_Query object based search.
+   *
+   * @return CRM_Core_DAO
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function getSearchQueryResults(): CRM_Core_DAO {
+    $form = $this;
+    $queryParams = $form->get('queryParams');
+    $isTest = FALSE;
+    if (is_array($queryParams)) {
+      foreach ($queryParams as $fields) {
+        if ($fields[0] === 'contribution_test') {
+          $isTest = TRUE;
+          break;
+        }
+      }
+    }
+    if (!$isTest) {
+      $queryParams[] = [
+        'contribution_test',
+        '=',
+        0,
+        0,
+        0,
+      ];
+    }
+    $returnProperties = ['contribution_id' => 1];
+    $sortOrder = $sortCol = NULL;
+    if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
+      $sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER);
+      //Include sort column in select clause.
+      $sortCol = trim(str_replace(['`', 'asc', 'desc'], '', $sortOrder));
+      $returnProperties[$sortCol] = 1;
+    }
+
+    $form->_includesSoftCredits = CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($queryParams);
+    $query = new CRM_Contact_BAO_Query($queryParams, $returnProperties, NULL, FALSE, FALSE,
+      CRM_Contact_BAO_Query::MODE_CONTRIBUTE
+    );
+    // @todo the function CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled should handle this
+    // can we remove? if not why not?
+    if ($form->_includesSoftCredits) {
+      $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 ';
+    }
+    return $query->searchQuery(0, 0, $sortOrder);
+  }
+
   /**
    * Build all the data structures needed to build the form.
    */
@@ -50,11 +104,11 @@ class CRM_Contribute_Form_Task extends CRM_Core_Form_Task {
   }
 
   /**
-   * @param \CRM_Core_Form_Task $form
+   * @param \CRM_Contribute_Form_Task $form
    *
    * @throws \CRM_Core_Exception
    */
-  public static function preProcessCommon(&$form) {
+  public static function preProcessCommon(&$form): void {
     $form->_contributionIds = [];
 
     $values = $form->getSearchFormValues();
@@ -63,50 +117,8 @@ class CRM_Contribute_Form_Task extends CRM_Core_Form_Task {
 
     $ids = $form->getSelectedIDs($values);
     if (!$ids) {
-      $queryParams = $form->get('queryParams');
-      $isTest = FALSE;
-      if (is_array($queryParams)) {
-        foreach ($queryParams as $fields) {
-          if ($fields[0] === 'contribution_test') {
-            $isTest = TRUE;
-            break;
-          }
-        }
-      }
-      if (!$isTest) {
-        $queryParams[] = [
-          'contribution_test',
-          '=',
-          0,
-          0,
-          0,
-        ];
-      }
-      $returnProperties = ['contribution_id' => 1];
-      $sortOrder = $sortCol = NULL;
-      if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
-        $sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER);
-        //Include sort column in select clause.
-        $sortCol = trim(str_replace(['`', 'asc', 'desc'], '', $sortOrder));
-        $returnProperties[$sortCol] = 1;
-      }
-
-      $form->_includesSoftCredits = CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($queryParams);
-      $query = new CRM_Contact_BAO_Query($queryParams, $returnProperties, NULL, FALSE, FALSE,
-        CRM_Contact_BAO_Query::MODE_CONTRIBUTE
-      );
-      // @todo the function CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled should handle this
-      // can we remove? if not why not?
-      if ($form->_includesSoftCredits) {
-        $contactIds = $contributionContactIds = [];
-        $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);
+      $result = $form->getSearchQueryResults();
+      $contributionContactIds = $contactIds = [];
       while ($result->fetch()) {
         $ids[] = $result->contribution_id;
         if ($form->_includesSoftCredits) {