Skip to content

Commit

Permalink
Merge branch '2.4-develop' into ref-AdminMassOrdersCancelCompleteAndC…
Browse files Browse the repository at this point in the history
…losedAPITest
  • Loading branch information
engcom-Charlie committed Dec 15, 2020
2 parents bc231ce + ad29452 commit de26bef
Show file tree
Hide file tree
Showing 96 changed files with 3,618 additions and 391 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,22 @@ public function getImagesJson()
*/
private function sortImagesByPosition($images)
{
if (is_array($images)) {
$nullPositions = [];
foreach ($images as $index => $image) {
if ($image['position'] === null) {
$nullPositions[] = $image;
unset($images[$index]);
}
}
if (is_array($images) && !empty($images)) {
usort(
$images,
function ($imageA, $imageB) {
return ($imageA['position'] < $imageB['position']) ? -1 : 1;
}
);
}
return $images;
return array_merge($images, $nullPositions);
}

/**
Expand Down
90 changes: 82 additions & 8 deletions app/code/Magento/Catalog/Block/Product/View/Options/Type/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
*/
namespace Magento\Catalog\Block\Product\View\Options\Type;

use DateTimeZone;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Data\Form\FilterFactory;
use Magento\Framework\Stdlib\DateTime;

/**
* Product options text type block
*
Expand All @@ -27,22 +32,30 @@ class Date extends \Magento\Catalog\Block\Product\View\Options\AbstractOptions
*/
protected $_catalogProductOptionTypeDate;

/**
* @var FilterFactory
*/
private $filterFactory;

/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Framework\Pricing\Helper\Data $pricingHelper
* @param \Magento\Catalog\Helper\Data $catalogData
* @param \Magento\Catalog\Model\Product\Option\Type\Date $catalogProductOptionTypeDate
* @param array $data
* @param FilterFactory|null $filterFactory
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Framework\Pricing\Helper\Data $pricingHelper,
\Magento\Catalog\Helper\Data $catalogData,
\Magento\Catalog\Model\Product\Option\Type\Date $catalogProductOptionTypeDate,
array $data = []
array $data = [],
?FilterFactory $filterFactory = null
) {
$this->_catalogProductOptionTypeDate = $catalogProductOptionTypeDate;
parent::__construct($context, $pricingHelper, $catalogData, $data);
$this->filterFactory = $filterFactory ?? ObjectManager::getInstance()->get(FilterFactory::class);
}

/**
Expand Down Expand Up @@ -77,14 +90,24 @@ public function getDateHtml()
public function getCalendarDateHtml()
{
$option = $this->getOption();
$value = $this->getProduct()->getPreconfiguredValues()->getData('options/' . $option->getId() . '/date');
$values = $this->getProduct()->getPreconfiguredValues()->getData('options/' . $option->getId());

$yearStart = $this->_catalogProductOptionTypeDate->getYearStart();
$yearEnd = $this->_catalogProductOptionTypeDate->getYearEnd();

$dateFormat = $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT);
$dateFormat = $this->_localeDate->getDateFormatWithLongYear();
/** Escape RTL characters which are present in some locales and corrupt formatting */
$escapedDateFormat = preg_replace('/[^MmDdYy\/\.\-]/', '', $dateFormat);
$value = null;
if (is_array($values)) {
$date = $this->getInternalDateString($values);
if ($date !== null) {
$dateFilter = $this->filterFactory->create('date', ['format' => $escapedDateFormat]);
$value = $dateFilter->outputFilter($date);
} elseif (isset($values['date'])) {
$value = $values['date'];
}
}
$calendar = $this->getLayout()->createBlock(
\Magento\Framework\View\Element\Html\Date::class
)->setId(
Expand Down Expand Up @@ -158,8 +181,8 @@ public function getTimeHtml()
* Return drop-down html with range of values
*
* @param string $name Id/name of html select element
* @param int $from Start position
* @param int $to End position
* @param int $from Start position
* @param int $to End position
* @param int|null $value Value selected
* @return string Formatted Html
*/
Expand Down Expand Up @@ -209,9 +232,8 @@ protected function _getHtmlSelect($name, $value = null)

$select->setExtraParams($extraParams);
if ($value === null) {
$value = $this->getProduct()->getPreconfiguredValues()->getData(
'options/' . $option->getId() . '/' . $name
);
$values = $this->getProduct()->getPreconfiguredValues()->getData('options/' . $option->getId());
$value = is_array($values) ? $this->parseDate($values, $name) : null;
}
if ($value !== null) {
$select->setValue($value);
Expand All @@ -233,4 +255,56 @@ protected function _getValueWithLeadingZeros($value)
}
return $value < 10 ? '0' . $value : $value;
}

