Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Export.php
Go to the documentation of this file.
1 <?php
8 
18 {
19  const FILTER_ELEMENT_GROUP = 'export_filter';
20 
21  const FILTER_ELEMENT_SKIP = 'skip_attr';
22 
26  const FIELDS_ENCLOSURE = 'fields_enclosure';
27 
31  const FILTER_TYPE_SELECT = 'select';
32 
33  const FILTER_TYPE_MULTISELECT = 'multiselect';
34 
35  const FILTER_TYPE_INPUT = 'input';
36 
37  const FILTER_TYPE_DATE = 'date';
38 
39  const FILTER_TYPE_NUMBER = 'number';
40 
46  protected $_entityAdapter;
47 
53  protected $_writer;
54 
58  protected $_exportConfig;
59 
63  protected $_entityFactory;
64 
68  protected $_exportAdapterFac;
69 
73  private static $backendTypeToFilterMapper = [
74  'datetime' => self::FILTER_TYPE_DATE,
75  'decimal' => self::FILTER_TYPE_NUMBER,
76  'int' => self::FILTER_TYPE_NUMBER,
77  'varchar' => self::FILTER_TYPE_INPUT,
78  'text' => self::FILTER_TYPE_INPUT
79  ];
80 
89  public function __construct(
90  \Psr\Log\LoggerInterface $logger,
91  \Magento\Framework\Filesystem $filesystem,
92  \Magento\ImportExport\Model\Export\ConfigInterface $exportConfig,
93  \Magento\ImportExport\Model\Export\Entity\Factory $entityFactory,
94  \Magento\ImportExport\Model\Export\Adapter\Factory $exportAdapterFac,
95  array $data = []
96  ) {
97  $this->_exportConfig = $exportConfig;
98  $this->_entityFactory = $entityFactory;
99  $this->_exportAdapterFac = $exportAdapterFac;
100  parent::__construct($logger, $filesystem, $data);
101  }
102 
110  protected function _getEntityAdapter()
111  {
112  if (!$this->_entityAdapter) {
113  $entities = $this->_exportConfig->getEntities();
114 
115  if (isset($entities[$this->getEntity()])) {
116  try {
117  $this->_entityAdapter = $this->_entityFactory->create($entities[$this->getEntity()]['model']);
118  } catch (\Exception $e) {
119  $this->_logger->critical($e);
120  throw new \Magento\Framework\Exception\LocalizedException(
121  __('Please enter a correct entity model.')
122  );
123  }
124  if (!$this->_entityAdapter instanceof \Magento\ImportExport\Model\Export\Entity\AbstractEntity &&
125  !$this->_entityAdapter instanceof \Magento\ImportExport\Model\Export\AbstractEntity
126  ) {
127  throw new \Magento\Framework\Exception\LocalizedException(
128  __(
129  'The entity adapter object must be an instance of %1 or %2.',
130  \Magento\ImportExport\Model\Export\Entity\AbstractEntity::class,
131  \Magento\ImportExport\Model\Export\AbstractEntity::class
132  )
133  );
134  }
135 
136  // check for entity codes integrity
137  if ($this->getEntity() != $this->_entityAdapter->getEntityTypeCode()) {
138  throw new \Magento\Framework\Exception\LocalizedException(
139  __('The input entity code is not equal to entity adapter code.')
140  );
141  }
142  } else {
143  throw new \Magento\Framework\Exception\LocalizedException(__('Please enter a correct entity.'));
144  }
145  $this->_entityAdapter->setParameters($this->getData());
146  }
147  return $this->_entityAdapter;
148  }
149 
156  protected function _getWriter()
157  {
158  if (!$this->_writer) {
159  $fileFormats = $this->_exportConfig->getFileFormats();
160 
161  if (isset($fileFormats[$this->getFileFormat()])) {
162  try {
163  $this->_writer = $this->_exportAdapterFac->create($fileFormats[$this->getFileFormat()]['model']);
164  } catch (\Exception $e) {
165  $this->_logger->critical($e);
166  throw new \Magento\Framework\Exception\LocalizedException(
167  __('Please enter a correct entity model.')
168  );
169  }
170  if (!$this->_writer instanceof \Magento\ImportExport\Model\Export\Adapter\AbstractAdapter) {
171  throw new \Magento\Framework\Exception\LocalizedException(
172  __(
173  'The adapter object must be an instance of %1.',
174  \Magento\ImportExport\Model\Export\Adapter\AbstractAdapter::class
175  )
176  );
177  }
178  } else {
179  throw new \Magento\Framework\Exception\LocalizedException(__('Please correct the file format.'));
180  }
181  }
182  return $this->_writer;
183  }
184 
191  public function export()
192  {
193  if (isset($this->_data[self::FILTER_ELEMENT_GROUP])) {
194  $this->addLogComment(__('Begin export of %1', $this->getEntity()));
195  $result = $this->_getEntityAdapter()->setWriter($this->_getWriter())->export();
196  $countRows = substr_count(trim($result), "\n");
197  if (!$countRows) {
198  throw new \Magento\Framework\Exception\LocalizedException(__('There is no data for the export.'));
199  }
200  if ($result) {
201  $this->addLogComment([__('Exported %1 rows.', $countRows), __('The export is finished.')]);
202  }
203  return $result;
204  } else {
205  throw new \Magento\Framework\Exception\LocalizedException(__('Please provide filter data.'));
206  }
207  }
208 
215  public function filterAttributeCollection(\Magento\Framework\Data\Collection $collection)
216  {
217  return $this->_getEntityAdapter()->filterAttributeCollection($collection);
218  }
219 
228  public static function getAttributeFilterType(\Magento\Eav\Model\Entity\Attribute $attribute)
229  {
230  if ($attribute->usesSource() || $attribute->getFilterOptions()) {
231  return 'multiselect' == $attribute->getFrontendInput() ?
233  }
234 
235  if (isset(self::$backendTypeToFilterMapper[$attribute->getBackendType()])) {
236  return self::$backendTypeToFilterMapper[$attribute->getBackendType()];
237  }
238 
239  if ($attribute->isStatic()) {
241  }
242 
243  throw new \Magento\Framework\Exception\LocalizedException(
244  __('We can\'t determine the attribute filter type.')
245  );
246  }
247 
255  public static function getStaticAttributeFilterType(\Magento\Eav\Model\Entity\Attribute $attribute)
256  {
257  if (in_array($attribute->getAttributeCode(), ['category_ids', 'media_gallery'])) {
259  }
260  $columns = $attribute->getFlatColumns();
261  if (empty($columns)) {
263  }
264  switch ($columns[$attribute->getAttributeCode()]['type']) {
265  case \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER:
266  case \Magento\Framework\DB\Ddl\Table::TYPE_BIGINT:
268  break;
269  case \Magento\Framework\DB\Ddl\Table::TYPE_DATE:
270  case \Magento\Framework\DB\Ddl\Table::TYPE_DATETIME:
271  case \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP:
273  break;
274  default:
276  }
277  return $type;
278  }
279 
285  public function getContentType()
286  {
287  return $this->_getWriter()->getContentType();
288  }
289 
296  public function getEntity()
297  {
298  if (empty($this->_data['entity'])) {
299  throw new \Magento\Framework\Exception\LocalizedException(__('Entity is unknown'));
300  }
301  return $this->_data['entity'];
302  }
303 
310  {
311  return $this->_getEntityAdapter()->getAttributeCollection();
312  }
313 
320  public function getFileFormat()
321  {
322  if (empty($this->_data['file_format'])) {
323  throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t identify this file format.'));
324  }
325  return $this->_data['file_format'];
326  }
327 
333  public function getFileName()
334  {
335  $fileName = null;
336  $entityAdapter = $this->_getEntityAdapter();
337  if ($entityAdapter instanceof \Magento\ImportExport\Model\Export\AbstractEntity) {
338  $fileName = $entityAdapter->getFileName();
339  }
340  if (!$fileName) {
341  $fileName = $this->getEntity();
342  }
343  return $fileName . '_' . date('Ymd_His') . '.' . $this->_getWriter()->getFileExtension();
344  }
345 }
getData($key='', $index=null)
Definition: DataObject.php:119
static getAttributeFilterType(\Magento\Eav\Model\Entity\Attribute $attribute)
Definition: Export.php:228
__()
Definition: __.php:13
$logger
$columns
Definition: default.phtml:15
$type
Definition: item.phtml:13
$fileName
Definition: translate.phtml:15
static getStaticAttributeFilterType(\Magento\Eav\Model\Entity\Attribute $attribute)
Definition: Export.php:255
__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=[])
Definition: Export.php:89
filterAttributeCollection(\Magento\Framework\Data\Collection $collection)
Definition: Export.php:215
$filesystem