Skip to content

Commit

Permalink
Merge pull request #1194 from magento-tsg/2.1.8-develop-pr17
Browse files Browse the repository at this point in the history
[TSG] Backporting for 2.1 (pr17) (2.1.8)
  • Loading branch information
Volodymyr Klymenko authored Jun 15, 2017
2 parents 38108c7 + ad09918 commit 21f6525
Show file tree
Hide file tree
Showing 84 changed files with 3,790 additions and 632 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ protected function _isAllowed()
}

/**
* Upload file controller action
* Upload file controller action.
*
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
$imageId = $this->_request->getParam('param_name', 'image');
try {
$result = $this->imageUploader->saveFileToTmpDir('image');
$result = $this->imageUploader->saveFileToTmpDir($imageId);

$result['cookie'] = [
'name' => $this->_getSession()->getName(),
Expand Down
66 changes: 59 additions & 7 deletions app/code/Magento/Catalog/Controller/Adminhtml/Category/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Magento\Catalog\Controller\Adminhtml\Category;

use Magento\Store\Model\StoreManagerInterface;
use Magento\Catalog\Api\Data\CategoryAttributeInterface;

/**
* Class Save
Expand Down Expand Up @@ -48,6 +49,13 @@ class Save extends \Magento\Catalog\Controller\Adminhtml\Category
*/
private $storeManager;

/**
* Config instance holder.
*
* @var \Magento\Eav\Model\Config
*/
private $eavConfig;

