Skip to content

Commit

Permalink
Merge branch 'MAGETWO-48688' of github.corp.magento.com:magento-troll…
Browse files Browse the repository at this point in the history
…/magento2ce into MAGETWO-51814
  • Loading branch information
slopukhov committed Apr 14, 2016
2 parents 2a3f901 + 167d58a commit 975ba0e
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 41 deletions.
42 changes: 42 additions & 0 deletions app/code/Magento/Catalog/Controller/Adminhtml/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ abstract class Category extends \Magento\Backend\App\Action
*/
const ADMIN_RESOURCE = 'Magento_Catalog::categories';

/**
* @var \Magento\Framework\Stdlib\DateTime\Filter\DateTime
*/
private $dateTimeFilter;

/**
* Initialize requested category and put it into registry.
* Root category can be returned, if inappropriate store/category is specified
Expand Down Expand Up @@ -107,4 +112,41 @@ protected function ajaxRequestResponse($category, $resultPage)
$resultJson->setData($eventResponse->getData());
return $resultJson;
}

/**
* @return \Magento\Framework\Stdlib\DateTime\Filter\DateTime
*
* @deprecated
*/
private function getDateTimeFilter()
{
if ($this->dateTimeFilter === null) {
$this->dateTimeFilter = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Stdlib\DateTime\Filter\DateTime::class);
}
return $this->dateTimeFilter;
}

/**
* Datetime data preprocessing
*
* @param \Magento\Catalog\Model\Category $category
* @param array $postData
*
* @return array
*/
protected function dateTimePreprocessing($category, $postData)
{
$dateFieldFilters = [];
$attributes = $category->getAttributes();
foreach ($attributes as $attrKey => $attribute) {
if ($attribute->getBackend()->getType() == 'datetime') {
if (array_key_exists($attrKey, $postData) && $postData[$attrKey] != '') {
$dateFieldFilters[$attrKey] = $this->getDateTimeFilter();
}
}
}
$inputFilter = new \Zend_Filter_Input($dateFieldFilters, [], $postData);
return $inputFilter->getUnescaped();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ public function execute()
return $resultRedirect->setPath('catalog/*/', ['_current' => true, 'id' => null]);
}

/**
* Check if there are data in session (if there was an exception on saving category)
*/
$categoryData = $this->_getSession()->getCategoryData(true);
if (is_array($categoryData)) {
unset($categoryData['image']);
$category->addData($categoryData);
}

$resultPageFactory = $this->_objectManager->get('Magento\Framework\View\Result\PageFactory');
/** @var \Magento\Backend\Model\View\Result\Page $resultPage */
$resultPage = $resultPageFactory->create();
Expand Down
14 changes: 7 additions & 7 deletions app/code/Magento/Catalog/Controller/Adminhtml/Category/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,16 @@ public function execute()
}