/**
* Get internal date format of provided value
*
* @param array $value
* @return string|null
*/
private function getInternalDateString(array $value): ?string
{
$result = null;
if (!empty($value['date']) && !empty($value['date_internal'])) {
$dateTimeZone = new DateTimeZone($this->_localeDate->getConfigTimezone());
$dateTimeObject = date_create_from_format(
DateTime::DATETIME_PHP_FORMAT,
$value['date_internal'],
$dateTimeZone
);
if ($dateTimeObject !== false) {
$result = $dateTimeObject->format(DateTime::DATE_PHP_FORMAT);
}
} elseif (!empty($value['day']) && !empty($value['month']) && !empty($value['year'])) {
$dateTimeObject = $this->_localeDate->date();
$dateTimeObject->setDate((int) $value['year'], (int) $value['month'], (int) $value['day']);
$result = $dateTimeObject->format(DateTime::DATE_PHP_FORMAT);
}
return $result;
}

/**
* Parse option value and return the requested part
*
* @param array $value
* @param string $part [year, month, day, hour, minute, day_part]
* @return string|null
*/
private function parseDate(array $value, string $part): ?string
{
$result = null;
if (!empty($value['date']) && !empty($value['date_internal'])) {
$formatDate = explode(' ', $value['date_internal']);
$date = explode('-', $formatDate[0]);
$value['year'] = $date[0];
$value['month'] = $date[1];
$value['day'] = $date[2];
}

if (isset($value[$part])) {
$result = (string) $value[$part];
}

return $result;
}
}
39 changes: 30 additions & 9 deletions app/code/Magento/Catalog/Model/Product/Gallery/CreateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,29 +267,50 @@ protected function processNewAndExistingImages($product, array &$images)
{
foreach ($images as &$image) {
if (empty($image['removed'])) {
$isNew = empty($image['value_id']);
$data = $this->processNewImage($product, $image);

if (!$product->isObjectNew()) {
$this->resourceModel->deleteGalleryValueInStore(
$image['value_id'],
$product->getData($this->metadata->getLinkField()),
$product->getStoreId()
);
}
// Add per store labels, position, disabled
$data['value_id'] = $image['value_id'];
$data['label'] = isset($image['label']) ? $image['label'] : '';
$data['position'] = isset($image['position']) ? (int)$image['position'] : 0;
$data['position'] = isset($image['position']) && $image['position'] !== ''
? (int)$image['position']
: null;
$data['disabled'] = isset($image['disabled']) ? (int)$image['disabled'] : 0;
$data['store_id'] = (int)$product->getStoreId();

$data[$this->metadata->getLinkField()] = (int)$product->getData($this->metadata->getLinkField());

$this->resourceModel->insertGalleryValueInStore($data);
$this->saveGalleryStoreValue($product, $data);
if ($isNew && $data['store_id'] !== Store::DEFAULT_STORE_ID) {
$dataForDefaultScope = $data;
$dataForDefaultScope['store_id'] = Store::DEFAULT_STORE_ID;
$dataForDefaultScope['disabled'] = 0;
$dataForDefaultScope['label'] = null;
$this->saveGalleryStoreValue($product, $dataForDefaultScope);
}
}
}
}

/**
* Save media gallery store value
*
* @param Product $product
* @param array $data
*/
private function saveGalleryStoreValue(Product $product, array $data): void
{
if (!$product->isObjectNew()) {
$this->resourceModel->deleteGalleryValueInStore(
$data['value_id'],
$data[$this->metadata->getLinkField()],
$data['store_id']
);
}
$this->resourceModel->insertGalleryValueInStore($data);
}

/**
* Processes image as new.
*
Expand Down
32 changes: 29 additions & 3 deletions app/code/Magento/Catalog/Model/Product/Gallery/ReadHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ public function execute($entity, $arguments = [])

$this->addMediaDataToProduct(
$entity,
$mediaEntries
$this->sortMediaEntriesByPosition($mediaEntries)
);

return $entity;
}

Expand Down Expand Up @@ -108,7 +108,7 @@ public function getAttribute()
* Find default value
*
* @param string $key
* @param string[] &$image
* @param string[] $image
* @return string
* @deprecated 101.0.1
* @since 101.0.0
Expand All @@ -121,4 +121,30 @@ protected function findDefaultValue($key, &$image)

return '';
}

/**
* Sort media entries by position
*
* @param array $mediaEntries
* @return array
*/
private function sortMediaEntriesByPosition(array $mediaEntries): array
{
$mediaEntriesWithNullPositions = [];
foreach ($mediaEntries as $index => $mediaEntry) {
if ($mediaEntry['position'] === null) {
$mediaEntriesWithNullPositions[] = $mediaEntry;
unset($mediaEntries[$index]);
}
}
if (!empty($mediaEntries)) {
usort(
$mediaEntries,
function ($entryA, $entryB) {
return ($entryA['position'] < $entryB['position']) ? -1 : 1;
}
);
}
return array_merge($mediaEntries, $mediaEntriesWithNullPositions);
}
}
24 changes: 21 additions & 3 deletions app/code/Magento/Catalog/Model/Product/Option/Type/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

