Skip to content

Commit

Permalink
Merge branch '2.4-develop' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
marcostrama authored Sep 16, 2020
2 parents 16b8d6a + b9c13fd commit 65b5ee4
Show file tree
Hide file tree
Showing 153 changed files with 7,453 additions and 1,000 deletions.
10 changes: 5 additions & 5 deletions app/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
#ini_set('display_errors', 1);

/* PHP version validation */
if (!defined('PHP_VERSION_ID') || PHP_VERSION_ID < 70103) {
if (!defined('PHP_VERSION_ID') || PHP_VERSION_ID < 70300) {
if (PHP_SAPI == 'cli') {
echo 'Magento supports PHP 7.1.3 or later. ' .
'Please read https://devdocs.magento.com/guides/v2.3/install-gde/system-requirements-tech.html';
echo 'Magento supports PHP 7.3.0 or later. ' .
'Please read https://devdocs.magento.com/guides/v2.4/install-gde/system-requirements-tech.html';
} else {
echo <<<HTML
<div style="font:12px/1.35em arial, helvetica, sans-serif;">
<p>Magento supports PHP 7.1.3 or later. Please read
<a target="_blank" href="https://devdocs.magento.com/guides/v2.3/install-gde/system-requirements-tech.html">
<p>Magento supports PHP 7.3.0 or later. Please read
<a target="_blank" href="https://devdocs.magento.com/guides/v2.4/install-gde/system-requirements-tech.html">
Magento System Requirements</a>.
</div>
HTML;
Expand Down
13 changes: 12 additions & 1 deletion app/code/Magento/Catalog/Model/Product/Gallery/CreateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -596,10 +596,21 @@ private function canRemoveImage(ProductInterface $product, string $imageFile) :b
$canRemoveImage = true;
$gallery = $this->getImagesForAllStores($product);
$storeId = $product->getStoreId();
$storeIds = [];
$storeIds[] = 0;
$websiteIds = array_map('intval', $product->getWebsiteIds() ?? []);
foreach ($this->storeManager->getStores() as $store) {
if (in_array((int) $store->getWebsiteId(), $websiteIds, true)) {
$storeIds[] = (int) $store->getId();
}
}

if (!empty($gallery)) {
foreach ($gallery as $image) {
if ($image['filepath'] === $imageFile && (int) $image['store_id'] !== $storeId) {
if (in_array((int) $image['store_id'], $storeIds)
&& $image['filepath'] === $imageFile
&& (int) $image['store_id'] !== $storeId
) {
$canRemoveImage = false;
}
}
Expand Down
94 changes: 89 additions & 5 deletions app/code/Magento/Catalog/Model/Product/Gallery/UpdateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,69 @@
*/
namespace Magento\Catalog\Model\Product\Gallery;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Media\Config;
use Magento\Catalog\Model\ResourceModel\Product\Gallery;
use Magento\Framework\EntityManager\Operation\ExtensionInterface;
use Magento\Eav\Model\ResourceModel\AttributeValue;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Framework\Filesystem;
use Magento\Framework\Json\Helper\Data;
use Magento\MediaStorage\Helper\File\Storage\Database;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;

/**
* Update handler for catalog product gallery.
*
* @api
* @since 101.0.0
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class UpdateHandler extends \Magento\Catalog\Model\Product\Gallery\CreateHandler
class UpdateHandler extends CreateHandler
{
/**
* @var AttributeValue
*/
private $attributeValue;

/**
* @param MetadataPool $metadataPool
* @param ProductAttributeRepositoryInterface $attributeRepository
* @param Gallery $resourceModel
* @param Data $jsonHelper
* @param Config $mediaConfig
* @param Filesystem $filesystem
* @param Database $fileStorageDb
* @param StoreManagerInterface|null $storeManager
* @param AttributeValue|null $attributeValue
*/
public function __construct(
MetadataPool $metadataPool,
ProductAttributeRepositoryInterface $attributeRepository,
Gallery $resourceModel,
Data $jsonHelper,
Config $mediaConfig,
Filesystem $filesystem,
Database $fileStorageDb,
StoreManagerInterface $storeManager = null,
?AttributeValue $attributeValue = null
) {
parent::__construct(
$metadataPool,
$attributeRepository,
$resourceModel,
$jsonHelper,
$mediaConfig,
$filesystem,
$fileStorageDb,
$storeManager
);
$this->attributeValue = $attributeValue ?: ObjectManager::getInstance()->get(AttributeValue::class);
}

/**
* @inheritdoc
*
Expand All @@ -26,6 +78,7 @@ protected function processDeletedImages($product, array &$images)
$filesToDelete = [];
$recordsToDelete = [];
$picturesInOtherStores = [];
$imagesToDelete = [];

foreach ($this->resourceModel->getProductImages($product, $this->extractStoreIds($product)) as $image) {
$picturesInOtherStores[$image['filepath']] = true;
Expand All @@ -38,6 +91,7 @@ protected function processDeletedImages($product, array &$images)
continue;
}
$recordsToDelete[] = $image['value_id'];
$imagesToDelete[] = $image['file'];
$catalogPath = $this->mediaConfig->getBaseMediaPath();
$isFile = $this->mediaDirectory->isFile($catalogPath . $image['file']);
// only delete physical files if they are not used by any other products and if this file exist
Expand All @@ -48,8 +102,8 @@ protected function processDeletedImages($product, array &$images)
}
}

$this->deleteMediaAttributeValues($product, $imagesToDelete);
$this->resourceModel->deleteGallery($recordsToDelete);

$this->removeDeletedImages($filesToDelete);
}

Expand Down Expand Up @@ -94,14 +148,14 @@ protected function processNewImage($product, array &$image)
/**
* Retrieve store ids from product.
*
* @param \Magento\Catalog\Model\Product $product
* @param Product $product
* @return array
* @since 101.0.0
*/
protected function extractStoreIds($product)
{
$storeIds = $product->getStoreIds();
$storeIds[] = \Magento\Store\Model\Store::DEFAULT_STORE_ID;
$storeIds[] = Store::DEFAULT_STORE_ID;

// Removing current storeId.
$storeIds = array_flip($storeIds);
Expand All @@ -125,5 +179,35 @@ protected function removeDeletedImages(array $files)
foreach ($files as $filePath) {
$this->mediaDirectory->delete($catalogPath . '/' . $filePath);
}
return null;
}

/**
* Delete media attributes values for given images
*
* @param Product $product
* @param string[] $images
*/
private function deleteMediaAttributeValues(Product $product, array $images): void
{
if ($images) {
$values = $this->attributeValue->getValues(
ProductInterface::class,
$product->getData($this->metadata->getLinkField()),
$this->mediaConfig->getMediaAttributeCodes()
);
$valuesToDelete = [];
foreach ($values as $value) {
if (in_array($value['value'], $images, true)) {
$valuesToDelete[] = $value;
}
}
if ($valuesToDelete) {
$this->attributeValue->deleteValues(
ProductInterface::class,
$valuesToDelete
);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,17 @@ define([
_addItem: function (event, imageData) {
var count = this.element.find(this.options.imageSelector).length,
element,
imgElement;
imgElement,
position = count + 1,
lastElement = this.element.find(this.options.imageSelector + ':last');

if (lastElement.length === 1) {
position = parseInt(lastElement.data('imageData').position || count, 10) + 1;
}
imageData = $.extend({
'file_id': imageData['value_id'] ? imageData['value_id'] : Math.random().toString(33).substr(2, 18),
'disabled': imageData.disabled ? imageData.disabled : 0,
'position': count + 1,
'position': position,
sizeLabel: bytesToSize(imageData.size)
}, imageData);

Expand All @@ -206,7 +211,7 @@ define([
if (count === 0) {
element.prependTo(this.element);
} else {
element.insertAfter(this.element.find(this.options.imageSelector + ':last'));
element.insertAfter(lastElement);
}

if (!this.options.initialized &&
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogInventory\Model\ResourceModel;

use Magento\CatalogInventory\Api\Data\StockStatusInterface;
use Magento\CatalogInventory\Api\StockConfigurationInterface;
use Magento\CatalogInventory\Model\Stock;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Select;

/**
* Generic in-stock status filter
*/
class StockStatusFilter implements StockStatusFilterInterface
{
private const TABLE_NAME = 'cataloginventory_stock_status';
/**
* @var ResourceConnection
*/
private $resource;

/**
* @var StockConfigurationInterface
*/
private $stockConfiguration;

/**
* @param ResourceConnection $resource
* @param StockConfigurationInterface $stockConfiguration
*/
public function __construct(
ResourceConnection $resource,
StockConfigurationInterface $stockConfiguration
) {
$this->resource = $resource;
$this->stockConfiguration = $stockConfiguration;
}

/**
* @inheritDoc
*/
public function execute(
Select $select,
string $productTableAlias,
string $stockStatusTableAlias = self::TABLE_ALIAS,
?int $websiteId = null
): Select {
$stockStatusTable = $this->resource->getTableName(self::TABLE_NAME);
$joinCondition = [
"{$stockStatusTableAlias}.product_id = {$productTableAlias}.entity_id",
$select->getConnection()->quoteInto(
"{$stockStatusTableAlias}.website_id = ?",
$this->stockConfiguration->getDefaultScopeId()
),
$select->getConnection()->quoteInto(
"{$stockStatusTableAlias}.stock_id = ?",
Stock::DEFAULT_STOCK_ID
)
];
$select->join(
[$stockStatusTableAlias => $stockStatusTable],
implode(' AND ', $joinCondition),
[]
);
$select->where("{$stockStatusTableAlias}.stock_status = ?", StockStatusInterface::STATUS_IN_STOCK);

return $select;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogInventory\Model\ResourceModel;

use Magento\Framework\DB\Select;

/**
* In stock status filter interface.
*/
interface StockStatusFilterInterface
{
public const TABLE_ALIAS = 'stock_status';

/**
* Add in-stock status constraint to the select.
*
* @param Select $select
* @param string $productTableAliasAlias
* @param string $stockStatusTableAlias
* @param int|null $websiteId
* @return Select
*/
public function execute(
Select $select,
string $productTableAliasAlias,
string $stockStatusTableAlias = self::TABLE_ALIAS,
?int $websiteId = null
): Select;
}
1 change: 1 addition & 0 deletions app/code/Magento/CatalogInventory/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<preference for="Magento\CatalogInventory\Model\Spi\StockStateProviderInterface" type="Magento\CatalogInventory\Model\StockStateProvider" />

<preference for="Magento\CatalogInventory\Model\ResourceModel\QtyCounterInterface" type="Magento\CatalogInventory\Model\ResourceModel\Stock" />
<preference for="Magento\CatalogInventory\Model\ResourceModel\StockStatusFilterInterface" type="Magento\CatalogInventory\Model\ResourceModel\StockStatusFilter" />
<type name="Magento\Catalog\Model\Product\Attribute\Repository">
<plugin name="filterCustomAttribute" type="Magento\CatalogInventory\Model\Plugin\FilterCustomAttribute" />
</type>
Expand Down
6 changes: 5 additions & 1 deletion app/code/Magento/Checkout/Model/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ public function getQuote()
}
} else {
$quote->setIsCheckoutCart(true);
$quote->setCustomerIsGuest(1);
$this->_eventManager->dispatch('checkout_quote_init', ['quote' => $quote]);
}
}
Expand Down Expand Up @@ -382,8 +383,10 @@ public function loadCustomerQuote()

if ($customerQuote->getId() && $this->getQuoteId() != $customerQuote->getId()) {
if ($this->getQuoteId()) {
$quote = $this->getQuote();
$quote->setCustomerIsGuest(0);
$this->quoteRepository->save(
$customerQuote->merge($this->getQuote())->collectTotals()
$customerQuote->merge($quote)->collectTotals()
);
$newQuote = $this->quoteRepository->get($customerQuote->getId());
$this->quoteRepository->save(
Expand All @@ -402,6 +405,7 @@ public function loadCustomerQuote()
$this->getQuote()->getBillingAddress();
$this->getQuote()->getShippingAddress();
$this->getQuote()->setCustomer($this->_customerSession->getCustomerDataObject())
->setCustomerIsGuest(0)
->setTotalsCollectedFlag(false)
->collectTotals();
$this->quoteRepository->save($this->getQuote());
Expand Down
Loading

0 comments on commit 65b5ee4

Please sign in to comment.