Skip to content

Commit 50c33e5

Browse files
author
He, Joan(johe)
committed
Merge pull request #172 from magento-firedrakes/MAGETWO-45273
[Firedrakes] MAGETWO-45273: UI component Export button
2 parents 320f41d + 62b37aa commit 50c33e5

File tree

8 files changed

+150
-8
lines changed

8 files changed

+150
-8
lines changed

app/code/Magento/Customer/etc/adminhtml/di.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,14 @@
1313
</arguments>
1414
</type>
1515
<preference for="Magento\Framework\Session\SessionManagerInterface" type="Magento\Backend\Model\Session" />
16+
<type name="Magento\Ui\Model\Export\MetadataProvider">
17+
<arguments>
18+
<argument name="data" xsi:type="array">
19+
<item name="customer_listing" xsi:type="array">
20+
<item name="created_at" xsi:type="string">created_at</item>
21+
<item name="last_visit_at" xsi:type="string">last_visit_at</item>
22+
</item>
23+
</argument>
24+
</arguments>
25+
</type>
1626
</config>

app/code/Magento/Sales/etc/adminhtml/di.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,37 @@
1313
</argument>
1414
</arguments>
1515
</type>
16+
<type name="Magento\Ui\Model\Export\MetadataProvider">
17+
<arguments>
18+
<argument name="data" xsi:type="array">
19+
<item name="sales_order_grid" xsi:type="array">
20+
<item name="created_at" xsi:type="string">created_at</item>
21+
</item>
22+
<item name="sales_order_creditmemo_grid" xsi:type="array">
23+
<item name="created_at" xsi:type="string">created_at</item>
24+
<item name="order_created_at" xsi:type="string">order_created_at</item>
25+
</item>
26+
<item name="sales_order_invoice_grid" xsi:type="array">
27+
<item name="created_at" xsi:type="string">created_at</item>
28+
<item name="order_created_at" xsi:type="string">order_created_at</item>
29+
</item>
30+
<item name="sales_order_shipment_grid" xsi:type="array">
31+
<item name="created_at" xsi:type="string">created_at</item>
32+
<item name="order_created_at" xsi:type="string">order_created_at</item>
33+
</item>
34+
<item name="sales_order_view_creditmemo_grid" xsi:type="array">
35+
<item name="created_at" xsi:type="string">created_at</item>
36+
<item name="order_created_at" xsi:type="string">order_created_at</item>
37+
</item>
38+
<item name="sales_order_view_invoice_grid" xsi:type="array">
39+
<item name="created_at" xsi:type="string">created_at</item>
40+
<item name="order_created_at" xsi:type="string">order_created_at</item>
41+
</item>
42+
<item name="sales_order_view_shipment_grid" xsi:type="array">
43+
<item name="created_at" xsi:type="string">created_at</item>
44+
<item name="order_created_at" xsi:type="string">order_created_at</item>
45+
</item>
46+
</argument>
47+
</arguments>
48+
</type>
1649
</config>

app/code/Magento/Ui/Model/Export/ConvertToCsv.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public function getCsvFile()
6666
$stream->lock();
6767
$stream->writeCsv($this->metadataProvider->getHeaders($component));
6868
foreach ($searchResult->getItems() as $document) {
69+
$this->metadataProvider->convertDate($document, $component->getName());
6970
$stream->writeCsv($this->metadataProvider->getRowData($document, $fields, $options));
7071
}
7172
$stream->unlock();

app/code/Magento/Ui/Model/Export/ConvertToXml.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ public function getXmlFile()
128128
/** @var SearchResultInterface $searchResult */
129129
$searchResult = $component->getContext()->getDataProvider()->getSearchResult();
130130

131+
$this->prepareItems($component->getName(), $searchResult->getItems());
132+
131133
/** @var SearchResultIterator $searchResultIterator */
132134
$searchResultIterator = $this->iteratorFactory->create(['items' => $searchResult->getItems()]);
133135

@@ -153,4 +155,16 @@ public function getXmlFile()
153155
'rm' => true // can delete file after use
154156
];
155157
}
158+
159+
/**
160+
* @param string $componentName
161+
* @param array $items
162+
* @return void
163+
*/
164+
protected function prepareItems($componentName, array $items = [])
165+
{
166+
foreach ($items as $document) {
167+
$this->metadataProvider->convertDate($document, $componentName);
168+
}
169+
}
156170
}

app/code/Magento/Ui/Model/Export/MetadataProvider.php

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Magento\Ui\Component\Filters\Type\Select;
1212
use Magento\Ui\Component\Listing\Columns;
1313
use Magento\Ui\Component\MassAction\Filter;
14+
use Magento\Framework\Locale\ResolverInterface;
15+
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
1416

