1
0
mirror of synced 2024-11-26 23:36:08 +03:00

Начал делать использование бд вместо файлов для свойств

This commit is contained in:
a.belikin 2024-11-13 20:23:47 +03:00
parent 7b9c2e4f96
commit 0e50565f63
3 changed files with 145 additions and 69 deletions

View File

@ -944,10 +944,10 @@ if ($STEP === 1) {
if (promises.length > 0) {
Promise.all(promises)
.finally(() => {
formElem.submit();
// formElem.submit();
});
} else {
formElem.submit();
// formElem.submit();
}
});
@ -963,8 +963,8 @@ if ($STEP === 1) {
let customPropCatalogId = deleteButton.closest('.iblockExportTable').data('type');
let values = {
'title': deletedPropTitle,
'code': deletedPropCode,
'title': deletedPropTitle,
};
if (customPropsToDelete.hasOwnProperty(customPropCatalogId)) {

View File

@ -1,9 +1,12 @@
<?php
namespace Intaro\RetailCrm\Controller;
use Bitrix\Main\Engine\Controller;
use Bitrix\Main\Result;
use Bitrix\Main\Error;
use Bitrix\Main\Application;
use Intaro\RetailCrm\Icml\SettingsService;
/**
* @category Integration
@ -15,65 +18,109 @@ use Bitrix\Main\Error;
*/
class CustomExportProps extends Controller
{
private function getRequestData(): array
{
$data = $this->getRequest()->getInput();
if ($data === null) {
}
return json_decode($data, true);
}
public function saveAction()
{
$request = $this->getRequest()->getInput();
$response = new Result();
if ($request === null) {
$response->setStatus(new Error('Ошибка'));
}
$requestData = json_decode($request, true);
$properties = $requestData['properties'];
$profileId = $requestData['profileId'];
foreach ($properties as $catalogId => $propertyArray) {
$newPropertiesString = '';
foreach ($propertyArray as $property) {
$newPropertiesString .= PHP_EOL . $property['code'] . ' = ' . $property['title'];
}
$filePath = sprintf(
'%s/%s_profileId_%s_catalogId_%s.txt',
$_SERVER['DOCUMENT_ROOT'] . '/local',
'icml_property_retailcrm',
$profileId,
$catalogId
$settingsService = SettingsService::getInstance(
[],
null
);
$saveResult = file_put_contents($filePath, $newPropertiesString, FILE_APPEND);
$requestData = $this->getRequestData();
$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);
}
if (!$saveResult) {
$response->setStatus(new Error('Ошибка'));
$dbConnection->commitTransaction();
} catch (\Throwable $e) {
$dbConnection->rollbackTransaction();
}
// return $response->setStatus(Result::SUCCESS);
}
public function deleteAction()
{
$request = $this->getRequest()->getInput();
$settingsService = SettingsService::getInstance(
[],
null
);
$requestData = json_decode($request, true);
$properties = $requestData['properties'];
$requestData = $this->getRequestData();
$props = $requestData['properties'];
$profileId = $requestData['profileId'];
foreach ($properties 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);
$dbConnection = Application::getInstance()->getConnection();
try {
$dbConnection->startTransaction();
foreach ($props as $catalogId => $propsArray) {
$catalogCustomProps = [];
foreach ($propsArray as $property) {
$propStringToDelete = PHP_EOL . $property['code'] . ' = ' . $property['title'];
$fileContent = str_replace($propStringToDelete, '', $fileContent);
}
file_put_contents($filePath, $fileContent);
$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) {
$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

@ -8,8 +8,12 @@ use CCatalogGroup;
use CCatalogSku;
use CCatalogVat;
use CIBlock;
use COption;
use Bitrix\Main\Config\Option;
use Intaro\RetailCrm\Service\Hl;
use RetailcrmConfigProvider;
use Bitrix\Main\Application;
use Bitrix\Main\Entity\Query;
/**
* Отвечает за управление настройками выгрузки icml каталога
@ -33,6 +37,8 @@ class SettingsService
*/
public const INFOBLOCK_WITH_SKU = 'P';
private const MODULE_ID = 'intaro.retailcrm';
/**
* @var array
*/
@ -295,36 +301,16 @@ class SettingsService
private function parseNewProps(): array
{
global $APPLICATION;
global $PROFILE_ID;
$currentProfile = $PROFILE_ID;
$currentProfileCatalogIds = $this->getProfileCatalogIds();
$result = [];
foreach ($currentProfileCatalogIds as $catalogId) {
$customPropsFilePath = sprintf(
'%s/%s_profileId_%s_catalogId_%s.txt',
$_SERVER['DOCUMENT_ROOT'] . '/local',
'icml_property_retailcrm',
$currentProfile,
$catalogId,
);
$text = $APPLICATION->GetFileContent($customPropsFilePath);
$catalogCustomProps = $this->getCustomProps($currentProfile, $catalogId);
if ($text === false) {
continue;
}
preg_match_all('/\w+\s*=\s*\w+[ *\w+]*/mu', $text, $matches);
foreach ($matches[0] as $newProp) {
$elements = explode("=", $newProp);
if (empty($elements[0]) || empty($elements[1])) {
continue;
}
$result[$catalogId][trim($elements[0])] = trim($elements[1]);
foreach ($catalogCustomProps as $prop) {
$result[$catalogId][$prop['code']] = $prop['title'];
}
}
@ -813,4 +799,47 @@ class SettingsService
return [$arIBlockList, $intCountChecked, $intCountAvailIBlock, $arIBlockList['iblockExport'] ?? false];
}
private function getCustomPropsOptionName(string $profileId, string $catalogId): string
{
return sprintf(
'customProps_profileId_%s_catalogId_%s',
$profileId,
$catalogId
);
}
public function getCustomProps(string $profileId, string $catalogId)
{
$optionName = $this->getCustomPropsOptionName($profileId, $catalogId);
return unserialize(COption::GetOptionString(self::MODULE_ID, $optionName));
}
public function deleteCustomProps(
string $profileId,
string $catalogId,
array $propsToDelete
): void
{
$currentCatalogProps = $this->getCustomProps($profileId, $catalogId);
$updatedCatalogProps = array_diff($currentCatalogProps, $propsToDelete);
if (empty($updatedCatalogProps)) {
$this->setCustomProps($profileId, $catalogId, []);
}
$this->setCustomProps($profileId, $catalogId, $updatedCatalogProps);
}
public function setCustomProps(
string $profileId,
string $catalogId,
array $props
): void {
$optionName = $this->getCustomPropsOptionName($profileId, $catalogId);
$propsString = serialize($props);
COption::SetOptionString(self::MODULE_ID, $optionName, $propsString);
}
}