Projet

Général

Profil

Paste
Télécharger (15,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / commerce / modules / order / commerce_order.install @ 70a4c29b

1
<?php
2

    
3
/**
4
 * Implements hook_schema().
5
 */
6
function commerce_order_schema() {
7
  $schema = array();
8

    
9
  $schema['commerce_order'] = array(
10
    'description' => 'The base table for orders.',
11
    'fields' => array(
12
      'order_id' => array(
13
        'description' => 'The primary identifier for an order.',
14
        'type' => 'serial',
15
        'unsigned' => TRUE,
16
        'not null' => TRUE,
17
      ),
18
      'order_number' => array(
19
        'description' => 'The order number displayed to the customer.',
20
        'type' => 'varchar',
21
        'length' => 255,
22
        'not null' => FALSE,
23
      ),
24
      'revision_id' => array(
25
        'description' => 'The current {commerce_order_revision}.revision_id version identifier.',
26
        'type' => 'int',
27
        'unsigned' => TRUE,
28
        'not null' => FALSE,
29
      ),
30
      'type' => array(
31
        'description' => 'The type of this order.',
32
        'type' => 'varchar',
33
        'length' => 255,
34
        'not null' => TRUE,
35
        'default' => '',
36
      ),
37
      'uid' => array(
38
        'description' => 'The {users}.uid that owns this order.',
39
        'type' => 'int',
40
        'not null' => TRUE,
41
        'default' => 0,
42
      ),
43
      'mail' => array(
44
        'description' => 'The e-mail address associated with the order.',
45
        'type' => 'varchar',
46
        'length' => 255,
47
        'not null' => TRUE,
48
        'default' => '',
49
      ),
50
      'status' => array(
51
        'description' => 'The status name of this order.',
52
        'type' => 'varchar',
53
        'length' => '255',
54
        'not null' => TRUE,
55
      ),
56
      'created' => array(
57
        'description' => 'The Unix timestamp when the order was created.',
58
        'type' => 'int',
59
        'not null' => TRUE,
60
        'default' => 0,
61
      ),
62
      'changed' => array(
63
        'description' => 'The Unix timestamp when the order was most recently saved.',
64
        'type' => 'int',
65
        'not null' => TRUE,
66
        'default' => 0,
67
      ),
68
      'hostname' => array(
69
        'description' => 'The IP address that created this order.',
70
        'type' => 'varchar',
71
        'length' => 128,
72
        'not null' => TRUE,
73
        'default' => '',
74
      ),
75
      'data' => array(
76
        'type' => 'blob',
77
        'not null' => FALSE,
78
        'size' => 'big',
79
        'serialize' => TRUE,
80
        'description' => 'A serialized array of additional data.',
81
      ),
82
    ),
83
    'primary key' => array('order_id'),
84
    'unique keys' => array(
85
      'order_number' => array('order_number'),
86
      'revision_id' => array('revision_id'),
87
    ),
88
    'indexes' => array(
89
      'uid' => array('uid'),
90
    ),
91
    'foreign keys' => array(
92
      'current_revision' => array(
93
        'table' => 'commerce_order_revision',
94
        'columns'=> array('revision_id' => 'revision_id'),
95
      ),
96
      'owner' => array(
97
        'table' => 'users',
98
        'columns' => array('uid' => 'uid'),
99
      ),
100
    ),
101
  );
102

    
103
  $schema['commerce_order_revision'] = array(
104
    'description' => 'Saves information about each saved revision of a {commerce_order}.',
105
    'fields' => array(
106
      'order_id' => array(
107
        'description' => 'The {commerce_order}.order_id of the order this revision belongs to.',
108
        'type' => 'int',
109
        'unsigned' => TRUE,
110
        'not null' => TRUE,
111
        'default' => 0,
112
      ),
113
      'order_number' => array(
114
        'description' => 'The order number displayed to the customer for this revision.',
115
        'type' => 'varchar',
116
        'length' => 255,
117
        'not null' => FALSE,
118
      ),
119
      'revision_id' => array(
120
        'description' => 'The primary identifier for this revision.',
121
        'type' => 'serial',
122
        'unsigned' => TRUE,
123
        'not null' => TRUE,
124
      ),
125
      'revision_uid' => array(
126
        'description' => 'The {users}.uid that owns the order at this revision.',
127
        'type' => 'int',
128
        'not null' => TRUE,
129
        'default' => 0,
130
      ),
131
      'mail' => array(
132
        'description' => 'The e-mail address associated with the order at this revision.',
133
        'type' => 'varchar',
134
        'length' => 255,
135
        'not null' => TRUE,
136
      ),
137
      'status' => array(
138
        'description' => 'The status name of this revision.',
139
        'type' => 'varchar',
140
        'length' => '255',
141
        'not null' => TRUE,
142
      ),
143
      'log' => array(
144
        'description' => 'The log entry explaining the changes in this version.',
145
        'type' => 'text',
146
        'not null' => TRUE,
147
        'size' => 'big',
148
      ),
149
      'revision_timestamp' => array(
150
        'description' => 'The Unix timestamp when this revision was created.',
151
        'type' => 'int',
152
        'not null' => TRUE,
153
        'default' => 0,
154
      ),
155
      'revision_hostname' => array(
156
        'description' => 'The IP address that created this order.',
157
        'type' => 'varchar',
158
        'length' => 128,
159
        'not null' => TRUE,
160
        'default' => '',
161
      ),
162
      'data' => array(
163
        'type' => 'blob',
164
        'not null' => FALSE,
165
        'size' => 'big',
166
        'serialize' => TRUE,
167
        'description' => 'A serialized array of additional data.',
168
      ),
169
    ),
170
    'primary key' => array('revision_id'),
171
    'indexes' => array(
172
      'order_id' => array('order_id'),
173
    ),
174
    'foreign keys' => array(
175
      'order' => array(
176
        'table' => 'commerce_order',
177
        'columns'=> array('order_id' => 'order_id'),
178
      ),
179
      'owner' => array(
180
        'table' => 'users',
181
        'columns' => array('uid' => 'uid'),
182
      ),
183
    ),
184
  );
185

    
186
  return $schema;
187
}
188

    
189
/**
190
 * Implements hook_uninstall().
191
 */
192
function commerce_order_uninstall() {
193
  // Delete any field instance attached to an order type.
194
  module_load_include('module', 'commerce');
195
  commerce_delete_instances('commerce_order');
196

    
197
  variable_del('commerce_order_help_text');
198
}
199

    
200
/**
201
 * Between 7.x-1.0-beta2 and 7.x-1.0-beta3 we determined we needed to revise the
202
 * way we handled price amounts, preferring to preserve integer amounts as
203
 * loaded from the database until formatting them as decimal values upon display
204
 * instead of converting them to decimals upon loading. The initial reasons and
205
 * related issues are outlined in http://drupal.org/node/1124416.
206
 *
207
 * While the fix did not involve changing the database schema at all, it did
208
 * change the way price amounts were stored in the components array of a price's
209
 * data array. Therefore, the following update functions are responsible for
210
 * loading and resaving entities the change will affect, primarily to result in
211
 * a recalculated order total components array./
212
 */
213

    
214
/**
215
 * Loads and resaves all the products on the site, updating the default price
216
 * field to have proper component price amount values.
217
 */
218
function commerce_order_update_7100(&$sandbox) {
219
  // Ensure there are no stale prices in the field cache.
220
  field_cache_clear();
221

    
222
  // Establish the progress variables.
223
  if (!isset($sandbox['progress'])) {
224
    $sandbox['progress'] = 0;
225
    $sandbox['current_product_id'] = 0;
226
    $sandbox['max'] = db_query("SELECT COUNT(DISTINCT product_id) FROM {commerce_product}")->fetchField();
227
  }
228

    
229
  // Load the next 50 products.
230
  $products = db_select('commerce_product', 'cp')
231
    ->fields('cp', array('product_id'))
232
    ->condition('product_id', $sandbox['current_product_id'], '>')
233
    ->range(0, 50)
234
    ->orderBy('product_id', 'ASC')
235
    ->execute();
236

    
237
  // Loop over the products, loading, adjusting, and resaving each one.
238
  foreach ($products as $product) {
239
    $product = commerce_product_load($product->product_id);
240

    
241
    // If the commerce_price field has a components array, multiply its price
242
    // amounts by the proper value for its currency.
243
    if (!empty($product->commerce_price)) {
244
      foreach ($product->commerce_price as $langcode => &$data) {
245
        foreach ($data as $delta => &$item) {
246
          if (!empty($item['data']['components'])) {
247
            foreach ($item['data']['components'] as $key => &$component) {
248
              $component['price']['amount'] = commerce_currency_decimal_to_amount($component['price']['amount'], $component['price']['currency_code'], FALSE);
249
            }
250
          }
251
        }
252
      }
253
    }
254

    
255
    commerce_product_save($product);
256

    
257
    $sandbox['progress']++;
258
    $sandbox['current_product_id'] = $product->product_id;
259
  }
260

    
261
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
262

    
263
  return t('All products have been loaded and saved with updated price component arrays.');
264
}
265

    
266
/**
267
 * Loads and resaves all the line items on the site, updating the unit price
268
 * field to have proper component price amount values.
269
 */
270
function commerce_order_update_7101(&$sandbox) {
271
  // Ensure there are no stale prices in the field cache.
272
  field_cache_clear();
273

    
274
  // Establish the progress variables.
275
  if (!isset($sandbox['progress'])) {
276
    $sandbox['progress'] = 0;
277
    $sandbox['current_line_item_id'] = 0;
278
    $sandbox['max'] = db_query("SELECT COUNT(DISTINCT line_item_id) FROM {commerce_line_item}")->fetchField();
279
  }
280

    
281
  // Load the next 50 line items.
282
  $line_items = db_select('commerce_line_item', 'cli')
283
    ->fields('cli', array('line_item_id'))
284
    ->condition('line_item_id', $sandbox['current_line_item_id'], '>')
285
    ->range(0, 50)
286
    ->orderBy('line_item_id', 'ASC')
287
    ->execute();
288

    
289
  // Loop over the line items, loading, adjusting, and resaving each one.
290
  foreach ($line_items as $line_item) {
291
    $line_item = commerce_line_item_load($line_item->line_item_id);
292

    
293
    // If the commerce_unit_price field has a components array, multiply its
294
    // amounts by the proper value for its currency.
295
    if (!empty($line_item->commerce_unit_price)) {
296
      foreach ($line_item->commerce_unit_price as $langcode => &$data) {
297
        foreach ($data as $delta => &$item) {
298
          if (!empty($item['data']['components'])) {
299
            foreach ($item['data']['components'] as $key => &$component) {
300
              $component['price']['amount'] = commerce_currency_decimal_to_amount($component['price']['amount'], $component['price']['currency_code'], FALSE);
301
            }
302
          }
303
        }
304
      }
305
    }
306

    
307
    commerce_line_item_save($line_item);
308

    
309
    $sandbox['progress']++;
310
    $sandbox['current_line_item_id'] = $line_item->line_item_id;
311
  }
312

    
313
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
314

    
315
  return t('All line items have been loaded and saved with updated price component arrays.');
316
}
317

    
318
/**
319
 * Loads and resaves all the orders on the site to rebuild the order total price
320
 * component arrays.
321
 */
322
function commerce_order_update_7102(&$sandbox) {
323
  // Ensure there are no stale prices in the field cache.
324
  field_cache_clear();
325

    
326
  // Establish the progress variables.
327
  if (!isset($sandbox['progress'])) {
328
    $sandbox['progress'] = 0;
329
    $sandbox['current_order_id'] = 0;
330
    $sandbox['max'] = db_query("SELECT COUNT(DISTINCT order_id) FROM {commerce_order}")->fetchField();
331
  }
332

    
333
  // Load the next 50 orders.
334
  $orders = db_select('commerce_order', 'co')
335
    ->fields('co', array('order_id'))
336
    ->condition('order_id', $sandbox['current_order_id'], '>')
337
    ->range(0, 50)
338
    ->orderBy('order_id', 'ASC')
339
    ->execute();
340

    
341
  // Loop over the orders, loading and resaving each one.
342
  foreach ($orders as $order) {
343
    $order = commerce_order_load($order->order_id);
344

    
345
    // Save the order as a new revision with an update log message.
346
    $order->revision = TRUE;
347
    $order->log = t('Order updated for 7.0-1.0-beta3 price component changes.');
348

    
349
    commerce_order_save($order);
350

    
351
    $sandbox['progress']++;
352
    $sandbox['current_order_id'] = $order->order_id;
353
  }
354

    
355
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
356

    
357
  return t('All orders have been loaded and saved with updated price component arrays.');
358
}
359

    
360
/**
361
 * Truncates the pre-calculated price table.
362
 */
363
function commerce_order_update_7103() {
364
  db_truncate('commerce_calculated_price')->execute();
365
  return t('The calculated price table has been cleared. If your site uses product sell price pre-calculation, you will need to recalculate these prices.');
366
}
367

    
368
/**
369
 * Update permission names for order entity management.
370
 */
371
function commerce_order_update_7104() {
372
  // Load utility functions.
373
  module_load_install('commerce');
374

    
375
  $map = array(
376
    'administer orders' => 'administer commerce_order entities',
377
    'access orders' => 'view any commerce_order entity',
378
    'create orders' => 'create commerce_order entities',
379
    'edit any order' => 'edit any commerce_order entity',
380
    'edit own orders' => 'edit own commerce_order entities',
381
    'view own orders' => 'view own commerce_order entities',
382
  );
383

    
384
  commerce_update_rename_permissions($map);
385

    
386
  return t('Role and custom View permissions updated for order entity management. Access checks in modules and permissions on default Views must be updated manually.');
387
}
388

    
389
/**
390
 * Add an index to the commerce_order_revision table on order_id.
391
 */
392
function commerce_order_update_7105() {
393
  if (db_index_exists('commerce_order_revision', 'order_id')) {
394
    db_drop_index('commerce_order_revision', 'order_id');
395
  }
396

    
397
  db_add_index('commerce_order_revision', 'order_id', array('order_id'));
398
}
399

    
400
/**
401
 * Assign the new 'Configure order settings' permission to roles that already
402
 * have the 'Administer orders' permission.
403
 */
404
function commerce_order_update_7106() {
405
  $roles = db_query("SELECT * FROM {role_permission} WHERE permission = 'administer commerce_order entities'")->fetchAllAssoc('rid', PDO::FETCH_ASSOC);
406

    
407
  foreach ($roles as $rid => $permission) {
408
    db_insert('role_permission')
409
      ->fields(array('rid', 'permission', 'module'), array($rid, 'configure order settings', 'commerce_order'))
410
      ->execute();
411
  }
412

    
413
  return t('All roles that had the <em>Administer orders</em> permission now also have the new <em>Configure order settings</em> permission.');
414
}
415

    
416
/**
417
 * Add an index to the commerce_order table on uid.
418
 */
419
function commerce_order_update_7107() {
420
  if (db_index_exists('commerce_order', 'uid')) {
421
    db_drop_index('commerce_order', 'uid');
422
  }
423

    
424
  db_add_index('commerce_order', 'uid', array('uid'));
425

    
426
  return t('Database index added to the uid column of the commerce_order table.');
427
}
428

    
429
/**
430
 * Allow NULL values for order_number and revision_id on {commerce_order}, and
431
 * order_number on {commerce_order_revision}.
432
 */
433
function commerce_order_update_7108() {
434
  $order_number_spec = array(
435
    'description' => 'The order number displayed to the customer.',
436
    'type' => 'varchar',
437
    'length' => 255,
438
    'not null' => FALSE,
439
  );
440
  $revision_id_spec = array(
441
    'description' => 'The current {commerce_order_revision}.revision_id version identifier.',
442
    'type' => 'int',
443
    'unsigned' => TRUE,
444
    'not null' => FALSE,
445
    'default' => 0,
446
  );
447
  $order_number_revision_spec = array(
448
    'description' => 'The order number displayed to the customer for this revision.',
449
    'type' => 'varchar',
450
    'length' => 255,
451
    'not null' => FALSE,
452
  );
453

    
454
  db_change_field('commerce_order', 'order_number', 'order_number', $order_number_spec);
455
  db_change_field('commerce_order', 'revision_id', 'revision_id', $revision_id_spec);
456
  db_change_field('commerce_order_revision', 'order_number', 'order_number', $order_number_revision_spec);
457

    
458
  return t('Schema for the commerce_order and commerce_order_revision tables has been updated.');
459
}
460

    
461
/**
462
 * Remove the default value for revision_id on {commerce_order}.
463
 */
464
function commerce_order_update_7109() {
465
  db_change_field('commerce_order', 'revision_id', 'revision_id', array(
466
    'description' => 'The current {commerce_order_revision}.revision_id version identifier.',
467
    'type' => 'int',
468
    'unsigned' => TRUE,
469
    'not null' => FALSE,
470
  ));
471

    
472
  return t('Schema for the commerce_order table has been updated.');
473
}