Extract field wrangling to determineReturnProperties
authoreileen <emcnaughton@wikimedia.org>
Mon, 8 Jul 2019 23:22:00 +0000 (11:22 +1200)
committereileen <emcnaughton@wikimedia.org>
Tue, 9 Jul 2019 01:58:07 +0000 (13:58 +1200)
This encases the wrangling of the field format to it's own function

CRM/Export/BAO/Export.php
CRM/Export/BAO/ExportProcessor.php

index 1320d24f6124b5c00aaec032388e350ed342c88a..b4a3c7631930e4a61846c97777b4aa23a75d33cf 100644 (file)
@@ -180,51 +180,7 @@ class CRM_Export_BAO_Export {
     );
 
     $processor = new CRM_Export_BAO_ExportProcessor($exportMode, $fields, $queryOperator, $mergeSameHousehold, $isPostalOnly);
-    $returnProperties = [];
-
-    if ($fields) {
-      foreach ($fields as $key => $value) {
-        $fieldName = CRM_Utils_Array::value(1, $value);
-        if (!$fieldName || $processor->isHouseholdMergeRelationshipTypeKey($fieldName)) {
-          continue;
-        }
-
-        if ($processor->isRelationshipTypeKey($fieldName) && (!empty($value[2]) || !empty($value[4]))) {
-          $returnProperties[$fieldName] = $processor->setRelationshipReturnProperties($value, $fieldName);
-        }
-        elseif (is_numeric(CRM_Utils_Array::value(2, $value))) {
-          $locationName = CRM_Core_PseudoConstant::getName('CRM_Core_BAO_Address', 'location_type_id', $value[2]);
-          if ($fieldName == 'phone') {
-            $returnProperties['location'][$locationName]['phone-' . CRM_Utils_Array::value(3, $value)] = 1;
-          }
-          elseif ($fieldName == 'im') {
-            $returnProperties['location'][$locationName]['im-' . CRM_Utils_Array::value(3, $value)] = 1;
-          }
-          else {
-            $returnProperties['location'][$locationName][$fieldName] = 1;
-          }
-        }
-        else {
-          //hack to fix component fields
-          //revert mix of event_id and title
-          if ($fieldName == 'event_id') {
-            $returnProperties['event_id'] = 1;
-          }
-          else {
-            $returnProperties[$fieldName] = 1;
-          }
-        }
-      }
-      $defaultExportMode = $processor->defaultReturnProperty();
-      if ($defaultExportMode) {
-        $returnProperties[$defaultExportMode] = 1;
-      }
-    }
-    else {
-      $returnProperties = $processor->getDefaultReturnProperties();
-    }
-    // @todo - we are working towards this being entirely a property of the processor
-    $processor->setReturnProperties($returnProperties);
+    $returnProperties = $processor->getReturnProperties();
     $paymentTableId = $processor->getPaymentTableID();
 
     if ($mergeSameAddress) {
index e83faf46731a4501054af557a68f6b889f5d64b4..b2707dea11f76f5d2b58eb979e509f6c276512ce 100644 (file)
@@ -184,7 +184,8 @@ class CRM_Export_BAO_ExportProcessor {
     $this->setRequestedFields($requestedFields);
     $this->setRelationshipTypes();
     $this->setIsMergeSameHousehold($isMergeSameHousehold);
-    $this->setisPostalableOnly($isPostalableOnly);
+    $this->setIsPostalableOnly($isPostalableOnly);
+    $this->setReturnProperties($this->determineReturnProperties());
   }
 
   /**
@@ -1439,4 +1440,55 @@ class CRM_Export_BAO_ExportProcessor {
     return $property;
   }
 
+  /**
+   * Determine the required return properties from the input parameters.
+   *
+   * @return array
+   */
+  public function determineReturnProperties() {
+    if ($this->getRequestedFields()) {
+      $returnProperties = [];
+      foreach ($this->getRequestedFields() as $key => $value) {
+        $fieldName = CRM_Utils_Array::value(1, $value);
+        if (!$fieldName || $this->isHouseholdMergeRelationshipTypeKey($fieldName)) {
+          continue;
+        }
+
+        if ($this->isRelationshipTypeKey($fieldName) && (!empty($value[2]) || !empty($value[4]))) {
+          $returnProperties[$fieldName] = $this->setRelationshipReturnProperties($value, $fieldName);
+        }
+        elseif (is_numeric(CRM_Utils_Array::value(2, $value))) {
+          $locationName = CRM_Core_PseudoConstant::getName('CRM_Core_BAO_Address', 'location_type_id', $value[2]);
+          if ($fieldName == 'phone') {
+            $returnProperties['location'][$locationName]['phone-' . CRM_Utils_Array::value(3, $value)] = 1;
+          }
+          elseif ($fieldName == 'im') {
+            $returnProperties['location'][$locationName]['im-' . CRM_Utils_Array::value(3, $value)] = 1;
+          }
+          else {
+            $returnProperties['location'][$locationName][$fieldName] = 1;
+          }
+        }
+        else {
+          //hack to fix component fields
+          //revert mix of event_id and title
+          if ($fieldName == 'event_id') {
+            $returnProperties['event_id'] = 1;
+          }
+          else {
+            $returnProperties[$fieldName] = 1;
+          }
+        }
+      }
+      $defaultExportMode = $this->defaultReturnProperty();
+      if ($defaultExportMode) {
+        $returnProperties[$defaultExportMode] = 1;
+      }
+    }
+    else {
+      $returnProperties = $this->getDefaultReturnProperties();
+    }
+    return $returnProperties;
+  }
+
 }