Skip to content

Commit f4da450

Browse files
author
Oleksii Korshenko
authored
Merge pull request #1609 from magento-engcom/2.1-develop-prs
#11378 Save region correctly to save sales address from admin [backport] by @raumatbel #11451 [Backport 2.1-develop] #11328 : app:config:dump adds extra space every time in multiline array value by @adrian-martinez-interactiv4
2 parents 580bcd1 + f5ec69e commit f4da450

File tree

3 files changed

+161
-27
lines changed

3 files changed

+161
-27
lines changed

app/code/Magento/Sales/Controller/Adminhtml/Order/AddressSave.php

Lines changed: 101 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,105 @@
44
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
55
* See COPYING.txt for license details.
66
*/
7+
78
namespace Magento\Sales\Controller\Adminhtml\Order;
89

9-
class AddressSave extends \Magento\Sales\Controller\Adminhtml\Order
10+
use Magento\Backend\App\Action\Context;
11+
use Magento\Backend\Model\View\Result\Redirect;
12+
use Magento\Directory\Model\RegionFactory;
13+
use Magento\Sales\Api\OrderManagementInterface;
14+
use Magento\Sales\Api\OrderRepositoryInterface;
15+
use Magento\Sales\Api\Data\OrderAddressInterface;
16+
use Magento\Sales\Controller\Adminhtml\Order;
17+
use Magento\Sales\Model\Order\Address as ModelOrderAddress;
18+
use Psr\Log\LoggerInterface;
19+
use Magento\Framework\Registry;
20+
use Magento\Framework\App\Response\Http\FileFactory;
21+
use Magento\Framework\Translate\InlineInterface;
22+
use Magento\Framework\View\Result\PageFactory;
23+
use Magento\Framework\Controller\Result\JsonFactory;
24+
use Magento\Framework\View\Result\LayoutFactory;
25+
use Magento\Framework\Controller\Result\RawFactory;
26+
use Magento\Framework\Exception\LocalizedException;
27+
use Magento\Framework\App\ObjectManager;
28+
29+
/**
30+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
31+
*/
32+
class AddressSave extends Order
1033
{
1134
/**
1235
* Authorization level of a basic admin session
1336
*
1437
* @see _isAllowed()
1538
*/
1639
const ADMIN_RESOURCE = 'Magento_Sales::actions_edit';
40+
41+
/**
42+
* @var RegionFactory
43+
*/
44+
private $regionFactory;
45+
46+
/**
47+
* @param Context $context
48+
* @param Registry $coreRegistry
49+
* @param FileFactory $fileFactory
50+
* @param InlineInterface $translateInline
51+
* @param PageFactory $resultPageFactory
52+
* @param JsonFactory $resultJsonFactory
53+
* @param LayoutFactory $resultLayoutFactory
54+
* @param RawFactory $resultRawFactory
55+
* @param OrderManagementInterface $orderManagement
56+
* @param OrderRepositoryInterface $orderRepository
57+
* @param LoggerInterface $logger
58+
* @param RegionFactory|null $regionFactory
59+
*
60+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
61+
*/
62+
public function __construct(
63+
Context $context,
64+
Registry $coreRegistry,
65+
FileFactory $fileFactory,
66+
InlineInterface $translateInline,
67+
PageFactory $resultPageFactory,
68+
JsonFactory $resultJsonFactory,
69+
LayoutFactory $resultLayoutFactory,
70+
RawFactory $resultRawFactory,
71+
OrderManagementInterface $orderManagement,
72+
OrderRepositoryInterface $orderRepository,
73+
LoggerInterface $logger,
74+
RegionFactory $regionFactory = null
75+
) {
76+
$this->regionFactory = $regionFactory ?: ObjectManager::getInstance()->get(RegionFactory::class);
77+
parent::__construct(
78+
$context,
79+
$coreRegistry,
80+
$fileFactory,
81+
$translateInline,
82+
$resultPageFactory,
83+
$resultJsonFactory,
84+
$resultLayoutFactory,
85+
$resultRawFactory,
86+
$orderManagement,
87+
$orderRepository,
88+
$logger
89+
);
90+
}
1791

1892
/**
1993
* Save order address
2094
*
21-
* @return \Magento\Backend\Model\View\Result\Redirect
95+
* @return Redirect
2296
*/
2397
public function execute()
2498
{
2599
$addressId = $this->getRequest()->getParam('address_id');
26-
/** @var $address \Magento\Sales\Api\Data\OrderAddressInterface|\Magento\Sales\Model\Order\Address */
27-
$address = $this->_objectManager->create('Magento\Sales\Api\Data\OrderAddressInterface')->load($addressId);
100+
/** @var $address OrderAddressInterface|ModelOrderAddress */
101+
$address = $this->_objectManager->create(
102+
OrderAddressInterface::class
103+
)->load($addressId);
28104
$data = $this->getRequest()->getPostValue();
105+
$data = $this->updateRegionData($data);
29106
$resultRedirect = $this->resultRedirectFactory->create();
30107
if ($data && $address->getId()) {
31108
$address->addData($data);
@@ -38,15 +115,34 @@ public function execute()
38115
]
39116
);
40117
$this->messageManager->addSuccess(__('You updated the order address.'));
118+
41119
return $resultRedirect->setPath('sales/*/view', ['order_id' => $address->getParentId()]);
42-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
120+
} catch (LocalizedException $e) {
43121
$this->messageManager->addError($e->getMessage());
44122
} catch (\Exception $e) {
45123
$this->messageManager->addException($e, __('We can\'t update the order address right now.'));
46124
}
125+
47126
return $resultRedirect->setPath('sales/*/address', ['address_id' => $address->getId()]);
48127
} else {
49128
return $resultRedirect->setPath('sales/*/');
50129
}
51130
}
131+
132+
/**
133+
* Update region data
134+
*
135+
* @param array $attributeValues
136+
* @return array
137+
*/
138+
private function updateRegionData($attributeValues)
139+
{
140+
if (!empty($attributeValues['region_id'])) {
141+
$newRegion = $this->regionFactory->create()->load($attributeValues['region_id']);
142+
$attributeValues['region_code'] = $newRegion->getCode();
143+
$attributeValues['region'] = $newRegion->getDefaultName();
144+
}
145+
146+
return $attributeValues;
147+
}
52148
}

