addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev); $this->addTask(ts('Convert import mappings to use names'), 'convertMappingFieldLabelsToNames', $rev); } /** * Convert saved mapping fields for contribution imports to use name rather than * label. * * Currently the 'name' column in civicrm_mapping_field holds names like * 'First Name' or, more tragically 'Contact ID (match to contact)'. * * This updates them to hold the name - eg. 'total_amount'. * * @return bool * @throws \API_Exception */ public static function convertMappingFieldLabelsToNames(): bool { $mappings = MappingField::get(FALSE) ->setSelect(['id', 'name']) ->addWhere('mapping_id.mapping_type_id:name', '=', 'Import Contribution') ->execute(); $fields = CRM_Contribute_BAO_Contribution::importableFields('All', FALSE); $fieldMap = []; foreach ($fields as $fieldName => $field) { $fieldMap[$field['title']] = $fieldName; } $fieldMap[ts('Soft Credit')] = 'soft_credit'; $fieldMap[ts('Pledge Payment')] = 'pledge_payment'; $fieldMap[ts(ts('Pledge ID'))] = 'pledge_id'; foreach ($mappings as $mapping) { if (!empty($fieldMap[$mapping['name']])) { MappingField::update(FALSE) ->addWhere('id', '=', $mapping['id']) ->addValue('name', $fieldMap[$mapping['name']]) ->execute(); } } return TRUE; } }