Skip to content

Commit 18aefd8

Browse files
author
Bohdan Korablov
committed
Merge remote-tracking branch 'mainlinec/develop' into MAGETWO-51898
2 parents a2fcc14 + 6a377bb commit 18aefd8

File tree

88 files changed

+1143
-379
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+1143
-379
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Category.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ abstract class Category extends \Magento\Backend\App\Action
1717
*/
1818
const ADMIN_RESOURCE = 'Magento_Catalog::categories';
1919

20+
/**
21+
* @var \Magento\Framework\Stdlib\DateTime\Filter\DateTime
22+
*/
23+
private $dateTimeFilter;
24+
2025
/**
2126
* Initialize requested category and put it into registry.
2227
* Root category can be returned, if inappropriate store/category is specified
@@ -107,4 +112,41 @@ protected function ajaxRequestResponse($category, $resultPage)
107112
$resultJson->setData($eventResponse->getData());
108113
return $resultJson;
109114
}
115+
116+
/**
117+
* @return \Magento\Framework\Stdlib\DateTime\Filter\DateTime
118+
*
119+
* @deprecated
120+
*/
121+
private function getDateTimeFilter()
122+
{
123+
if ($this->dateTimeFilter === null) {
124+
$this->dateTimeFilter = \Magento\Framework\App\ObjectManager::getInstance()
125+
->get(\Magento\Framework\Stdlib\DateTime\Filter\DateTime::class);
126+
}
127+
return $this->dateTimeFilter;
128+
}
129+
130+
/**
131+
* Datetime data preprocessing
132+
*
133+
* @param \Magento\Catalog\Model\Category $category
134+
* @param array $postData
135+
*
136+
* @return array
137+
*/
138+
protected function dateTimePreprocessing($category, $postData)
139+
{
140+
$dateFieldFilters = [];
141+
$attributes = $category->getAttributes();
142+
foreach ($attributes as $attrKey => $attribute) {
143+
if ($attribute->getBackend()->getType() == 'datetime') {
144+
if (array_key_exists($attrKey, $postData) && $postData[$attrKey] != '') {
145+
$dateFieldFilters[$attrKey] = $this->getDateTimeFilter();
146+
}
147+
}
148+
}
149+
$inputFilter = new \Zend_Filter_Input($dateFieldFilters, [], $postData);
150+
return $inputFilter->getUnescaped();
151+
}
110152
}

app/code/Magento/Catalog/Controller/Adminhtml/Category/Add.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ public function execute()
5050
return $resultRedirect->setPath('catalog/*/', ['_current' => true, 'id' => null]);
5151
}
5252

53+
/**
54+
* Check if there are data in session (if there was an exception on saving category)
55+
*/
56+
$categoryData = $this->_getSession()->getCategoryData(true);
57+
if (is_array($categoryData)) {
58+
unset($categoryData['image']);
59+
$category->addData($categoryData);
60+
}
61+
5362
$resultPageFactory = $this->_objectManager->get('Magento\Framework\View\Result\PageFactory');
5463
/** @var \Magento\Backend\Model\View\Result\Page $resultPage */
5564
$resultPage = $resultPageFactory->create();

app/code/Magento/Catalog/Controller/Adminhtml/Category/Edit.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,16 @@ public function execute()
8080
}
8181