lib/internal/Magento/Framework/App/DeploymentConfig/Writer/PhpFormatter.php

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,48 @@ class PhpFormatter implements FormatterInterface
2121
public function format($data, array $comments = [])
2222
{
2323
if (!empty($comments) && is_array($data)) {
24-
$elements = array();
24+
return "<?php\nreturn array (\n" . $this->formatData($data, $comments) . "\n);\n";
25+
}
26+
return "<?php\nreturn " . var_export($data, true) . ";\n";
27+
}
28+
29+
/**
30+
* Format supplied data
31+
*
32+
* @param string[] $data
33+
* @param string[] $comments
34+
* @param string $prefix
35+
* @return string
36+
*/
37+
private function formatData($data, $comments = [], $prefix = ' ')
38+
{
39+
$elements = [];
40+
41+
if (is_array($data)) {
2542
foreach ($data as $key => $value) {
26-
$comment = ' ';
2743
if (!empty($comments[$key])) {
28-
$comment = " /**\n * " . str_replace("\n", "\n * ", var_export($comments[$key], true)) . "\n */\n";
44+
$elements[] = $prefix . '/**';
45+
$elements[] = $prefix . ' * For the section: ' . $key;
46+
47+
foreach (explode("\n", $comments[$key]) as $commentLine) {
48+
$elements[] = $prefix . ' * ' . $commentLine;
49+
}
50+
51+
$elements[] = $prefix . " */";
52+
}
53+
54+
$elements[] = $prefix . var_export($key, true) . ' => ' .
55+
(!is_array($value) ? var_export($value, true) . ',' : '');
56+
57+
if (is_array($value)) {
58+
$elements[] = $prefix . 'array (';
59+
$elements[] = $this->formatData($value, [], ' ' . $prefix);
60+
$elements[] = $prefix . '),';
2961
}
30-
$space = is_array($value) ? " \n" : ' ';
31-
$elements[] = $comment . var_export($key, true) . ' =>' . $space . var_export($value, true);
3262
}
33-
return "<?php\nreturn array (\n" . implode(",\n", str_replace("\n", "\n ", $elements)) . "\n);\n";
63+
return implode("\n", $elements);
3464
}
35-
return "<?php\nreturn " . var_export($data, true) . ";\n";
65+
66+
return var_export($data, true);
3667
}
3768
}

