Skip to content

Commit

Permalink
Merge branch 'develop' of github.corp.magento.com:magento2/magento2ce…
Browse files Browse the repository at this point in the history
… into bugs
  • Loading branch information
ishakhsuvarov committed Apr 15, 2016
2 parents 92f42b1 + ea52f98 commit a7b60da
Show file tree
Hide file tree
Showing 32 changed files with 448 additions and 96 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
22 changes: 21 additions & 1 deletion app/code/Magento/Catalog/Model/Product/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ class Option extends AbstractExtensibleModel implements ProductCustomOptionInter
*/
protected $optionRepository;

/**
* Option type percent
*/
protected static $typePercent = 'percent';

/**#@+
* Constants
*/
Expand Down Expand Up @@ -424,7 +429,7 @@ public function afterSave()
*/
public function getPrice($flag = false)
{
if ($flag && $this->getPriceType() == 'percent') {
if ($flag && $this->getPriceType() == self::$typePercent) {
$basePrice = $this->getProduct()->getPriceInfo()->getPrice(BasePrice::PRICE_CODE)->getValue();
$price = $basePrice * ($this->_getData(self::KEY_PRICE) / 100);
return $price;
Expand Down Expand Up @@ -827,6 +832,21 @@ public function getExtensionAttributes()
return $this->_getExtensionAttributes();
}

/**
* Return regular price.
*
* @return float|int
*/
public function getRegularPrice()
{
if ($this->getPriceType() == self::$typePercent) {
$basePrice = $this->getProduct()->getPriceInfo()->getPrice('regular_price')->getAmount()->getValue();
$price = $basePrice * ($this->_getData(self::KEY_PRICE) / 100);
return $price;
}
return $this->_getData(self::KEY_PRICE);
}

/**
* @param Product $product
* @return \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
Expand Down
13 changes: 13 additions & 0 deletions app/code/Magento/Catalog/Setup/UpgradeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,20 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface
]
);
}

if (version_compare($context->getVersion(), '2.0.7') < 0) {
/** @var EavSetup $eavSetupF */
$eavSetup= $this->eavSetupFactory->create(['setup' => $setup]);

$eavSetup->updateAttribute(
ProductAttributeInterface::ENTITY_TYPE_CODE,
'meta_description',
[
'note' => 'Maximum 255 chars. Meta Description should optimally be between 150-160 characters'
]
);
}

$setup->endSetup();
}
}
26 changes: 26 additions & 0 deletions app/code/Magento/Catalog/Test/Unit/Model/Product/OptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\Catalog\Test\Unit\Model\Product;

use \Magento\Catalog\Model\Product\Option;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;

class OptionTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -33,4 +34,29 @@ public function testGetProductSku()
$this->productMock->expects($this->once())->method('getSku')->willReturn($productSku);
$this->assertEquals($productSku, $this->model->getProductSku());
}

public function testGetRegularPrice()
{
$priceInfoMock = $this->getMockForAbstractClass(
'Magento\Framework\Pricing\PriceInfoInterface',
[],
'',
false,
false,
true,
['getAmount', 'getPrice']
);
$priceInfoMock->expects($this->once())->method('getPrice')->willReturnSelf();
$amountMock = $this->getMockForAbstractClass('Magento\Framework\Pricing\Amount\AmountInterface');
$priceInfoMock->expects($this->once())->method('getAmount')->willReturn($amountMock);

$this->productMock->expects($this->once())->method('getPriceInfo')->willReturn($priceInfoMock);

$amountMock->expects($this->once())->method('getValue')->willReturn(50);
$this->model->setPrice(50);
$this->model->setPriceType(\Magento\Catalog\Model\Product\Option\Value::TYPE_PERCENT);
$this->assertEquals(25, $this->model->getRegularPrice());
$this->model->setPriceType(null);
$this->assertEquals(50, $this->model->getRegularPrice());
}
}
2 changes: 1 addition & 1 deletion app/code/Magento/Catalog/etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magento_Catalog" setup_version="2.0.6">
<module name="Magento_Catalog" setup_version="2.0.7">
<sequence>
<module name="Magento_Eav"/>
<module name="Magento_Cms"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ protected function critical($e)
private function roundTime($timeStamp)
{
if (is_numeric($timeStamp) && $timeStamp != 0) {
$timeStamp = $this->dateTime->timestamp($this->dateTime->date('Y-m-d 00:00:00'));
$timeStamp = $this->dateTime->timestamp($this->dateTime->date('Y-m-d 00:00:00', $timeStamp));
}

return $timeStamp;
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Eav/Setup/EavSetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ public function addAttributeOption($option)
*
* @param int|string $entityTypeId
* @param int|string $id
* @param string $field
* @param string|array $field
* @param mixed $value
* @param int $sortOrder
* @return $this
Expand Down
Loading

0 comments on commit a7b60da

Please sign in to comment.