remove never-used option value
[civicrm-core.git] / CRM / Upgrade / Incremental / php / FiveTwentyThree.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 /**
13 * Upgrade logic for FiveTwentyThree */
14 class CRM_Upgrade_Incremental_php_FiveTwentyThree 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 if ($rev == '5.23.alpha1' && version_compare($currentVer, '4.7', '>=')) {
33 if ($this->hasConfigBackendData()) {
34 $preUpgradeMessage .= '<br/>' . ts("WARNING: The column \"<code>civicrm_domain.config_backend</code>\" is <a href='%2'>flagged for removal</a>. However, the upgrader has detected data in this copy of \"<code>civicrm_domain.config_backend</code>\". Please <a href='%1' target='_blank'>report</a> anything you can about the usage of this column. In the mean-time, the data will be preserved.", [
35 1 => 'https://civicrm.org/bug-reporting',
36 2 => 'https://lab.civicrm.org/dev/core/issues/1387',
37 ]);
38 }
39 }
40 }
41
42 /**
43 * Compute any messages which should be displayed after upgrade.
44 *
45 * @param string $postUpgradeMessage
46 * alterable.
47 * @param string $rev
48 * an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs.
49 */
50 public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
51 // Example: Generate a post-upgrade message.
52 // if ($rev == '5.12.34') {
53 // $postUpgradeMessage .= '<br /><br />' . ts("By default, CiviCRM now disables the ability to import directly from SQL. To use this feature, you must explicitly grant permission 'import SQL datasource'.");
54 // }
55 }
56
57 /*
58 * Important! All upgrade functions MUST add a 'runSql' task.
59 * Uncomment and use the following template for a new upgrade version
60 * (change the x in the function name):
61 */
62
63 // /**
64 // * Upgrade function.
65 // *
66 // * @param string $rev
67 // */
68 // public function upgrade_5_0_x($rev) {
69 // $this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev);
70 // $this->addTask('Do the foo change', 'taskFoo', ...);
71 // // Additional tasks here...
72 // // Note: do not use ts() in the addTask description because it adds unnecessary strings to transifex.
73 // // The above is an exception because 'Upgrade DB to %1: SQL' is generic & reusable.
74 // }
75
76 /**
77 * Upgrade function.
78 *
79 * @param string $rev
80 */
81 public function upgrade_5_23_alpha1($rev) {
82 $this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev);
83 $this->addTask('Remove Google + location option', 'removeGooglePlusOption');
84 $this->addTask('dev/mailing#59 Add in IMAP_XOAUTH2 protocol option for mailbox access', 'addXoauth2ProtocolOption');
85 $this->addTask('dev/translation#34 Fix contact-reference option for Postal Code', 'fixContactRefOptionPostalCode');
86
87 // (dev/core#1387) This column was dropped in 4.7.alpha1, but it was still created on new installs.
88 if (!$this->hasConfigBackendData()) {
89 $this->addTask('Drop column "civicrm_domain.config_backend"', 'dropColumn', 'civicrm_domain', 'config_backend');
90 }
91 }
92
93 /**
94 * Add in the IMAP XOAUTH2 mailing protocol option
95 */
96 public static function addXoauth2ProtocolOption(CRM_Queue_TaskContext $ctx) {
97 CRM_Core_BAO_OptionValue::ensureOptionValueExists([
98 'option_group_id' => 'mail_protocol',
99 'name' => 'IMAP_XOAUTH2',
100 'label' => 'IMAP XOAUTH2',
101 'is_active' => FALSE,
102 ]);
103 return TRUE;
104 }
105
106 /**
107 * Remove Google + option value option for website type
108 * only if there is no websites using it
109 */
110 public static function removeGooglePlusOption(CRM_Queue_TaskContext $ctx) {
111 $googlePlusValue = CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Website', 'website_type_id', 'Google_');
112 if ($googlePlusValue) {
113 $values = CRM_Core_DAO::executeQuery("SELECT * FROM civicrm_website WHERE website_type_id = %1", [1 => [$googlePlusValue, 'Positive']])->fetchAll();
114 if (empty($values)) {
115 $optionGroup = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_option_group WHERE name = 'website_type'");
116 \Civi\Api4\OptionValue::delete()
117 ->addWhere('value', '=', $googlePlusValue)
118 ->addWhere('option_group_id', '=', $optionGroup)
119 ->setCheckPermissions(FALSE)
120 ->execute();
121 }
122 }
123 return TRUE;
124 }
125
126 /**
127 * Fix the Contact Reference 'Postal Code' option.
128 */
129 public static function fixContactRefOptionPostalCode(CRM_Queue_TaskContext $ctx) {
130 $optionGroup = \Civi\Api4\OptionGroup::get()
131 ->setSelect(['id'])
132 ->addWhere('name', '=', 'contact_reference_options')
133 ->setCheckPermissions(FALSE)
134 ->execute()
135 ->first();
136
137 if (!$optionGroup) {
138 return TRUE;
139 }
140
141 $optionValue = \Civi\Api4\OptionValue::get()
142 ->setSelect(['id', 'name'])
143 ->addWhere('option_group_id', '=', $optionGroup['id'])
144 ->addWhere('label', '=', ts('Postal Code'))
145 ->setCheckPermissions(FALSE)
146 ->execute()
147 ->first();
148
149 if (!$optionValue || $optionValue['name'] == 'postal_code') {
150 return TRUE;
151 }
152
153 \Civi\Api4\OptionValue::update()
154 ->addWhere('id', '=', $optionValue['id'])
155 ->addValue('name', 'postal_code')
156 ->setCheckPermissions(FALSE)
157 ->execute();
158
159 return TRUE;
160 }
161
162 /**
163 * @return bool
164 */
165 private function hasConfigBackendData() {
166 return CRM_Core_BAO_SchemaHandler::checkIfFieldExists('civicrm_domain', 'config_backend')
167 && CRM_Core_DAO::singleValueQuery('SELECT count(*) c FROM `civicrm_domain` WHERE config_backend IS NOT NULL') > 0;
168 }
169
170 }