3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
12 use Civi\Api4\MappingField
;
15 * Upgrade logic for the 5.51.x series.
17 * Each minor version in the series is handled by either a `5.51.x.mysql.tpl` file,
18 * or a function in this class named `upgrade_5_51_x`.
19 * If only a .tpl file exists for a version, it will be run automatically.
20 * If the function exists, it must explicitly add the 'runSql' task if there is a corresponding .mysql.tpl.
22 * This class may also implement `setPreUpgradeMessage()` and `setPostUpgradeMessage()` functions.
24 class CRM_Upgrade_Incremental_php_FiveFiftyOne
extends CRM_Upgrade_Incremental_Base
{
27 * Upgrade step; adds tasks including 'runSql'.
30 * The version number matching this function name
32 public function upgrade_5_51_alpha1($rev): void
{
33 $this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev);
34 $this->addTask(ts('Convert import mappings to use names'), 'convertMappingFieldLabelsToNames', $rev);
38 * Convert saved mapping fields for contribution imports to use name rather than
41 * Currently the 'name' column in civicrm_mapping_field holds names like
42 * 'First Name' or, more tragically 'Contact ID (match to contact)'.
44 * This updates them to hold the name - eg. 'total_amount'.
47 * @throws \API_Exception
49 public static function convertMappingFieldLabelsToNames(): bool {
50 $mappings = MappingField
::get(FALSE)
51 ->setSelect(['id', 'name'])
52 ->addWhere('mapping_id.mapping_type_id:name', '=', 'Import Contribution')
54 $fields = CRM_Contribute_BAO_Contribution
::importableFields('All', FALSE);
56 foreach ($fields as $fieldName => $field) {
57 $fieldMap[$field['title']] = $fieldName;
59 $fieldMap[ts('Soft Credit')] = 'soft_credit';
60 $fieldMap[ts('Pledge Payment')] = 'pledge_payment';
61 $fieldMap[ts(ts('Pledge ID'))] = 'pledge_id';
63 foreach ($mappings as $mapping) {
64 if (!empty($fieldMap[$mapping['name']])) {
65 MappingField
::update(FALSE)
66 ->addWhere('id', '=', $mapping['id'])
67 ->addValue('name', $fieldMap[$mapping['name']])