8282
/**
83-
* Check if we have data in session (if during category save was exception)
83+
* Check if there are data in session (if there was an exception on saving category)
8484
*/
85-
$data = $this->_getSession()->getCategoryData(true);
86-
if (isset($data['general'])) {
87-
if (isset($data['general']['image']['delete'])) {
88-
$data['general']['image'] = null;
85+
$categoryData = $this->_getSession()->getCategoryData(true);
86+
if (is_array($categoryData)) {
87+
if (isset($categoryData['image']['delete'])) {
88+
$categoryData['image'] = null;
8989
} else {
90-
unset($data['general']['image']);
90+
unset($categoryData['image']);
9191
}
92-
$category->addData($data['general']);
92+
$category->addData($categoryData);
9393
}
9494

9595
/** @var \Magento\Backend\Model\View\Result\Page $resultPage */

app/code/Magento/Catalog/Controller/Adminhtml/Category/Save.php

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
/**
1111
* Class Save
12-
*
12+
*
1313
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1414
*/
1515
class Save extends \Magento\Catalog\Controller\Adminhtml\Category
@@ -34,15 +34,13 @@ class Save extends \Magento\Catalog\Controller\Adminhtml\Category
3434
* @var array
3535
*/
3636
protected $stringToBoolInputs = [
37-
'general' => [
38-
'custom_use_parent_settings',
39-
'custom_apply_to_products',
40-
'is_active',
41-
'include_in_menu',
42-
'is_anchor',
43-
'use_default' => ['url_key'],
44-
'use_config' => ['available_sort_by', 'filter_price_range', 'default_sort_by']
45-
]
37+
'custom_use_parent_settings',
38+
'custom_apply_to_products',
39+
'is_active',
40+
'include_in_menu',
41+
'is_anchor',
42+
'use_default' => ['url_key'],
43+
'use_config' => ['available_sort_by', 'filter_price_range', 'default_sort_by']
4644
];
4745

4846
/**
@@ -110,15 +108,18 @@ public function execute()
110108
}
111109

112110
$data['general'] = $this->getRequest()->getPostValue();
113-
$isNewCategory = !isset($data['general']['entity_id']);
114-
$data = $this->stringToBoolConverting($data);
115-
$data = $this->imagePreprocessing($data);
116-
$storeId = isset($data['general']['store_id']) ? $data['general']['store_id'] : null;
111+
$categoryPostData = $data['general'];
112+
113+
$isNewCategory = !isset($categoryPostData['entity_id']);
114+
$categoryPostData = $this->stringToBoolConverting($categoryPostData);
115+
$categoryPostData = $this->imagePreprocessing($categoryPostData);
116+
$categoryPostData = $this->dateTimePreprocessing($category, $categoryPostData);
117+
$storeId = isset($categoryPostData['store_id']) ? $categoryPostData['store_id'] : null;
117118
$store = $this->storeManager->getStore($storeId);
118119
$this->storeManager->setCurrentStore($store->getCode());
119-
$parentId = isset($data['general']['parent']) ? $data['general']['parent'] : null;
120-
if ($data['general']) {
121-
$category->addData($this->_filterCategoryPostData($data['general']));
120+
$parentId = isset($categoryPostData['parent']) ? $categoryPostData['parent'] : null;
121+
if ($categoryPostData) {
122+
$category->addData($this->_filterCategoryPostData($categoryPostData));
122123
if ($isNewCategory) {
123124
$parentCategory = $this->getParentCategory($parentId, $storeId);
124125
$category->setPath($parentCategory->getPath());
@@ -128,10 +129,10 @@ public function execute()
128129
/**
129130
* Process "Use Config Settings" checkboxes
130131
*/
131-
$generalPost = $data['general'];
132+
132133
$useConfig = [];
133-
if (isset($generalPost['use_config']) && !empty($generalPost['use_config'])) {
134-
foreach ($generalPost['use_config'] as $attributeCode => $attributeValue) {
134+
if (isset($categoryPostData['use_config']) && !empty($categoryPostData['use_config'])) {
135+
foreach ($categoryPostData['use_config'] as $attributeCode => $attributeValue) {
135136
if ($attributeValue) {
136137
$useConfig[] = $attributeCode;
137138
$category->setData($attributeCode, null);
@@ -141,11 +142,11 @@ public function execute()
141142

142143
$category->setAttributeSetId($category->getDefaultAttributeSetId());
143144

144-
if (isset($data['general']['category_products'])
145-
&& is_string($data['general']['category_products'])
145+
if (isset($categoryPostData['category_products'])
146+
&& is_string($categoryPostData['category_products'])
146147
&& !$category->getProductsReadonly()
147148
) {
148-
$products = json_decode($data['general']['category_products'], true);
149+
$products = json_decode($categoryPostData['category_products'], true);
149150
$category->setPostedProducts($products);
150151
}
151152
$this->_eventManager->dispatch(
@@ -156,8 +157,8 @@ public function execute()
156157
/**
157158
* Check "Use Default Value" checkboxes values
158159
*/
159-
if (isset($generalPost['use_default']) && !empty($generalPost['use_default'])) {
160-
foreach ($generalPost['use_default'] as $attributeCode => $attributeValue) {
160+
if (isset($categoryPostData['use_default']) && !empty($categoryPostData['use_default'])) {
161+
foreach ($categoryPostData['use_default'] as $attributeCode => $attributeValue) {
161162
if ($attributeValue) {
162163
$category->setData($attributeCode, null);
163164
}
@@ -197,15 +198,15 @@ public function execute()
197198
} catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
198199
$this->messageManager->addError($e->getMessage());
199200
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
200-
$this->_getSession()->setCategoryData($data);
201+
$this->_getSession()->setCategoryData($categoryPostData);
201202
} catch (\Magento\Framework\Exception\LocalizedException $e) {
202203
$this->messageManager->addError($e->getMessage());
203204
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
204-
$this->_getSession()->setCategoryData($data);
205+
$this->_getSession()->setCategoryData($categoryPostData);
205206
} catch (\Exception $e) {
206207
$this->messageManager->addError(__('Something went wrong while saving the category.'));
207208
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
208-
$this->_getSession()->setCategoryData($data);
209+
$this->_getSession()->setCategoryData($categoryPostData);
209210
}
210211
}
211212

@@ -248,9 +249,9 @@ public function execute()
248249
*/
249250
public function imagePreprocessing($data)
250251
{
251-
if (empty($data['general']['image'])) {
252-
unset($data['general']['image']);
253-
$data['general']['image']['delete'] = true;
252+
if (empty($data['image'])) {
253+
unset($data['image']);
254+
$data['image']['delete'] = true;
254255
}
255256
return $data;
256257
}

app/code/Magento/Catalog/Controller/Adminhtml/Product/Save.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ private function getCategoryLinkManagement()
251251

252252
/**
253253
* @return StoreManagerInterface
254+
* @deprecated
254255
*/
255256
private function getStoreManager()
256257
{
@@ -265,6 +266,7 @@ private function getStoreManager()
265266
* Retrieve data persistor
266267
*
267268
* @return DataPersistorInterface|mixed
269+
* @deprecated
268270
*/
269271
protected function getDataPersistor()
270272
{

app/code/Magento/Catalog/Controller/Adminhtml/Product/Validate.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Backend\App\Action;
1010
use Magento\Catalog\Controller\Adminhtml\Product;
1111
use Magento\Framework\App\ObjectManager;
12+
use Magento\Store\Model\StoreManagerInterface;
1213

1314
/**
1415
* Product validate
@@ -47,6 +48,11 @@ class Validate extends \Magento\Catalog\Controller\Adminhtml\Product
4748
*/
4849
protected $initializationHelper;
4950

51+
/**
52+
* @var StoreManagerInterface
53+
*/
54+
private $storeManager;
55+
5056
/**
5157
* @param Action\Context $context
5258
* @param Builder $productBuilder
@@ -91,10 +97,12 @@ public function execute()
9197
if ($productData && !isset($productData['stock_data']['use_config_manage_stock'])) {
9298
$productData['stock_data']['use_config_manage_stock'] = 0;
9399
}
100+
$storeId = $this->getRequest()->getParam('store', 0);
101+
$store = $this->getStoreManager()->getStore($storeId);
102+
$this->getStoreManager()->setCurrentStore($store->getCode());
94103
/* @var $product \Magento\Catalog\Model\Product */
95104
$product = $this->productFactory->create();
96105
$product->setData('_edit_mode', true);
97-
$storeId = $this->getRequest()->getParam('store');
98106
if ($storeId) {
99107
$product->setStoreId($storeId);
100108
}
@@ -137,6 +145,19 @@ public function execute()
137145
return $this->resultJsonFactory->create()->setData($response);
138146
}
139147

148+
/**
149+
* @return StoreManagerInterface
150+
* @deprecated
151+
*/
152+
private function getStoreManager()
153+
{
154+
if (null === $this->storeManager) {
155+
$this->storeManager = \Magento\Framework\App\ObjectManager::getInstance()
156+
->get('Magento\Store\Model\StoreManagerInterface');
157+
}
158+
return $this->storeManager;
159+
}
160+
140161
/**
141162
* @return Initialization\Helper
142163
* @deprecated

app/code/Magento/Catalog/Model/Category/Attribute/Backend/Image.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ public function afterSave($object)
111111
} catch (\Exception $e) {
112112
$this->_logger->critical($e);
113113
}
114+
} elseif (isset($value[0]['name'])) {
115+
$object->setData($this->getAttribute()->getName(), $value[0]['name']);
116+
$this->getAttribute()->getEntity()->saveAttribute($object, $this->getAttribute()->getName());
114117
}
115118
return $this;
116119
}

app/code/Magento/Catalog/Model/Category/DataProvider.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,7 @@ public function getData()
201201
return $this->loadedData;
202202
}
203203
$category = $this->getCurrentCategory();
204-
if (!$category->getId()) {
205-
return [];
206-
} else {
204+
if ($category) {
207205
$categoryData = $category->getData();
208206
$categoryData = $this->addUseDefaultSettings($category, $categoryData);
209207
$categoryData = $this->addUseConfigSettings($categoryData);

app/code/Magento/Catalog/Model/Product/Option.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ class Option extends AbstractExtensibleModel implements ProductCustomOptionInter
6363
*/
6464
protected $optionRepository;
6565

66+
/**
67+
* Option type percent
68+
*/
69+
protected static $typePercent = 'percent';
70+
6671
/**#@+
6772
* Constants
6873
*/
@@ -424,7 +429,7 @@ public function afterSave()
424429
*/
425430
public function getPrice($flag = false)
426431
{
427-
if ($flag && $this->getPriceType() == 'percent') {
432+
if ($flag && $this->getPriceType() == self::$typePercent) {
428433
$basePrice = $this->getProduct()->getPriceInfo()->getPrice(BasePrice::PRICE_CODE)->getValue();
429434
$price = $basePrice * ($this->_getData(self::KEY_PRICE) / 100);
430435
return $price;
@@ -827,6 +832,21 @@ public function getExtensionAttributes()
827832
return $this->_getExtensionAttributes();
828833
}
829834

835+
/**
836+
* Return regular price.
837+
*
838+
* @return float|int
839+
*/
840+
public function getRegularPrice()
841+
{
842+
if ($this->getPriceType() == self::$typePercent) {
843+
$basePrice = $this->getProduct()->getPriceInfo()->getPrice('regular_price')->getAmount()->getValue();
844+
$price = $basePrice * ($this->_getData(self::KEY_PRICE) / 100);
845+
return $price;
846+
}
847+
return $this->_getData(self::KEY_PRICE);
848+
}
849+
830850
/**
831851
* @param Product $product
832852
* @return \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection

app/code/Magento/Catalog/Model/ProductRepository.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -522,14 +522,11 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
522522
$product->setCanSaveCustomOptions(true);
523523
}
524524

525-
$useValidation = \Magento\Store\Model\Store::ADMIN_CODE === $this->storeManager->getStore()->getCode();
526-
if ($useValidation) {
527-
$validationResult = $this->resourceModel->validate($product);
528-
if (true !== $validationResult) {
529-
throw new \Magento\Framework\Exception\CouldNotSaveException(
530-
__('Invalid product data: %1', implode(',', $validationResult))
531-
);
532-
}
525+
$validationResult = $this->resourceModel->validate($product);
526+
if (true !== $validationResult) {
527+
throw new \Magento\Framework\Exception\CouldNotSaveException(
528+
__('Invalid product data: %1', implode(',', $validationResult))
529+
);
533530
}
534531

535532
try {

0 commit comments

Comments
 (0)