Skip to content

Commit b45300f

Browse files
committed
[ORDER] Refactored export of Orders
1 parent 0b0063f commit b45300f

File tree

7 files changed

+88
-69
lines changed

7 files changed

+88
-69
lines changed
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
namespace FireGento\FastSimpleExport\Model\Export\Adapter;
2+
namespace FireGento\FastSimpleExport\Model\Adapter;
33
use Magento\ImportExport\Model\Export\Adapter\AbstractAdapter;
44
use Magento\Framework\Filesystem;
55
use Magento\Framework\App\Filesystem\DirectoryList;
@@ -10,6 +10,7 @@ class ArrayAdapter extends \Magento\ImportExport\Model\Export\Adapter\AbstractAd
1010
* @var int
1111
*/
1212
protected $length = 0;
13+
protected $arr = array();
1314

1415
public function __construct(
1516
\Magento\Framework\Filesystem $filesystem,
@@ -20,10 +21,11 @@ public function __construct(
2021
}
2122
public function writeRow(array $rowData)
2223
{
24+
$this->arr[] = $rowData;
2325
$this->length++;
2426
}
2527
public function getContents()
2628
{
27-
return str_repeat("a\n",$this->length+1);
29+
return $this->arr;
2830
}
2931
}
Lines changed: 25 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
* * See LICENSE.md bundled with this module for license details.
66
*
77
*/
8-
namespace FireGento\FastSimpleExport\Model\Export;
8+
namespace FireGento\FastSimpleExport\Model\Entity\Order;
99