/**
* Constructor
*
Expand All @@ -72,8 +80,9 @@ public function __construct(
}

/**
* Filter category data
* Filter category data.
*
* @deprecated
* @param array $rawData
* @return array
*/
Expand Down Expand Up @@ -126,7 +135,7 @@ public function execute()
$this->storeManager->setCurrentStore($store->getCode());
$parentId = isset($categoryPostData['parent']) ? $categoryPostData['parent'] : null;
if ($categoryPostData) {
$category->addData($this->_filterCategoryPostData($categoryPostData));
$category->addData($categoryPostData);
if ($isNewCategory) {
$parentCategory = $this->getParentCategory($parentId, $storeId);
$category->setPath($parentCategory->getPath());
Expand Down Expand Up @@ -248,21 +257,48 @@ public function execute()
}

/**
* Image data preprocessing
* Sets image attribute data to false, if image was removed.
*
* @param array $data
*
* @return array
*/
public function imagePreprocessing($data)
public function imagePreprocessing(array $data)
{
if (empty($data['image'])) {
unset($data['image']);
$data['image']['delete'] = true;
$emptyImageAttributes = $this->getEmptyImageAttributes($data);
$attributeCodes = array_keys($emptyImageAttributes);
foreach ($attributeCodes as $attributeCode) {
$data[$attributeCode] = false;
}

return $data;
}

/**
* Get image attributes without value.
*
* @param array $data
* @return array
*/
private function getEmptyImageAttributes(array $data)
{
$result = [];
$entityType = $this->getConfig()->getEntityType(CategoryAttributeInterface::ENTITY_TYPE_CODE);
foreach ($entityType->getAttributeCollection() as $attribute) {
$attributeCode = $attribute->getAttributeCode();
$backendModel = $attribute->getBackend();
if (isset($data[$attributeCode])) {
continue;
}
if (!$backendModel instanceof \Magento\Catalog\Model\Category\Attribute\Backend\Image) {
continue;
}
$result[$attributeCode] = $attribute;
}

return $result;
}

/**
* Converting inputs from string to boolean
*
Expand Down Expand Up @@ -346,4 +382,20 @@ protected function getRedirectParams($isNewCategory, $hasError, $categoryId, $pa
}
return ['path' => $path, 'params' => $params];
}

/**
* Get Config instance.
*
* @return \Magento\Eav\Model\Config
*/
private function getConfig()
{
if (null === $this->eavConfig) {
$this->eavConfig = \Magento\Framework\App\ObjectManager::getInstance()->get(
\Magento\Eav\Model\Config::class
);
}

return $this->eavConfig;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,18 @@ public function initializeFromData(\Magento\Catalog\Model\Product $product, arra
$customOptions = [];
foreach ($options as $customOptionData) {
if (empty($customOptionData['is_delete'])) {
if (empty($customOptionData['option_id'])) {
$customOptionData['option_id'] = null;
}

if (isset($customOptionData['values'])) {
$customOptionData['values'] = array_filter($customOptionData['values'], function ($valueData) {
return empty($valueData['is_delete']);
});
}

$customOption = $this->getCustomOptionFactory()->create(['data' => $customOptionData]);
$customOption->setProductSku($product->getSku());
$customOption->setOptionId(null);
$customOptions[] = $customOption;
}
}
Expand Down Expand Up @@ -330,21 +334,59 @@ public function mergeProductOptions($productOptions, $overwriteOptions)
return $productOptions;
}

foreach ($productOptions as $index => $option) {
foreach ($productOptions as $optionIndex => $option) {
$optionId = $option['option_id'];
$option = $this->overwriteValue(
$optionId,
$option,
$overwriteOptions
);

if (!isset($overwriteOptions[$optionId])) {
continue;
if (isset($option['values']) && isset($overwriteOptions[$optionId]['values'])) {
foreach ($option['values'] as $valueIndex => $value) {
if (isset($value['option_type_id'])) {
$valueId = $value['option_type_id'];
$value = $this->overwriteValue(
$valueId,
$value,
$overwriteOptions[$optionId]['values']
);

$option['values'][$valueIndex] = $value;
}
}
}

$productOptions[$optionIndex] = $option;
}

return $productOptions;
}

/**
* Overwrite values of fields to default, if there are option id and field
* name in array overwriteOptions.
*
* @param int $optionId
* @param array $option
* @param array $overwriteOptions
*
* @return array
*/
private function overwriteValue($optionId, $option, $overwriteOptions)
{
if (isset($overwriteOptions[$optionId])) {
foreach ($overwriteOptions[$optionId] as $fieldName => $overwrite) {
if ($overwrite && isset($option[$fieldName]) && isset($option['default_' . $fieldName])) {
$productOptions[$index][$fieldName] = $option['default_' . $fieldName];
$option[$fieldName] = $option['default_' . $fieldName];
if ('title' == $fieldName) {
$option['is_delete_store_title'] = 1;
}
}
}
}

return $productOptions;
return $option;
}

/**
Expand All @@ -354,8 +396,9 @@ private function getCustomOptionFactory()
{
if (null === $this->customOptionFactory) {
$this->customOptionFactory = \Magento\Framework\App\ObjectManager::getInstance()
->get('Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory');
->get(\Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory::class);
}

return $this->customOptionFactory;
}

Expand All @@ -366,8 +409,9 @@ private function getProductLinkFactory()
{
if (null === $this->productLinkFactory) {
$this->productLinkFactory = \Magento\Framework\App\ObjectManager::getInstance()
->get('Magento\Catalog\Api\Data\ProductLinkInterfaceFactory');
->get(\Magento\Catalog\Api\Data\ProductLinkInterfaceFactory::class);
}

return $this->productLinkFactory;
}

Expand All @@ -378,8 +422,9 @@ private function getProductRepository()
{
if (null === $this->productRepository) {
$this->productRepository = \Magento\Framework\App\ObjectManager::getInstance()
->get('Magento\Catalog\Api\ProductRepositoryInterface\Proxy');
->get('\Magento\Catalog\Api\ProductRepositoryInterface\Proxy');
}

return $this->productRepository;
}

Expand All @@ -392,6 +437,7 @@ private function getLinkResolver()
if (!is_object($this->linkResolver)) {
$this->linkResolver = ObjectManager::getInstance()->get(LinkResolver::class);
}

return $this->linkResolver;
}

Expand All @@ -406,6 +452,7 @@ private function getDateTimeFilter()
$this->dateTimeFilter = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Stdlib\DateTime\Filter\DateTime::class);
}

return $this->dateTimeFilter;
}
}
9 changes: 6 additions & 3 deletions app/code/Magento/Catalog/Model/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -652,14 +652,16 @@ public function formatUrlKey($str)
}

/**
* Retrieve image URL
* Get image url by attribute code.
*
* @param string $attributeCode
* @return string
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getImageUrl()
public function getImageUrl($attributeCode = 'image')
{
$url = false;
$image = $this->getImage();
$image = $this->getData($attributeCode);
if ($image) {
if (is_string($image)) {
$url = $this->_storeManager->getStore()->getBaseUrl(
Expand All @@ -671,6 +673,7 @@ public function getImageUrl()
);
}
}

return $url;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,45 @@ public function __construct(
}

/**
* Get image uploader
* Avoiding saving potential upload data to DB.
* Will set empty image attribute value if image was not uploaded.
*
* @param \Magento\Framework\DataObject $object
* @return $this
*/
public function beforeSave($object)
{
$attributeName = $this->getAttribute()->getName();
$value = $object->getData($attributeName);
$imageName = $this->getUploadedImageName($value);

if ($imageName) {
$object->setData($attributeName, $imageName);
} else if (!is_string($value)) {
$object->setData($attributeName, '');
}

return parent::beforeSave($object);
}

/**
* Gets image name from $value array.
* Will return empty string in case $value is not an array.
*
* @param array $value Attribute value
* @return string
*/
private function getUploadedImageName($value)
{
if (is_array($value) && isset($value[0]['name'])) {
return $value[0]['name'];
}

return '';
}

/**
* Get image uploader.
*
* @return \Magento\Catalog\Model\ImageUploader
*
Expand All @@ -79,26 +117,25 @@ public function __construct(
private function getImageUploader()
{
if ($this->imageUploader === null) {
$this->imageUploader = \Magento\Framework\App\ObjectManager::getInstance()->get(
'Magento\Catalog\CategoryImageUpload'
);
$this->imageUploader = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Catalog\CategoryImageUpload::class);
}

return $this->imageUploader;
}

/**
* Save uploaded file and set its name to category
* Save uploaded file and set its name to category.
*
* @param \Magento\Framework\DataObject $object
* @return \Magento\Catalog\Model\Category\Attribute\Backend\Image
*/
public function afterSave($object)
{
$image = $object->getData($this->getAttribute()->getName(), null);

if ($image !== null) {
$imageName = $object->getData($this->getAttribute()->getName(), null);
if ($imageName) {
try {
$this->getImageUploader()->moveFileFromTmp($image);
$this->getImageUploader()->moveFileFromTmp($imageName);
} catch (\Exception $e) {
$this->_logger->critical($e);
}
Expand Down
Loading

0 comments on commit 21f6525

Please sign in to comment.