Skip to content

Commit

Permalink
Merge pull request #377 from magento-troll/MAGETWO-43102
Browse files Browse the repository at this point in the history
[Troll] Sprint 52
  • Loading branch information
Ganin, Roman(rganin) committed Feb 22, 2016
2 parents 0e6dcf2 + 9829aa4 commit 4cce2bc
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 57 deletions.
14 changes: 8 additions & 6 deletions app/code/Magento/Catalog/Controller/Adminhtml/Category/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function execute()

$data['general'] = $this->getRequest()->getPostValue();
$isNewCategory = !isset($data['general']['entity_id']);
$data = $this->stringToBoolConverting($this->stringToBoolInputs, $data);
$data = $this->stringToBoolConverting($data);
$data = $this->imagePreprocessing($data);
$storeId = isset($data['general']['store_id']) ? $data['general']['store_id'] : null;
$parentId = isset($data['general']['parent']) ? $data['general']['parent'] : null;
Expand Down Expand Up @@ -234,8 +234,7 @@ public function imagePreprocessing($data)
{
if (!isset($_FILES) || (isset($_FILES['image']) && $_FILES['image']['name'] === '' )) {
unset($data['general']['image']);
if (
isset($data['general']['savedImage']['delete']) &&
if (isset($data['general']['savedImage']['delete']) &&
$data['general']['savedImage']['delete']
) {
$data['general']['image']['delete'] = $data['general']['savedImage']['delete'];
Expand All @@ -247,17 +246,20 @@ public function imagePreprocessing($data)
/**
* Converting inputs from string to boolean
*
* @param array $stringToBoolInputs
* @param array $data
* @param array $stringToBoolInputs
*
* @return array
*/
public function stringToBoolConverting($stringToBoolInputs, $data)
public function stringToBoolConverting($data, $stringToBoolInputs = null)
{
if (null === $stringToBoolInputs) {
$stringToBoolInputs = $this->stringToBoolInputs;
}
foreach ($stringToBoolInputs as $key => $value) {
if (is_array($value)) {
if (isset($data[$key])) {
$data[$key] = $this->stringToBoolConverting($value, $data[$key]);
$data[$key] = $this->stringToBoolConverting($data[$key], $value);
}
} else {
if (isset($data[$value])) {
Expand Down
5 changes: 3 additions & 2 deletions app/code/Magento/Catalog/Model/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -571,9 +571,9 @@ public function getStoreIds()
public function getStoreId()
{
if ($this->hasData('store_id')) {
return $this->_getData('store_id');
return (int)$this->_getData('store_id');
}
return $this->_storeManager->getStore()->getId();
return (int)$this->_storeManager->getStore()->getId();
}

/**
Expand Down Expand Up @@ -1261,6 +1261,7 @@ public function toFlatArray()
}

//@codeCoverageIgnoreStart

/**
* Set parent category ID
*
Expand Down
44 changes: 36 additions & 8 deletions app/code/Magento/Catalog/Model/Category/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
use Magento\Eav\Model\Config;
use Magento\Eav\Model\Entity\Type;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Ui\Component\Form\Field;
use Magento\Ui\DataProvider\EavValidationRules;
use Magento\Catalog\Model\CategoryFactory;
use Magento\Framework\Exception\NoSuchEntityException;

/**
* Class DataProvider
Expand All @@ -22,6 +25,11 @@
*/
class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{
/**
* @var string
*/
protected $requestScopeFieldName = 'store';

/**
* @var array
*/
Expand Down Expand Up @@ -86,8 +94,7 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
/**
* @var \Magento\Framework\App\RequestInterface
*/
private $request;

protected $request;

/**
* @var Config
Expand All @@ -100,7 +107,12 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
private $storeManager;

/**
* Constructor
* @var CategoryFactory
*/
private $categoryFactory;

/**
* DataProvider constructor
*
* @param string $name
* @param string $primaryFieldName
Expand All @@ -111,9 +123,9 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
* @param \Magento\Framework\Registry $registry
* @param Config $eavConfig
* @param \Magento\Framework\App\RequestInterface $request
* @param CategoryFactory $categoryFactory
* @param array $meta
* @param array $data
* @throws \Magento\Framework\Exception\LocalizedException
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -126,6 +138,7 @@ public function __construct(
\Magento\Framework\Registry $registry,
Config $eavConfig,
\Magento\Framework\App\RequestInterface $request,
CategoryFactory $categoryFactory,
array $meta = [],
array $data = []
) {
Expand All @@ -136,6 +149,7 @@ public function __construct(
$this->registry = $registry;
$this->storeManager = $storeManager;
$this->request = $request;
$this->categoryFactory = $categoryFactory;
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
$this->meta = $this->prepareMeta($this->meta);
}
Expand Down Expand Up @@ -282,7 +296,7 @@ protected function addUseConfigSettings($categoryData)
protected function addUseDefaultSettings($category, $categoryData)
{
if ($category->getExistsStoreValueFlag('url_key') ||
$category->getStoreId() === \Magento\Store\Model\Store::DEFAULT_STORE_ID
$category->getStoreId() === Store::DEFAULT_STORE_ID
) {
$categoryData['use_default']['url_key'] = false;
} else {
Expand All @@ -296,10 +310,25 @@ protected function addUseDefaultSettings($category, $categoryData)
* Get current category
*
* @return Category
* @throws NoSuchEntityException
*/
public function getCurrentCategory()
{
return $this->registry->registry('category');
$category = $this->registry->registry('category');
if ($category) {
return $category;
}
$requestId = $this->request->getParam($this->requestFieldName);
$requestScope = $this->request->getParam($this->requestScopeFieldName, Store::DEFAULT_STORE_ID);
if ($requestId) {
$category = $this->categoryFactory->create();
$category->setStoreId($requestScope);
$category->load($requestId);
if (!$category->getId()) {
throw NoSuchEntityException::singleField('id', $requestId);
}
}
return $category;
}

/**
Expand Down Expand Up @@ -349,7 +378,6 @@ protected function filterFields($categoryData)
public function getDefaultMetaData($result)
{
$result['parent']['default'] = (int)$this->request->getParam('parent');
$result['is_anchor']['default'] = false;
$result['use_config.available_sort_by']['default'] = true;
$result['use_config.default_sort_by']['default'] = true;
$result['use_config.filter_price_range']['default'] = true;
Expand Down Expand Up @@ -418,14 +446,14 @@ protected function getFieldsMap()
[
'custom_use_parent_settings',
'custom_apply_to_products',
'custom_design',
'page_layout',
'custom_layout_update',
],
'schedule_design_update' =>
[
'custom_design_from',
'custom_design_to',
'custom_design',
],
'category_view_optimization' =>
[
Expand Down
24 changes: 9 additions & 15 deletions app/code/Magento/Catalog/Model/ResourceModel/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ class Category extends AbstractResource
*/
protected $entityManager;

/**
* @var Category\AggregateCount
*/
protected $aggregateCount;

/**
* Category constructor.
* @param \Magento\Eav\Model\Entity\Context $context
Expand All @@ -82,6 +87,7 @@ class Category extends AbstractResource
* @param Category\TreeFactory $categoryTreeFactory
* @param Category\CollectionFactory $categoryCollectionFactory
* @param EntityManager $entityManager
* @param Category\AggregateCount $aggregateCount
* @param array $data
*/
public function __construct(
Expand All @@ -92,6 +98,7 @@ public function __construct(
\Magento\Catalog\Model\ResourceModel\Category\TreeFactory $categoryTreeFactory,
\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
EntityManager $entityManager,
Category\AggregateCount $aggregateCount,
$data = []
) {
parent::__construct(
Expand All @@ -104,8 +111,8 @@ public function __construct(
$this->_categoryCollectionFactory = $categoryCollectionFactory;
$this->_eventManager = $eventManager;
$this->entityManager = $entityManager;

$this->connectionName = 'catalog';
$this->aggregateCount = $aggregateCount;
}

/**
Expand Down Expand Up @@ -184,20 +191,8 @@ protected function _getTree()
protected function _beforeDelete(\Magento\Framework\DataObject $object)
{
parent::_beforeDelete($object);

/**
* Update children count for all parent categories
*/
$parentIds = $object->getParentIds();
if ($parentIds) {
$childDecrease = $object->getChildrenCount() + 1;
// +1 is itself
$data = ['children_count' => new \Zend_Db_Expr('children_count - ' . $childDecrease)];
$where = ['entity_id IN(?)' => $parentIds];
$this->getConnection()->update($this->getEntityTable(), $data, $where);
}
$this->aggregateCount->processDelete($object);
$this->deleteChildren($object);
return $this;
}

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


if (!$this->entityManager->has(\Magento\Catalog\Api\Data\CategoryInterface::class, $entityId)) {
$object->isObjectNew(true);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Model\ResourceModel\Category;

use Magento\Catalog\Model\Category;

/**
* Class AggregateCount
*/
class AggregateCount
{
/**
* @param Category $category
* @return void
*/
public function processDelete(Category $category)
{
/** @var \Magento\Catalog\Model\ResourceModel\Category $resourceModel */
$resourceModel = $category->getResource();
/**
* Update children count for all parent categories
*/
$parentIds = $category->getParentIds();
if ($parentIds) {
$childDecrease = $category->getChildrenCount() + 1;
// +1 is itself
$data = ['children_count' => new \Zend_Db_Expr('children_count - ' . $childDecrease)];
$where = ['entity_id IN(?)' => $parentIds];
$resourceModel->getConnection()->update($resourceModel->getEntityTable(), $data, $where);
}
}
}
Loading

0 comments on commit 4cce2bc

Please sign in to comment.