|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2016 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Catalog\Model\Category\Link; |
| 7 | + |
| 8 | +use Magento\Catalog\Api\Data\CategoryLinkInterface; |
| 9 | +use Magento\Catalog\Model\Indexer\Product\Category; |
| 10 | +use Magento\Framework\EntityManager\Operation\ExtensionInterface; |
| 11 | + |
| 12 | +/** |
| 13 | + * Save handler for catalog product link. |
| 14 | + */ |
| 15 | +class SaveHandler implements ExtensionInterface |
| 16 | +{ |
| 17 | + |
| 18 | + /** |
| 19 | + * @var \Magento\Catalog\Model\ResourceModel\Product\CategoryLink |
| 20 | + */ |
| 21 | + private $productCategoryLink; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var \Magento\Framework\EntityManager\HydratorPool |
| 25 | + */ |
| 26 | + private $hydratorPool; |
| 27 | + |
| 28 | + /** |
| 29 | + * SaveHandler constructor. |
| 30 | + * |
| 31 | + * @param \Magento\Catalog\Model\ResourceModel\Product\CategoryLink $productCategoryLink |
| 32 | + * @param \Magento\Framework\EntityManager\HydratorPool $hydratorPool |
| 33 | + */ |
| 34 | + public function __construct( |
| 35 | + \Magento\Catalog\Model\ResourceModel\Product\CategoryLink $productCategoryLink, |
| 36 | + \Magento\Framework\EntityManager\HydratorPool $hydratorPool |
| 37 | + ) { |
| 38 | + $this->productCategoryLink = $productCategoryLink; |
| 39 | + $this->hydratorPool = $hydratorPool; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @param object $entity |
| 44 | + * @param array $arguments |
| 45 | + * @return object |
| 46 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 47 | + */ |
| 48 | + public function execute($entity, $arguments = []) |
| 49 | + { |
| 50 | + $entity->setIsChangedCategories(false); |
| 51 | + |
| 52 | + $extensionAttributes = $entity->getExtensionAttributes(); |
| 53 | + if ($extensionAttributes === null && !$entity->hasCategoryIds()) { |
| 54 | + return $entity; |
| 55 | + } |
| 56 | + |
| 57 | + $modelCategoryLinks = $this->getCategoryLinksPositions($entity); |
| 58 | + |
| 59 | + $dtoCategoryLinks = $extensionAttributes->getCategoryLinks(); |
| 60 | + if ($dtoCategoryLinks !== null) { |
| 61 | + $hydrator = $this->hydratorPool->getHydrator(CategoryLinkInterface::class); |
| 62 | + $dtoCategoryLinks = array_map(function ($categoryLink) use ($hydrator) { |
| 63 | + return $hydrator->extract($categoryLink) ; |
| 64 | + }, $dtoCategoryLinks); |
| 65 | + $processLinks = $this->mergeCategoryLinks($dtoCategoryLinks, $modelCategoryLinks); |
| 66 | + } else { |
| 67 | + $processLinks = $modelCategoryLinks; |
| 68 | + } |
| 69 | + |
| 70 | + $affectedCategoryIds = $this->productCategoryLink->saveCategoryLinks($entity, $processLinks); |
| 71 | + |
| 72 | + if (!empty($affectedCategoryIds)) { |
| 73 | + $entity->setAffectedCategoryIds($affectedCategoryIds); |
| 74 | + $entity->setIsChangedCategories(true); |
| 75 | + } |
| 76 | + |
| 77 | + return $entity; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @param object $entity |
| 82 | + * @return array |
| 83 | + */ |
| 84 | + private function getCategoryLinksPositions($entity) |
| 85 | + { |
| 86 | + $result = []; |
| 87 | + $currentCategoryLinks = $this->productCategoryLink->getCategoryLinks($entity, $entity->getCategoryIds()); |
| 88 | + foreach ($entity->getCategoryIds() as $categoryId) { |
| 89 | + $key = array_search($categoryId, array_column($currentCategoryLinks, 'category_id')); |
| 90 | + if ($key === false) { |
| 91 | + $result[] = ['category_id' => (int)$categoryId, 'position' => 0]; |
| 92 | + } else { |
| 93 | + $result[] = $currentCategoryLinks[$key]; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return $result; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Merge category links |
| 102 | + * |
| 103 | + * @param array $newCategoryPositions |
| 104 | + * @param array $oldCategoryPositions |
| 105 | + * @return array |
| 106 | + */ |
| 107 | + private function mergeCategoryLinks($newCategoryPositions, $oldCategoryPositions) |
| 108 | + { |
| 109 | + $result = []; |
| 110 | + if (empty($newCategoryPositions)) { |
| 111 | + return $result; |
| 112 | + } |
| 113 | + |
| 114 | + foreach ($newCategoryPositions as $newCategoryPosition) { |
| 115 | + $key = array_search( |
| 116 | + $newCategoryPosition['category_id'], |
| 117 | + array_column($oldCategoryPositions, 'category_id') |
| 118 | + ); |
| 119 | + |
| 120 | + if ($key === false) { |
| 121 | + $result[] = $newCategoryPosition; |
| 122 | + } elseif ($oldCategoryPositions[$key]['position'] != $newCategoryPosition['position']) { |
| 123 | + $result[] = $newCategoryPositions[$key]; |
| 124 | + unset($oldCategoryPositions[$key]); |
| 125 | + } |
| 126 | + } |
| 127 | + $result = array_merge($result, $oldCategoryPositions); |
| 128 | + |
| 129 | + return $result; |
| 130 | + } |
| 131 | +} |
0 commit comments