Skip to content

Commit

Permalink
Merge pull request #2 from magento/2.2-develop
Browse files Browse the repository at this point in the history
PR2
  • Loading branch information
Carlos Lizaga authored Nov 22, 2017
2 parents d4e666f + 08b20b5 commit b64e470
Show file tree
Hide file tree
Showing 60 changed files with 2,257 additions and 569 deletions.
9 changes: 7 additions & 2 deletions app/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,17 @@
unset($_SERVER['ORIG_PATH_INFO']);
}

if (!empty($_SERVER['MAGE_PROFILER'])
if (
(!empty($_SERVER['MAGE_PROFILER']) || file_exists(BP . '/var/profiler.flag'))
&& isset($_SERVER['HTTP_ACCEPT'])
&& strpos($_SERVER['HTTP_ACCEPT'], 'text/html') !== false
) {
$profilerFlag = isset($_SERVER['MAGE_PROFILER']) && strlen($_SERVER['MAGE_PROFILER'])
? $_SERVER['MAGE_PROFILER']
: trim(file_get_contents(BP . '/var/profiler.flag'));

\Magento\Framework\Profiler::applyConfig(
$_SERVER['MAGE_PROFILER'],
$profilerFlag,
BP,
!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,14 @@ public function setCollection($collection)
$this->_collection->setPageSize($limit);
}
if ($this->getCurrentOrder()) {
$this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
if ($this->getCurrentOrder() == 'position') {
$this->_collection->addAttributeToSort(
$this->getCurrentOrder(),
$this->getCurrentDirection()
)->addAttributeToSort('entity_id', $this->getCurrentDirection());
} else {
$this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
}
}
return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,25 +369,55 @@ protected function getAttributeValues($entityIds, $storeId)
}
$values = [];

foreach ($entityIds as $entityId) {
$values[$entityId] = [];
$linkIds = $this->getLinkIds($entityIds);
foreach ($linkIds as $linkId) {
$values[$linkId] = [];
}

$attributes = $this->getAttributes();
$attributesType = ['varchar', 'int', 'decimal', 'text', 'datetime'];
$linkField = $this->getCategoryMetadata()->getLinkField();
foreach ($attributesType as $type) {
foreach ($this->getAttributeTypeValues($type, $entityIds, $storeId) as $row) {
if (isset($row[$this->getCategoryMetadata()->getLinkField()]) && isset($row['attribute_id'])) {
if (isset($row[$linkField]) && isset($row['attribute_id'])) {
$attributeId = $row['attribute_id'];
if (isset($attributes[$attributeId])) {
$attributeCode = $attributes[$attributeId]['attribute_code'];
$values[$row[$this->getCategoryMetadata()->getLinkField()]][$attributeCode] = $row['value'];
$values[$row[$linkField]][$attributeCode] = $row['value'];
}
}
}
}

return $values;
}

/**
* Translate entity ids into link ids
*
* Used for rows with no EAV attributes set.
*
* @param array $entityIds
* @return array
*/
private function getLinkIds(array $entityIds)
{
$linkField = $this->getCategoryMetadata()->getLinkField();
if ($linkField === 'entity_id') {
return $entityIds;
}

$select = $this->connection->select()->from(
['e' => $this->connection->getTableName($this->getTableName('catalog_category_entity'))],
[$linkField]
)->where(
'e.entity_id IN (?)',
$entityIds
);

return $this->connection->fetchCol($select);
}

/**
* Return attribute values for given entities and store of specific attribute type
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,22 @@ protected function populateFlatTables(array $stores)
}
/** @TODO Do something with chunks */
$categoriesIdsChunks = array_chunk($categoriesIds[$store->getRootCategoryId()], 500);

foreach ($categoriesIdsChunks as $categoriesIdsChunk) {
$attributesData = $this->getAttributeValues($categoriesIdsChunk, $store->getId());
$linkField = $this->categoryMetadata->getLinkField();

$data = [];
foreach ($categories[$store->getRootCategoryId()] as $category) {
if (!isset($attributesData[$category[$this->categoryMetadata->getLinkField()]])) {
if (!isset($attributesData[$category[$linkField]])) {
continue;
}
$category['store_id'] = $store->getId();
$data[] = $this->prepareValuesToInsert(
array_merge($category, $attributesData[$category[$this->categoryMetadata->getLinkField()]])
array_merge($category, $attributesData[$category[$linkField]])
);
}

$this->connection->insertMultiple(
$this->addTemporaryTableSuffix($this->getMainStoreTable($store->getId())),
$data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,24 @@ public function reindex(array $entityIds = [], $useTempTable = false)
$categoriesIdsChunk = $this->filterIdsByStore($categoriesIdsChunk, $store);

$attributesData = $this->getAttributeValues($categoriesIdsChunk, $store->getId());
$linkField = $this->categoryMetadata->getLinkField();
$data = [];
foreach ($categoriesIdsChunk as $categoryId) {
if (!isset($attributesData[$categoryId])) {
continue;
}

try {
$category = $this->categoryRepository->get($categoryId);
} catch (NoSuchEntityException $e) {
continue;
}

$categoryData = $category->getData();
if (!isset($attributesData[$categoryData[$linkField]])) {
continue;
}

$data[] = $this->prepareValuesToInsert(
array_merge(
$category->getData(),
$attributesData[$categoryId],
$categoryData,
$attributesData[$categoryData[$linkField]],
['store_id' => $store->getId()]
)
);
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Catalog/Model/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -1712,7 +1712,7 @@ public function isInStock()
* Get attribute text by its code
*
* @param string $attributeCode Code of the attribute
* @return string
* @return string|array|null
*/
public function getAttributeText($attributeCode)
{
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/Catalog/Model/Product/Option/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ public function getProduct()
public function saveValues()
{
foreach ($this->getValues() as $value) {
$this->isDeleted(false);
$this->setData(
$value
)->setData(
Expand Down
19 changes: 10 additions & 9 deletions app/code/Magento/CatalogImportExport/Model/Import/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Magento\Catalog\Model\Product\Visibility;
use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface as ValidatorInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem;
use Magento\Framework\Model\ResourceModel\Db\ObjectRelationProcessor;
use Magento\Framework\Model\ResourceModel\Db\TransactionManagerInterface;
Expand Down Expand Up @@ -712,7 +713,7 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
* @param \Magento\CatalogInventory\Model\Spi\StockStateProviderInterface $stockStateProvider
* @param \Magento\Catalog\Helper\Data $catalogData
* @param \Magento\ImportExport\Model\Import\Config $importConfig
* @param Proxy\Product\ResourceFactory $resourceFactory
* @param Proxy\Product\ResourceModelFactory $resourceFactory
* @param Product\OptionFactory $optionFactory
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $setColFactory
* @param Product\Type\Factory $productTypeFactory
Expand Down Expand Up @@ -1084,12 +1085,12 @@ protected function _initTypeModels()
$params = [$this, $productTypeName];
if (!($model = $this->_productTypeFactory->create($productTypeConfig['model'], ['params' => $params]))
) {
throw new \Magento\Framework\Exception\LocalizedException(
throw new LocalizedException(
__('Entity type model \'%1\' is not found', $productTypeConfig['model'])
);
}
if (!$model instanceof \Magento\CatalogImportExport\Model\Import\Product\Type\AbstractType) {
throw new \Magento\Framework\Exception\LocalizedException(
throw new LocalizedException(
__(
'Entity type model must be an instance of '
. \Magento\CatalogImportExport\Model\Import\Product\Type\AbstractType::class
Expand Down Expand Up @@ -1551,6 +1552,7 @@ public function getImagesFromRow(array $rowData)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
* @throws LocalizedException
*/
protected function _saveProducts()
{
Expand Down Expand Up @@ -1611,7 +1613,7 @@ protected function _saveProducts()

// wrong attribute_set_code was received
if (!$attributeSetId) {
throw new \Magento\Framework\Exception\LocalizedException(
throw new LocalizedException(
__(
'Wrong attribute set code "%1", please correct it and try again.',
$rowData['attribute_set_code']
Expand Down Expand Up @@ -1998,7 +2000,7 @@ protected function _getUploader()
}

if (!$this->_fileUploader->setTmpDir($tmpPath)) {
throw new \Magento\Framework\Exception\LocalizedException(
throw new LocalizedException(
__('File directory \'%1\' is not readable.', $tmpPath)
);
}
Expand All @@ -2007,7 +2009,7 @@ protected function _getUploader()

$this->_mediaDirectory->create($destinationPath);
if (!$this->_fileUploader->setDestDir($destinationPath)) {
throw new \Magento\Framework\Exception\LocalizedException(
throw new LocalizedException(
__('File directory \'%1\' is not writable.', $destinationPath)
);
}
Expand All @@ -2029,6 +2031,7 @@ public function getUploader()
* Return a new file name if the same file is already exists.
*
* @param string $fileName
* @param bool $renameFileOff
* @return string
*/
protected function uploadMediaFiles($fileName, $renameFileOff = false)
Expand Down Expand Up @@ -2753,9 +2756,7 @@ private function _customFieldsMapping($rowData)
}

/**
* Validate data rows and save bunches to DB
*
* @return $this
* {@inheritdoc}
*/
protected function _saveValidatedBunches()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ public function prepareAttributesWithDefaultValueForSave(array $rowData, $withDe
public function clearEmptyData(array $rowData)
{
foreach ($this->_getProductAttributes($rowData) as $attrCode => $attrParams) {
if (!$attrParams['is_static'] && empty($rowData[$attrCode])) {
if (!$attrParams['is_static'] && !isset($rowData[$attrCode])) {
unset($rowData[$attrCode]);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\CatalogInventory\Observer;

use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\CatalogInventory\Api\StockConfigurationInterface;
use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory;
use Magento\CatalogInventory\Api\StockItemRepositoryInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

/**
* Add Stock items to product collection.
*/
class AddStockItemsObserver implements ObserverInterface
{
/**
* @var StockItemCriteriaInterfaceFactory
*/
private $criteriaInterfaceFactory;

/**
* @var StockItemRepositoryInterface
*/
private $stockItemRepository;

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

/**
* AddStockItemsObserver constructor.
*
* @param StockItemCriteriaInterfaceFactory $criteriaInterfaceFactory
* @param StockItemRepositoryInterface $stockItemRepository
* @param StockConfigurationInterface $stockConfiguration
*/
public function __construct(
StockItemCriteriaInterfaceFactory $criteriaInterfaceFactory,
StockItemRepositoryInterface $stockItemRepository,
StockConfigurationInterface $stockConfiguration
) {
$this->criteriaInterfaceFactory = $criteriaInterfaceFactory;
$this->stockItemRepository = $stockItemRepository;
$this->stockConfiguration = $stockConfiguration;
}

/**
* Add stock items to products in collection.
*
* @param Observer $observer
* @return void
*/
public function execute(Observer $observer)
{
/** @var Collection $productCollection */
$productCollection = $observer->getData('collection');
$productIds = array_keys($productCollection->getItems());
$criteria = $this->criteriaInterfaceFactory->create();
$criteria->setProductsFilter($productIds);
$criteria->setScopeFilter($this->stockConfiguration->getDefaultScopeId());
$stockItemCollection = $this->stockItemRepository->getList($criteria);
foreach ($stockItemCollection->getItems() as $item) {
/** @var Product $product */
$product = $productCollection->getItemById($item->getProductId());
$productExtension = $product->getExtensionAttributes();
$productExtension->setStockItem($item);
$product->setExtensionAttributes($productExtension);
}
}
}
Loading

0 comments on commit b64e470

Please sign in to comment.