1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* Base Migrations for Commerce Products used in DF. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
class DemoFrameworkBaseProducts extends Migration { |
9
|
|
|
|
10
|
|
|
public function __construct($arguments) { |
11
|
|
|
parent::__construct($arguments); |
12
|
|
|
$this->description = t('Import products.'); |
13
|
|
|
|
14
|
|
|
// Create a map object for tracking the relationships between source rows |
15
|
|
|
$this->map = new MigrateSQLMap($this->machineName, |
16
|
|
|
array( |
17
|
|
|
'sku' => array( |
18
|
|
|
'type' => 'varchar', |
19
|
|
|
'length' => 32, |
20
|
|
|
'not null' => TRUE, |
21
|
|
|
), |
22
|
|
|
), |
23
|
|
|
MigrateDestinationEntityAPI::getKeySchema('commerce_product', 'product') |
24
|
|
|
); |
25
|
|
|
|
26
|
|
|
$this->destination = new MigrateDestinationEntityAPI('commerce_product', 'product'); |
27
|
|
|
|
28
|
|
|
// Define a default import path. |
29
|
|
|
$import_path = drupal_get_path('module', 'df_commerce') . '/import/'; |
30
|
|
|
|
31
|
|
|
// Create a MigrateSource object. |
32
|
|
|
$this->source = new MigrateSourceCSV($import_path . 'df_commerce.base.products.csv', $this->csvcolumns(), array('header_rows' => 1)); |
33
|
|
|
|
34
|
|
|
$this->addFieldMapping('title', 'title'); |
35
|
|
|
$this->addFieldMapping('sku', 'sku'); |
36
|
|
|
$this->addFieldMapping('commerce_price', 'commerce_price'); |
37
|
|
|
|
38
|
|
|
// Images |
39
|
|
|
$this->addFieldMapping('field_images', 'images'); |
40
|
|
|
$this->addFieldMapping('field_images:file_replace')->defaultValue(FILE_EXISTS_REPLACE); |
41
|
|
|
$this->addFieldMapping('field_images:source_dir')->defaultValue($import_path . 'images'); |
42
|
|
|
$this->addFieldMapping('field_images:destination_file', 'filename'); |
43
|
|
|
|
44
|
|
|
$this->addFieldMapping('uid', 'uid'); |
45
|
|
|
$this->addFieldMapping('language', 'language'); |
46
|
|
|
$this->addFieldMapping('status', 'status'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
View Code Duplication |
function csvcolumns() { |
|
|
|
|
50
|
|
|
$columns[0] = array('title', 'Title'); |
|
|
|
|
51
|
|
|
$columns[1] = array('sku', 'SKU'); |
52
|
|
|
$columns[2] = array('commerce_price', 'Price'); |
53
|
|
|
$columns[3] = array('images', 'Images'); |
54
|
|
|
return $columns; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
function prepareRow($row) { |
|
|
|
|
58
|
|
|
$row->images = explode(', ', $row->images); |
59
|
|
|
$row->uid = 1; |
60
|
|
|
$row->language = LANGUAGE_NONE; |
61
|
|
|
$row->status = 1; |
62
|
|
|
return TRUE; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.