Merge pull request #23561 from eileenmcnaughton/import_catcher_
[civicrm-core.git] / CRM / Upgrade / Incremental / php / FiveFiftyOne.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 use Civi\Api4\MappingField;
13
14 /**
15 * Upgrade logic for the 5.51.x series.
16 *
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.
21 *
22 * This class may also implement `setPreUpgradeMessage()` and `setPostUpgradeMessage()` functions.
23 */
24 class CRM_Upgrade_Incremental_php_FiveFiftyOne extends CRM_Upgrade_Incremental_Base {
25
26 /**
27 * Upgrade step; adds tasks including 'runSql'.
28 *
29 * @param string $rev
30 * The version number matching this function name
31 */
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);
35 }
36
37 /**
38 * Convert saved mapping fields for contribution imports to use name rather than
39 * label.
40 *
41 * Currently the 'name' column in civicrm_mapping_field holds names like
42 * 'First Name' or, more tragically 'Contact ID (match to contact)'.
43 *
44 * This updates them to hold the name - eg. 'total_amount'.
45 *
46 * @return bool
47 * @throws \API_Exception
48 */
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')
53 ->execute();
54 $fields = CRM_Contribute_BAO_Contribution::importableFields('All', FALSE);
55 $fieldMap = [];
56 foreach ($fields as $fieldName => $field) {
57 $fieldMap[$field['title']] = $fieldName;
58 }
59 $fieldMap[ts('Soft Credit')] = 'soft_credit';
60 $fieldMap[ts('Pledge Payment')] = 'pledge_payment';
61 $fieldMap[ts(ts('Pledge ID'))] = 'pledge_id';
62
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']])
68 ->execute();
69 }
70 }
71 return TRUE;
72 }
73
74 }