/**
* Check if we have data in session (if during category save was exception)
* Check if there are data in session (if there was an exception on saving category)
*/
$data = $this->_getSession()->getCategoryData(true);
if (isset($data['general'])) {
if (isset($data['general']['image']['delete'])) {
$data['general']['image'] = null;
$categoryData = $this->_getSession()->getCategoryData(true);
if (is_array($categoryData)) {
if (isset($categoryData['image']['delete'])) {
$categoryData['image'] = null;
} else {
unset($data['general']['image']);
unset($categoryData['image']);
}
$category->addData($data['general']);
$category->addData($categoryData);
}

/** @var \Magento\Backend\Model\View\Result\Page $resultPage */
Expand Down
63 changes: 32 additions & 31 deletions app/code/Magento/Catalog/Controller/Adminhtml/Category/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

/**
* Class Save
*
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Save extends \Magento\Catalog\Controller\Adminhtml\Category
Expand All @@ -34,15 +34,13 @@ class Save extends \Magento\Catalog\Controller\Adminhtml\Category
* @var array
*/
protected $stringToBoolInputs = [
'general' => [
'custom_use_parent_settings',
'custom_apply_to_products',
'is_active',
'include_in_menu',
'is_anchor',
'use_default' => ['url_key'],
'use_config' => ['available_sort_by', 'filter_price_range', 'default_sort_by']
]
'custom_use_parent_settings',
'custom_apply_to_products',
'is_active',
'include_in_menu',
'is_anchor',
'use_default' => ['url_key'],
'use_config' => ['available_sort_by', 'filter_price_range', 'default_sort_by']
];

/**
Expand Down Expand Up @@ -110,15 +108,18 @@ public function execute()
}

$data['general'] = $this->getRequest()->getPostValue();
$isNewCategory = !isset($data['general']['entity_id']);
$data = $this->stringToBoolConverting($data);
$data = $this->imagePreprocessing($data);
$storeId = isset($data['general']['store_id']) ? $data['general']['store_id'] : null;
$categoryPostData = $data['general'];

$isNewCategory = !isset($categoryPostData['entity_id']);
$categoryPostData = $this->stringToBoolConverting($categoryPostData);
$categoryPostData = $this->imagePreprocessing($categoryPostData);
$categoryPostData = $this->dateTimePreprocessing($category, $categoryPostData);
$storeId = isset($categoryPostData['store_id']) ? $categoryPostData['store_id'] : null;
$store = $this->storeManager->getStore($storeId);
$this->storeManager->setCurrentStore($store->getCode());
$parentId = isset($data['general']['parent']) ? $data['general']['parent'] : null;
if ($data['general']) {
$category->addData($this->_filterCategoryPostData($data['general']));
$parentId = isset($categoryPostData['parent']) ? $categoryPostData['parent'] : null;
if ($categoryPostData) {
$category->addData($this->_filterCategoryPostData($categoryPostData));
if ($isNewCategory) {
$parentCategory = $this->getParentCategory($parentId, $storeId);
$category->setPath($parentCategory->getPath());
Expand All @@ -128,10 +129,10 @@ public function execute()
/**
* Process "Use Config Settings" checkboxes
*/
$generalPost = $data['general'];

$useConfig = [];
if (isset($generalPost['use_config']) && !empty($generalPost['use_config'])) {
foreach ($generalPost['use_config'] as $attributeCode => $attributeValue) {
if (isset($categoryPostData['use_config']) && !empty($categoryPostData['use_config'])) {
foreach ($categoryPostData['use_config'] as $attributeCode => $attributeValue) {
if ($attributeValue) {
$useConfig[] = $attributeCode;
$category->setData($attributeCode, null);
Expand All @@ -141,11 +142,11 @@ public function execute()

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

if (isset($data['general']['category_products'])
&& is_string($data['general']['category_products'])
if (isset($categoryPostData['category_products'])
&& is_string($categoryPostData['category_products'])
&& !$category->getProductsReadonly()
) {
$products = json_decode($data['general']['category_products'], true);
$products = json_decode($categoryPostData['category_products'], true);
$category->setPostedProducts($products);
}
$this->_eventManager->dispatch(
Expand All @@ -156,8 +157,8 @@ public function execute()
/**
* Check "Use Default Value" checkboxes values
*/
if (isset($generalPost['use_default']) && !empty($generalPost['use_default'])) {
foreach ($generalPost['use_default'] as $attributeCode => $attributeValue) {
if (isset($categoryPostData['use_default']) && !empty($categoryPostData['use_default'])) {
foreach ($categoryPostData['use_default'] as $attributeCode => $attributeValue) {
if ($attributeValue) {
$category->setData($attributeCode, null);
}
Expand Down Expand Up @@ -197,15 +198,15 @@ public function execute()
} catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
$this->messageManager->addError($e->getMessage());
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
$this->_getSession()->setCategoryData($data);
$this->_getSession()->setCategoryData($categoryPostData);
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->messageManager->addError($e->getMessage());
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
$this->_getSession()->setCategoryData($data);
$this->_getSession()->setCategoryData($categoryPostData);
} catch (\Exception $e) {
$this->messageManager->addError(__('Something went wrong while saving the category.'));
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
$this->_getSession()->setCategoryData($data);
$this->_getSession()->setCategoryData($categoryPostData);
}
}

Expand Down Expand Up @@ -248,9 +249,9 @@ public function execute()
*/
public function imagePreprocessing($data)
{
if (empty($data['general']['image'])) {
unset($data['general']['image']);
$data['general']['image']['delete'] = true;
if (empty($data['image'])) {
unset($data['image']);
$data['image']['delete'] = true;
}
return $data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ public function afterSave($object)
} catch (\Exception $e) {
$this->_logger->critical($e);
}
} elseif (isset($value[0]['name'])) {
$object->setData($this->getAttribute()->getName(), $value[0]['name']);
$this->getAttribute()->getEntity()->saveAttribute($object, $this->getAttribute()->getName());
}
return $this;
}
Expand Down
4 changes: 1 addition & 3 deletions app/code/Magento/Catalog/Model/Category/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,7 @@ public function getData()
return $this->loadedData;
}
$category = $this->getCurrentCategory();
if (!$category->getId()) {
return [];
} else {
if ($category) {
$categoryData = $category->getData();
$categoryData = $this->addUseDefaultSettings($category, $categoryData);
$categoryData = $this->addUseConfigSettings($categoryData);
Expand Down

0 comments on commit 975ba0e

Please sign in to comment.