Skip to content

Commit

Permalink
Merge remote-tracking branch 'mainline/develop' into MPI-PR-PayPal
Browse files Browse the repository at this point in the history
  • Loading branch information
iivashchenko committed Oct 18, 2016
2 parents 1814285 + 3926be4 commit 3fbe2b5
Show file tree
Hide file tree
Showing 40 changed files with 1,508 additions and 522 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,14 @@ public function __construct(
}

/**
* Returns the bundle product options
* Will return cached options data if the product options are already initialized
* In a case when $stripSelection parameter is true will reload stored bundle selections collection from DB
*
* @param bool $stripSelection
* @return array
*/
public function getOptions()
public function getOptions($stripSelection = false)
{
if (!$this->options) {
$product = $this->getProduct();
Expand All @@ -96,7 +101,7 @@ public function getOptions()

$this->options = $optionCollection->appendSelections(
$selectionCollection,
false,
$stripSelection,
$this->catalogProduct->getSkipSaleableCheck()
);
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
?>

<?php /* @var $block \Magento\Bundle\Block\Adminhtml\Catalog\Product\Composite\Fieldset\Bundle */ ?>
<?php $options = $block->decorateArray($block->getOptions()); ?>
<?php $options = $block->decorateArray($block->getOptions(true)); ?>
<?php if (count($options)): ?>
<fieldset id="catalog_product_composite_configure_fields_bundle"
class="fieldset admin__fieldset composite-bundle<?php echo $block->getIsLastFieldset() ? ' last-fieldset' : '' ?>">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ protected function _isAllowed()
*/
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
38 changes: 30 additions & 8 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,11 @@ class Save extends \Magento\Catalog\Controller\Adminhtml\Category
*/
private $storeManager;

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

/**
* Constructor
*
Expand All @@ -56,31 +62,35 @@ class Save extends \Magento\Catalog\Controller\Adminhtml\Category
* @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
* @param \Magento\Framework\View\LayoutFactory $layoutFactory
* @param StoreManagerInterface $storeManager
* @param \Magento\Eav\Model\Config $eavConfig
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
\Magento\Framework\View\LayoutFactory $layoutFactory,
StoreManagerInterface $storeManager
StoreManagerInterface $storeManager,
\Magento\Eav\Model\Config $eavConfig = null
) {
parent::__construct($context);
$this->resultRawFactory = $resultRawFactory;
$this->resultJsonFactory = $resultJsonFactory;
$this->layoutFactory = $layoutFactory;
$this->storeManager = $storeManager;
$this->eavConfig = $eavConfig
?: \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Eav\Model\Config::class);
}

/**
* Filter category data
*
* @deprecated
* @param array $rawData
* @return array
*/
protected function _filterCategoryPostData(array $rawData)
{
$data = $rawData;
// @todo It is a workaround to prevent saving this data in category model and it has to be refactored in future
if (isset($data['image']) && is_array($data['image'])) {
if (!empty($data['image']['delete'])) {
$data['image'] = null;
Expand Down Expand Up @@ -126,7 +136,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,18 +258,30 @@ 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)
{
if (empty($data['image'])) {
unset($data['image']);
$data['image']['delete'] = true;
$entityType = $this->eavConfig->getEntityType(CategoryAttributeInterface::ENTITY_TYPE_CODE);

foreach ($entityType->getAttributeCollection() as $attributeModel) {
$attributeCode = $attributeModel->getAttributeCode();
$backendModel = $attributeModel->getBackend();

if (isset($data[$attributeCode])) {
continue;
}

if (!$backendModel instanceof \Magento\Catalog\Model\Category\Attribute\Backend\Image) {
continue;
}

$data[$attributeCode] = false;
}

return $data;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,7 @@ public function initializeFromData(\Magento\Catalog\Model\Product $product, arra
$productData[$field] = [];
}
}

foreach ($productData['website_ids'] as $websiteId => $checkboxValue) {
if (!$checkboxValue) {
unset($productData['website_ids'][$websiteId]);
}
}
$productData['website_ids'] = $this->filterWebsiteIds($productData['website_ids']);

$wasLockedMedia = false;
if ($product->isLockedAttribute('media')) {
Expand Down Expand Up @@ -422,4 +417,23 @@ private function getDateTimeFilter()
}
return $this->dateTimeFilter;
}

/**
* Remove ids of non selected websites from $websiteIds array and return filtered data
* $websiteIds parameter expects array with website ids as keys and 1 (selected) or 0 (non selected) as values
* Only one id (default website ID) will be set to $websiteIds array when the single store mode is turned on
*
* @param array $websiteIds
* @return array
*/
private function filterWebsiteIds($websiteIds)
{
if (!$this->storeManager->isSingleStoreMode()) {
$websiteIds = array_filter((array)$websiteIds);
} else {
$websiteIds[$this->storeManager->getWebsite(true)->getId()] = 1;
}

return $websiteIds;
}
}
10 changes: 5 additions & 5 deletions app/code/Magento/Catalog/Model/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -652,14 +652,14 @@ public function formatUrlKey($str)
}

/**
* Retrieve image URL
*
* @return string
* @param string $attributeCode
* @return bool|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 Down
64 changes: 49 additions & 15 deletions app/code/Magento/Catalog/Model/Category/Attribute/Backend/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,13 @@ class Image extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
protected $_uploaderFactory;

/**
* Filesystem facade
*
* @var \Magento\Framework\Filesystem
*
* @deprecated
*/
protected $_filesystem;

/**
* File Uploader factory
*
* @var \Magento\MediaStorage\Model\File\UploaderFactory
*
* @deprecated
Expand All @@ -46,15 +42,16 @@ class Image extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
protected $_logger;

/**
* Image uploader
*
* @var \Magento\Catalog\Model\ImageUploader
*/
private $imageUploader;

/**
* Image constructor.
*
* @var string
*/
private $additionalData = '_additional_data_';

/**
* @param \Psr\Log\LoggerInterface $logger
* @param \Magento\Framework\Filesystem $filesystem
* @param \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory
Expand All @@ -70,19 +67,55 @@ public function __construct(
}

/**
* Get image uploader
* Gets image name from $value array.
* Will return empty string in a case when $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 '';
}

/**
* 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);

if ($imageName = $this->getUploadedImageName($value)) {
$object->setData($this->additionalData . $attributeName, $value);
$object->setData($attributeName, $imageName);
} else if (!is_string($value)) {
$object->setData($attributeName, '');
}

return parent::beforeSave($object);
}

/**
* @return \Magento\Catalog\Model\ImageUploader
*
* @deprecated
*/
private function getImageUploader()
{
if ($this->imageUploader === null) {
$this->imageUploader = \Magento\Framework\App\ObjectManager::getInstance()->get(
\Magento\Catalog\CategoryImageUpload::class
);
$this->imageUploader = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Catalog\CategoryImageUpload::class);
}

return $this->imageUploader;
}

Expand All @@ -94,15 +127,16 @@ private function getImageUploader()
*/
public function afterSave($object)
{
$image = $object->getData($this->getAttribute()->getName(), null);
$value = $object->getData($this->additionalData . $this->getAttribute()->getName());

if ($image !== null) {
if ($imageName = $this->getUploadedImageName($value)) {
try {
$this->getImageUploader()->moveFileFromTmp($image);
$this->getImageUploader()->moveFileFromTmp($imageName);
} catch (\Exception $e) {
$this->_logger->critical($e);
}
}

return $this;
}
}
33 changes: 28 additions & 5 deletions app/code/Magento/Catalog/Model/Category/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Magento\Ui\DataProvider\EavValidationRules;
use Magento\Catalog\Model\CategoryFactory;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Catalog\Model\Category\Attribute\Backend\Image as ImageBackendModel;

/**
* Class DataProvider
Expand Down Expand Up @@ -206,11 +207,8 @@ public function getData()
$categoryData = $this->addUseDefaultSettings($category, $categoryData);
$categoryData = $this->addUseConfigSettings($categoryData);
$categoryData = $this->filterFields($categoryData);
if (isset($categoryData['image'])) {
unset($categoryData['image']);
$categoryData['image'][0]['name'] = $category->getData('image');
$categoryData['image'][0]['url'] = $category->getImageUrl();
}
$categoryData = $this->convertValues($category, $categoryData);

$this->loadedData[$category->getId()] = $categoryData;
}
return $this->loadedData;
Expand Down Expand Up @@ -371,6 +369,31 @@ protected function filterFields($categoryData)
return array_diff_key($categoryData, array_flip($this->ignoreFields));
}

/**
* Converts category image data to acceptable for rendering format
*
* @param \Magento\Catalog\Model\Category $category
* @param array $categoryData
* @return array
*/
private function convertValues($category, $categoryData)
{
foreach ($category->getAttributes() as $attributeCode => $attribute) {
if (!isset($categoryData[$attributeCode])) {
continue;
}

if ($attribute->getBackend() instanceof ImageBackendModel) {
unset($categoryData[$attributeCode]);

$categoryData[$attributeCode][0]['name'] = $category->getData($attributeCode);
$categoryData[$attributeCode][0]['url'] = $category->getImageUrl($attributeCode);
}
}

return $categoryData;
}

/**
* Category's fields default values
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2182,12 +2182,17 @@ public function addMediaGalleryData()

$mediaGalleries = [];
$linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField();
$items = $this->getItems();

$select->where('entity.' . $linkField . ' IN (?)', array_map(function ($item) {
return $item->getId();
}, $items));

foreach ($this->getConnection()->fetchAll($select) as $row) {
$mediaGalleries[$row[$linkField]][] = $row;
}

foreach ($this->getItems() as $item) {
foreach ($items as $item) {
$mediaEntries = isset($mediaGalleries[$item->getId()]) ? $mediaGalleries[$item->getId()] : [];
$this->getGalleryReadHandler()->addMediaDataToProduct($item, $mediaEntries);
}
Expand Down
Loading

0 comments on commit 3fbe2b5

Please sign in to comment.