diff --git a/intaro.retailcrm/export/export_run.php b/intaro.retailcrm/export/export_run.php index 13bc75d8..dee1703d 100644 --- a/intaro.retailcrm/export/export_run.php +++ b/intaro.retailcrm/export/export_run.php @@ -38,7 +38,8 @@ if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/bitrix/php_interface/retailcrm/exp } } - $settingService = SettingsService::getInstance([], ''); + global $PROFILE_ID; + $settingService = SettingsService::getInstance([], '', $PROFILE_ID); $iblockPropertySku = []; $iblockPropertySkuHl = []; $iblockPropertyUnitSku = []; diff --git a/intaro.retailcrm/export/export_setup.php b/intaro.retailcrm/export/export_setup.php index cda81a72..05a06418 100644 --- a/intaro.retailcrm/export/export_setup.php +++ b/intaro.retailcrm/export/export_setup.php @@ -26,7 +26,8 @@ CModule::IncludeModule('intaro.retailcrm'); //TODO заменить вызов на сервис-локатор, когда он приедет $settingsService = SettingsService::getInstance( $arOldSetupVars ?? [], - $ACTION + $ACTION, + $PROFILE_ID ); $isSetupModulePage = $settingsService->isSetupModulePage(); @@ -473,10 +474,10 @@ if ($STEP === 1) { } ?> customPropList[$catalogId]; + $catalogId = $arIBlock['ID']; + $catalogCustomProps = $settingsService->customPropList[$catalogId]; + if (!empty($catalogCustomProps)) { foreach ($catalogCustomProps as $catalogCustomPropCode => $catalogCustomPropValue) { ?> diff --git a/intaro.retailcrm/lib/controller/customexportprops.php b/intaro.retailcrm/lib/controller/customexportprops.php index e8a3828b..79568b63 100644 --- a/intaro.retailcrm/lib/controller/customexportprops.php +++ b/intaro.retailcrm/lib/controller/customexportprops.php @@ -31,15 +31,16 @@ class CustomExportProps extends Controller public function saveAction() { - $settingsService = SettingsService::getInstance( - [], - null - ); - $requestData = $this->getRequestData(); $props = $requestData['properties']; $profileId = $requestData['profileId']; + $settingsService = SettingsService::getInstance( + [], + null, + $profileId + ); + foreach ($props as $catalogId => $propsArray) { $catalogCustomProps = []; foreach ($propsArray as $property) { @@ -49,22 +50,23 @@ class CustomExportProps extends Controller ]; } $settingsService - ->setCatalogCustomPropsOptionName($profileId) + ->setCatalogCustomPropsOptionName($catalogId) ->saveCustomProps($catalogId, $catalogCustomProps); } } public function deleteAction() { - $settingsService = SettingsService::getInstance( - [], - null - ); - $requestData = $this->getRequestData(); $props = $requestData['properties']; $profileId = $requestData['profileId']; + $settingsService = SettingsService::getInstance( + [], + null, + $profileId + ); + foreach ($props as $catalogId => $propsArray) { $catalogCustomProps = []; foreach ($propsArray as $property) { diff --git a/intaro.retailcrm/lib/icml/settingsservice.php b/intaro.retailcrm/lib/icml/settingsservice.php index 27c0b142..04ae7e3c 100644 --- a/intaro.retailcrm/lib/icml/settingsservice.php +++ b/intaro.retailcrm/lib/icml/settingsservice.php @@ -131,7 +131,7 @@ class SettingsService * @param array $arOldSetupVars * @param string|null $action */ - private function __construct(array $arOldSetupVars, ?string $action) + private function __construct(array $arOldSetupVars, ?string $action, string $profileId) { $this->arOldSetupVars = $arOldSetupVars; $this->action = $action; @@ -147,8 +147,9 @@ class SettingsService $this->getPriceTypes(); $this->getVatRates(); - $this->exportProfileId = $this->getExportProfileId(); - $this->profileCatalogsOptionName = $this->getProfileCatalogsOptionName(); + $this->exportProfileId = $profileId; + $this->setProfileCatalogsOptionName(); + $this->deleteEmptyProfileCatalogs(); $this->customPropList = $this->getNewProps(); $this->defaultPropList = $this->getIblockPropsPreset(); @@ -161,10 +162,10 @@ class SettingsService * * @return \Intaro\RetailCrm\Icml\SettingsService|null */ - public static function getInstance(array $arOldSetupVars, ?string $action): ?SettingsService + public static function getInstance(array $arOldSetupVars, ?string $action, ?string $profileId): ?SettingsService { if (is_null(self::$instance)) { - self::$instance = new self($arOldSetupVars, $action); + self::$instance = new self($arOldSetupVars, $action, $profileId); } return self::$instance; @@ -792,7 +793,7 @@ class SettingsService public function setCatalogCustomPropsOptionName(string $catalogId): self { $this->catalogCustomPropsOptionName = sprintf( - 'customProps_exportProfileId_%s_catalogId_%s', + 'exportCustomProps_ProfileId_%s_catalogId_%s', $this->exportProfileId, $catalogId ); @@ -800,19 +801,12 @@ class SettingsService return $this; } - private function getExportProfileId(): string + private function setProfileCatalogsOptionName(): void { - global $PROFILE_ID; - - return $PROFILE_ID; + $this->profileCatalogsOptionName = sprintf('exportProfileId_%s_catalogs', $this->exportProfileId); } - private function getProfileCatalogsOptionName(): string - { - return sprintf('exportProfile_%s_catalogs', $this->exportProfileId); - } - - public function getCustomProps(): ?array + private function getCustomProps(): ?array { $props = unserialize(COption::GetOptionString(self::MODULE_ID, $this->catalogCustomPropsOptionName)); @@ -832,24 +826,23 @@ class SettingsService )); if (empty($updatedCatalogProps)) { - COption::RemoveOption(self::MODULE_ID, $this->catalogCustomPropsOptionName); - COption::RemoveOption(self::MODULE_ID, $this->profileCatalogsOptionName); + $this->deleteOptionEntry($this->catalogCustomPropsOptionName); + $this->deleteProfileCatalog($catalogId); } else { - $this->updateProfileCatalogs($catalogId); - $this->updateCustomPropsOptionEntry($updatedCatalogProps); + $this->updateCustomProps($updatedCatalogProps); } } - public function updateCustomPropsOptionEntry(array $updatedProps) + private function updateCustomProps(array $updatedProps) { - COption::SetOptionString(self::MODULE_ID, $this->catalogCustomPropsOptionName, ''); - $this->setCustomPropsOptionEntry($updatedProps); + $serializedProps = serialize($updatedProps); + $this->updateOptionEntry($this->catalogCustomPropsOptionName, $serializedProps); } - private function setCustomPropsOptionEntry(array $props) + private function setCustomProps(array $props) { $propsString = serialize($props); - COption::SetOptionString(self::MODULE_ID, $this->catalogCustomPropsOptionName, $propsString); + $this->setOptionEntry($this->catalogCustomPropsOptionName, $propsString); } public function saveCustomProps(string $catalogId, array $newProps): void @@ -857,12 +850,12 @@ class SettingsService $currentProps = $this->getCustomProps(); if (is_null($currentProps)) { - $this->setCustomPropsOptionEntry($newProps); - $this->setProfileCatalogs($catalogId); + $this->setProfileCatalog($catalogId); + $this->setCustomProps($newProps); } else { $updatedProps = array_merge($currentProps, $newProps); - $this->updateCustomPropsOptionEntry($updatedProps); $this->updateProfileCatalogs($catalogId); + $this->updateCustomProps($updatedProps); } } @@ -877,18 +870,57 @@ class SettingsService return $catalogs; } - private function setProfileCatalogs(string $catalogId): void + private function setProfileCatalog(string $catalogId): void { $catalogs = serialize([$catalogId]); - COption::SetOptionString(self::MODULE_ID, $this->profileCatalogsOptionName, $catalogs); + $this->setOptionEntry($this->profileCatalogsOptionName, $catalogs); } private function updateProfileCatalogs(string $catalogId): void { - $currentCatalogs = unserialize(COption::GetOptionString(self::MODULE_ID, $this->profileCatalogsOptionName)); - $updatedCatalogs = serialize([...$currentCatalogs, $catalogId]); + $currentCatalogs = $this->getProfileCatalogs(); - COption::SetOptionString(self::MODULE_ID, $this->profileCatalogsOptionName, ''); - COption::SetOptionString(self::MODULE_ID, $this->profileCatalogsOptionName, $updatedCatalogs); + if (!in_array($catalogId, $currentCatalogs)) { + $updatedCatalogs = serialize(array_merge($currentCatalogs, [$catalogId])); + $this->updateOptionEntry($this->profileCatalogsOptionName, $updatedCatalogs); + } + } + + private function deleteProfileCatalog(string $catalogId): void + { + $currentCatalogs = $this->getProfileCatalogs(); + $catalogIdIndex = array_search($catalogId, $currentCatalogs); + + if ($catalogIdIndex !== false) { + unset($currentCatalogs[$catalogIdIndex]); + } + + $updatedCatalogs = serialize($currentCatalogs); + $this->updateOptionEntry($this->profileCatalogsOptionName, $updatedCatalogs); + } + + private function deleteEmptyProfileCatalogs(): void + { + $currentCatalogs = $this->getProfileCatalogs(); + + if (is_null($currentCatalogs)) { + $this->deleteOptionEntry($this->profileCatalogsOptionName); + } + } + + private function setOptionEntry(string $name, string $value) + { + $setResult = COption::SetOptionString(self::MODULE_ID, $name, $value); + } + + private function updateOptionEntry(string $name, string $value) + { + COption::SetOptionString(self::MODULE_ID, $name, ''); + COption::SetOptionString(self::MODULE_ID, $name, $value); + } + + private function deleteOptionEntry(string $name) + { + COption::RemoveOption(self::MODULE_ID, $name); } } diff --git a/intaro.retailcrm/lib/icml/xmlofferbuilder.php b/intaro.retailcrm/lib/icml/xmlofferbuilder.php index 7d4050de..f0001034 100644 --- a/intaro.retailcrm/lib/icml/xmlofferbuilder.php +++ b/intaro.retailcrm/lib/icml/xmlofferbuilder.php @@ -115,8 +115,8 @@ class XmlOfferBuilder public function __construct(XmlSetup $setup, array $measure, ?string $serverName) { $this->setup = $setup; + $this->setSettingsService(); $this->purchasePriceNull = RetailcrmConfigProvider::getCrmPurchasePrice(); - $this->settingsService = SettingsService::getInstance([], ''); $this->vatRates = $this->settingsService->vatRates; $this->measures = $this->prepareMeasures($measure); $this->serverName = $serverName; @@ -141,6 +141,12 @@ class XmlOfferBuilder return $this->xmlOffer; } + private function setSettingsService(): void + { + global $PROFILE_ID; + $this->settingsService = SettingsService::getInstance([], '', $PROFILE_ID); + } + /** * @param array $categories */