1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Exporter |
5
|
|
|
* @author Iurii Makukh |
6
|
|
|
* @copyright Copyright (c) 2017, Iurii Makukh |
7
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+ |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace gplcart\modules\export\handlers; |
11
|
|
|
|
12
|
|
|
use gplcart\core\models\File; |
13
|
|
|
use gplcart\core\models\Price; |
14
|
|
|
use gplcart\core\models\Product; |
15
|
|
|
use gplcart\core\models\Store; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Handler for Exporter module |
19
|
|
|
*/ |
20
|
|
|
class Export |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Product model instance |
25
|
|
|
* @var \gplcart\core\models\Product $product |
26
|
|
|
*/ |
27
|
|
|
protected $product; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Price model instance |
31
|
|
|
* @var \gplcart\core\models\Price $price |
32
|
|
|
*/ |
33
|
|
|
protected $price; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* File model instance |
37
|
|
|
* @var \gplcart\core\models\File $file |
38
|
|
|
*/ |
39
|
|
|
protected $file; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Store model instance |
43
|
|
|
* @var \gplcart\core\models\Store $store |
44
|
|
|
*/ |
45
|
|
|
protected $store; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* An array of the current job data |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
protected $job = array(); |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Export constructor. |
55
|
|
|
* @param Product $product |
56
|
|
|
* @param Price $price |
57
|
|
|
* @param File $file |
58
|
|
|
* @param Store $store |
59
|
|
|
*/ |
60
|
|
|
public function __construct(Product $product, Price $price, File $file, Store $store) |
61
|
|
|
{ |
62
|
|
|
$this->file = $file; |
63
|
|
|
$this->price = $price; |
64
|
|
|
$this->store = $store; |
65
|
|
|
$this->product = $product; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Processes one job iteration |
70
|
|
|
* @param array $job |
71
|
|
|
*/ |
72
|
|
|
public function process(array &$job) |
73
|
|
|
{ |
74
|
|
|
$this->job = &$job; |
75
|
|
|
|
76
|
|
|
$options = $this->job['data']['options']; |
77
|
|
|
$options['limit'] = array($this->job['done'], $this->job['data']['limit']); |
78
|
|
|
|
79
|
|
|
$items = (array) $this->product->getList($options); |
80
|
|
|
|
81
|
|
|
foreach ($items as $product) { |
82
|
|
|
$data = $this->prepare($product); |
83
|
|
|
gplcart_file_csv($this->job['data']['file'], $data, $this->job['data']['delimiter']); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (empty($items)) { |
87
|
|
|
$this->job['status'] = false; |
88
|
|
|
$this->job['done'] = $this->job['total']; |
89
|
|
|
} else { |
90
|
|
|
$this->job['done'] += count($items); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Prepares export data |
96
|
|
|
* @param array $product |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
protected function prepare(array $product) |
100
|
|
|
{ |
101
|
|
|
$data = array(); |
102
|
|
|
foreach ($this->job['data']['header'] as $key => $value) { |
103
|
|
|
$data[$key] = isset($product[$key]) ? $product[$key] : ''; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$this->prepareImages($data, $product); |
107
|
|
|
$this->preparePrice($data, $product); |
108
|
|
|
$this->prepareImages($data, $product); |
109
|
|
|
|
110
|
|
|
return $data; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Prepares prices |
115
|
|
|
* @param array $data |
116
|
|
|
* @param array $product |
117
|
|
|
*/ |
118
|
|
|
protected function preparePrice(array &$data, array $product) |
119
|
|
|
{ |
120
|
|
|
if (isset($data['price'])) { |
121
|
|
|
$data['price'] = $this->price->decimal($data['price'], $product['currency']); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Prepares images |
127
|
|
|
* @param array $data |
128
|
|
|
* @param array $product |
129
|
|
|
* @return null|array |
130
|
|
|
*/ |
131
|
|
|
protected function prepareImages(array &$data, array $product) |
132
|
|
|
{ |
133
|
|
|
if (!isset($data['images'])) { |
134
|
|
|
return null; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$images = $this->getImages($product); |
138
|
|
|
|
139
|
|
|
if (empty($images)) { |
140
|
|
|
return null; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$store = $this->store->get($product['store_id']); |
144
|
|
|
|
145
|
|
|
$paths = array(); |
146
|
|
|
|
147
|
|
|
foreach ($images as $image) { |
148
|
|
|
if (isset($store['domain'])) { |
149
|
|
|
$path = $this->store->getUrl($store); |
150
|
|
|
$paths[] = "$path/files/{$image['path']}"; |
151
|
|
|
} else { |
152
|
|
|
$paths[] = $image['path']; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$data['images'] = implode($this->job['data']['multiple'], $paths); |
157
|
|
|
return $data; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Returns an array of product images |
162
|
|
|
* @param array $product |
163
|
|
|
* @return array |
164
|
|
|
*/ |
165
|
|
|
protected function getImages(array $product) |
166
|
|
|
{ |
167
|
|
|
$options = array( |
168
|
|
|
'order' => 'asc', |
169
|
|
|
'sort' => 'weight', |
170
|
|
|
'file_type' => 'image', |
171
|
|
|
'entity' => 'product', |
172
|
|
|
'entity_id' => $product['product_id'] |
173
|
|
|
); |
174
|
|
|
|
175
|
|
|
return (array) $this->file->getList($options); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
} |
179
|
|
|
|