diff --git a/vendor/magento/module-shared-catalog/Model/Repository.php b/vendor/magento/module-shared-catalog/Model/Repository.php index 4c257c5dd0eb..b61033a0e6e9 100644 --- a/vendor/magento/module-shared-catalog/Model/Repository.php +++ b/vendor/magento/module-shared-catalog/Model/Repository.php @@ -7,74 +7,82 @@ namespace Magento\SharedCatalog\Model; +use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface; +use Magento\Framework\Exception\StateException; +use Magento\SharedCatalog\Api\Data\SearchResultsInterfaceFactory; +use Magento\SharedCatalog\Api\Data\SharedCatalogInterface; +use Magento\SharedCatalog\Api\ProductItemManagementInterface; use Magento\SharedCatalog\Api\SharedCatalogRepositoryInterface; +use Magento\SharedCatalog\Model\ResourceModel\SharedCatalog; +use Magento\SharedCatalog\Model\ResourceModel\SharedCatalog\CollectionFactory; /** * Shared catalog repository. + * */ class Repository implements SharedCatalogRepositoryInterface { /** * List of shared Catalogs. * - * @var \Magento\SharedCatalog\Api\Data\SharedCatalogInterface[] + * @var SharedCatalogInterface[] */ private $instances = []; /** - * @var \Magento\SharedCatalog\Model\ResourceModel\SharedCatalog + * @var SharedCatalog */ private $sharedCatalogResource; /** - * @var \Magento\SharedCatalog\Model\ResourceModel\SharedCatalog\CollectionFactory + * @var CollectionFactory */ private $sharedCatalogCollectionFactory; /** - * @var \Magento\SharedCatalog\Api\Data\SearchResultsInterfaceFactory + * @var SearchResultsInterfaceFactory */ private $searchResultsFactory; /** - * @var \Magento\SharedCatalog\Api\ProductItemManagementInterface + * @var ProductItemManagementInterface */ private $sharedCatalogProductItemManagement; /** - * @var \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface + * @var CollectionProcessorInterface */ private $collectionProcessor; /** - * @var \Magento\SharedCatalog\Model\SharedCatalogValidator + * @var SharedCatalogValidator */ private $validator; /** - * @var \Magento\SharedCatalog\Model\SaveHandler + * @var SaveHandler */ private $saveHandler; /** * Repository constructor. * - * @param ResourceModel\SharedCatalog $sharedCatalogResource - * @param ResourceModel\SharedCatalog\CollectionFactory $sharedCatalogCollectionFactory - * @param \Magento\SharedCatalog\Api\Data\SearchResultsInterfaceFactory $searchResultsFactory - * @param \Magento\SharedCatalog\Api\ProductItemManagementInterface $sharedCatalogProductItemManagement - * @param \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface $collectionProcessor - * @param \Magento\SharedCatalog\Model\SharedCatalogValidator $validator - * @param \Magento\SharedCatalog\Model\SaveHandler $saveHandler + * @param SharedCatalog $sharedCatalogResource + * @param CollectionFactory $sharedCatalogCollectionFactory + * @param SearchResultsInterfaceFactory $searchResultsFactory + * @param ProductItemManagementInterface $sharedCatalogProductItemManagement + * @param CollectionProcessorInterface $collectionProcessor + * @param SharedCatalogValidator $validator + * @param SaveHandler $saveHandler */ public function __construct( - \Magento\SharedCatalog\Model\ResourceModel\SharedCatalog $sharedCatalogResource, - \Magento\SharedCatalog\Model\ResourceModel\SharedCatalog\CollectionFactory $sharedCatalogCollectionFactory, - \Magento\SharedCatalog\Api\Data\SearchResultsInterfaceFactory $searchResultsFactory, - \Magento\SharedCatalog\Api\ProductItemManagementInterface $sharedCatalogProductItemManagement, - \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface $collectionProcessor, - \Magento\SharedCatalog\Model\SharedCatalogValidator $validator, - \Magento\SharedCatalog\Model\SaveHandler $saveHandler + SharedCatalog $sharedCatalogResource, + CollectionFactory $sharedCatalogCollectionFactory, + SearchResultsInterfaceFactory $searchResultsFactory, + ProductItemManagementInterface $sharedCatalogProductItemManagement, + CollectionProcessorInterface $collectionProcessor, + SharedCatalogValidator $validator, + SaveHandler $saveHandler ) { $this->sharedCatalogResource = $sharedCatalogResource; $this->sharedCatalogCollectionFactory = $sharedCatalogCollectionFactory; @@ -86,9 +94,9 @@ public function __construct( } /** - * {@inheritdoc} + * @inheritdoc */ - public function save(\Magento\SharedCatalog\Api\Data\SharedCatalogInterface $sharedCatalog) + public function save(SharedCatalogInterface $sharedCatalog) { if ($sharedCatalog->getId()) { $prevSharedCatalogData = $sharedCatalog->getData(); @@ -101,7 +109,7 @@ public function save(\Magento\SharedCatalog\Api\Data\SharedCatalogInterface $sha } /** - * {@inheritdoc} + * @inheritdoc */ public function get($sharedCatalogId) { @@ -109,7 +117,7 @@ public function get($sharedCatalogId) /** @var \Magento\SharedCatalog\Model\ResourceModel\SharedCatalog\Collection $collection */ $collection = $this->sharedCatalogCollectionFactory->create(); $collection->addFieldToFilter('entity_id', ['eq' => $sharedCatalogId]); - /** @var \Magento\SharedCatalog\Api\Data\SharedCatalogInterface $sharedCatalog */ + /** @var SharedCatalogInterface $sharedCatalog */ $sharedCatalog = $collection->getFirstItem(); $this->validator->checkSharedCatalogExist($sharedCatalog); $this->instances[$sharedCatalogId] = $sharedCatalog; @@ -118,9 +126,9 @@ public function get($sharedCatalogId) } /** - * {@inheritdoc} + * @inheritdoc */ - public function delete(\Magento\SharedCatalog\Api\Data\SharedCatalogInterface $sharedCatalog) + public function delete(SharedCatalogInterface $sharedCatalog) { if ($this->validator->isSharedCatalogPublic($sharedCatalog)) { try { @@ -129,12 +137,11 @@ public function delete(\Magento\SharedCatalog\Api\Data\SharedCatalogInterface $s $this->sharedCatalogProductItemManagement->deleteItems($sharedCatalog); unset($this->instances[$sharedCatalogId]); } catch (\Exception $e) { - throw new \Exception( + throw new StateException( __( 'Cannot delete shared catalog with id %1', $sharedCatalog->getId() ), - 0, $e ); } @@ -143,7 +150,7 @@ public function delete(\Magento\SharedCatalog\Api\Data\SharedCatalogInterface $s } /** - * {@inheritdoc} + * @inheritdoc */ public function deleteById($sharedCatalogId) { @@ -153,7 +160,7 @@ public function deleteById($sharedCatalogId) } /** - * {@inheritdoc} + * @inheritdoc */ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $criteria) { diff --git a/vendor/magento/module-shared-catalog/Model/ResourceModel/SharedCatalog.php b/vendor/magento/module-shared-catalog/Model/ResourceModel/SharedCatalog.php index 1c9e8f6fea04..a691afb3e7f2 100644 --- a/vendor/magento/module-shared-catalog/Model/ResourceModel/SharedCatalog.php +++ b/vendor/magento/module-shared-catalog/Model/ResourceModel/SharedCatalog.php @@ -7,7 +7,9 @@ namespace Magento\SharedCatalog\Model\ResourceModel; use Magento\Framework\App\ObjectManager; +use Magento\SharedCatalog\Api\Data\SharedCatalogInterface; use Magento\SharedCatalog\Model\CustomerGroupManagement; +use Magento\Framework\Exception\StateException; /** * SharedCatalog page mysql resource. @@ -88,6 +90,14 @@ protected function _afterDelete(\Magento\Framework\Model\AbstractModel $object) { parent::_afterDelete($object); - $this->customerGroupManagement->deleteCustomerGroupById($object); + try { + $this->customerGroupManagement->deleteCustomerGroupById($object); + } catch (StateException $e) { + if ($object->getType() !== SharedCatalogInterface::TYPE_CUSTOM) { + throw $e; + } + } + + return $this; } } diff --git a/vendor/magento/module-shared-catalog/Model/SharedCatalogValidator.php b/vendor/magento/module-shared-catalog/Model/SharedCatalogValidator.php index 95184c6d5c61..e52da5809137 100644 --- a/vendor/magento/module-shared-catalog/Model/SharedCatalogValidator.php +++ b/vendor/magento/module-shared-catalog/Model/SharedCatalogValidator.php @@ -6,6 +6,8 @@ namespace Magento\SharedCatalog\Model; use Magento\Framework\App\ObjectManager; +use Magento\Framework\Exception\InputException; +use Magento\SharedCatalog\Api\Data\SharedCatalogInterface; use Magento\Store\Api\GroupRepositoryInterface; /** @@ -75,16 +77,18 @@ public function __construct( /** * Validate shared catalog. * - * @param \Magento\SharedCatalog\Api\Data\SharedCatalogInterface $sharedCatalog - * @throws \Magento\Framework\Exception\InputException|\Magento\Framework\Exception\NoSuchEntityException + * @param SharedCatalogInterface $sharedCatalog + * @throws InputException|\Magento\Framework\Exception\NoSuchEntityException * @return void */ - public function validate(\Magento\SharedCatalog\Api\Data\SharedCatalogInterface $sharedCatalog) + public function validate(SharedCatalogInterface $sharedCatalog) { $this->getAllowedCustomerTaxClasses(); if ($sharedCatalog->getId()) { $this->checkSharedCatalogExist($sharedCatalog); + } else { + $this->checkExistingSharedCatalogWithCustomerGroup($sharedCatalog); } $this->validateSharedCatalogData($sharedCatalog); @@ -102,12 +106,12 @@ public function validate(\Magento\SharedCatalog\Api\Data\SharedCatalogInterface /** * Is public catalog duplicated. * - * @param \Magento\SharedCatalog\Api\Data\SharedCatalogInterface $sharedCatalog + * @param SharedCatalogInterface $sharedCatalog * @return bool */ - public function isCatalogPublicTypeDuplicated(\Magento\SharedCatalog\Api\Data\SharedCatalogInterface $sharedCatalog) + public function isCatalogPublicTypeDuplicated(SharedCatalogInterface $sharedCatalog) { - if ($sharedCatalog->getType() != \Magento\SharedCatalog\Api\Data\SharedCatalogInterface::TYPE_PUBLIC) { + if ($sharedCatalog->getType() != SharedCatalogInterface::TYPE_PUBLIC) { return false; } @@ -123,14 +127,14 @@ public function isCatalogPublicTypeDuplicated(\Magento\SharedCatalog\Api\Data\Sh /** * Is direct change public catalog to custom. * - * @param \Magento\SharedCatalog\Api\Data\SharedCatalogInterface $sharedCatalog + * @param SharedCatalogInterface $sharedCatalog * @throws \Magento\Framework\Exception\LocalizedException * @return bool */ public function isDirectChangeToCustom( - \Magento\SharedCatalog\Api\Data\SharedCatalogInterface $sharedCatalog + SharedCatalogInterface $sharedCatalog ) { - if ($sharedCatalog->getType() != \Magento\SharedCatalog\Api\Data\SharedCatalogInterface::TYPE_CUSTOM) { + if ($sharedCatalog->getType() != SharedCatalogInterface::TYPE_CUSTOM) { return false; } @@ -156,11 +160,11 @@ public function isDirectChangeToCustom( /** * Check Shared Catalog exist. * - * @param \Magento\SharedCatalog\Api\Data\SharedCatalogInterface $sharedCatalog + * @param SharedCatalogInterface $sharedCatalog * @throws \Magento\Framework\Exception\NoSuchEntityException * @return void */ - public function checkSharedCatalogExist(\Magento\SharedCatalog\Api\Data\SharedCatalogInterface $sharedCatalog) + public function checkSharedCatalogExist(SharedCatalogInterface $sharedCatalog) { if (!$this->getOriginalSharedCatalog($sharedCatalog)->getId()) { throw new \Magento\Framework\Exception\NoSuchEntityException(__('Requested Shared Catalog is not found')); @@ -170,11 +174,11 @@ public function checkSharedCatalogExist(\Magento\SharedCatalog\Api\Data\SharedCa /** * Check type Shared Catalog. * - * @param \Magento\SharedCatalog\Api\Data\SharedCatalogInterface $sharedCatalog + * @param SharedCatalogInterface $sharedCatalog * @throws \Magento\Framework\Exception\LocalizedException|\Magento\Framework\Exception\NoSuchEntityException * @return bool */ - public function isSharedCatalogPublic(\Magento\SharedCatalog\Api\Data\SharedCatalogInterface $sharedCatalog) + public function isSharedCatalogPublic(SharedCatalogInterface $sharedCatalog) { $this->checkSharedCatalogExist($sharedCatalog); if ($sharedCatalog->getType() == $sharedCatalog::TYPE_PUBLIC) { @@ -192,15 +196,15 @@ public function isSharedCatalogPublic(\Magento\SharedCatalog\Api\Data\SharedCata /** * Get original SharedCatalog. * - * @param \Magento\SharedCatalog\Api\Data\SharedCatalogInterface $sharedCatalog - * @return \Magento\SharedCatalog\Api\Data\SharedCatalogInterface $sharedCatalog + * @param SharedCatalogInterface $sharedCatalog + * @return SharedCatalogInterface $sharedCatalog */ - private function getOriginalSharedCatalog(\Magento\SharedCatalog\Api\Data\SharedCatalogInterface $sharedCatalog) + private function getOriginalSharedCatalog(SharedCatalogInterface $sharedCatalog) { /** @var \Magento\SharedCatalog\Model\ResourceModel\SharedCatalog\Collection $collection */ $collection = $this->sharedCatalogCollectionFactory->create(); $collection->addFieldToFilter($sharedCatalog::SHARED_CATALOG_ID, ['eq' => $sharedCatalog->getId()]); - /** @var \Magento\SharedCatalog\Api\Data\SharedCatalogInterface $sharedCatalog */ + /** @var SharedCatalogInterface $sharedCatalog */ $sharedCatalog = $collection->getFirstItem(); return $sharedCatalog; @@ -229,18 +233,18 @@ private function getAllowedCustomerTaxClasses() /** * Validate shared catalog required fields. * - * @param \Magento\SharedCatalog\Api\Data\SharedCatalogInterface $sharedCatalog + * @param SharedCatalogInterface $sharedCatalog * @return void - * @throws \Magento\Framework\Exception\InputException + * @throws InputException */ - private function validateSharedCatalogData(\Magento\SharedCatalog\Api\Data\SharedCatalogInterface $sharedCatalog) + private function validateSharedCatalogData(SharedCatalogInterface $sharedCatalog) { if (!$sharedCatalog->getId() && (empty($sharedCatalog->getName()) || ($this->validateStore && $sharedCatalog->getStoreId() === null) || $sharedCatalog->getTaxClassId() === null) ) { - throw new \Magento\Framework\Exception\InputException( + throw new InputException( __( 'Cannot create a shared catalog because some information is missing. ' . 'Please make sure you provided Store Group ID, Name and Tax Class.' @@ -252,11 +256,11 @@ private function validateSharedCatalogData(\Magento\SharedCatalog\Api\Data\Share /** * Check is shared catalog type id exists. * - * @param \Magento\SharedCatalog\Api\Data\SharedCatalogInterface $sharedCatalog + * @param SharedCatalogInterface $sharedCatalog * @return void * @throws \Magento\Framework\Exception\NoSuchEntityException */ - private function validateSharedCatalogType(\Magento\SharedCatalog\Api\Data\SharedCatalogInterface $sharedCatalog) + private function validateSharedCatalogType(SharedCatalogInterface $sharedCatalog) { if (!array_key_exists($sharedCatalog->getType(), $sharedCatalog->getAvailableTypes())) { throw \Magento\Framework\Exception\NoSuchEntityException::singleField( @@ -269,12 +273,12 @@ private function validateSharedCatalogType(\Magento\SharedCatalog\Api\Data\Share /** * Check is shared catalog tax class id exists. * - * @param \Magento\SharedCatalog\Api\Data\SharedCatalogInterface $sharedCatalog + * @param SharedCatalogInterface $sharedCatalog * @return void * @throws \Magento\Framework\Exception\NoSuchEntityException */ private function validateSharedCatalogTaxClass( - \Magento\SharedCatalog\Api\Data\SharedCatalogInterface $sharedCatalog + SharedCatalogInterface $sharedCatalog ) { $allowedCustomerTaxClasses = $this->getAllowedCustomerTaxClasses(); if (!isset($allowedCustomerTaxClasses[$sharedCatalog->getTaxClassId()])) { @@ -288,14 +292,14 @@ private function validateSharedCatalogTaxClass( /** * Check is shared catalog name duplicated. * - * @param \Magento\SharedCatalog\Api\Data\SharedCatalogInterface $sharedCatalog + * @param SharedCatalogInterface $sharedCatalog * @return void - * @throws \Magento\Framework\Exception\InputException + * @throws InputException */ - private function validateSharedCatalogName(\Magento\SharedCatalog\Api\Data\SharedCatalogInterface $sharedCatalog) + private function validateSharedCatalogName(SharedCatalogInterface $sharedCatalog) { if (mb_strlen($sharedCatalog->getName()) > \Magento\Customer\Api\Data\GroupInterface::GROUP_CODE_MAX_LENGTH) { - throw new \Magento\Framework\Exception\InputException( + throw new InputException( __( 'The maximum allowed catalog name length is %1 characters.', \Magento\Customer\Api\Data\GroupInterface::GROUP_CODE_MAX_LENGTH @@ -308,11 +312,11 @@ private function validateSharedCatalogName(\Magento\SharedCatalog\Api\Data\Share if (!empty($sharedCatalog->getId())) { $collection->addFieldToFilter($sharedCatalog::SHARED_CATALOG_ID, ['neq' => $sharedCatalog->getId()]); } - /** @var \Magento\SharedCatalog\Api\Data\SharedCatalogInterface $existSharedCatalog */ + /** @var SharedCatalogInterface $existSharedCatalog */ $existSharedCatalog = $collection->getFirstItem(); if ((bool)$existSharedCatalog->getId()) { - throw new \Magento\Framework\Exception\InputException( + throw new InputException( __( 'A catalog named %catalogName already exists. Please select a different name.', ['catalogName' => $sharedCatalog->getName()] @@ -324,20 +328,48 @@ private function validateSharedCatalogName(\Magento\SharedCatalog\Api\Data\Share /** * Check is shared catalog customer group id changed. * - * @param \Magento\SharedCatalog\Api\Data\SharedCatalogInterface $sharedCatalog + * @param SharedCatalogInterface $sharedCatalog * @return void - * @throws \Magento\Framework\Exception\InputException + * @throws InputException */ - private function validateCustomerGroupChanges(\Magento\SharedCatalog\Api\Data\SharedCatalogInterface $sharedCatalog) + private function validateCustomerGroupChanges(SharedCatalogInterface $sharedCatalog) { $originalSharedCatalog = $this->getOriginalSharedCatalog($sharedCatalog); if ($originalSharedCatalog && $originalSharedCatalog->getCustomerGroupId() && $originalSharedCatalog->getCustomerGroupId() != $sharedCatalog->getCustomerGroupId() ) { - throw new \Magento\Framework\Exception\InputException( + throw new InputException( __('You cannot change the customer group for a shared catalog.') ); } } + + /** + * Check if there are other shared catalogs with the same customer group. + * + * @param SharedCatalogInterface $sharedCatalog + * @return void + * @throws InputException + */ + private function checkExistingSharedCatalogWithCustomerGroup(SharedCatalogInterface $sharedCatalog): void + { + /** @var \Magento\SharedCatalog\Model\ResourceModel\SharedCatalog\Collection $collection */ + $collection = $this->sharedCatalogCollectionFactory->create(); + $collection->addFieldToFilter( + $sharedCatalog::CUSTOMER_GROUP_ID, + [ + 'eq' => $sharedCatalog->getCustomerGroupId() + ] + ); + + /** @var SharedCatalogInterface $existSharedCatalog */ + $existSharedCatalog = $collection->getFirstItem(); + + if ($existSharedCatalog->getId()) { + throw new InputException( + __('A catalog already exists with customer group id %1.', $sharedCatalog->getCustomerGroupId()) + ); + } + } } diff --git a/vendor/magento/module-shared-catalog/Plugin/Source/SharedCatalogGroupsProcessor.php b/vendor/magento/module-shared-catalog/Plugin/Source/SharedCatalogGroupsProcessor.php index c915d963c56e..93e447757edd 100644 --- a/vendor/magento/module-shared-catalog/Plugin/Source/SharedCatalogGroupsProcessor.php +++ b/vendor/magento/module-shared-catalog/Plugin/Source/SharedCatalogGroupsProcessor.php @@ -60,7 +60,9 @@ public function prepareGroups(array $groups) foreach ($sharedCatalogs->getItems() as $sharedCatalog) { $sharedCatalogGroupId = $sharedCatalog->getCustomerGroupId(); - if ($customerGroupOptions[$sharedCatalogGroupId]['value'] == $sharedCatalogGroupId) { + if (isset($customerGroupOptions[$sharedCatalogGroupId]) && + $customerGroupOptions[$sharedCatalogGroupId]['value'] == $sharedCatalogGroupId + ) { unset($customerGroupOptions[$sharedCatalogGroupId]); $sharedCatalogOptions[] = [ diff --git a/vendor/magento/module-shared-catalog/i18n/en_US.csv b/vendor/magento/module-shared-catalog/i18n/en_US.csv index 33f45e7e065a..a18687c8457c 100644 --- a/vendor/magento/module-shared-catalog/i18n/en_US.csv +++ b/vendor/magento/module-shared-catalog/i18n/en_US.csv @@ -82,17 +82,17 @@ exception,exception "Delete ""%1""","Delete ""%1""" "Catalog and Tier Price","Catalog and Tier Price" "Group or Catalog","Group or Catalog" -"You must enable the shared catalog feature in +"You must enable the shared catalog feature in Stores -> Configuration -> B2B Features to use the shared catalogs in the storefront. - Otherwise, the categories and products you're configuring in shared catalogs will - not be restricted to customers.","You must enable the shared catalog feature in + Otherwise, the categories and products you're configuring in shared catalogs will + not be restricted to customers.","You must enable the shared catalog feature in Stores -> Configuration -> B2B Features to use the shared catalogs in the storefront. - Otherwise, the categories and products you're configuring in shared catalogs will + Otherwise, the categories and products you're configuring in shared catalogs will not be restricted to customers." -"You must enable the shared catalog feature in Stores -> Configuration -> B2B Features - to use the shared catalogs in the storefront. Otherwise, the categories and products you're - configuring in shared catalogs will not be restricted to customers.","You must enable the shared catalog feature in Stores -> Configuration -> B2B Features - to use the shared catalogs in the storefront. Otherwise, the categories and products you're +"You must enable the shared catalog feature in Stores -> Configuration -> B2B Features + to use the shared catalogs in the storefront. Otherwise, the categories and products you're + configuring in shared catalogs will not be restricted to customers.","You must enable the shared catalog feature in Stores -> Configuration -> B2B Features + to use the shared catalogs in the storefront. Otherwise, the categories and products you're configuring in shared catalogs will not be restricted to customers." Next,Next "Edit Configurations","Edit Configurations" @@ -177,3 +177,4 @@ tier_price_form,tier_price_form "Add Price","Add Price" Website,Website Quantity,Quantity +"A catalog already exists with customer group id %1.","A catalog already exists with customer group id %1."