Skip to content

Commit

Permalink
🔃 [EngCom] Public Pull Requests - 2.2-develop
Browse files Browse the repository at this point in the history
Accepted Public Pull Requests:
 - #13034: Magento 2.2 Develop fix for #12221 Google Analytics Pageview Triggered twice (by @bhargavmehta)
 - #13036: #12705: Integrity constraint violation error after re… (by @vinayshah)
 - #13044: Fix Newsletter Subscribe Workflow (by @torhoehn)
 - #12668: Fix for reverting stock twice for cancelled orders (by @dverkade)
 - magento-engcom/magento2ce#1018: 6486: Unable to save certain product properties via Rest API (by @nmalevanec)


Fixed GitHub Issues:
 - #12221: Google analytics pageview being triggered twice (reported by @alexhadley) has been fixed in #13034 by @bhargavmehta in 2.2-develop branch
   Related commits:
     1. 149d08f
     2. e851948

 - #12705: Integrity constraint violation error after reordering product with custom options (reported by @alena-marchenko) has been fixed in #13036 by @vinayshah in 2.2-develop branch
   Related commits:
     1. 843bc62
     2. f89e04d
     3. df96b51

 - #12876: Multiple newsletter confirmation emails sent (reported by @nfourteen) has been fixed in #13044 by @torhoehn in 2.2-develop branch
   Related commits:
     1. 9ab75ba
     2. 93453a8

 - #9969: Cancel order and restore quote methods increase stocks twice (reported by @simpleadm) has been fixed in #12668 by @dverkade in 2.2-develop branch
   Related commits:
     1. 2c1d6a4

 - #6486: Unable to save certain product properties via Rest API (reported by @cherreman) has been fixed in magento-engcom/magento2ce#1018 by @nmalevanec in 2.2-develop branch
   Related commits:
     1. fa6782f
     2. 9db0273
     3. 1d37cce
     4. de67388
     5. d132c7e
  • Loading branch information
magento-team authored Jan 11, 2018
2 parents 5bd0104 + 4afdd8b commit 8848d94
Show file tree
Hide file tree
Showing 11 changed files with 549 additions and 264 deletions.
186 changes: 14 additions & 172 deletions app/code/Magento/Catalog/Model/ProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
*/
namespace Magento\Catalog\Model;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Product\Gallery\MimeTypeExtensionMap;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Framework\Api\Data\ImageContentInterface;
use Magento\Framework\Api\Data\ImageContentInterfaceFactory;
use Magento\Framework\Api\ImageContentValidatorInterface;
use Magento\Framework\Api\ImageProcessorInterface;
Expand All @@ -18,10 +16,8 @@
use Magento\Framework\DB\Adapter\DeadlockException;
use Magento\Framework\DB\Adapter\LockWaitException;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\StateException;
use Magento\Framework\Exception\ValidatorException;

/**
Expand Down Expand Up @@ -116,11 +112,15 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
protected $fileSystem;

/**
* @deprecated
* @see \Magento\Catalog\Model\MediaGalleryProcessor
* @var ImageContentInterfaceFactory
*/
protected $contentFactory;

/**
* @deprecated
* @see \Magento\Catalog\Model\MediaGalleryProcessor
* @var ImageProcessorInterface
*/
protected $imageProcessor;
Expand All @@ -131,7 +131,7 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
protected $extensionAttributesJoinProcessor;

/**
* @var \Magento\Catalog\Model\Product\Gallery\Processor
* @var ProductRepository\MediaGalleryProcessor
*/
protected $mediaGalleryProcessor;

Expand Down Expand Up @@ -329,6 +329,9 @@ protected function initializeProductData(array $productData, $createNew)
unset($productData['media_gallery']);
if ($createNew) {
$product = $this->productFactory->create();
if (isset($productData['price']) && !isset($productData['product_type'])) {
$product->setTypeId(Product\Type::TYPE_SIMPLE);
}
if ($this->storeManager->hasSingleStore()) {
$product->setWebsiteIds([$this->storeManager->getStore(true)->getWebsiteId()]);
}
Expand Down Expand Up @@ -375,53 +378,6 @@ private function assignProductToWebsites(\Magento\Catalog\Model\Product $product
$product->setWebsiteIds($websiteIds);
}

/**
* @param ProductInterface $product
* @param array $newEntry
* @return $this
* @throws InputException
* @throws StateException
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function processNewMediaGalleryEntry(

This comment has been minimized.

Copy link
@ihor-sviziev

ihor-sviziev Jan 12, 2018

Contributor

This change is not backward compatible. It should be marked as "deprecated", but not removed

ProductInterface $product,
array $newEntry
) {
/** @var ImageContentInterface $contentDataObject */
$contentDataObject = $newEntry['content'];

/** @var \Magento\Catalog\Model\Product\Media\Config $mediaConfig */
$mediaConfig = $product->getMediaConfig();
$mediaTmpPath = $mediaConfig->getBaseTmpMediaPath();

$relativeFilePath = $this->imageProcessor->processImageContent($mediaTmpPath, $contentDataObject);
$tmpFilePath = $mediaConfig->getTmpMediaShortUrl($relativeFilePath);

if (!$product->hasGalleryAttribute()) {
throw new StateException(__('Requested product does not support images.'));
}

$imageFileUri = $this->getMediaGalleryProcessor()->addImage(
$product,
$tmpFilePath,
isset($newEntry['types']) ? $newEntry['types'] : [],
true,
isset($newEntry['disabled']) ? $newEntry['disabled'] : true
);
// Update additional fields that are still empty after addImage call
$this->getMediaGalleryProcessor()->updateImage(
$product,
$imageFileUri,
[
'label' => $newEntry['label'],
'position' => $newEntry['position'],
'disabled' => $newEntry['disabled'],
'media_type' => $newEntry['media_type'],
]
);
return $this;
}

