Merge pull request #23689 from eileenmcnaughton/member_really_labels
[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 $this->addTask('Add column "civicrm_queue.status"', 'addColumn', 'civicrm_queue',
36 'status', "varchar(16) NULL DEFAULT 'active' COMMENT 'Execution status'");
37 $this->addTask('Add column "civicrm_queue.error"', 'addColumn', 'civicrm_queue',
38 'error', "varchar(16) NULL COMMENT 'Fallback behavior for unhandled errors'");
39 $this->addTask('Backfill "civicrm_queue.status" and "civicrm_queue.error")', 'fillQueueColumns');
40 }
41
42 public static function fillQueueColumns($ctx): bool {
43 // Generally, anything we do here is nonsensical because there shouldn't be much real world data,
44 // and the goal is to require something specific going forward (for anything that has an automatic runner).
45 // But this ensures that satisfy the invariant.
46 //
47 // What default value of "error" should apply to pre-existing queues (if they somehow exist)?
48 // Go back to our heuristic "short-term/finite queue <=> abort" vs "long-term/infinite queue <=> log".
49 // We don't have adequate data to differentiate these, so some will be wrong/suboptimal.
50 // What's the impact of getting it wrong?
51 // - For a finite/short-term queue, work has finished already (or will finish soon), so there is
52 // very limited impact to wrongly setting `error=delete`.
53 // - For an infinite/long-term queue, work will continue indefinitely into the future. The impact
54 // of wrongly setting `error=abort` would continue indefinitely to the future.
55 // Therefore, backfilling `error=log` is less-problematic than backfilling `error=abort`.
56 CRM_Core_DAO::executeQuery('UPDATE civicrm_queue SET error = "delete" WHERE runner IS NOT NULL AND error IS NULL');
57 CRM_Core_DAO::executeQuery('UPDATE civicrm_queue SET status = IF(runner IS NULL, NULL, "active")');
58 return TRUE;
59 }
60
61 /**
62 * Convert saved mapping fields for contribution imports to use name rather than
63 * label.
64 *
65 * Currently the 'name' column in civicrm_mapping_field holds names like
66 * 'First Name' or, more tragically 'Contact ID (match to contact)'.
67 *
68 * This updates them to hold the name - eg. 'total_amount'.
69 *
70 * @return bool
71 * @throws \API_Exception
72 */
73 public static function convertMappingFieldLabelsToNames(): bool {
74 // Contribution fields....
75 $mappings = MappingField::get(FALSE)
76 ->setSelect(['id', 'name'])
77 ->addWhere('mapping_id.mapping_type_id:name', '=', 'Import Contribution')
78 ->execute();
79 $fields = CRM_Contribute_BAO_Contribution::importableFields('All', FALSE);
80 $fieldMap = [];
81 foreach ($fields as $fieldName => $field) {
82 $fieldMap[$field['title']] = $fieldName;
83 if (!empty($field['html']['label'])) {
84 $fieldMap[$field['html']['label']] = $fieldName;
85 }
86 }
87 $fieldMap[ts('Soft Credit')] = 'soft_credit';
88 $fieldMap[ts('Pledge Payment')] = 'pledge_payment';
89 $fieldMap[ts(ts('Pledge ID'))] = 'pledge_id';
90 $fieldMap[ts(ts('Financial Type'))] = 'financial_type_id';
91 $fieldMap[ts(ts('Payment Method'))] = 'payment_instrument_id';
92 $fieldMap[ts('- do not import -')] = 'do_not_import';
93
94 // Membership fields
95 foreach ($mappings as $mapping) {
96 if (!empty($fieldMap[$mapping['name']])) {
97 MappingField::update(FALSE)
98 ->addWhere('id', '=', $mapping['id'])
99 ->addValue('name', $fieldMap[$mapping['name']])
100 ->execute();
101 }
102 }
103
104 // Membership fields...
105 // Yes - I know they could be combined - but it's also less confusing this way.
106 $mappings = MappingField::get(FALSE)
107 ->setSelect(['id', 'name'])
108 ->addWhere('mapping_id.mapping_type_id:name', '=', 'Import Membership')
109 ->execute();
110 $fields = CRM_Member_BAO_Membership::importableFields('All', FALSE);;
111 $fieldMap = [];
112 foreach ($fields as $fieldName => $field) {
113 $fieldMap[$field['title']] = $fieldName;
114 if (!empty($field['html']['label'])) {
115 $fieldMap[$field['html']['label']] = $fieldName;
116 }
117 }
118 $fieldMap[ts('- do not import -')] = 'do_not_import';
119
120 foreach ($mappings as $mapping) {
121 if (!empty($fieldMap[$mapping['name']])) {
122 MappingField::update(FALSE)
123 ->addWhere('id', '=', $mapping['id'])
124 ->addValue('name', $fieldMap[$mapping['name']])
125 ->execute();
126 }
127 }
128
129 return TRUE;
130 }
131
132 }