Deprecate horrible function
authorEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 24 Aug 2022 05:19:07 +0000 (17:19 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 25 Aug 2022 23:39:11 +0000 (11:39 +1200)
CRM/Contact/Form/Search/Builder.php
CRM/Contribute/Import/Form/MapField.php
CRM/Core/BAO/Mapping.php

index 4046d48c65a280b63db0b9d5ba47dfc1194ec4ad..006f3d384a118f1b05d1308893c86b414422fc99 100644 (file)
@@ -687,7 +687,7 @@ class CRM_Contact_Form_Search_Builder extends CRM_Contact_Form_Search {
     if (isset($mappingId)) {
       list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingImProvider,
         $mappingRelation, $mappingOperator, $mappingValue
-        ) = CRM_Core_BAO_Mapping::getMappingFields($mappingId);
+        ) = $this->getMappingFields($mappingId);
 
       $blkCnt = count($mappingName);
       if ($blkCnt >= $blockCount) {
@@ -826,4 +826,81 @@ class CRM_Contact_Form_Search_Builder extends CRM_Contact_Form_Search {
     $form->setDefaultAction('refresh');
   }
 
+  /**
+   * Get the mapping fields.
+   *
+   * @param int $mappingId
+   *   Mapping id.
+   *
+   * @param bool $addPrimary
+   *   Add the key 'Primary' when the field is a location field AND there is
+   *   no location type (meaning Primary)?
+   *
+   * @return array
+   *   array of mapping fields
+   */
+  private function getMappingFields($mappingId, $addPrimary = FALSE) {
+    //mapping is to be loaded from database
+    $mapping = new CRM_Core_DAO_MappingField();
+    $mapping->mapping_id = $mappingId;
+    $mapping->orderBy('column_number');
+    $mapping->find();
+
+    $mappingName = $mappingLocation = $mappingContactType = $mappingPhoneType = [];
+    $mappingImProvider = $mappingRelation = $mappingOperator = $mappingValue = $mappingWebsiteType = [];
+    while ($mapping->fetch()) {
+      $mappingName[$mapping->grouping][$mapping->column_number] = $mapping->name;
+      $mappingContactType[$mapping->grouping][$mapping->column_number] = $mapping->contact_type;
+
+      if (!empty($mapping->location_type_id)) {
+        $mappingLocation[$mapping->grouping][$mapping->column_number] = $mapping->location_type_id;
+      }
+      elseif ($addPrimary) {
+        if (CRM_Contact_BAO_Contact::isFieldHasLocationType($mapping->name)) {
+          $mappingLocation[$mapping->grouping][$mapping->column_number] = ts('Primary');
+        }
+        else {
+          $mappingLocation[$mapping->grouping][$mapping->column_number] = NULL;
+        }
+      }
+
+      if (!empty($mapping->phone_type_id)) {
+        $mappingPhoneType[$mapping->grouping][$mapping->column_number] = $mapping->phone_type_id;
+      }
+
+      // get IM service provider type id from mapping fields
+      if (!empty($mapping->im_provider_id)) {
+        $mappingImProvider[$mapping->grouping][$mapping->column_number] = $mapping->im_provider_id;
+      }
+
+      if (!empty($mapping->website_type_id)) {
+        $mappingWebsiteType[$mapping->grouping][$mapping->column_number] = $mapping->website_type_id;
+      }
+
+      if (!empty($mapping->relationship_type_id)) {
+        $mappingRelation[$mapping->grouping][$mapping->column_number] = "{$mapping->relationship_type_id}_{$mapping->relationship_direction}";
+      }
+
+      if (!empty($mapping->operator)) {
+        $mappingOperator[$mapping->grouping][$mapping->column_number] = $mapping->operator;
+      }
+
+      if (isset($mapping->value)) {
+        $mappingValue[$mapping->grouping][$mapping->column_number] = $mapping->value;
+      }
+    }
+
+    return [
+      $mappingName,
+      $mappingContactType,
+      $mappingLocation,
+      $mappingPhoneType,
+      $mappingImProvider,
+      $mappingRelation,
+      $mappingOperator,
+      $mappingValue,
+      $mappingWebsiteType,
+    ];
+  }
+
 }
index d4b5a816227cfe9eed1fece07fc381835995e968..60bd25688cd00d9eb39226f094767bb04b76d83d 100644 (file)
@@ -174,14 +174,12 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField {
       $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', [1 => $i]), NULL);
       $jsSet = FALSE;
       if ($this->getSubmittedValue('savedMapping')) {
-        // $mappingContactType is not really a contact type - the data has been mangled
-        // into that field - see https://lab.civicrm.org/dev/core/-/issues/654
-        [$mappingName, $mappingContactType] = CRM_Core_BAO_Mapping::getMappingFields($savedMappingID);
         $fieldMapping = $fieldMappings[$i] ?? NULL;
-        $mappingContactType = $mappingContactType[1];
-        if (isset($fieldMappings[$i])) {
+        if ($fieldMapping) {
           if ($fieldMapping['name'] !== ts('do_not_import')) {
-            $softField = $mappingContactType[$i] ?? '';
+            // $mapping contact_type is not really a contact type - the data has been mangled
+            // into that field - see https://lab.civicrm.org/dev/core/-/issues/654
+            $softField = $fieldMapping['contact_type'] ?? '';
 
             if (!$softField) {
               $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
index 2878f9a7e38338399aa16fad9edee6e1d138d5d6..f85151dcba796f363d262d7887c7e04528dd0669 100644 (file)
@@ -150,8 +150,10 @@ class CRM_Core_BAO_Mapping extends CRM_Core_DAO_Mapping implements \Civi\Core\Ho
    *
    * @return array
    *   array of mapping fields
+   * @deprecated to be removed.
    */
   public static function getMappingFields($mappingId, $addPrimary = FALSE) {
+    CRM_Core_Error::deprecatedFunctionWarning('api');
     //mapping is to be loaded from database
     $mapping = new CRM_Core_DAO_MappingField();
     $mapping->mapping_id = $mappingId;