1517
class MetadataProvider
1618
{
@@ -24,13 +26,45 @@ class MetadataProvider
2426
*/
2527
protected $columns;
2628

29+
/**
30+
* @var TimezoneInterface
31+
*/
32+
protected $localeDate;
33+
34+
/**
35+
* @var string
36+
*/
37+
protected $locale;
38+
39+
/**
40+
* @var string
41+
*/
42+
protected $dateFormat;
43+
44+
/**
45+
* @var array
46+
*/
47+
protected $data;
48+
2749
/**
2850
* @param Filter $filter
51+
* @param TimezoneInterface $localeDate
52+
* @param ResolverInterface $localeResolver
53+
* @param string $dateFormat
54+
* @param array $data
2955
*/
3056
public function __construct(
31-
Filter $filter
57+
Filter $filter,
58+
TimezoneInterface $localeDate,
59+
ResolverInterface $localeResolver,
60+
$dateFormat = 'M j, Y H:i:s A',
61+
array $data = []
3262
) {
3363
$this->filter = $filter;
64+
$this->localeDate = $localeDate;
65+
$this->locale = $localeResolver->getLocale();
66+
$this->dateFormat = $dateFormat;
67+
$this->data = $data;
3468
}
3569

3670
/**
@@ -191,4 +225,30 @@ public function getOptions()
191225
}
192226
return $options;
193227
}
228+
229+
/**
230+
* Convert document date(UTC) fields to default scope specified
231+
*
232+
* @param \Magento\Framework\Api\Search\DocumentInterface $document
233+
* @param string $componentName
234+
* @return void
235+
*/
236+
public function convertDate($document, $componentName)
237+
{
238+
if (!isset($this->data[$componentName])) {
239+
return;
240+
}
241+
foreach ($this->data[$componentName] as $field) {
242+
$fieldValue = $document->getData($field);
243+
if (!$fieldValue) {
244+
continue;
245+
}
246+
$convertedDate = $this->localeDate->date(
247+
new \DateTime($fieldValue, new \DateTimeZone('UTC')),
248+
$this->locale,
249+
true
250+
);
251+
$document->setData($field, $convertedDate->format($this->dateFormat));
252+
}
253+
}
194254
}

app/code/Magento/Ui/Test/Unit/Model/Export/ConvertToCsvTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ public function testGetCsvFile()
131131
->method('getRowData')
132132
->with($document, [], [])
133133
->willReturn($data);
134+
$this->metadataProvider->expects($this->once())
135+
->method('convertDate')
136+
->with($document, $componentName);
134137

135138
$result = $this->model->getCsvFile();
136139
$this->assertTrue(is_array($result));

app/code/Magento/Ui/Test/Unit/Model/Export/ConvertToXmlTest.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ public function testGetXmlFile()
145145
{
146146
$componentName = 'component_name';
147147

148-
$this->mockComponent($componentName);
148+
$document = $this->getMockBuilder('Magento\Framework\Api\Search\DocumentInterface')
149+
->getMockForAbstractClass();
150+
151+
$this->mockComponent($componentName, $document);
149152
$this->mockStream();
150153
$this->mockFilter();
151154
$this->mockDirectory();
@@ -155,6 +158,9 @@ public function testGetXmlFile()
155158
->method('getHeaders')
156159
->with($this->component)
157160
->willReturn([]);
161+
$this->metadataProvider->expects($this->once())
162+
->method('convertDate')
163+
->with($document, $componentName);
158164

159165
$result = $this->model->getXmlFile();
160166
$this->assertTrue(is_array($result));
@@ -216,8 +222,9 @@ protected function mockExcel($componentName)
216222

217223
/**
218224
* @param string $componentName
225+
* @param null|object $document
219226
*/
220-
protected function mockComponent($componentName)
227+
protected function mockComponent($componentName, $document = null)
221228
{
222229
$context = $this->getMockBuilder('Magento\Framework\View\Element\UiComponent\ContextInterface')
223230
->setMethods(['getDataProvider'])
@@ -248,9 +255,18 @@ protected function mockComponent($componentName)
248255
->method('getSearchResult')
249256
->willReturn($searchResult);
250257

251-
$searchResult->expects($this->once())
252-
->method('getItems')
253-
->willReturn([]);
258+
if ($document) {
259+
$searchResult->expects($this->at(0))
260+
->method('getItems')
261+
->willReturn([$document]);
262+
$searchResult->expects($this->at(1))
263+
->method('getItems')
264+
->willReturn([]);
265+
} else {
266+
$searchResult->expects($this->at(0))
267+
->method('getItems')
268+
->willReturn([]);
269+
}
254270
}
255271

256272
protected function mockFilter()

app/code/Magento/Ui/Test/Unit/Model/Export/MetadataProviderTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Ui\Component\MassAction\Filter;
99
use Magento\Ui\Model\Export\MetadataProvider;
10+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1011

1112
class MetadataProviderTest extends \PHPUnit_Framework_TestCase
1213
{
@@ -26,8 +27,12 @@ public function setUp()
2627
->disableOriginalConstructor()
2728
->getMock();
2829

29-
$this->model = new MetadataProvider(
30-
$this->filter
30+
$objectManager = new ObjectManager($this);
31+
$this->model = $objectManager->getObject(
32+
'Magento\Ui\Model\Export\MetadataProvider',
33+
[
34+
'filter' => $this->filter,
35+
]
3136
);
3237
}
3338

0 commit comments

Comments
 (0)