-
Notifications
You must be signed in to change notification settings - Fork 5
/
commerce.install
318 lines (279 loc) · 10.8 KB
/
commerce.install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
/**
* Performs database updates and uninstallation cleanup for the Commerce module.
*/
/**
* Implements hook_install().
*/
function commerce_install() {
// Give commerce.module a higher weight than field.module so we can use
// hook_system_info_alter() to override the dependencies it adds.
$weight = db_select('system', 's')
->fields('s', array('weight'))
->condition('name', 'field', '=')
->execute()
->fetchField();
db_update('system')
->fields(array('weight' => $weight + 1))
->condition('name', 'commerce', '=')
->execute();
}
/**
* Implements hook_uninstall().
*/
function commerce_uninstall() {
config_clear('commerce.settings', 'commerce_default_currency');
config_clear('commerce.settings', 'commerce_enabled_currencies');
config_clear('commerce.settings', 'commerce_password_length');
}
/**
* Update Rules to use new prefixed parameter names and tokens.
*/
function commerce_update_7100() {
backdrop_flush_all_caches();
// Loop over all defined Rules configurations.
foreach (rules_config_load_multiple(FALSE) as $rule) {
$events = FALSE;
if ($rule instanceof RulesContainerPlugin) {
if ($rule instanceof RulesReactionRule) {
$events = $rule->events();
}
_commerce_update_rule_container_tokens($rule, $events);
}
else {
_commerce_update_rule_tokens($rule, $events);
}
}
return t('Rules updated to match new parameter names and token names.');
}
/**
* Iterates over a container's children to recursively find non-container
* plugins whose parameters should be updated.
*/
function _commerce_update_rule_container_tokens($rule, $events) {
foreach ($rule->getIterator() as $child) {
if ($child instanceof RulesContainerPlugin) {
if ($rule instanceof RulesReactionRule) {
$events = $rule->events();
}
_commerce_update_rule_container_tokens($child, $events);
}
else {
_commerce_update_rule_tokens($child, $events);
}
}
}
/**
* Given a Rule configuration, iterates over its settings to update parameter
* names to use the new prefixed names and parameter values to use the new
* prefixed tokens that match the new event variable names.
*/
function _commerce_update_rule_tokens($rule, $events) {
$save = FALSE;
// Determine if this rule configuration requires parameters.
$info = $rule->info();
if (!empty($info['parameter'])) {
// Get the map of old parameter names to new parameter names.
$map_parameters_old_new = _commerce_map_rules_parameters_old_new();
$map_parameters_new_old = array_flip($map_parameters_old_new);
// Iterate over the parameters of the configuration.
foreach ($info['parameter'] as $parameter_key => $parameter_info) {
// If the current parameter maps to an old parameter name...
if (!empty($map_parameters_new_old[$parameter_key])) {
$old_parameter = $map_parameters_new_old[$parameter_key];
// Loop over the settings array.
foreach ($rule->settings as $settings_key => $settings_value) {
// Get the parameter name of the setting.
$parts = explode(':', $settings_key, 2);
// If it equals the old parameter name our current parameter maps to...
if ($parts[0] == $old_parameter) {
// Recreate the setting with the new parameter name.
$new_settings_key = $parameter_key . ':' . $parts[1];
$rule->settings[$new_settings_key] = $settings_value;
// Unset the setting with the old parameter name.
unset($rule->settings[$settings_key]);
// Save the updated configuration.
$save = TRUE;
}
}
}
}
}
// If this plugin was ultimately derived from a reaction rule with a set of
// events...
if (!empty($events)) {
// Get the map of old data selector values to new data selector values.
$map_selector_tokens = _commerce_map_rules_selector_tokens();
$event_info = rules_fetch_data('event_info');
// Loop over each event that this plugin's parent item uses.
foreach ($events as $event) {
// If the event has variables...
if (!empty($event_info[$event]['variables'])) {
// Loop over the variables on the event looking for any new Commerce
// variable names...
foreach ($event_info[$event]['variables'] as $variable_name => $variable_info) {
// If the variable name matches one whose tokens need to be updated
// in this plugin...
if (in_array($variable_name, _commerce_event_entity_variables())) {
// Loop over this plugin's settings looking for data selectors.
foreach ($rule->settings as $settings_key => &$settings_value) {
if (substr($settings_key, -7) == ':select') {
// Break apart the selector value looking for tokens not using
// the prefixed name.
$parts = explode(':', $settings_value);
// Only check the first part of the data selector, as anything
// deeper is a property name that needn't be changed.
$parts[0] = strtr($parts[0], '_', '-');
// If the current part has been mapped to a new token name...
if (!empty($map_selector_tokens[$parts[0]])) {
// Replace it now and mark this rule for saving.
$parts[0] = $map_selector_tokens[$parts[0]];
$settings_value = implode(':', $parts);
$save = TRUE;
}
}
elseif (is_string($settings_value)) {
// Otherwise this setting's value might contain a token that
// needs to be updated.
$changed_tokens = array();
$value_tokens = array();
foreach (token_scan($settings_value) as $token_type => $tokens) {
$value_tokens += array_values($tokens);
}
if (!empty($value_tokens)) {
// Loop over the tokens we found in the value looking for any
// that need updating.
foreach ($value_tokens as $value_token) {
$parts = explode(':', trim($value_token, '[]'));
$changed = FALSE;
// Consider each part of the token in turn, attempting to
// translate the part to a new value.
foreach ($parts as $index => &$part) {
if (!empty($map_selector_tokens[strtr($part, '_', '-')])) {
$part = $map_selector_tokens[strtr($part, '_', '-')];
$changed = TRUE;
}
}
// Because a part of this token changed, add it to our array
// of all changed tokens.
if ($changed) {
$changed_tokens[$value_token] = '[' . implode(':', $parts) . ']';
}
}
}
// Translate the settings value overall with changed tokens.
if (!empty($changed_tokens)) {
$settings_value = strtr($settings_value, $changed_tokens);
$save = TRUE;
}
}
}
}
}
}
}
}
// Save the rule configuration now if specified.
if ($save) {
$rule->save();
}
}
/**
* Maps old action and condition parameter names to new prefixed names.
*/
function _commerce_map_rules_parameters_old_new() {
return array(
'order' => 'commerce_order',
'order_unchanged' => 'commerce_order_unchanged',
'product' => 'commerce_product',
'product_unchanged' => 'commerce_product_unchanged',
'line_item' => 'commerce_line_item',
'line_item_unchanged' => 'commerce_line_item_unchanged',
'customer_profile' => 'commerce_customer_profile',
'customer_profile_unchanged' => 'commerce_customer_profile_unchanged',
'payment_transaction' => 'commerce_payment_transaction',
'payment_transaction_unchanged' => 'commerce_payment_transaction_unchanged',
);
}
/**
* Returns an array of newly prefixed Commerce entity event variable names.
*/
function _commerce_event_entity_variables() {
return array(
'commerce_order',
'commerce_order_unchanged',
'commerce_product',
'commerce_product_unchanged',
'commerce_line_item',
'commerce_line_item_unchanged',
'commerce_customer_profile',
'commerce_customer_profile_unchanged',
'commerce_payment_transaction',
'commerce_payment_transaction_unchanged',
);
}
/**
* Maps old data selector values for event variables to new prefixed values.
*/
function _commerce_map_rules_selector_tokens() {
return array(
'order' => 'commerce-order',
'order-unchanged' => 'commerce-order-unchanged',
'product' => 'commerce-product',
'product-unchanged' => 'commerce-product-unchanged',
'line-item' => 'commerce-line-item',
'line-item-unchanged' => 'commerce-line-item-unchanged',
'customer' => 'commerce-customer-profile',
'customer-unchanged' => 'commerce-customer-unchanged',
'transaction' => 'commerce-payment-transaction',
'transaction-unchanged' => 'commerce-transaction-unchanged',
);
}
/**
* Utility function: rename a set of permissions.
*/
function commerce_update_rename_permissions($map) {
// Easy part: rename the permissions in {role_permission}.
foreach ($map as $old_name => $new_name) {
db_update('role_permission')
->fields(array('permission' => $new_name))
->condition('permission', $old_name)
->execute();
}
// Trickier: rename the permission for the in-database Views.
foreach (views_get_all_views() as $view) {
if ($view->type == t('Default')) {
continue;
}
$save_view = FALSE;
foreach ($view->display as $display_name => $display) {
if (!empty($display->display_options['access']['type']) && $display->display_options['access']['type'] == 'perm') {
$permission_name = $display->display_options['access']['perm'];
if (isset($map[$permission_name])) {
$display->display_options['access']['perm'] = $map[$permission_name];
$save_view = TRUE;
}
}
}
if ($save_view) {
$view->save();
}
}
}
/**
* Implements hook_update_last_removed().
*/
function commerce_update_last_removed() {
return 7102;
}
/**
* Implements hook_update_N().
*/
function commerce_update_1000() {
$config = config('commerce.settings');
$config->set('commerce_default_currency', update_variable_get('commerce_default_currency', 'USD'));
$config->set('commerce_enabled_currencies', update_variable_get('commerce_enabled_currencies', ['USD']));
$config->save();
update_variable_del('commerce_default_currency');
update_variable_del('commerce_enabled_currencies');
}