Skip to content

Commit 4cce2bc

Browse files
author
Ganin, Roman(rganin)
committed
Merge pull request #377 from magento-troll/MAGETWO-43102
[Troll] Sprint 52
2 parents 0e6dcf2 + 9829aa4 commit 4cce2bc

File tree

11 files changed

+121
-57
lines changed

11 files changed

+121
-57
lines changed

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

+8-6
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function execute()
100100

101101
$data['general'] = $this->getRequest()->getPostValue();
102102
$isNewCategory = !isset($data['general']['entity_id']);
103-
$data = $this->stringToBoolConverting($this->stringToBoolInputs, $data);
103+
$data = $this->stringToBoolConverting($data);
104104
$data = $this->imagePreprocessing($data);
105105
$storeId = isset($data['general']['store_id']) ? $data['general']['store_id'] : null;
106106
$parentId = isset($data['general']['parent']) ? $data['general']['parent'] : null;
@@ -234,8 +234,7 @@ public function imagePreprocessing($data)
234234
{
235235
if (!isset($_FILES) || (isset($_FILES['image']) && $_FILES['image']['name'] === '' )) {
236236
unset($data['general']['image']);
237-
if (
238-
isset($data['general']['savedImage']['delete']) &&
237+
if (isset($data['general']['savedImage']['delete']) &&
239238
$data['general']['savedImage']['delete']
240239
) {
241240
$data['general']['image']['delete'] = $data['general']['savedImage']['delete'];
@@ -247,17 +246,20 @@ public function imagePreprocessing($data)
247246
/**
248247
* Converting inputs from string to boolean
249248
*
250-
* @param array $stringToBoolInputs
251249
* @param array $data
250+
* @param array $stringToBoolInputs
252251
*
253252
* @return array
254253
*/
255-
public function stringToBoolConverting($stringToBoolInputs, $data)
254+
public function stringToBoolConverting($data, $stringToBoolInputs = null)
256255
{
256+
if (null === $stringToBoolInputs) {
257+
$stringToBoolInputs = $this->stringToBoolInputs;
258+
}
257259
foreach ($stringToBoolInputs as $key => $value) {
258260
if (is_array($value)) {
259261
if (isset($data[$key])) {
260-
$data[$key] = $this->stringToBoolConverting($value, $data[$key]);
262+
$data[$key] = $this->stringToBoolConverting($data[$key], $value);
261263
}
262264
} else {
263265
if (isset($data[$value])) {

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -571,9 +571,9 @@ public function getStoreIds()
571571
public function getStoreId()
572572
{
573573
if ($this->hasData('store_id')) {
574-
return $this->_getData('store_id');
574+
return (int)$this->_getData('store_id');
575575
}
576-
return $this->_storeManager->getStore()->getId();
576+
return (int)$this->_storeManager->getStore()->getId();
577577
}
578578

579579
/**
@@ -1261,6 +1261,7 @@ public function toFlatArray()
12611261
}
12621262

12631263
//@codeCoverageIgnoreStart
1264+
12641265
/**
12651266
* Set parent category ID
12661267
*

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

+36-8
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
use Magento\Eav\Model\Config;
1212
use Magento\Eav\Model\Entity\Type;
1313
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
14+
use Magento\Store\Model\Store;
1415
use Magento\Store\Model\StoreManagerInterface;
1516
use Magento\Ui\Component\Form\Field;
1617
use Magento\Ui\DataProvider\EavValidationRules;
18+
use Magento\Catalog\Model\CategoryFactory;
19+
use Magento\Framework\Exception\NoSuchEntityException;
1720

1821
/**
1922
* Class DataProvider
@@ -22,6 +25,11 @@
2225
*/
2326
class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
2427
{
28+
/**
29+
* @var string
30+
*/
31+
protected $requestScopeFieldName = 'store';
32+
2533
/**
2634
* @var array
2735
*/
@@ -86,8 +94,7 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
8694
/**
8795
* @var \Magento\Framework\App\RequestInterface
8896
*/
89-
private $request;
90-
97+
protected $request;
9198

9299
/**
93100
* @var Config
@@ -100,7 +107,12 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
100107
private $storeManager;
101108

102109
/**
103-
* Constructor
110+
* @var CategoryFactory
111+
*/
112+
private $categoryFactory;
113+
114+
/**
115+
* DataProvider constructor
104116
*
105117
* @param string $name
106118
* @param string $primaryFieldName
@@ -111,9 +123,9 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
111123
* @param \Magento\Framework\Registry $registry
112124
* @param Config $eavConfig
113125
* @param \Magento\Framework\App\RequestInterface $request
126+
* @param CategoryFactory $categoryFactory
114127
* @param array $meta
115128
* @param array $data
116-
* @throws \Magento\Framework\Exception\LocalizedException
117129
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
118130
*/
119131
public function __construct(
@@ -126,6 +138,7 @@ public function __construct(
126138
\Magento\Framework\Registry $registry,
127139
Config $eavConfig,
128140
\Magento\Framework\App\RequestInterface $request,
141+
CategoryFactory $categoryFactory,
129142
array $meta = [],
130143
array $data = []
131144
) {
@@ -136,6 +149,7 @@ public function __construct(
136149
$this->registry = $registry;
137150
$this->storeManager = $storeManager;
138151
$this->request = $request;
152+
$this->categoryFactory = $categoryFactory;
139153
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
140154
$this->meta = $this->prepareMeta($this->meta);
141155
}
@@ -282,7 +296,7 @@ protected function addUseConfigSettings($categoryData)
282296
protected function addUseDefaultSettings($category, $categoryData)
283297
{
284298
if ($category->getExistsStoreValueFlag('url_key') ||
285-
$category->getStoreId() === \Magento\Store\Model\Store::DEFAULT_STORE_ID
299+
$category->getStoreId() === Store::DEFAULT_STORE_ID
286300
) {
287301
$categoryData['use_default']['url_key'] = false;
288302
} else {
@@ -296,10 +310,25 @@ protected function addUseDefaultSettings($category, $categoryData)
296310
* Get current category
297311
*
298312
* @return Category
313+
* @throws NoSuchEntityException
299314
*/
300315
public function getCurrentCategory()
301316
{
302-
return $this->registry->registry('category');
317+
$category = $this->registry->registry('category');
318+
if ($category) {
319+
return $category;
320+
}
321+
$requestId = $this->request->getParam($this->requestFieldName);
322+
$requestScope = $this->request->getParam($this->requestScopeFieldName, Store::DEFAULT_STORE_ID);
323+
if ($requestId) {
324+
$category = $this->categoryFactory->create();
325+
$category->setStoreId($requestScope);
326+
$category->load($requestId);
327+
if (!$category->getId()) {
328+
throw NoSuchEntityException::singleField('id', $requestId);
329+
}
330+
}
331+
return $category;
303332
}
304333

305334
/**
@@ -349,7 +378,6 @@ protected function filterFields($categoryData)
349378
public function getDefaultMetaData($result)
350379
{
351380
$result['parent']['default'] = (int)$this->request->getParam('parent');
352-
$result['is_anchor']['default'] = false;
353381
$result['use_config.available_sort_by']['default'] = true;
354382
$result['use_config.default_sort_by']['default'] = true;
355383
$result['use_config.filter_price_range']['default'] = true;
@@ -418,14 +446,14 @@ protected function getFieldsMap()
418446
[
419447
'custom_use_parent_settings',
420448
'custom_apply_to_products',
449+
'custom_design',
421450
'page_layout',
422451
'custom_layout_update',
423452
],
424453
'schedule_design_update' =>
425454
[
426455
'custom_design_from',
427456
'custom_design_to',
428-
'custom_design',
429457
],
430458
'category_view_optimization' =>
431459
[

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

+9-15
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ class Category extends AbstractResource
7373
*/
7474
protected $entityManager;
7575

76+
/**
77+
* @var Category\AggregateCount
78+
*/
79+
protected $aggregateCount;
80+
7681
/**
7782
* Category constructor.
7883
* @param \Magento\Eav\Model\Entity\Context $context
@@ -82,6 +87,7 @@ class Category extends AbstractResource
8287
* @param Category\TreeFactory $categoryTreeFactory
8388
* @param Category\CollectionFactory $categoryCollectionFactory
8489
* @param EntityManager $entityManager
90+
* @param Category\AggregateCount $aggregateCount
8591
* @param array $data
8692
*/
8793
public function __construct(
@@ -92,6 +98,7 @@ public function __construct(
9298
\Magento\Catalog\Model\ResourceModel\Category\TreeFactory $categoryTreeFactory,
9399
\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
94100
EntityManager $entityManager,
101+
Category\AggregateCount $aggregateCount,
95102
$data = []
96103
) {
97104
parent::__construct(
@@ -104,8 +111,8 @@ public function __construct(
104111
$this->_categoryCollectionFactory = $categoryCollectionFactory;
105112
$this->_eventManager = $eventManager;
106113
$this->entityManager = $entityManager;
107-
108114
$this->connectionName = 'catalog';
115+
$this->aggregateCount = $aggregateCount;
109116
}
110117

111118
/**
@@ -184,20 +191,8 @@ protected function _getTree()
184191
protected function _beforeDelete(\Magento\Framework\DataObject $object)
185192
{
186193
parent::_beforeDelete($object);
187-
188-
/**
189-
* Update children count for all parent categories
190-
*/
191-
$parentIds = $object->getParentIds();
192-
if ($parentIds) {
193-
$childDecrease = $object->getChildrenCount() + 1;
194-
// +1 is itself
195-
$data = ['children_count' => new \Zend_Db_Expr('children_count - ' . $childDecrease)];
196-
$where = ['entity_id IN(?)' => $parentIds];
197-
$this->getConnection()->update($this->getEntityTable(), $data, $where);
198-
}
194+
$this->aggregateCount->processDelete($object);
199195
$this->deleteChildren($object);
200-
return $this;
201196
}
202197

203198
/**
@@ -1009,7 +1004,6 @@ public function load($object, $entityId, $attributes = [])
10091004
*/
10101005
$this->entityManager->load(CategoryInterface::class, $object, $entityId);
10111006

1012-
10131007
if (!$this->entityManager->has(\Magento\Catalog\Api\Data\CategoryInterface::class, $entityId)) {
10141008
$object->isObjectNew(true);
10151009
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Model\ResourceModel\Category;
7+
8+
use Magento\Catalog\Model\Category;
9+
10+
/**
11+
* Class AggregateCount
12+
*/
13+
class AggregateCount
14+
{
15+
/**
16+
* @param Category $category
17+
* @return void
18+
*/
19+
public function processDelete(Category $category)
20+
{
21+
/** @var \Magento\Catalog\Model\ResourceModel\Category $resourceModel */
22+
$resourceModel = $category->getResource();
23+
/**
24+
* Update children count for all parent categories
25+
*/
26+
$parentIds = $category->getParentIds();
27+
if ($parentIds) {
28+
$childDecrease = $category->getChildrenCount() + 1;
29+
// +1 is itself
30+
$data = ['children_count' => new \Zend_Db_Expr('children_count - ' . $childDecrease)];
31+
$where = ['entity_id IN(?)' => $parentIds];
32+
$resourceModel->getConnection()->update($resourceModel->getEntityTable(), $data, $where);
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)