Add upgrade for label to name conversion
authorEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 9 Jun 2022 03:49:03 +0000 (15:49 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 9 Jun 2022 04:27:50 +0000 (16:27 +1200)
It wouldn't be that much more to go further & use the group.fieldname format - ....

Add upgrade script to convert saved field mappings to use names

CRM/Upgrade/Incremental/php/FiveFiftyOne.php

index 3d1a83f16b5328614c1e992b36a597b698d88eba..4f440dfb01d179de395ea42fe8a27d8226e13a7d 100644 (file)
@@ -198,6 +198,30 @@ class CRM_Upgrade_Incremental_php_FiveFiftyOne extends CRM_Upgrade_Incremental_B
       }
     }
 
+    // Multiple custom
+    $mappings = MappingField::get(FALSE)
+      ->setSelect(['id', 'name'])
+      ->addWhere('mapping_id.mapping_type_id:name', '=', 'Import Multi value custom data')
+      ->execute();
+    $allFields = civicrm_api3('custom_field', 'get', ['custom_group_id.is_multiple' => TRUE, 'return' => ['label', 'custom_group_id.title']])['values'];
+    $fieldMap = [];
+    foreach ($allFields as $field) {
+      $label = $field['label'] . ' :: ' . $field['custom_group_id.title'];
+      $fieldMap[$label] = 'custom_' . $field['id'];
+    }
+
+    $fieldMap[ts('- do not import -')] = 'do_not_import';
+    $fieldMap[ts('Contact ID')] = 'contact_id';
+    $fieldMap[ts('External Identifier')] = 'external_identifier';
+    foreach ($mappings as $mapping) {
+      if (!empty($fieldMap[$mapping['name']])) {
+        MappingField::update(FALSE)
+          ->addWhere('id', '=', $mapping['id'])
+          ->addValue('name', $fieldMap[$mapping['name']])
+          ->execute();
+      }
+    }
+
     return TRUE;
   }