lib/internal/Magento/Framework/App/Test/Unit/DeploymentConfig/Writer/PhpFormatterTest.php

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
76
namespace Magento\Framework\App\Test\Unit\DeploymentConfig\Writer;
87

9-
use \Magento\Framework\App\DeploymentConfig\Writer\PhpFormatter;
8+
use Magento\Framework\App\DeploymentConfig\Writer\PhpFormatter;
109

1110
class PhpFormatterTest extends \PHPUnit_Framework_TestCase
1211
{
1312
/**
1413
* @dataProvider formatWithCommentDataProvider
15-
* @param string|array $data
16-
* @param array $comments
14+
* @param string[] $data
15+
* @param string[] $comments
1716
* @param string $expectedResult
1817
*/
1918
public function testFormat($data, $comments, $expectedResult)
@@ -22,6 +21,9 @@ public function testFormat($data, $comments, $expectedResult)
2221
$this->assertEquals($expectedResult, $formatter->format($data, $comments));
2322
}
2423

24+
/**
25+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
26+
*/
2527
public function formatWithCommentDataProvider()
2628
{
2729
$array = [
@@ -45,9 +47,9 @@ public function formatWithCommentDataProvider()
4547
];
4648
$comments1 = ['ns2' => 'comment for namespace 2'];
4749
$comments2 = [
48-
'ns1' => 'comment for namespace 1',
49-
'ns2' => "comment for namespace 2.\nNext comment for namespace 2",
50-
'ns3' => 'comment for namespace 3',
50+
'ns1' => 'comment for\' namespace 1',
51+
'ns2' => "comment for namespace 2.\nNext comment for' namespace 2",
52+
'ns3' => 'comment for" namespace 3',
5153
'ns4' => 'comment for namespace 4',
5254
'ns5' => 'comment for unexisted namespace 5',
5355
];
@@ -68,7 +70,8 @@ public function formatWithCommentDataProvider()
6870
),
6971
),
7072
/**
71-
* 'comment for namespace 2'
73+
* For the section: ns2
74+
* comment for namespace 2
7275
*/
7376
'ns2' =>
7477
array (
@@ -78,15 +81,16 @@ public function formatWithCommentDataProvider()
7881
),
7982
),
8083
'ns3' => 'just text',
81-
'ns4' => 'just text'
84+
'ns4' => 'just text',
8285
);
8386
8487
TEXT;
8588
$expectedResult2 = <<<TEXT
8689
<?php
8790
return array (
8891
/**
89-
* 'comment for namespace 1'
92+
* For the section: ns1
93+
* comment for' namespace 1
9094
*/
9195
'ns1' =>
9296
array (
@@ -102,8 +106,9 @@ public function formatWithCommentDataProvider()
102106
),
103107
),
104108
/**
105-
* 'comment for namespace 2.
106-
* Next comment for namespace 2'
109+
* For the section: ns2
110+
* comment for namespace 2.
111+
* Next comment for' namespace 2
107112
*/
108113
'ns2' =>
109114
array (
@@ -113,13 +118,15 @@ public function formatWithCommentDataProvider()
113118
),
114119
),
115120
/**
116-
* 'comment for namespace 3'
121+
* For the section: ns3
122+
* comment for" namespace 3
117123
*/
118124
'ns3' => 'just text',
119125
/**
120-
* 'comment for namespace 4'
126+
* For the section: ns4
127+
* comment for namespace 4
121128
*/
122-
'ns4' => 'just text'
129+
'ns4' => 'just text',
123130
);
124131
125132
TEXT;

0 commit comments

Comments
 (0)