-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcimport.module
executable file
·168 lines (146 loc) · 3.55 KB
/
cimport.module
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
<?php
/**
* @file
* Commerce import custom module.
*
*/
/**
* Ignore missing files during import.
*/
define('CIMPORT_IGNORE_MISSING_FILES', TRUE);
// Include the includes.
foreach (glob(drupal_get_path('module', 'cimport') . "/includes/*.inc") as $filename) {
include $filename;
}
// Include the classes bases.
foreach (glob(drupal_get_path('module', 'cimport') . "/includes/base/*.php") as $filename) {
include $filename;
}
// Include the classes descendants.
foreach (glob(drupal_get_path('module', 'cimport') . "/includes/desc/*.php") as $filename) {
include $filename;
}
/**
* Return import config.
*
* Use this to configure the import.
*/
function cimport_get_config() {
$config = array(
'source' => array(
// Field to group products into displays.
'group_field' => 'group',
),
'dest' => array(
// Product type to use.
'type' => 'product',
// Destination dir where to move the files.
'dest_dir' => 'public://products',
// Product Display ct.
'content_type' => 'product_display',
// Product Display reference field name.
'product_field' => 'field_product_variations',
),
);
return $config;
}
/**
* Drush import callback.
*/
function cimport_run($run_conf = array()) {
$config = cimport_get_config();
// Get data.
$source = new DSCSource($config['source']);
$data = $source->getData();
$count = $source->count();
$count_products = $count['products'];
$count_nodes = $count['nodes'];
$media = $source->getMedia();
unset($source);
// No data. Quit.
if (!count($data)) {
if (function_exists('drush_log')) {
drush_log('No valid data to import. Quit.', 'error');
}
return FALSE;
}
// Missing files. Warn.
if (count($media['missing'])) {
if (function_exists('drush_log')) {
drush_log(count($media['missing']) . ' files missing.', 'warning');
if (!CIMPORT_IGNORE_MISSING_FILES) {
return FALSE;
}
}
}
// Main Loop.
$count = 0;
foreach ($data as &$pack) {
$products = array();
$count++;
foreach ($pack as &$entry) {
$product = new DSCProduct($entry, $config['dest']);
$the_product = $product->getProduct();
if (!empty($the_product->sku)) {
$products[] = $the_product;
}
unset($product);
}
if (empty($run_conf['products_only'])) {
$display = new DSCNode($products, $config['dest'], $pack);
unset($display);
}
if (function_exists('drush_log')) {
_drush_print_progress($count / $count_nodes);
}
}
return array(
'products' => $count_products,
'nodes' => $count_nodes,
);
}
/**
* Drush stats callback.
*/
function cimport_stats() {
$config = cimport_get_config();
// Get data.
$source = new DSCSource($config['source']);
$count = $source->count();
$count['media'] = $source->getMedia();
unset($source);
return $count;
}
/**
* Implements hook_drush_help().
*/
function cimport_drush_help($command) {
switch ($command) {
case 'drush:my-command':
return dt('Run my command');
}
}
/**
* Deletes listed products.
*
* @param bool $run
* If not TRUE, will perform an empty, stats run.
*
* @return array
* Operation result.
*/
function cimport_del($run = FALSE) {
$config = cimport_get_config();
$eraser = new DSCEraser($config);
$del_prods = $eraser->count();
if ($run === TRUE) {
$eraser->run();
$del_prods = $eraser->delProdsCount();
}
$del_nodes = $eraser->delNodesCount();
unset($eraser);
return array(
'prods' => $del_prods,
'nodes' => $del_nodes,
);
}