Projet

Général

Profil

Paste
Télécharger (11 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / commerce / commerce.install @ a5572547

1
<?php
2

    
3
/**
4
 * Performs database updates and uninstallation cleanup for the Commerce module.
5
 */
6

    
7

    
8
/**
9
 * Implements hook_install().
10
 */
11
function commerce_install() {
12
  // Give commerce.module a higher weight than field.module so we can use
13
  // hook_system_info_alter() to override the dependencies it adds.
14
  $weight = db_select('system', 's')
15
              ->fields('s', array('weight'))
16
              ->condition('name', 'field', '=')
17
              ->execute()
18
              ->fetchField();
19
  db_update('system')
20
    ->fields(array('weight' => $weight + 1))
21
    ->condition('name', 'commerce', '=')
22
    ->execute();
23
}
24

    
25
/**
26
 * Implements hook_uninstall().
27
 */
28
function commerce_uninstall() {
29
  variable_del('commerce_default_currency');
30
  variable_del('commerce_enabled_currencies');
31
  variable_del('commerce_password_length');
32
}
33

    
34
/**
35
 * Update Rules to use new prefixed parameter names and tokens.
36
 */
37
function commerce_update_7100() {
38
  drupal_flush_all_caches();
39

    
40
  // Loop over all defined Rules configurations.
41
  foreach (rules_config_load_multiple(FALSE) as $rule) {
42
    $events = FALSE;
43

    
44
    if ($rule instanceof RulesContainerPlugin) {
45
      if ($rule instanceof RulesReactionRule) {
46
        $events = $rule->events();
47
      }
48

    
49
      _commerce_update_rule_container_tokens($rule, $events);
50
    }
51
    else {
52
      _commerce_update_rule_tokens($rule, $events);
53
    }
54
  }
55

    
56
  return t('Rules updated to match new parameter names and token names.');
57
}
58

    
59
/**
60
 * Iterates over a container's children to recursively find non-container
61
 * plugins whose parameters should be updated.
62
 */
63
function _commerce_update_rule_container_tokens($rule, $events) {
64
  foreach ($rule->getIterator() as $child) {
65
    if ($child instanceof RulesContainerPlugin) {
66
      if ($rule instanceof RulesReactionRule) {
67
        $events = $rule->events();
68
      }
69

    
70
      _commerce_update_rule_container_tokens($child, $events);
71
    }
72
    else {
73
      _commerce_update_rule_tokens($child, $events);
74
    }
75
  }
76
}
77

    
78
/**
79
 * Given a Rule configuration, iterates over its settings to update parameter
80
 * names to use the new prefixed names and parameter values to use the new
81
 * prefixed tokens that match the new event variable names.
82
 */
83
function _commerce_update_rule_tokens($rule, $events) {
84
  $save = FALSE;
85

    
86
  // Determine if this rule configuration requires parameters.
87
  $info = $rule->info();
88

    
89
  if (!empty($info['parameter'])) {
90
    // Get the map of old parameter names to new parameter names.
91
    $map_parameters_old_new = _commerce_map_rules_parameters_old_new();
92
    $map_parameters_new_old = array_flip($map_parameters_old_new);
93

    
94
    // Iterate over the parameters of the configuration.
95
    foreach ($info['parameter'] as $parameter_key => $parameter_info) {
96
      // If the current parameter maps to an old parameter name...
97
      if (!empty($map_parameters_new_old[$parameter_key])) {
98
        $old_parameter = $map_parameters_new_old[$parameter_key];
99

    
100
        // Loop over the settings array.
101
        foreach ($rule->settings as $settings_key => $settings_value) {
102
          // Get the parameter name of the setting.
103
          $parts = explode(':', $settings_key, 2);
104

    
105
          // If it equals the old parameter name our current parameter maps to...
106
          if ($parts[0] == $old_parameter) {
107
            // Recreate the setting with the new parameter name.
108
            $new_settings_key = $parameter_key . ':' . $parts[1];
109
            $rule->settings[$new_settings_key] = $settings_value;
110

    
111
            // Unset the setting with the old parameter name.
112
            unset($rule->settings[$settings_key]);
113

    
114
            // Save the updated configuration.
115
            $save = TRUE;
116
          }
117
        }
118
      }
119
    }
120
  }
121

    
122
  // If this plugin was ultimately derived from a reaction rule with a set of
123
  // events...
124
  if (!empty($events)) {
125
    // Get the map of old data selector values to new data selector values.
126
    $map_selector_tokens = _commerce_map_rules_selector_tokens();
127

    
128
    $event_info = rules_fetch_data('event_info');
129

    
130
    // Loop over each event that this plugin's parent item uses.
131
    foreach ($events as $event) {
132
      // If the event has variables...
133
      if (!empty($event_info[$event]['variables'])) {
134
        // Loop over the variables on the event looking for any new Commerce
135
        // variable names...
136
        foreach ($event_info[$event]['variables'] as $variable_name => $variable_info) {
137
          // If the variable name matches one whose tokens need to be updated
138
          // in this plugin...
139
          if (in_array($variable_name, _commerce_event_entity_variables())) {
140
            // Loop over this plugin's settings looking for data selectors.
141
            foreach ($rule->settings as $settings_key => &$settings_value) {
142
              if (substr($settings_key, -7) == ':select') {
143
                // Break apart the selector value looking for tokens not using
144
                // the prefixed name.
145
                $parts = explode(':', $settings_value);
146

    
147
                // Only check the first part of the data selector, as anything
148
                // deeper is a property name that needn't be changed.
149
                $parts[0] = strtr($parts[0], '_', '-');
150

    
151
                // If the current part has been mapped to a new token name...
152
                if (!empty($map_selector_tokens[$parts[0]])) {
153
                  // Replace it now and mark this rule for saving.
154
                  $parts[0] = $map_selector_tokens[$parts[0]];
155
                  $settings_value = implode(':', $parts);
156
                  $save = TRUE;
157
                }
158
              }
159
              elseif (is_string($settings_value)) {
160
                // Otherwise this setting's value might contain a token that
161
                // needs to be updated.
162
                $changed_tokens = array();
163
                $value_tokens = array();
164

    
165
                foreach (token_scan($settings_value) as $token_type => $tokens) {
166
                  $value_tokens += array_values($tokens);
167
                }
168

    
169
                if (!empty($value_tokens)) {
170
                  // Loop over the tokens we found in the value looking for any
171
                  // that need updating.
172
                  foreach ($value_tokens as $value_token) {
173
                    $parts = explode(':', trim($value_token, '[]'));
174
                    $changed = FALSE;
175

    
176
                    // Consider each part of the token in turn, attempting to
177
                    // translate the part to a new value.
178
                    foreach ($parts as $index => &$part) {
179
                      if (!empty($map_selector_tokens[strtr($part, '_', '-')])) {
180
                        $part = $map_selector_tokens[strtr($part, '_', '-')];
181
                        $changed = TRUE;
182
                      }
183
                    }
184

    
185
                    // Because a part of this token changed, add it to our array
186
                    // of all changed tokens.
187
                    if ($changed) {
188
                      $changed_tokens[$value_token] = '[' . implode(':', $parts) . ']';
189
                    }
190
                  }
191
                }
192

    
193
                // Translate the settings value overall with changed tokens.
194
                if (!empty($changed_tokens)) {
195
                  $settings_value = strtr($settings_value, $changed_tokens);
196
                  $save = TRUE;
197
                }
198
              }
199
            }
200
          }
201
        }
202
      }
203
    }
204
  }
205

    
206
  // Save the rule configuration now if specified.
207
  if ($save) {
208
    $rule->save();
209
  }
210
}
211

    
212
/**
213
 * Maps old action and condition parameter names to new prefixed names.
214
 */
215
function _commerce_map_rules_parameters_old_new() {
216
  return array(
217
    'order' => 'commerce_order',
218
    'order_unchanged' => 'commerce_order_unchanged',
219
    'product' => 'commerce_product',
220
    'product_unchanged' => 'commerce_product_unchanged',
221
    'line_item' => 'commerce_line_item',
222
    'line_item_unchanged' => 'commerce_line_item_unchanged',
223
    'customer_profile' => 'commerce_customer_profile',
224
    'customer_profile_unchanged' => 'commerce_customer_profile_unchanged',
225
    'payment_transaction' => 'commerce_payment_transaction',
226
    'payment_transaction_unchanged' => 'commerce_payment_transaction_unchanged',
227
  );
228
}
229

    
230
/**
231
 * Returns an array of newly prefixed Commerce entity event variable names.
232
 */
233
function _commerce_event_entity_variables() {
234
  return array(
235
    'commerce_order',
236
    'commerce_order_unchanged',
237
    'commerce_product',
238
    'commerce_product_unchanged',
239
    'commerce_line_item',
240
    'commerce_line_item_unchanged',
241
    'commerce_customer_profile',
242
    'commerce_customer_profile_unchanged',
243
    'commerce_payment_transaction',
244
    'commerce_payment_transaction_unchanged',
245
  );
246
}
247

    
248
/**
249
 * Maps old data selector values for event variables to new prefixed values.
250
 */
251
function _commerce_map_rules_selector_tokens() {
252
  return array(
253
    'order' => 'commerce-order',
254
    'order-unchanged' => 'commerce-order-unchanged',
255
    'product' => 'commerce-product',
256
    'product-unchanged' => 'commerce-product-unchanged',
257
    'line-item' => 'commerce-line-item',
258
    'line-item-unchanged' => 'commerce-line-item-unchanged',
259
    'customer' => 'commerce-customer-profile',
260
    'customer-unchanged' => 'commerce-customer-unchanged',
261
    'transaction' => 'commerce-payment-transaction',
262
    'transaction-unchanged' => 'commerce-transaction-unchanged',
263
  );
264
}
265

    
266
/**
267
 * Utility function: rename a set of permissions.
268
 */
269
function commerce_update_rename_permissions($map) {
270
  // Easy part: rename the permissions in {role_permission}.
271
  foreach ($map as $old_name => $new_name) {
272
    db_update('role_permission')
273
      ->fields(array('permission' => $new_name))
274
      ->condition('permission', $old_name)
275
      ->execute();
276
  }
277

    
278
  // Trickier: rename the permission for the in-database Views.
279
  foreach (views_get_all_views() as $view) {
280
    if ($view->type == t('Default')) {
281
      continue;
282
    }
283

    
284
    $save_view = FALSE;
285
    foreach ($view->display as $display_name => $display) {
286
      if (!empty($display->display_options['access']['type']) && $display->display_options['access']['type'] == 'perm') {
287
        $permission_name = $display->display_options['access']['perm'];
288
        if (isset($map[$permission_name])) {
289
          $display->display_options['access']['perm'] = $map[$permission_name];
290
          $save_view = TRUE;
291
        }
292
      }
293
    }
294

    
295
    if ($save_view) {
296
      $view->save();
297
    }
298
  }
299
}
300

    
301
/**
302
 * Clear the cache of currency data so it can be rebuilt to include new
303
 * formatting parameters.
304
 */
305
function commerce_update_7101() {
306
  cache_clear_all('commerce_currencies:', 'cache', TRUE);
307
  return t('Cached currency data has been deleted and will be rebuilt to include new formatting parameters.');
308
}
309

    
310
/**
311
 * Give commerce.module a higher weight than field.module so we can use
312
 * hook_system_info_alter() to remove the dependencies it adds.
313
 */
314
function commerce_update_7102() {
315
  $weight = db_select('system', 's')
316
              ->fields('s', array('weight'))
317
              ->condition('name', 'field', '=')
318
              ->execute()
319
              ->fetchField();
320
  db_update('system')
321
    ->fields(array('weight' => $weight + 1))
322
    ->condition('name', 'commerce', '=')
323
    ->execute();
324
  return t('The module weight for Commerce has been increased.');
325
}