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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\ImportExport\Model;
/**
* Export model
*
* @api
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @since 100.0.2
*/
class Export extends \Magento\ImportExport\Model\AbstractModel
{
const FILTER_ELEMENT_GROUP = 'export_filter';
const FILTER_ELEMENT_SKIP = 'skip_attr';
/**
* Allow multiple values wrapping in double quotes for additional attributes.
*/
const FIELDS_ENCLOSURE = 'fields_enclosure';
/**
* Filter fields types.
*/
const FILTER_TYPE_SELECT = 'select';
const FILTER_TYPE_MULTISELECT = 'multiselect';
const FILTER_TYPE_INPUT = 'input';
const FILTER_TYPE_DATE = 'date';
const FILTER_TYPE_NUMBER = 'number';
/**
* Entity adapter.
*
* @var \Magento\ImportExport\Model\Export\Entity\AbstractEntity
*/
protected $_entityAdapter;
/**
* Writer object instance.
*
* @var \Magento\ImportExport\Model\Export\Adapter\AbstractAdapter
*/
protected $_writer;
/**
* @var \Magento\ImportExport\Model\Export\ConfigInterface
*/
protected $_exportConfig;
/**
* @var \Magento\ImportExport\Model\Export\Entity\Factory
*/
protected $_entityFactory;
/**
* @var \Magento\ImportExport\Model\Export\Adapter\Factory
*/
protected $_exportAdapterFac;
/**
* @var array
*/
private static $backendTypeToFilterMapper = [
'datetime' => self::FILTER_TYPE_DATE,
'decimal' => self::FILTER_TYPE_NUMBER,
'int' => self::FILTER_TYPE_NUMBER,
'varchar' => self::FILTER_TYPE_INPUT,
'text' => self::FILTER_TYPE_INPUT
];
/**
* @param \Psr\Log\LoggerInterface $logger
* @param \Magento\Framework\Filesystem $filesystem
* @param \Magento\ImportExport\Model\Export\ConfigInterface $exportConfig
* @param \Magento\ImportExport\Model\Export\Entity\Factory $entityFactory
* @param \Magento\ImportExport\Model\Export\Adapter\Factory $exportAdapterFac
* @param array $data
*/
public function __construct(
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\Filesystem $filesystem,
\Magento\ImportExport\Model\Export\ConfigInterface $exportConfig,
\Magento\ImportExport\Model\Export\Entity\Factory $entityFactory,
\Magento\ImportExport\Model\Export\Adapter\Factory $exportAdapterFac,
array $data = []
) {
$this->_exportConfig = $exportConfig;
$this->_entityFactory = $entityFactory;
$this->_exportAdapterFac = $exportAdapterFac;
parent::__construct($logger, $filesystem, $data);
}
/**
* Create instance of entity adapter and return it
*
* @return \Magento\ImportExport\Model\Export\Entity\AbstractEntity
* |\Magento\ImportExport\Model\Export\AbstractEntity
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function _getEntityAdapter()
{
if (!$this->_entityAdapter) {
$entities = $this->_exportConfig->getEntities();
if (isset($entities[$this->getEntity()])) {
try {
$this->_entityAdapter = $this->_entityFactory->create($entities[$this->getEntity()]['model']);
} catch (\Exception $e) {
$this->_logger->critical($e);
throw new \Magento\Framework\Exception\LocalizedException(
__('Please enter a correct entity model.')
);
}
if (!$this->_entityAdapter instanceof \Magento\ImportExport\Model\Export\Entity\AbstractEntity &&
!$this->_entityAdapter instanceof \Magento\ImportExport\Model\Export\AbstractEntity
) {
throw new \Magento\Framework\Exception\LocalizedException(
__(
'The entity adapter object must be an instance of %1 or %2.',
\Magento\ImportExport\Model\Export\Entity\AbstractEntity::class,
\Magento\ImportExport\Model\Export\AbstractEntity::class
)
);
}
// check for entity codes integrity
if ($this->getEntity() != $this->_entityAdapter->getEntityTypeCode()) {
throw new \Magento\Framework\Exception\LocalizedException(
__('The input entity code is not equal to entity adapter code.')
);
}
} else {
throw new \Magento\Framework\Exception\LocalizedException(__('Please enter a correct entity.'));
}
$this->_entityAdapter->setParameters($this->getData());
}
return $this->_entityAdapter;
}
/**
* Get writer object.
*
* @return \Magento\ImportExport\Model\Export\Adapter\AbstractAdapter
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function _getWriter()
{
if (!$this->_writer) {
$fileFormats = $this->_exportConfig->getFileFormats();
if (isset($fileFormats[$this->getFileFormat()])) {
try {
$this->_writer = $this->_exportAdapterFac->create($fileFormats[$this->getFileFormat()]['model']);
} catch (\Exception $e) {
$this->_logger->critical($e);
throw new \Magento\Framework\Exception\LocalizedException(
__('Please enter a correct entity model.')
);
}
if (!$this->_writer instanceof \Magento\ImportExport\Model\Export\Adapter\AbstractAdapter) {
throw new \Magento\Framework\Exception\LocalizedException(
__(
'The adapter object must be an instance of %1.',
\Magento\ImportExport\Model\Export\Adapter\AbstractAdapter::class
)
);
}
} else {
throw new \Magento\Framework\Exception\LocalizedException(__('Please correct the file format.'));
}
}
return $this->_writer;
}
/**
* Export data.
*
* @return string
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function export()
{
if (isset($this->_data[self::FILTER_ELEMENT_GROUP])) {
$this->addLogComment(__('Begin export of %1', $this->getEntity()));
$result = $this->_getEntityAdapter()->setWriter($this->_getWriter())->export();
$countRows = substr_count(trim($result), "\n");
if (!$countRows) {
throw new \Magento\Framework\Exception\LocalizedException(__('There is no data for the export.'));
}
if ($result) {
$this->addLogComment([__('Exported %1 rows.', $countRows), __('The export is finished.')]);
}
return $result;
} else {
throw new \Magento\Framework\Exception\LocalizedException(__('Please provide filter data.'));
}
}
/**
* Clean up already loaded attribute collection.
*
* @param \Magento\Framework\Data\Collection $collection
* @return \Magento\Framework\Data\Collection
*/
public function filterAttributeCollection(\Magento\Framework\Data\Collection $collection)
{
return $this->_getEntityAdapter()->filterAttributeCollection($collection);
}
/**
* Determine filter type for specified attribute.
*
* @static
* @param \Magento\Eav\Model\Entity\Attribute $attribute
* @return string
* @throws \Magento\Framework\Exception\LocalizedException
*/
public static function getAttributeFilterType(\Magento\Eav\Model\Entity\Attribute $attribute)
{
if ($attribute->usesSource() || $attribute->getFilterOptions()) {
return 'multiselect' == $attribute->getFrontendInput() ?
self::FILTER_TYPE_MULTISELECT : self::FILTER_TYPE_SELECT;
}
if (isset(self::$backendTypeToFilterMapper[$attribute->getBackendType()])) {
return self::$backendTypeToFilterMapper[$attribute->getBackendType()];
}
if ($attribute->isStatic()) {
return self::getStaticAttributeFilterType($attribute);
}
throw new \Magento\Framework\Exception\LocalizedException(
__('We can\'t determine the attribute filter type.')
);
}
/**
* Determine filter type for static attribute.
*
* @static
* @param \Magento\Eav\Model\Entity\Attribute $attribute
* @return string
*/
public static function getStaticAttributeFilterType(\Magento\Eav\Model\Entity\Attribute $attribute)
{
if (in_array($attribute->getAttributeCode(), ['category_ids', 'media_gallery'])) {
return self::FILTER_TYPE_INPUT;
}
$columns = $attribute->getFlatColumns();
if (empty($columns)) {
return self::FILTER_TYPE_INPUT;
}
switch ($columns[$attribute->getAttributeCode()]['type']) {
case \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER:
case \Magento\Framework\DB\Ddl\Table::TYPE_BIGINT:
$type = self::FILTER_TYPE_NUMBER;
break;
case \Magento\Framework\DB\Ddl\Table::TYPE_DATE:
case \Magento\Framework\DB\Ddl\Table::TYPE_DATETIME:
case \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP:
$type = self::FILTER_TYPE_DATE;
break;
default:
$type = self::FILTER_TYPE_INPUT;
}
return $type;
}
/**
* MIME-type for 'Content-Type' header.
*
* @return string
*/
public function getContentType()
{
return $this->_getWriter()->getContentType();
}
/**
* Override standard entity getter.
*
* @return string
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getEntity()
{
if (empty($this->_data['entity'])) {
throw new \Magento\Framework\Exception\LocalizedException(__('Entity is unknown'));
}
return $this->_data['entity'];
}
/**
* Entity attributes collection getter.
*
* @return \Magento\Framework\Data\Collection
*/
public function getEntityAttributeCollection()
{
return $this->_getEntityAdapter()->getAttributeCollection();
}
/**
* Override standard entity getter.
*
* @return string
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getFileFormat()
{
if (empty($this->_data['file_format'])) {
throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t identify this file format.'));
}
return $this->_data['file_format'];
}
/**
* Return file name for downloading.
*
* @return string
*/
public function getFileName()
{
$fileName = null;
$entityAdapter = $this->_getEntityAdapter();
if ($entityAdapter instanceof \Magento\ImportExport\Model\Export\AbstractEntity) {
$fileName = $entityAdapter->getFileName();
}
if (!$fileName) {
$fileName = $this->getEntity();
}
return $fileName . '_' . date('Ymd_His') . '.' . $this->_getWriter()->getFileExtension();
}
}