|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\Catalog\Model\ResourceModel\Attribute; |
| 8 | + |
| 9 | +use Magento\Eav\Model\Entity\Attribute\AbstractAttribute; |
| 10 | +use Magento\Framework\EntityManager\EntityMetadataInterface; |
| 11 | +use Magento\Store\Api\Data\StoreInterface; |
| 12 | +use Magento\Store\Model\Store; |
| 13 | +use Magento\Store\Model\StoreManagerInterface; |
| 14 | +use Magento\Catalog\Model\ResourceModel\Eav\Attribute as CatalogEavAttribute; |
| 15 | +use Magento\Store\Model\Website; |
| 16 | +use Magento\Framework\Model\Entity\ScopeInterface; |
| 17 | + |
| 18 | +/** |
| 19 | + * Builds scope-related conditions for catalog attributes |
| 20 | + * |
| 21 | + * Class ConditionBuilder |
| 22 | + * @package Magento\Catalog\Model\ResourceModel\Attribute |
| 23 | + */ |
| 24 | +class ConditionBuilder |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var StoreManagerInterface |
| 28 | + */ |
| 29 | + private $storeManager; |
| 30 | + |
| 31 | + /** |
| 32 | + * ConditionBuilder constructor |
| 33 | + * @param StoreManagerInterface $storeManager |
| 34 | + */ |
| 35 | + public function __construct(StoreManagerInterface $storeManager) |
| 36 | + { |
| 37 | + $this->storeManager = $storeManager; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Returns conditions for existing attribute actions (update/delete) if attribute scope is "website" |
| 42 | + * |
| 43 | + * @param AbstractAttribute $attribute |
| 44 | + * @param EntityMetadataInterface $metadata |
| 45 | + * @param ScopeInterface[] $scopes |
| 46 | + * @param string $linkFieldValue |
| 47 | + * @return array |
| 48 | + */ |
| 49 | + public function buildExistingAttributeWebsiteScope( |
| 50 | + AbstractAttribute $attribute, |
| 51 | + EntityMetadataInterface $metadata, |
| 52 | + array $scopes, |
| 53 | + $linkFieldValue |
| 54 | + ) { |
| 55 | + $website = $this->getWebsiteForWebsiteScope($scopes); |
| 56 | + if (!$website) { |
| 57 | + return []; |
| 58 | + } |
| 59 | + $storeIds = $website->getStoreIds(); |
| 60 | + |
| 61 | + $condition = [ |
| 62 | + $metadata->getLinkField() . ' = ?' => $linkFieldValue, |
| 63 | + 'attribute_id = ?' => $attribute->getAttributeId(), |
| 64 | + ]; |
| 65 | + |
| 66 | + $conditions = []; |
| 67 | + foreach ($storeIds as $storeId) { |
| 68 | + $identifier = $metadata->getEntityConnection()->quoteIdentifier(Store::STORE_ID); |
| 69 | + $condition[$identifier . ' = ?'] = $storeId; |
| 70 | + $conditions[] = $condition; |
| 71 | + } |
| 72 | + |
| 73 | + return $conditions; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Returns conditions for new attribute action (insert) if attribute scope is "website" |
| 78 | + * |
| 79 | + * @param AbstractAttribute $attribute |
| 80 | + * @param EntityMetadataInterface $metadata |
| 81 | + * @param ScopeInterface[] $scopes |
| 82 | + * @param string $linkFieldValue |
| 83 | + * @return array |
| 84 | + */ |
| 85 | + public function buildNewAttributesWebsiteScope( |
| 86 | + AbstractAttribute $attribute, |
| 87 | + EntityMetadataInterface $metadata, |
| 88 | + array $scopes, |
| 89 | + $linkFieldValue |
| 90 | + ) { |
| 91 | + $website = $this->getWebsiteForWebsiteScope($scopes); |
| 92 | + if (!$website) { |
| 93 | + return []; |
| 94 | + } |
| 95 | + $storeIds = $website->getStoreIds(); |
| 96 | + |
| 97 | + $condition = [ |
| 98 | + $metadata->getLinkField() => $linkFieldValue, |
| 99 | + 'attribute_id' => $attribute->getAttributeId(), |
| 100 | + ]; |
| 101 | + |
| 102 | + $conditions = []; |
| 103 | + foreach ($storeIds as $storeId) { |
| 104 | + $condition[Store::STORE_ID] = $storeId; |
| 105 | + $conditions[] = $condition; |
| 106 | + } |
| 107 | + |
| 108 | + return $conditions; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @param array $scopes |
| 113 | + * @return null|Website |
| 114 | + */ |
| 115 | + private function getWebsiteForWebsiteScope(array $scopes) |
| 116 | + { |
| 117 | + $store = $this->getStoreFromScopes($scopes); |
| 118 | + return $store ? $store->getWebsite() : null; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @param ScopeInterface[] $scopes |
| 123 | + * @return StoreInterface|null |
| 124 | + */ |
| 125 | + private function getStoreFromScopes(array $scopes) |
| 126 | + { |
| 127 | + foreach ($scopes as $scope) { |
| 128 | + if (Store::STORE_ID === $scope->getIdentifier()) { |
| 129 | + return $this->storeManager->getStore($scope->getValue()); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return null; |
| 134 | + } |
| 135 | +} |
0 commit comments