diff --git a/intaro.retailcrm/lib/controller/customexportprops.php b/intaro.retailcrm/lib/controller/customexportprops.php index 9119932b..664ed815 100644 --- a/intaro.retailcrm/lib/controller/customexportprops.php +++ b/intaro.retailcrm/lib/controller/customexportprops.php @@ -40,23 +40,15 @@ class CustomExportProps extends Controller $props = $requestData['properties']; $profileId = $requestData['profileId']; - $dbConnection = Application::getInstance()->getConnection(); - try { - $dbConnection->startTransaction(); - foreach ($props as $catalogId => $propsArray) { - $catalogCustomProps = []; - foreach ($propsArray as $property) { - $catalogCustomProps[] = [ - 'code' => $property['code'], - 'title' => $property['title'] - ]; - } - $settingsService->setCustomProps($profileId, $catalogId, $catalogCustomProps); + foreach ($props as $catalogId => $propsArray) { + $catalogCustomProps = []; + foreach ($propsArray as $property) { + $catalogCustomProps[] = [ + 'code' => $property['code'], + 'title' => $property['title'] + ]; } - - $dbConnection->commitTransaction(); - } catch (\Throwable $e) { - $dbConnection->rollbackTransaction(); + $settingsService->saveCustomProps($profileId, $catalogId, $catalogCustomProps); } } @@ -71,56 +63,15 @@ class CustomExportProps extends Controller $props = $requestData['properties']; $profileId = $requestData['profileId']; - $dbConnection = Application::getInstance()->getConnection(); - - try { - $dbConnection->startTransaction(); - - foreach ($props as $catalogId => $propsArray) { - $catalogCustomProps = []; - foreach ($propsArray as $property) { - $catalogCustomProps[] = [ - 'code' => $property['code'], - 'title' => $property['title'] - ]; - } - $settingsService->deleteCustomProps($profileId, $catalogId, $catalogCustomProps); -// $filePath = sprintf( -// '%s/%s_profileId_%s_catalogId_%s.txt', -// $_SERVER['DOCUMENT_ROOT'] . '/local', -// 'icml_property_retailcrm', -// $profileId, -// $catalogId -// ); -// $fileContent = file_get_contents($filePath); -// -// foreach ($propsArray as $property) { -// $propStringToDelete = PHP_EOL . $property['code'] . ' = ' . $property['title']; -// $fileContent = str_replace($propStringToDelete, '', $fileContent); -// } -// file_put_contents($filePath, $fileContent); - + foreach ($props as $catalogId => $propsArray) { + $catalogCustomProps = []; + foreach ($propsArray as $property) { + $catalogCustomProps[] = [ + 'code' => $property['code'], + 'title' => $property['title'] + ]; } - } catch (\Throwable $e) { - $dbConnection->rollbackTransaction(); - // Добавить возврат ответа с ошибкой - } - -// foreach ($props as $catalogId => $propsArray) { -// $filePath = sprintf( -// '%s/%s_profileId_%s_catalogId_%s.txt', -// $_SERVER['DOCUMENT_ROOT'] . '/local', -// 'icml_property_retailcrm', -// $profileId, -// $catalogId -// ); -// $fileContent = file_get_contents($filePath); -// -// foreach ($propsArray as $property) { -// $propStringToDelete = PHP_EOL . $property['code'] . ' = ' . $property['title']; -// $fileContent = str_replace($propStringToDelete, '', $fileContent); -// } -// file_put_contents($filePath, $fileContent); - + $settingsService->removeCustomProps($profileId, $catalogId, $catalogCustomProps); } + } } \ No newline at end of file diff --git a/intaro.retailcrm/lib/icml/settingsservice.php b/intaro.retailcrm/lib/icml/settingsservice.php index 5c04f46e..319abd39 100644 --- a/intaro.retailcrm/lib/icml/settingsservice.php +++ b/intaro.retailcrm/lib/icml/settingsservice.php @@ -816,30 +816,63 @@ class SettingsService return unserialize(COption::GetOptionString(self::MODULE_ID, $optionName)); } - public function deleteCustomProps( + public function removeCustomProps( string $profileId, string $catalogId, array $propsToDelete - ): void - { + ): void { $currentCatalogProps = $this->getCustomProps($profileId, $catalogId); - $updatedCatalogProps = array_diff($currentCatalogProps, $propsToDelete); + $updatedCatalogProps = array_values(array_filter( + $currentCatalogProps, + fn ($currentProp) => !in_array($currentProp, $propsToDelete) + )); if (empty($updatedCatalogProps)) { - $this->setCustomProps($profileId, $catalogId, []); + $this->removeCustomPropsOptionEntry($profileId, $catalogId); + } else { + $this->updateCustomPropsOptionEntry($profileId, $catalogId, $updatedCatalogProps); } - - $this->setCustomProps($profileId, $catalogId, $updatedCatalogProps); } - public function setCustomProps( + public function updateCustomPropsOptionEntry( + string $profileId, + string $catalogId, + array $updatedProps + ) { + $this->removeCustomPropsOptionEntry($profileId, $catalogId); + $this->setCustomPropsOptionEntry($profileId, $catalogId, $updatedProps); + } + + private function removeCustomPropsOptionEntry($profileId, $catalogId) + { + $optionName = $this->getCustomPropsOptionName($profileId, $catalogId); + $delRes = COption::RemoveOption(self::MODULE_ID, $optionName); + } + + private function setCustomPropsOptionEntry( string $profileId, string $catalogId, array $props - ): void { + ) + { $optionName = $this->getCustomPropsOptionName($profileId, $catalogId); $propsString = serialize($props); - COption::SetOptionString(self::MODULE_ID, $optionName, $propsString); + $setResult = COption::SetOptionString(self::MODULE_ID, $optionName, $propsString); + } + + public function saveCustomProps( + string $profileId, + string $catalogId, + array $newProps + ): void { + $currentProps = $this->getCustomProps($profileId, $catalogId); + + if (empty($currentProps)) { + $this->setCustomPropsOptionEntry($profileId, $catalogId, $newProps); + } else { + $updatedProps = array_merge($currentProps, $newProps); + $this->updateCustomPropsOptionEntry($profileId, $catalogId, $updatedProps); + } } }