/**
* Process product links, creating new links, updating and deleting existing links
*
Expand Down Expand Up @@ -480,67 +436,6 @@ private function processLinks(\Magento\Catalog\Api\Data\ProductInterface $produc
return $this;
}

/**
* Process Media gallery data before save product.
*
* Compare Media Gallery Entries Data with existing Media Gallery
* * If Media entry has not value_id set it as new
* * If Existing entry 'value_id' absent in Media Gallery set 'removed' flag
* * Merge Existing and new media gallery
*
* @param ProductInterface $product contains only existing media gallery items
* @param array $mediaGalleryEntries array which contains all media gallery items
* @return $this
* @throws InputException
* @throws StateException
* @throws LocalizedException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
protected function processMediaGallery(ProductInterface $product, $mediaGalleryEntries)
{
$existingMediaGallery = $product->getMediaGallery('images');
$newEntries = [];
$entriesById = [];
if (!empty($existingMediaGallery)) {
foreach ($mediaGalleryEntries as $entry) {
if (isset($entry['id'])) {
$entriesById[$entry['id']] = $entry;
} else {
$newEntries[] = $entry;
}
}
foreach ($existingMediaGallery as $key => &$existingEntry) {
if (isset($entriesById[$existingEntry['value_id']])) {
$updatedEntry = $entriesById[$existingEntry['value_id']];
if (array_key_exists('file', $updatedEntry) && $updatedEntry['file'] === null) {
unset($updatedEntry['file']);
}
$existingMediaGallery[$key] = array_merge($existingEntry, $updatedEntry);
} else {
//set the removed flag
$existingEntry['removed'] = true;
}
}
unset($existingEntry);
$product->setData('media_gallery', ["images" => $existingMediaGallery]);
} else {
$newEntries = $mediaGalleryEntries;
}

$this->getMediaGalleryProcessor()->clearMediaAttribute($product, array_keys($product->getMediaAttributes()));
$images = $product->getMediaGallery('images');
if ($images) {
foreach ($images as $image) {
if (!isset($image['removed']) && !empty($image['types'])) {
$this->getMediaGalleryProcessor()->setMediaAttribute($product, $image['types'], $image['file']);
}
}
}
$this->processEntries($product, $newEntries, $entriesById);

return $this;
}

/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
Expand Down Expand Up @@ -577,7 +472,10 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO

$this->processLinks($product, $productLinks);
if (isset($productDataArray['media_gallery_entries'])) {
$this->processMediaGallery($product, $productDataArray['media_gallery_entries']);
$this->getMediaGalleryProcessor()->processMediaGallery(
$product,
$productDataArray['media_gallery_entries']
);
}

if (!$product->getOptionsReadonly()) {
Expand Down Expand Up @@ -749,13 +647,13 @@ public function cleanCache()
}

/**
* @return Product\Gallery\Processor
* @return ProductRepository\MediaGalleryProcessor
*/
private function getMediaGalleryProcessor()
{
if (null === $this->mediaGalleryProcessor) {
$this->mediaGalleryProcessor = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Catalog\Model\Product\Gallery\Processor::class);
->get(ProductRepository\MediaGalleryProcessor::class);
}
return $this->mediaGalleryProcessor;
}
Expand All @@ -775,60 +673,4 @@ private function getCollectionProcessor()
}
return $this->collectionProcessor;
}

/**
* Convert extension attribute for product media gallery.
*
* @param array $newEntry
* @param array $extensionAttributes
* @return void
*/
private function processExtensionAttributes(array &$newEntry, array $extensionAttributes)
{
foreach ($extensionAttributes as $code => $value) {
if (is_array($value)) {
$this->processExtensionAttributes($newEntry, $value);
} else {
$newEntry[$code] = $value;
}
}
unset($newEntry['extension_attributes']);
}

/**
* Convert entries into product media gallery data and set to product.
*
* @param ProductInterface $product
* @param array $newEntries
* @param array $entriesById
* @throws InputException
* @throws LocalizedException
* @throws StateException
* @return void
*/
private function processEntries(ProductInterface $product, array $newEntries, array $entriesById)
{
foreach ($newEntries as $newEntry) {
if (!isset($newEntry['content'])) {
throw new InputException(__('The image content is not valid.'));
}
/** @var ImageContentInterface $contentDataObject */
$contentDataObject = $this->contentFactory->create()
->setName($newEntry['content'][ImageContentInterface::NAME])
->setBase64EncodedData($newEntry['content'][ImageContentInterface::BASE64_ENCODED_DATA])
->setType($newEntry['content'][ImageContentInterface::TYPE]);
$newEntry['content'] = $contentDataObject;
$this->processNewMediaGalleryEntry($product, $newEntry);

$finalGallery = $product->getData('media_gallery');
$newEntryId = key(array_diff_key($product->getData('media_gallery')['images'], $entriesById));
if (isset($newEntry['extension_attributes'])) {
$this->processExtensionAttributes($newEntry, $newEntry['extension_attributes']);
}
$newEntry = array_replace_recursive($newEntry, $finalGallery['images'][$newEntryId]);
$entriesById[$newEntryId] = $newEntry;
$finalGallery['images'][$newEntryId] = $newEntry;
$product->setData('media_gallery', $finalGallery);
}
}
}
Loading

0 comments on commit 8848d94

Please sign in to comment.