-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathyml_tools.class.inc
More file actions
376 lines (338 loc) · 8.52 KB
/
Copy pathyml_tools.class.inc
File metadata and controls
376 lines (338 loc) · 8.52 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
/**
* @file
* The code processing products and etc in the YML Tools module.
*/
interface YmlStoreTypeInterface {
/**
* Get store curency.
*/
public function getCurrency();
/**
* Get price for single product.
*
* @param object $product
* Product node.
*
* @return float
* Product price
*/
public function getProductPrice($product);
/**
* Get image for single product.
*
* @param object $product
* Product node
*
* @return string
* Product image url
*/
public function getProductImage($product);
/**
* Get SKU for single product.
*
* @param object $product
* Product node
*
* @return string
* Product SKU
*/
public function getProductSKU($product);
}
/**
* Abstract class for store type
*/
abstract class YmlStoreType {
protected $type;
/**
* Return store type.
*/
public function getType() {
return $this->type;
}
/**
* Return node type for export.
*/
public function getNodeTypes() {
$types = array();
$types_tmp = variable_get('yml_tools_types', array('product' => 'product'));
foreach ($types_tmp as $type) {
if (!empty($type)) {
$types[$type] = $type;
}
}
return $types;
}
/**
* Get store categories.
*/
public function getCategories() {
$categories = taxonomy_get_tree(variable_get('yml_tools_vid', ''));
return $categories;
}
/**
* Get store products.
*/
public function getProducts() {
$products = array();
$term_field = variable_get('yml_tools_term_field', '');
$descr_field = variable_get('yml_tools_descr_field', '');
$this->checkSettings();
$node_types = $this->getNodeTypes();
$query = new EntityFieldQuery();
$nodes = array();
foreach ($node_types as $type) {
$tmp_nodes = array();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', $type)
->propertyCondition('status', 1);
$result = $query->execute();
if (!empty($result['node'])) {
$nids = array_keys($result['node']);
$tmp_nodes = node_load_multiple($nids);
}
$nodes = array_merge($nodes, (array) $tmp_nodes);
}
foreach ($nodes as $id => $node) {
$validate_price = (float) $this->getProductPrice($node);
if (empty($validate_price)) {
continue;
}
$term = field_get_items('node', $node, $term_field);
if (isset($term[0])) {
$term = $term[0];
}
$description = field_get_items('node', $node, $descr_field);
if (isset($description[0])) {
$description = check_plain(strip_tags($description[0]['value']));
}
$title = check_plain($node->title);
$url = url('node/' . $node->nid, array('absolute' => TRUE));
$image = $this->getProductImage($node);
$price = round((float) $this->getProductPrice($node), 2);
$model = $this->getProductSKU($node);
$products[$id] = (object) array(
'title' => $title,
'url' => $url,
'category' => $term['tid'],
'price' => $price,
'description' => $description,
'image' => $image,
'model' => $model,
);
}
return $products;
}
/**
* Generate XML.
*/
public function getXML() {
$products_objects = $this->getProducts();
$categories_objects = $this->getCategories();
// Generate XML for each category and product from templates.
$categories = '';
foreach ($categories_objects as $category) {
$category->name = check_plain($category->name);
// Set the first parent category as the primary parent.
if (!empty($category->parents[0])) {
$category->parent = $category->parents[0];
}
$categories .= theme('yml_tools_category', array(
'category' => $category,
));
}
$products = '';
foreach ($products_objects as $product) {
$products .= theme('yml_tools_product', array(
'product' => $product,
'currency' => $this->getCurrency(),
));
}
// Return XML.
header('Content-type: application/xhtml+xml; charset=utf-8');
echo theme('yml_tools_xml', array(
'products' => $products,
'categories' => $categories,
'currency' => $this->getCurrency(),
));
exit();
}
/**
* Check module settings. Call exception if no settings.
*/
static public function checkSettings() {
$products = array();
if (variable_get('yml_tools_vid', '') == '') {
die('Please select primary vocabulary on YML export settings page!');
}
$ctypes = variable_get('yml_tools_types', array('product' => 'product'));
$enabled_ctypes = array();
foreach ($ctypes as $type_name => $enabled) {
if ($enabled) {
$enabled_ctypes[$type_name] = $type_name;
}
}
if (empty($enabled_ctypes)) {
die('Please select at least one node type on YML export settings page!');
}
}
}
/**
* Ubercart Store Class
*/
class YmlUbercartStore extends YmlStoreType implements YmlStoreTypeInterface {
/**
* Implements class constructor.
*/
public function __construct() {
$this->type = YML_TOOLS_STORE_TYPE_UBERCART;
}
/**
* Get content types of product.
*/
public function getContentTypes() {
$ctypes = uc_product_types();
$ctypes = array_combine($ctypes, $ctypes);
return array_keys($ctypes);
}
/**
* Get store curency.
*/
public function getCurrency() {
return variable_get('uc_currency_code', 'USD');
}
/**
* Get price for single product.
*
* @param object $product
* Product node.
*
* @return float
* Product price
*/
public function getProductPrice($product) {
return $product->sell_price;
}
/**
* Get image for single product.
*
* @param object $product
* Product node
*
* @return string
* Product image url
*/
public function getProductImage($product) {
$image_field = variable_get('yml_tools_image_field', '');
$path = '';
$image = field_get_items('node', $product, $image_field);
if (isset($image[0])) {
$path = file_create_url($image[0]['uri']);
}
return $path;
}
/**
* Get SKU for single product.
*
* @param object $product
* Product node
*
* @return string
* Product SKU
*/
public function getProductSKU($product) {
return check_plain($product->model);
}
}
/**
* Drupal Commerce Store Class
*/
class YmlCommerceStore extends ymlStoreType implements YmlStoreTypeInterface {
/**
* Implements class constructor.
*/
public function __construct() {
$this->type = YML_TOOLS_STORE_TYPE_COMMERCE;
}
/**
* Get content types of product.
*/
public function getContentTypes() {
$ctypes = commerce_product_types();
$ctypes = array_keys($ctypes);
$ctypes = array_combine($ctypes, $ctypes);
return $ctypes;
}
/**
* Get store curency.
*/
public function getCurrency() {
return commerce_default_currency();
}
/**
* Get image for single product.
*
* @param object $product
* Product node
*
* @return string
* Product image url
*/
public function getProductImage($product) {
$image_field = variable_get('yml_tools_image_field', '');
$path = '';
$product = commerce_product_load($product->nid);
$image = field_get_items('commerce_product', $product, $image_field);
if (isset($image[0])) {
$path = file_create_url($image[0]['uri']);
}
return $path;
}
/**
* Get price for single product.
*
* @param object $product
* Product node.
*
* @return float
* Product price
*/
public function getProductPrice($product) {
$product = commerce_product_load($product->nid);
$price = commerce_product_calculate_sell_price($product);
$price = commerce_currency_amount_to_decimal($price['amount'], $price['currency_code']);
return $price;
}
/**
* Get SKU for single product.
*
* @param object $product
* Product node
*
* @return string
* Product SKU
*/
public function getProductSKU($product) {
$product_wrapper = entity_metadata_wrapper('node', $product);
return $product_wrapper->field_product->get(0)->sku->value();
}
}
class YmlStore {
/**
* Factory method for create store.
*
* @return YmlUbercartStore|YmlCommerceStore|NULL
* Return store class
*/
static public function createStore() {
if (module_exists('uc_store')) {
return new YmlUbercartStore();
}
elseif (module_exists('commerce')) {
return new YmlCommerceStore();
}
else {
return NULL;
}
}
}