Skip to content

Commit

Permalink
Merge pull request #6385 from magento-l3/BUGFIX-11-23
Browse files Browse the repository at this point in the history
[tango] BUGFIX DELIVERY
  • Loading branch information
dhorytskyi authored Dec 13, 2020
2 parents 650f254 + a91d3a0 commit ad29452
Show file tree
Hide file tree
Showing 50 changed files with 1,900 additions and 73 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);
}
}
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>
Loading

0 comments on commit ad29452

Please sign in to comment.