Commit | Line | Data |
---|---|---|
ec7389d3 C |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
ec7389d3 | 5 | | | |
bc77d7c0 TO |
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 | | |
ec7389d3 C |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | /** | |
13 | * Upgrade logic for FiveTwentyOne */ | |
14 | class CRM_Upgrade_Incremental_php_FiveTwentyOne extends CRM_Upgrade_Incremental_Base { | |
15 | ||
16 | /** | |
17 | * Compute any messages which should be displayed beforeupgrade. | |
18 | * | |
19 | * Note: This function is called iteratively for each upcoming | |
20 | * revision to the database. | |
21 | * | |
22 | * @param string $preUpgradeMessage | |
23 | * @param string $rev | |
24 | * a version number, e.g. '4.4.alpha1', '4.4.beta3', '4.4.0'. | |
25 | * @param null $currentVer | |
26 | */ | |
27 | public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) { | |
28 | // Example: Generate a pre-upgrade message. | |
29 | // if ($rev == '5.12.34') { | |
30 | // $preUpgradeMessage .= '<p>' . ts('A new permission, "%1", has been added. This permission is now used to control access to the Manage Tags screen.', array(1 => ts('manage tags'))) . '</p>'; | |
31 | // } | |
32 | } | |
33 | ||
34 | /** | |
35 | * Compute any messages which should be displayed after upgrade. | |
36 | * | |
37 | * @param string $postUpgradeMessage | |
38 | * alterable. | |
39 | * @param string $rev | |
40 | * an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs. | |
41 | */ | |
42 | public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) { | |
19952e08 SL |
43 | if ($rev == '5.21.alpha1') { |
44 | // Find any option groups that were not converted during the upgrade. | |
45 | $notConverted = []; | |
fe806431 | 46 | $optionGroups = \Civi\Api4\OptionGroup::get(FALSE)->execute(); |
19952e08 SL |
47 | foreach ($optionGroups as $optionGroup) { |
48 | $trimmedName = trim($optionGroup['name']); | |
49 | if (strpos($trimmedName, ' ') !== FALSE) { | |
50 | $notConverted[] = $optionGroup['title']; | |
51 | } | |
52 | } | |
53 | if (count($notConverted)) { | |
54 | $postUpgradeMessage .= '<br /><br />' . ts("The Following option Groups have not been converted due to there being already another option group with the same name in the database") . "<ul><li>" . implode('</li><li>', $notConverted) . "</li></ul>"; | |
55 | } | |
56 | } | |
ec7389d3 C |
57 | } |
58 | ||
59 | /* | |
60 | * Important! All upgrade functions MUST add a 'runSql' task. | |
61 | * Uncomment and use the following template for a new upgrade version | |
62 | * (change the x in the function name): | |
63 | */ | |
64 | ||
65 | // /** | |
66 | // * Upgrade function. | |
67 | // * | |
68 | // * @param string $rev | |
69 | // */ | |
70 | // public function upgrade_5_0_x($rev) { | |
71 | // $this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev); | |
72 | // $this->addTask('Do the foo change', 'taskFoo', ...); | |
73 | // // Additional tasks here... | |
74 | // // Note: do not use ts() in the addTask description because it adds unnecessary strings to transifex. | |
75 | // // The above is an exception because 'Upgrade DB to %1: SQL' is generic & reusable. | |
76 | // } | |
77 | ||
6ebc7a89 SL |
78 | /** |
79 | * Upgrade function. | |
80 | * | |
81 | * @param string $rev | |
82 | */ | |
83 | public function upgrade_5_21_alpha1($rev) { | |
84 | $this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev); | |
85 | $this->addTask('dev/core#1405 Fix option group names that contain spaces', 'fixOptionGroupName'); | |
86 | } | |
87 | ||
88 | public static function fixOptionGroupName() { | |
fe806431 | 89 | $optionGroups = \Civi\Api4\OptionGroup::get(FALSE) |
6ebc7a89 SL |
90 | ->execute(); |
91 | foreach ($optionGroups as $optionGroup) { | |
92 | $name = trim($optionGroup['name']); | |
93 | if (strpos($name, ' ') !== FALSE) { | |
94 | $fixedName = CRM_Utils_String::titleToVar(strtolower($name)); | |
19952e08 SL |
95 | $check = \Civi\Api4\OptionGroup::get() |
96 | ->addWhere('name', '=', $fixedName) | |
6ebc7a89 SL |
97 | ->setCheckPermissions(FALSE) |
98 | ->execute(); | |
19952e08 SL |
99 | // Fix hard fail in upgrade due to name already in database dev/core#1447 |
100 | if (!count($check)) { | |
101 | \Civi::log()->debug('5.21 Upgrade Option Group name ' . $name . ' converted to ' . $fixedName); | |
102 | \Civi\Api4\OptionGroup::update() | |
103 | ->addWhere('id', '=', $optionGroup['id']) | |
104 | ->addValue('name', $fixedName) | |
105 | ->setCheckPermissions(FALSE) | |
106 | ->execute(); | |
107 | } | |
6ebc7a89 SL |
108 | } |
109 | } | |
110 | return TRUE; | |
111 | } | |
ec7389d3 C |
112 | |
113 | } |