namespace Magento\Catalog\Model\Product\Option\Type;

use Magento\Catalog\Model\Product\Exception as ProductException;
use Magento\Catalog\Helper\Product as ProductHelper;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem;
use Magento\Framework\Exception\LocalizedException;
use Magento\Catalog\Model\Product\Exception as ProductException;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\App\ObjectManager;

Expand Down Expand Up @@ -91,6 +92,11 @@ class File extends \Magento\Catalog\Model\Product\Option\Type\DefaultType
*/
private $filesystem;

/**
* @var ProductHelper
*/
private $productHelper;

/**
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
Expand All @@ -103,6 +109,7 @@ class File extends \Magento\Catalog\Model\Product\Option\Type\DefaultType
* @param array $data
* @param Filesystem $filesystem
* @param Json|null $serializer
* @param ProductHelper|null $productHelper
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -116,7 +123,8 @@ public function __construct(
\Magento\Framework\Escaper $escaper,
array $data = [],
Filesystem $filesystem = null,
Json $serializer = null
Json $serializer = null,
ProductHelper $productHelper = null
) {
$this->_itemOptionFactory = $itemOptionFactory;
$this->_urlBuilder = $urlBuilder;
Expand All @@ -129,6 +137,7 @@ public function __construct(
$this->validatorInfo = $validatorInfo;
$this->validatorFile = $validatorFile;
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
$this->productHelper = $productHelper ?: ObjectManager::getInstance()->get(ProductHelper::class);
parent::__construct($checkoutSession, $scopeConfig, $data);
}

Expand Down Expand Up @@ -223,12 +232,21 @@ public function validateUserValue($values)
$this->setIsValid(true);
$option = $this->getOption();

if (isset($values['files_prefix'])) {
$processingParams = ['files_prefix' => $values['files_prefix']];
$processingParams = array_merge($this->_getProcessingParams()->getData(), $processingParams);
$this->productHelper->addParamsToBuyRequest($this->getRequest(), $processingParams);
}

/*
* Check whether we receive uploaded file or restore file by: reorder/edit configuration or
* previous configuration with no newly uploaded file
*/
$fileInfo = null;
if (isset($values[$option->getId()]) && is_array($values[$option->getId()])) {
if (isset($values[$option->getId()])) {
if (is_string($values[$option->getId()])) {
$values[$option->getId()] = explode(',', $values[$option->getId()]);
}
// Legacy style, file info comes in array with option id index
$fileInfo = $values[$option->getId()];
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
<actionGroup name="AdminSearchGridByStringNoClearActionGroup">
<annotations>
<description>Search the Admin grid by string without clearing filters.</description>
</annotations>
<arguments>
<argument name="keyword" defaultValue="" type="string"/>
</arguments>

<fillField selector="{{AdminProductGridFilterSection.keywordSearch}}" userInput="{{keyword}}" stepKey="fillKeywordSearchField"/>
<click selector="{{AdminProductGridFilterSection.keywordSearchButton}}" stepKey="clickKeywordSearch"/>
<waitForPageLoad stepKey="waitForProductSearch"/>
</actionGroup>
</actionGroups>
4 changes: 4 additions & 0 deletions app/code/Magento/Catalog/Test/Mftf/Data/ProductData.xml
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,10 @@
<var key="sku" entityType="product" entityKey="sku" />
<requiredEntity type="product_option">ProductOptionValueDropdown</requiredEntity>
</entity>
<entity name="productWithFileOption" type="product">
<var key="sku" entityType="product" entityKey="sku" />
<requiredEntity type="product_option">ProductOptionFile</requiredEntity>
</entity>
<entity name="productWithDropdownAndFieldOptions" type="product">
<var key="sku" entityType="product" entityKey="sku" />
<requiredEntity type="product_option">ProductOptionValueDropdown</requiredEntity>
Expand Down
Loading

0 comments on commit de26bef

Please sign in to comment.