1
|
<?php
|
2
|
|
3
|
|
4
|
/**
|
5
|
* Implements hook_schema().
|
6
|
*/
|
7
|
function commerce_product_schema() {
|
8
|
$schema = array();
|
9
|
|
10
|
$schema['commerce_product'] = array(
|
11
|
'description' => 'The base table for products.',
|
12
|
'fields' => array(
|
13
|
'product_id' => array(
|
14
|
'description' => 'The primary identifier for a product, used internally only.',
|
15
|
'type' => 'serial',
|
16
|
'unsigned' => TRUE,
|
17
|
'not null' => TRUE,
|
18
|
),
|
19
|
'revision_id' => array(
|
20
|
'description' => 'The current {commerce_product_revision}.revision_id version identifier.',
|
21
|
'type' => 'int',
|
22
|
'unsigned' => TRUE,
|
23
|
'not null' => FALSE,
|
24
|
),
|
25
|
'sku' => array(
|
26
|
'description' => 'The unique, human-readable identifier for a product.',
|
27
|
'type' => 'varchar',
|
28
|
'length' => 255,
|
29
|
'not null' => TRUE,
|
30
|
),
|
31
|
'title' => array(
|
32
|
'description' => 'The title of this product, always treated as non-markup plain text.',
|
33
|
'type' => 'varchar',
|
34
|
'length' => 255,
|
35
|
'not null' => TRUE,
|
36
|
'default' => '',
|
37
|
),
|
38
|
'type' => array(
|
39
|
'description' => 'The {commerce_product_type}.type of this product.',
|
40
|
'type' => 'varchar',
|
41
|
'length' => 255,
|
42
|
'not null' => TRUE,
|
43
|
'default' => '',
|
44
|
),
|
45
|
'language' => array(
|
46
|
'description' => 'The {languages}.language of this product.',
|
47
|
'type' => 'varchar',
|
48
|
'length' => 32,
|
49
|
'not null' => TRUE,
|
50
|
'default' => '',
|
51
|
),
|
52
|
'uid' => array(
|
53
|
'description' => 'The {users}.uid that created this product.',
|
54
|
'type' => 'int',
|
55
|
'not null' => TRUE,
|
56
|
'default' => 0,
|
57
|
),
|
58
|
'status' => array(
|
59
|
'description' => 'Boolean indicating whether or not the product appears in lists and may be added to orders.',
|
60
|
'type' => 'int',
|
61
|
'size' => 'tiny',
|
62
|
'not null' => TRUE,
|
63
|
'default' => 1,
|
64
|
),
|
65
|
'created' => array(
|
66
|
'description' => 'The Unix timestamp when the product was created.',
|
67
|
'type' => 'int',
|
68
|
'not null' => TRUE,
|
69
|
'default' => 0,
|
70
|
),
|
71
|
'changed' => array(
|
72
|
'description' => 'The Unix timestamp when the product was most recently saved.',
|
73
|
'type' => 'int',
|
74
|
'not null' => TRUE,
|
75
|
'default' => 0,
|
76
|
),
|
77
|
'data' => array(
|
78
|
'type' => 'blob',
|
79
|
'not null' => FALSE,
|
80
|
'size' => 'big',
|
81
|
'serialize' => TRUE,
|
82
|
'description' => 'A serialized array of additional data.',
|
83
|
),
|
84
|
),
|
85
|
'primary key' => array('product_id'),
|
86
|
'indexes' => array(
|
87
|
'product_type' => array('type'),
|
88
|
'uid' => array('uid'),
|
89
|
),
|
90
|
'unique keys' => array(
|
91
|
'sku' => array('sku'),
|
92
|
'revision_id' => array('revision_id'),
|
93
|
),
|
94
|
'foreign keys' => array(
|
95
|
'current_revision' => array(
|
96
|
'table' => 'commerce_product_revision',
|
97
|
'columns'=> array('revision_id' => 'revision_id'),
|
98
|
),
|
99
|
'creator' => array(
|
100
|
'table' => 'users',
|
101
|
'columns' => array('uid' => 'uid'),
|
102
|
),
|
103
|
),
|
104
|
);
|
105
|
|
106
|
$schema['commerce_product_revision'] = array(
|
107
|
'description' => 'Saves information about each saved revision of a {commerce_product}.',
|
108
|
'fields' => array(
|
109
|
'product_id' => array(
|
110
|
'description' => 'The {commerce_product}.product_id of the product this revision belongs to.',
|
111
|
'type' => 'int',
|
112
|
'unsigned' => TRUE,
|
113
|
'not null' => TRUE,
|
114
|
'default' => 0,
|
115
|
),
|
116
|
'revision_id' => array(
|
117
|
'description' => 'The primary identifier for this revision.',
|
118
|
'type' => 'serial',
|
119
|
'unsigned' => TRUE,
|
120
|
'not null' => TRUE,
|
121
|
),
|
122
|
'sku' => array(
|
123
|
'description' => 'The unique, human-readable identifier of a product for this revision.',
|
124
|
'type' => 'varchar',
|
125
|
'length' => 255,
|
126
|
'not null' => TRUE,
|
127
|
),
|
128
|
'title' => array(
|
129
|
'description' => 'The title of this product for this revision',
|
130
|
'type' => 'varchar',
|
131
|
'length' => 255,
|
132
|
'not null' => TRUE,
|
133
|
'default' => '',
|
134
|
),
|
135
|
'revision_uid' => array(
|
136
|
'description' => 'The {users}.uid that owns the product at this revision.',
|
137
|
'type' => 'int',
|
138
|
'not null' => TRUE,
|
139
|
'default' => 0,
|
140
|
),
|
141
|
'status' => array(
|
142
|
'description' => 'The status of this revision.',
|
143
|
'type' => 'int',
|
144
|
'size' => 'tiny',
|
145
|
'not null' => TRUE,
|
146
|
'default' => 1,
|
147
|
),
|
148
|
'log' => array(
|
149
|
'description' => 'The log entry explaining the changes in this version.',
|
150
|
'type' => 'text',
|
151
|
'not null' => TRUE,
|
152
|
'size' => 'big',
|
153
|
),
|
154
|
'revision_timestamp' => array(
|
155
|
'description' => 'The Unix timestamp when this revision was created.',
|
156
|
'type' => 'int',
|
157
|
'not null' => TRUE,
|
158
|
'default' => 0,
|
159
|
),
|
160
|
'data' => array(
|
161
|
'type' => 'blob',
|
162
|
'not null' => FALSE,
|
163
|
'size' => 'big',
|
164
|
'serialize' => TRUE,
|
165
|
'description' => 'A serialized array of additional data for this revision.',
|
166
|
),
|
167
|
),
|
168
|
'primary key' => array('revision_id'),
|
169
|
'indexes' => array(
|
170
|
'product_id' => array('product_id'),
|
171
|
'revision_uid' => array('revision_uid'),
|
172
|
),
|
173
|
'foreign keys' => array(
|
174
|
'product' => array(
|
175
|
'table' => 'commerce_product',
|
176
|
'columns' => array('product_id' => 'product_id'),
|
177
|
),
|
178
|
'owner' => array(
|
179
|
'table' => 'users',
|
180
|
'columns' => array('revision_uid' => 'uid'),
|
181
|
),
|
182
|
),
|
183
|
);
|
184
|
|
185
|
return $schema;
|
186
|
}
|
187
|
|
188
|
/**
|
189
|
* Implements hook_uninstall().
|
190
|
*/
|
191
|
function commerce_product_uninstall() {
|
192
|
// Delete any field instance attached to a product type.
|
193
|
module_load_include('module', 'commerce');
|
194
|
commerce_delete_instances('commerce_product');
|
195
|
}
|
196
|
|
197
|
/**
|
198
|
* Changes the name of the 'type' index on the commerce_product table to ensure
|
199
|
* compatibility with sqlite despite http://drupal.org/node/1008128.
|
200
|
*/
|
201
|
function commerce_product_update_7100() {
|
202
|
if (db_index_exists('commerce_product', 'type')) {
|
203
|
db_drop_index('commerce_product', 'type');
|
204
|
}
|
205
|
db_add_index('commerce_product', 'product_type', array('type'));
|
206
|
}
|
207
|
|
208
|
/**
|
209
|
* Update permission names for product entity management.
|
210
|
*/
|
211
|
function commerce_product_update_7101() {
|
212
|
// Load utility functions.
|
213
|
module_load_install('commerce');
|
214
|
|
215
|
$map = array(
|
216
|
'administer products' => 'administer commerce_product entities',
|
217
|
'access products' => 'view any commerce_product entity',
|
218
|
);
|
219
|
$entity_info = entity_get_info('commerce_product');
|
220
|
foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
|
221
|
$map['create ' . $bundle_name . ' products'] = 'create commerce_product entities of bundle ' . $bundle_name;
|
222
|
$map['edit any ' . $bundle_name . ' product'] = 'edit any commerce_product entity of bundle ' . $bundle_name;
|
223
|
$map['edit own ' . $bundle_name . ' products'] = 'edit own commerce_product entities of bundle ' . $bundle_name;
|
224
|
}
|
225
|
|
226
|
commerce_update_rename_permissions($map);
|
227
|
|
228
|
return t('Role and custom View permissions updated for product entity management. Access checks in modules and permissions on default Views must be updated manually.');
|
229
|
}
|
230
|
|
231
|
/**
|
232
|
* Add an index to the commerce_product table on uid.
|
233
|
*/
|
234
|
function commerce_product_update_7102() {
|
235
|
if (db_index_exists('commerce_product', 'uid')) {
|
236
|
db_drop_index('commerce_product', 'uid');
|
237
|
}
|
238
|
|
239
|
db_add_index('commerce_product', 'uid', array('uid'));
|
240
|
|
241
|
return t('Database index added to the uid column of the commerce_product table.');
|
242
|
}
|
243
|
|
244
|
/**
|
245
|
* Add support for product revisions.
|
246
|
*/
|
247
|
function commerce_product_update_7103(&$sandbox) {
|
248
|
if (!isset($sandbox['progress'])) {
|
249
|
$commerce_product_revision_schema = array(
|
250
|
'description' => 'Saves information about each saved revision of a {commerce_product}.',
|
251
|
'fields' => array(
|
252
|
'product_id' => array(
|
253
|
'description' => 'The {commerce_product}.product_id of the product this revision belongs to.',
|
254
|
'type' => 'int',
|
255
|
'unsigned' => TRUE,
|
256
|
'not null' => TRUE,
|
257
|
'default' => 0,
|
258
|
),
|
259
|
'revision_id' => array(
|
260
|
'description' => 'The primary identifier for this revision.',
|
261
|
'type' => 'serial',
|
262
|
'unsigned' => TRUE,
|
263
|
'not null' => TRUE,
|
264
|
),
|
265
|
'sku' => array(
|
266
|
'description' => 'The unique, human-readable identifier of a product for this revision.',
|
267
|
'type' => 'varchar',
|
268
|
'length' => 255,
|
269
|
'not null' => TRUE,
|
270
|
),
|
271
|
'title' => array(
|
272
|
'description' => 'The title of this product for this revision',
|
273
|
'type' => 'varchar',
|
274
|
'length' => 255,
|
275
|
'not null' => TRUE,
|
276
|
'default' => '',
|
277
|
),
|
278
|
'revision_uid' => array(
|
279
|
'description' => 'The {users}.uid that owns the product at this revision.',
|
280
|
'type' => 'int',
|
281
|
'not null' => TRUE,
|
282
|
'default' => 0,
|
283
|
),
|
284
|
'status' => array(
|
285
|
'description' => 'The status of this revision.',
|
286
|
'type' => 'int',
|
287
|
'size' => 'tiny',
|
288
|
'not null' => TRUE,
|
289
|
'default' => 1,
|
290
|
),
|
291
|
'log' => array(
|
292
|
'description' => 'The log entry explaining the changes in this version.',
|
293
|
'type' => 'text',
|
294
|
'not null' => TRUE,
|
295
|
'size' => 'big',
|
296
|
),
|
297
|
'revision_timestamp' => array(
|
298
|
'description' => 'The Unix timestamp when this revision was created.',
|
299
|
'type' => 'int',
|
300
|
'not null' => TRUE,
|
301
|
'default' => 0,
|
302
|
),
|
303
|
'data' => array(
|
304
|
'type' => 'blob',
|
305
|
'not null' => FALSE,
|
306
|
'size' => 'big',
|
307
|
'serialize' => TRUE,
|
308
|
'description' => 'A serialized array of additional data for this revision.',
|
309
|
),
|
310
|
),
|
311
|
'primary key' => array('revision_id'),
|
312
|
'indexes' => array(
|
313
|
'product_id' => array('product_id'),
|
314
|
'revision_uid' => array('revision_uid'),
|
315
|
),
|
316
|
'foreign keys' => array(
|
317
|
'product' => array(
|
318
|
'table' => 'commerce_product',
|
319
|
'columns' => array('product_id' => 'product_id'),
|
320
|
),
|
321
|
'owner' => array(
|
322
|
'table' => 'users',
|
323
|
'columns' => array('revision_uid' => 'uid'),
|
324
|
),
|
325
|
),
|
326
|
);
|
327
|
|
328
|
if (!db_table_exists('commerce_product_revision')) {
|
329
|
db_create_table('commerce_product_revision', $commerce_product_revision_schema);
|
330
|
}
|
331
|
|
332
|
// If another module had added a {commerce_product}.revision_id field,
|
333
|
// then change it to the expected specification. Otherwise, add the field.
|
334
|
$product_revision_id_spec = array(
|
335
|
'description' => 'The current {commerce_product_revision}.revision_id version identifier.',
|
336
|
'type' => 'int',
|
337
|
'unsigned' => TRUE,
|
338
|
'not null' => FALSE,
|
339
|
'default' => NULL,
|
340
|
);
|
341
|
if (db_field_exists('commerce_product', 'revision_id')) {
|
342
|
// db_change_field() will fail if any records have type = NULL, so update
|
343
|
// them to the new default value.
|
344
|
db_update('commerce_product')->fields(array('revision_id' => $product_revision_id_spec['default']))->isNull('revision_id')->execute();
|
345
|
|
346
|
// Indexes using a field being changed must be dropped prior to calling
|
347
|
// db_change_field(). However, the database API doesn't provide a way to do
|
348
|
// this without knowing what the old indexes are. Therefore, it is the
|
349
|
// responsibility of the module that added them to drop them prior to
|
350
|
// allowing this module to be updated.
|
351
|
db_change_field('commerce_product', 'revision_id', 'revision_id', $product_revision_id_spec);
|
352
|
}
|
353
|
else {
|
354
|
db_add_field('commerce_product', 'revision_id', $product_revision_id_spec);
|
355
|
}
|
356
|
|
357
|
if (!db_index_exists('commerce_product', 'revision_id')) {
|
358
|
db_add_unique_key('commerce_product', 'revision_id', array('revision_id'));
|
359
|
}
|
360
|
}
|
361
|
|
362
|
$max_products = db_query('SELECT COUNT(DISTINCT product_id) FROM {commerce_product}')->fetchField();
|
363
|
|
364
|
// If we have already products in the {commerce_product} table we must create
|
365
|
// the current revision for them.
|
366
|
if ($max_products) {
|
367
|
if (!isset($sandbox['progress'])) {
|
368
|
$sandbox['progress'] = 0;
|
369
|
$sandbox['current_product_id'] = 0;
|
370
|
$sandbox['max'] = $max_products;
|
371
|
}
|
372
|
|
373
|
$products = db_select('commerce_product', 'cp')
|
374
|
->fields('cp', array('product_id', 'sku', 'title', 'uid', 'status', 'created', 'data'))
|
375
|
->condition('product_id', $sandbox['current_product_id'], '>')
|
376
|
->range(0, 50)
|
377
|
->orderBy('product_id', 'ASC')
|
378
|
->execute()->fetchAllAssoc('product_id', PDO::FETCH_ASSOC);
|
379
|
|
380
|
foreach ($products as $product) {
|
381
|
// The log can't be empty.
|
382
|
$product['log'] = '';
|
383
|
$product['revision_uid'] = $product['uid'];
|
384
|
$product['revision_timestamp'] = $product['created'];
|
385
|
unset($product['uid']);
|
386
|
unset($product['created']);
|
387
|
|
388
|
$revision_id = db_insert('commerce_product_revision')
|
389
|
->fields($product)
|
390
|
->execute();
|
391
|
db_update('commerce_product')
|
392
|
->fields(array('revision_id' => $revision_id))
|
393
|
->condition('product_id', $product['product_id'])
|
394
|
->execute();
|
395
|
|
396
|
$sandbox['progress']++;
|
397
|
$sandbox['current_product_id'] = $product['product_id'];
|
398
|
}
|
399
|
|
400
|
if ((empty($sandbox['progress']) || $sandbox['progress'] == $max_products)) {
|
401
|
$sandbox['progress'] = $sandbox['max'];
|
402
|
}
|
403
|
|
404
|
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
|
405
|
}
|
406
|
|
407
|
return t('The update for commerce product revisions ran successfully.');
|
408
|
}
|
409
|
|
410
|
/**
|
411
|
* Remove the default value for revision_id on {commerce_product}.
|
412
|
*/
|
413
|
function commerce_product_update_7104() {
|
414
|
db_change_field('commerce_product', 'revision_id', 'revision_id', array(
|
415
|
'description' => 'The current {commerce_product_revision}.revision_id version identifier.',
|
416
|
'type' => 'int',
|
417
|
'unsigned' => TRUE,
|
418
|
'not null' => FALSE,
|
419
|
));
|
420
|
|
421
|
return t('Schema for the commerce_product table has been updated.');
|
422
|
}
|