1010
/**
1111
* Export entity customer model
12-
*
13-
* @method \Magento\Customer\Model\ResourceModel\Attribute\Collection getAttributeCollection() getAttributeCollection()
1412
*/
13+
1514
class Order extends \Magento\ImportExport\Model\Export\Entity\AbstractEav
1615
{
1716
/**#@+
@@ -20,9 +19,6 @@ class Order extends \Magento\ImportExport\Model\Export\Entity\AbstractEav
2019
* Names that begins with underscore is not an attribute. This name convention is for
2120
* to avoid interference with same attribute name.
2221
*/
23-
const COLUMN_EMAIL = 'email';
24-
25-
const COLUMN_WEBSITE = '_website';
2622

2723
const COLUMN_STORE = '_store';
2824

@@ -31,14 +27,8 @@ class Order extends \Magento\ImportExport\Model\Export\Entity\AbstractEav
3127
/**#@+
3228
* Attribute collection name
3329
*/
34-
const ATTRIBUTE_COLLECTION_NAME = 'Magento\Customer\Model\ResourceModel\Attribute\Collection';
35-
36-
/**#@-*/
30+
const ATTRIBUTE_COLLECTION_NAME = 'FireGento\FastSimpleExport\Model\Entity\Order\OrderAttributeCollection';
3731

38-
/**#@+
39-
* XML path to page size parameter
40-
*/
41-
const XML_PATH_PAGE_SIZE = 'export/customer_page_size/customer';
4232

4333
/**#@-*/
4434

@@ -65,30 +55,30 @@ class Order extends \Magento\ImportExport\Model\Export\Entity\AbstractEav
6555
*
6656
* @var string[]
6757
*/
68-
protected $_indexValueAttributes = ['group_id', 'website_id', 'store_id'];
58+
protected $_indexValueAttributes = ['store_id'];
6959

7060
/**
7161
* Permanent entity columns.
7262
*
7363
* @var string[]
7464
*/
75-
protected $_permanentAttributes = [self::COLUMN_EMAIL, self::COLUMN_WEBSITE, self::COLUMN_STORE];
65+
protected $_permanentAttributes = [self::COLUMN_STORE];
7666

7767
/**
78-
* Customers whose data is exported
79-
*
80-
* @var \Magento\Customer\Model\ResourceModel\Customer\Collection
68+
* @var \Magento\Sales\Model\ResourceModel\Order\Collection|mixed
8169
*/
82-
protected $_customerCollection;
70+
protected $orderCollection;
71+
8372

8473
/**
74+
* Order constructor.
8575
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
8676
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
8777
* @param \Magento\ImportExport\Model\Export\Factory $collectionFactory
8878
* @param \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory $resourceColFactory
8979
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
9080
* @param \Magento\Eav\Model\Config $eavConfig
91-
* @param \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerColFactory
81+
* @param \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderFactory
9282
* @param array $data
9383
*/
9484
public function __construct(
@@ -98,9 +88,10 @@ public function __construct(
9888
\Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory $resourceColFactory,
9989
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
10090
\Magento\Eav\Model\Config $eavConfig,
101-
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $customerColFactory,
91+
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderFactory,
10292
array $data = []
103-
) {
93+
)
94+
{
10495
parent::__construct(
10596
$scopeConfig,
10697
$storeManager,
@@ -111,9 +102,7 @@ public function __construct(
111102
$data
112103
);
113104

114-
$this->_customerCollection = isset(
115-
$data['customer_collection']
116-
) ? $data['customer_collection'] : $customerColFactory->create();
105+
$this->orderCollection = $orderFactory->create();
117106

118107
$this->_initAttributeValues()->_initStores()->_initWebsites(true);
119108
}
@@ -125,33 +114,29 @@ public function __construct(
125114
*/
126115
public function export()
127116
{
128-
$this->_prepareEntityCollection($this->_getEntityCollection());
129117
$writer = $this->getWriter();
130-
131-
// create export file
132118
$writer->setHeaderCols($this->_getHeaderColumns());
133119
$this->_exportCollectionByPages($this->_getEntityCollection());
134-
135120
return $writer->getContents();
136121
}
137122

138123
/**
139-
* Get customers collection
140-
*
141-
* @return \Magento\Customer\Model\ResourceModel\Customer\Collection
124+
* {@inheritdoc}
142125
*/
143-
protected function _getEntityCollection()
126+
protected function _getHeaderColumns()
144127
{
145-
return $this->_customerCollection;
128+
$validAttributeCodes = $this->_getExportAttributeCodes();
129+
return array_merge($this->_permanentAttributes, $validAttributeCodes);
146130
}
147131

148132
/**
149-
* {@inheritdoc}
133+
* Get customers collection
134+
*
135+
* @return \Magento\Customer\Model\ResourceModel\Customer\Collection
150136
*/
151-
protected function _getHeaderColumns()
137+
protected function _getEntityCollection()
152138
{
153-
$validAttributeCodes = $this->_getExportAttributeCodes();
154-
return array_merge($this->_permanentAttributes, $validAttributeCodes, ['password']);
139+
return $this->orderCollection;
155140
}
156141

157142
/**
@@ -162,34 +147,11 @@ protected function _getHeaderColumns()
162147
*/
163148
public function exportItem($item)
164149
{
165-
$row = $this->_addAttributeValuesToRow($item);
166-
$row[self::COLUMN_WEBSITE] = $this->_websiteIdToCode[$item->getWebsiteId()];
150+
$row = $item->getData();
167151
$row[self::COLUMN_STORE] = $this->_storeIdToCode[$item->getStoreId()];
168-
169152
$this->getWriter()->writeRow($row);
170153
}
171154

172-
/**
173-
* Clean up already loaded attribute collection.
174-
*
175-
* @param \Magento\Framework\Data\Collection $collection
176-
* @return \Magento\Framework\Data\Collection
177-
*/
178-
public function filterAttributeCollection(\Magento\Framework\Data\Collection $collection)
179-
{
180-
/** @var $attribute \Magento\Customer\Model\Attribute */
181-
foreach (parent::filterAttributeCollection($collection) as $attribute) {
182-
if (!empty($this->_attributeOverrides[$attribute->getAttributeCode()])) {
183-
$data = $this->_attributeOverrides[$attribute->getAttributeCode()];
184-
185-
if (isset($data['options_method']) && method_exists($this, $data['options_method'])) {
186-
$data['filter_options'] = $this->{$data['options_method']}();
187-
}
188-
$attribute->addData($data);
189-
}
190-
}
191-
return $collection;
192-
}
193155

194156
/**
195157
* EAV entity type code getter.
@@ -198,7 +160,7 @@ public function filterAttributeCollection(\Magento\Framework\Data\Collection $co
198160
*/
199161
public function getEntityTypeCode()
200162
{
201-
return "sales_order";//$this->getAttributeCollection()->getEntityTypeCode();
163+
return $this->getAttributeCollection()->getEntityTypeCode();
202164
}
203165

204166
/**
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
namespace FireGento\FastSimpleExport\Model\Entity\Order;
3+
class OrderAttribute extends \Magento\Eav\Model\Entity\Attribute{
4+
5+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
namespace FireGento\FastSimpleExport\Model\Entity\Order;
3+
use Magento\Framework\Data\Collection\EntityFactoryInterface;
4+
class OrderAttributeCollection extends \Magento\Framework\Data\Collection
5+
{
6+
public function __construct(
7+
EntityFactoryInterface $entityFactory,
8+
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderFactory
9+
)
10+
{
11+
parent::__construct($entityFactory);
12+
$orderCollection = $orderFactory->create();
13+
$connection = $orderCollection->getResource()->getConnection();
14+
$fields = $connection->describeTable($orderCollection->getResource()->getMainTable());
15+
foreach(array_keys($fields) as $fieldName){
16+
$item = $entityFactory->create('\FireGento\FastSimpleExport\Model\Entity\Order\OrderAttribute');
17+
/** @var \\FireGento\FastSimpleExport\Model\Entity\Order\OrderAttribute $item */
18+
$item->setAttributeCode($fieldName);
19+
$this->addItem($item);
20+
21+
}
22+
23+
24+
25+
}
26+
27+
public function getEntityTypeCode(){
28+
return 'order';
29+
}
30+
}

Model/Export/Export.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
namespace FireGento\FastSimpleExport\Model\Export;
3+
class Export extends \Magento\ImportExport\Model\Export{
4+
/**
5+
* Export data.
6+
*
7+
* @return string
8+
* @throws \Magento\Framework\Exception\LocalizedException
9+
*/
10+
public function export()
11+
{
12+
if (isset($this->_data[self::FILTER_ELEMENT_GROUP])) {
13+
$this->addLogComment(__('Begin export of %1', $this->getEntity()));
14+
$result = $this->_getEntityAdapter()->setWriter($this->_getWriter())->export();
15+
return $result;
16+
} else {
17+
throw new \Magento\Framework\Exception\LocalizedException(__('Please provide filter data.'));
18+
}
19+
}
20+
}

Model/Exporter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ class Exporter
1313

1414
/**
1515
* Exporter constructor.
16-
* @param \Magento\ImportExport\Model\ExportFactory $exportFactory
16+
* @param \FireGento\FastSimpleExport\Model\Export\ExportFactory $exportFactory
1717
*/
1818
public function __construct(
19-
\Magento\ImportExport\Model\ExportFactory $exportFactory
19+
\FireGento\FastSimpleExport\Model\Export\ExportFactory $exportFactory
2020
)
2121
{
2222
$this->exportFactory = $exportFactory;

etc/export.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_ImportExport:etc/export.xsd">
9-
<entity name="sales_order" label="Sales Order" model="FireGento\FastSimpleExport\Model\Export\Order" entityAttributeFilterType="sales_order"/>
9+
<entity name="order" label="Sales Order" model="FireGento\FastSimpleExport\Model\Entity\Order\Order" entityAttributeFilterType="order"/>
1010

11-
<fileFormat name="array" label="Array" model="FireGento\FastSimpleExport\Model\Export\Adapter\ArrayAdapter" />
11+
<fileFormat name="array" label="Array" model="FireGento\FastSimpleExport\Model\Adapter\ArrayAdapter" />
1212
</config>

0 commit comments

Comments
 (0)