1
0
mirror of synced 2024-11-25 14:56:09 +03:00

Сделал сохранением свойств в таблице, работаю над их удалением

This commit is contained in:
a.belikin 2024-11-14 15:51:20 +03:00
parent 0e50565f63
commit e71c62b1ab
2 changed files with 60 additions and 76 deletions

View File

@ -40,23 +40,15 @@ class CustomExportProps extends Controller
$props = $requestData['properties']; $props = $requestData['properties'];
$profileId = $requestData['profileId']; $profileId = $requestData['profileId'];
$dbConnection = Application::getInstance()->getConnection(); foreach ($props as $catalogId => $propsArray) {
try { $catalogCustomProps = [];
$dbConnection->startTransaction(); foreach ($propsArray as $property) {
foreach ($props as $catalogId => $propsArray) { $catalogCustomProps[] = [
$catalogCustomProps = []; 'code' => $property['code'],
foreach ($propsArray as $property) { 'title' => $property['title']
$catalogCustomProps[] = [ ];
'code' => $property['code'],
'title' => $property['title']
];
}
$settingsService->setCustomProps($profileId, $catalogId, $catalogCustomProps);
} }
$settingsService->saveCustomProps($profileId, $catalogId, $catalogCustomProps);
$dbConnection->commitTransaction();
} catch (\Throwable $e) {
$dbConnection->rollbackTransaction();
} }
} }
@ -71,56 +63,15 @@ class CustomExportProps extends Controller
$props = $requestData['properties']; $props = $requestData['properties'];
$profileId = $requestData['profileId']; $profileId = $requestData['profileId'];
$dbConnection = Application::getInstance()->getConnection(); foreach ($props as $catalogId => $propsArray) {
$catalogCustomProps = [];
try { foreach ($propsArray as $property) {
$dbConnection->startTransaction(); $catalogCustomProps[] = [
'code' => $property['code'],
foreach ($props as $catalogId => $propsArray) { 'title' => $property['title']
$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);
} }
} catch (\Throwable $e) { $settingsService->removeCustomProps($profileId, $catalogId, $catalogCustomProps);
$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);
} }
}
} }

View File

@ -816,30 +816,63 @@ class SettingsService
return unserialize(COption::GetOptionString(self::MODULE_ID, $optionName)); return unserialize(COption::GetOptionString(self::MODULE_ID, $optionName));
} }
public function deleteCustomProps( public function removeCustomProps(
string $profileId, string $profileId,
string $catalogId, string $catalogId,
array $propsToDelete array $propsToDelete
): void ): void {
{
$currentCatalogProps = $this->getCustomProps($profileId, $catalogId); $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)) { 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 $profileId,
string $catalogId, string $catalogId,
array $props array $props
): void { )
{
$optionName = $this->getCustomPropsOptionName($profileId, $catalogId); $optionName = $this->getCustomPropsOptionName($profileId, $catalogId);
$propsString = serialize($props); $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);
}
} }
} }