Refactor out paymentFields
authoreileen <emcnaughton@wikimedia.org>
Wed, 18 Jul 2018 09:20:27 +0000 (21:20 +1200)
committereileen <emcnaughton@wikimedia.org>
Fri, 20 Jul 2018 21:37:16 +0000 (09:37 +1200)
Code tidy up on payent fields var

CRM/Export/BAO/Export.php
CRM/Export/BAO/ExportProcessor.php
tests/phpunit/CRM/Export/BAO/ExportTest.php

index f6f655a9f028035c953f3e7429b3b4aaf5823bf7..96615a9da980cc827d7999a72aa35abfef2255b4 100644 (file)
@@ -267,31 +267,25 @@ class CRM_Export_BAO_Export {
   public static function defineExtraProperties($queryMode) {
     switch ($queryMode) {
       case CRM_Contact_BAO_Query::MODE_EVENT:
-        $paymentFields = TRUE;
         $paymentTableId = 'participant_id';
         break;
 
       case CRM_Contact_BAO_Query::MODE_MEMBER:
-        $paymentFields = TRUE;
         $paymentTableId = 'membership_id';
         break;
 
       case CRM_Contact_BAO_Query::MODE_PLEDGE:
-        $paymentFields = TRUE;
         $paymentTableId = 'pledge_payment_id';
         break;
 
       case CRM_Contact_BAO_Query::MODE_CASE:
-        $paymentFields = FALSE;
         $paymentTableId = '';
         break;
 
       default:
-        $paymentFields = FALSE;
         $paymentTableId = '';
     }
     $extraProperties = array(
-      'paymentFields' => $paymentFields,
       'paymentTableId' => $paymentTableId,
     );
     return $extraProperties;
@@ -347,9 +341,9 @@ class CRM_Export_BAO_Export {
     $queryOperator = 'AND'
   ) {
 
-    $processor = new CRM_Export_BAO_ExportProcessor($exportMode, $queryOperator);
+    $processor = new CRM_Export_BAO_ExportProcessor($exportMode, $fields, $queryOperator);
     $returnProperties = array();
-    $paymentFields = $selectedPaymentFields = $paymentTableId = FALSE;
+    $selectedPaymentFields = FALSE;
 
     $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id');
     // Warning - this imProviders var is used in a somewhat fragile way - don't rename it
@@ -448,7 +442,6 @@ class CRM_Export_BAO_Export {
       }
 
       $extraProperties = self::defineExtraProperties($queryMode);
-      $paymentFields = $extraProperties['paymentFields'];
       $paymentTableId = $extraProperties['paymentTableId'];
 
       $returnProperties = array_merge($returnProperties, $processor->getAdditionalReturnProperties());
@@ -618,7 +611,7 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c
     $addPaymentHeader = FALSE;
 
     $paymentDetails = array();
-    if ($paymentFields || $selectedPaymentFields) {
+    if ($processor->isExportPaymentFields() || $selectedPaymentFields) {
 
       // get payment related in for event and members
       $paymentDetails = CRM_Contribute_BAO_Contribution::getContributionDetails($exportMode, $ids);
@@ -709,7 +702,7 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c
         }
 
         // add payment headers if required
-        if ($addPaymentHeader && $paymentFields) {
+        if ($addPaymentHeader && $processor->isExportPaymentFields()) {
           // @todo rather than do this for every single row do it before the loop starts.
           // where other header definitions take place.
           $headerRows = array_merge($headerRows, $paymentHeaders);
@@ -730,7 +723,7 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c
         // information, if appropriate.
         if ($addPaymentHeader) {
           if (!$selectedPaymentFields) {
-            if ($paymentFields) {
+            if ($processor->isExportPaymentFields()) {
               $paymentData = CRM_Utils_Array::value($row[$paymentTableId], $paymentDetails);
               if (!is_array($paymentData) || empty($paymentData)) {
                 $paymentData = $nullContributionDetails;
index fa8df0352a8094f9378716d7e757a52039eb733f..e5d21a9177f633aa66d64397b7fe012cbc001559 100644 (file)
@@ -62,16 +62,42 @@ class CRM_Export_BAO_ExportProcessor {
    */
   protected $queryOperator;
 
+  /**
+   * Requested output fields.
+   *
+   * If set to NULL then it is 'primary fields only'
+   * which actually means pretty close to all fields!
+   *
+   * @var array|null
+   */
+  protected $requestedFields;
+
   /**
    * CRM_Export_BAO_ExportProcessor constructor.
    *
    * @param int $exportMode
+   * @param array|NULL $requestedFields
    * @param string $queryOperator
    */
-  public function __construct($exportMode, $queryOperator) {
+  public function __construct($exportMode, $requestedFields, $queryOperator) {
     $this->setExportMode($exportMode);
     $this->setQueryMode();
     $this->setQueryOperator($queryOperator);
+    $this->setRequestedFields($requestedFields);
+  }
+
+  /**
+   * @return array|null
+   */
+  public function getRequestedFields() {
+    return $this->requestedFields;
+  }
+
+  /**
+   * @param array|null $requestedFields
+   */
+  public function setRequestedFields($requestedFields) {
+    $this->requestedFields = $requestedFields;
   }
 
   /**
@@ -218,4 +244,61 @@ class CRM_Export_BAO_ExportProcessor {
     return array_merge(array_fill_keys($missing, 1), $componentSpecificFields);
   }
 
+  /**
+   * Should payment fields be appended to the export.
+   *
+   * (This is pretty hacky so hopefully this function won't last long - notice
+   * how obviously it should be part of the above function!).
+   */
+  public function isExportPaymentFields() {
+    if ($this->getRequestedFields() === NULL
+      &&  in_array($this->getExportMode(), [
+        CRM_Contact_BAO_Query::MODE_EVENT,
+        CRM_Contact_BAO_Query::MODE_MEMBER,
+        CRM_Contact_BAO_Query::MODE_PLEDGE,
+      ])) {
+      return TRUE;
+    }
+    return FALSE;
+  }
+
+  /**
+   * Get the name of the id field in the table that connects contributions to the export entity.
+   */
+  public function getPaymentTableID() {
+    if ($this->getRequestedFields() === NULL) {
+      $mapping = [
+        CRM_Contact_BAO_Query::MODE_EVENT => 'participant_id',
+        CRM_Contact_BAO_Query::MODE_MEMBER => 'membership_id',
+        CRM_Contact_BAO_Query::MODE_PLEDGE => 'pledge_payment_id',
+      ];
+      return isset($mapping[$this->getQueryMode()]) ? $mapping[$this->getQueryMode()] : '';
+    }
+    return FALSE;
+  }
+
+  /**
+   * Get the default properties when not specified.
+   *
+   * In the UI this appears as 'Primary fields only' but in practice it's
+   * most of the kitchen sink and the hallway closet thrown in.
+   *
+   * Since CRM-952 custom fields are excluded, but no other form of mercy is shown.
+   *
+   * @return array
+   */
+  public function getDefaultReturnProperties() {
+    $returnProperties = [];
+    $fields = CRM_Contact_BAO_Contact::exportableFields('All', TRUE, TRUE);
+    $skippedFields = ($this->getQueryMode() === CRM_Contact_BAO_Query::MODE_CONTACTS) ? [] : ['groups', 'tags', 'notes'];
+
+    foreach ($fields as $key => $var) {
+      if ($key && (substr($key, 0, 6) != 'custom') && !in_array($key, $skippedFields)) {
+        $returnProperties[$key] = 1;
+      }
+    }
+    $returnProperties = array_merge($returnProperties, $this->getAdditionalReturnProperties());
+    return $returnProperties;
+  }
+
 }
index 2fc5a0f4056075af602110ce6165d72de90bcd40..3ec50e160a04d3057b7d599d15a010e5a123fc7e 100644 (file)
@@ -209,7 +209,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase {
     $pattern = '/as `?([^`,]*)/';
     $queryFieldAliases = array();
     preg_match_all($pattern, $select, $queryFieldAliases, PREG_PATTERN_ORDER);
-    $processor = new CRM_Export_BAO_ExportProcessor(CRM_Contact_BAO_Query::MODE_CONTRIBUTE, 'AND');
+    $processor = new CRM_Export_BAO_ExportProcessor(CRM_Contact_BAO_Query::MODE_CONTRIBUTE, NULL, 'AND');
     $processor->setQueryFields($query->_fields);
 
     list($outputFields) = CRM_Export_BAO_Export::getExportStructureArrays($returnProperties, $processor, $contactRelationshipTypes, '');