From ee02f45637d45684bc57d624d3305f6a48989c81 Mon Sep 17 00:00:00 2001 From: "a.belikin" Date: Wed, 6 Nov 2024 19:30:00 +0300 Subject: [PATCH 1/9] MVP --- intaro.retailcrm/export/export_setup.php | 204 +++++++++++++++++- intaro.retailcrm/include.php | 4 + .../bitrix/js/intaro/custom-props-export.js | 94 ++++++++ .../lib/controller/customexportprops.php | 54 +++++ 4 files changed, 353 insertions(+), 3 deletions(-) create mode 100644 intaro.retailcrm/install/export/bitrix/js/intaro/custom-props-export.js create mode 100644 intaro.retailcrm/lib/controller/customexportprops.php diff --git a/intaro.retailcrm/export/export_setup.php b/intaro.retailcrm/export/export_setup.php index d9d0f91e4..1baa20d64 100644 --- a/intaro.retailcrm/export/export_setup.php +++ b/intaro.retailcrm/export/export_setup.php @@ -110,7 +110,7 @@ } -
+ @@ -475,6 +475,7 @@ class="property-export" } ?> +

@@ -483,7 +484,21 @@ class="property-export" } ?> -
+ +



- '/bitrix/js/intaro/sms.js', 'rel' => [], ], + 'intaro_custom_props' => [ + 'js' => '/bitrix/js/intaro/custom-props-export.js', + 'rel' => [], + ], ]; foreach ($arJsConfig as $ext => $arExt) { diff --git a/intaro.retailcrm/install/export/bitrix/js/intaro/custom-props-export.js b/intaro.retailcrm/install/export/bitrix/js/intaro/custom-props-export.js new file mode 100644 index 000000000..3b055dccd --- /dev/null +++ b/intaro.retailcrm/install/export/bitrix/js/intaro/custom-props-export.js @@ -0,0 +1,94 @@ +const setupFieldsListElement = $('input[name="SETUP_FIELDS_LIST"]'); +let customProperties = [ + {code: 'newProperty2', title: 'Новое свойство'}, + {code: 'newProperty3', title: 'Новое свойство 2'}, +]; + +document.getElementById('submit-form').addEventListener('submit', function (formEvent) { + formEvent.preventDefault(); + // setCustomProperties(); + // addNewParametersToSetupFieldsList(); + + BX.ajax.runAction('intaro:retailcrm.api.customexportprops.save', { + json: {properties: customProperties}, + }).then(function(response) { + console.log(response); + // this.submit(); + }); +}); + +function setCustomProperties() +{ + let customPropertiesRaws = $('tr[data-type="custom-property-raw"]'); + let customPropertyTitle = ''; + let customPropertyCode = ''; + let productPropertyMatch = ''; + let offerPropertyMatch = ''; + + customPropertiesRaws.each(function (rawElem) { + let raw = $(rawElem); + console.log(raw.find('input[name=custom-property-title]').val()); + customPropertyTitle = raw.find('input[name=custom-property-title]').value; + customPropertyCode = getUniquePropertyCode(customPropertyTitle); + productPropertyMatch = raw.find('select[name=custom-product-property-select]').value; + offerPropertyMatch = raw.find('select[name=custom-offer-property-select]').value; + + customProperties.push({ + 'title': customPropertyTitle, + 'code': customPropertyCode, + 'productProperty': productPropertyMatch, + 'offerProperty': offerPropertyMatch + }); + }); +} + +function getUniquePropertyCode(customPropertyTitle) +{ + const setupFieldsListValues = setupFieldsListElement.val().split(','); + let uniqueValue = customPropertyTitle; + console.log(uniqueValue); + let counter = 0; + + while (setupFieldsListValues.includes(uniqueValue)) { + uniqueValue = `${customPropertyTitle}${++counter}`; + } + console.log(uniqueValue); + + return uniqueValue; +} + +function addNewParametersToSetupFieldsList() +{ + let newParams = ''; + let parametersToFill = [ + 'iblockPropertySku_', + 'iblockPropertyUnitSku_', + 'iblockPropertyProduct_', + 'iblockPropertyUnitProduct_', + 'highloadblockb_hlsys_marking_code_group_', + 'highloadblock_productb_hlsys_marking_code_group_', + 'highloadblockeshop_color_reference_', + 'highloadblock_producteshop_color_reference_', + 'highloadblockeshop_brand_reference_', + 'highloadblock_producteshop_brand_reference_' + ]; + + customProperties.forEach(function (property) { + parametersToFill.forEach(function (param) { + newParams += param + property.code; + }); + }); + + setupFieldsListElement.value = setupFieldsListElement.value + ',' + newParams; +} + +function createNewCustomPropertyTableRaw() +{ + let elementsToCopy = document.querySelectorAll('[data-type="custom-property-raw"]'); + + if (elementsToCopy.length > 0) { + const lastElement = elementsToCopy[elementsToCopy.length - 1]; + const copyElement = lastElement.cloneNode(true); + lastElement.after(copyElement); + } +} \ No newline at end of file diff --git a/intaro.retailcrm/lib/controller/customexportprops.php b/intaro.retailcrm/lib/controller/customexportprops.php new file mode 100644 index 000000000..e050c5b71 --- /dev/null +++ b/intaro.retailcrm/lib/controller/customexportprops.php @@ -0,0 +1,54 @@ + + * @license MIT + * @link http://retailcrm.ru + * @see http://retailcrm.ru/docs + */ +class CustomExportProps extends Controller +{ + public function saveAction() + { + $request = $this->getRequest()->getInput(); + $response = new Result(); + + if ($request === null) { + $response->setStatus(new Error('Ошибка')); + } + + $properties = json_decode($this->getRequest()->getInput(), true)['properties']; + + foreach ($properties as $catalogId => $propertyArray) { + $newPropertiesString = ''; + foreach ($propertyArray as $property) { + $newPropertiesString .= PHP_EOL . $property['code'] . ' = ' . $property['title']; + } + $filePath = sprintf( + '%s/%s_%s.txt', + $_SERVER['DOCUMENT_ROOT'] . '/local', + 'icml_property_retailcrm', + $catalogId + ); + $saveResult = file_put_contents($filePath, $newPropertiesString, FILE_APPEND); + } + + if (!$saveResult) { + $response->setStatus(new Error('Ошибка')); + } + +// return $response->setStatus(Result::SUCCESS); + } + + public function deleteAction() + { + + } +} \ No newline at end of file From 0ceee2c0d77c1e7f342b4ebb69d1b789b8f30a9e Mon Sep 17 00:00:00 2001 From: "a.belikin" Date: Thu, 7 Nov 2024 18:30:22 +0300 Subject: [PATCH 2/9] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B8=20=D0=BF=D0=BE=D1=81=D0=BB=D0=B5=20=D0=B4=D0=B5?= =?UTF-8?q?=D0=BC=D0=BE=20MVP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- intaro.retailcrm/export/export_setup.php | 102 +++++++--- .../bitrix/js/intaro/custom-props-export.js | 184 ++++++++++++++---- .../lib/controller/customexportprops.php | 7 +- 3 files changed, 233 insertions(+), 60 deletions(-) diff --git a/intaro.retailcrm/export/export_setup.php b/intaro.retailcrm/export/export_setup.php index 1baa20d64..c52c2fe20 100644 --- a/intaro.retailcrm/export/export_setup.php +++ b/intaro.retailcrm/export/export_setup.php @@ -592,6 +592,7 @@ class="adm-btn-save"> + diff --git a/intaro.retailcrm/install/export/bitrix/js/intaro/custom-props-export.js b/intaro.retailcrm/install/export/bitrix/js/intaro/custom-props-export.js index 3b055dccd..94065dc3f 100644 --- a/intaro.retailcrm/install/export/bitrix/js/intaro/custom-props-export.js +++ b/intaro.retailcrm/install/export/bitrix/js/intaro/custom-props-export.js @@ -1,63 +1,102 @@ -const setupFieldsListElement = $('input[name="SETUP_FIELDS_LIST"]'); -let customProperties = [ - {code: 'newProperty2', title: 'Новое свойство'}, - {code: 'newProperty3', title: 'Новое свойство 2'}, -]; +$(document).on('blur', 'input[name="custom-property-title"]', function () { + let inputElem = $(this); + let newPropertyTitle = inputElem.val(); -document.getElementById('submit-form').addEventListener('submit', function (formEvent) { + if (!newPropertyTitle) { + return; + } + + let newPropertyCode = getUniquePropertyCode(newPropertyTitle); + addCustomPropCodeToSelectAttributes(newPropertyCode, inputElem); +}); + +$('#submit-form').submit(function (formEvent) { + let formElem = formEvent.currentTarget; formEvent.preventDefault(); - // setCustomProperties(); - // addNewParametersToSetupFieldsList(); + setCustomProperties(); + addParamsToSetupFieldsList(); BX.ajax.runAction('intaro:retailcrm.api.customexportprops.save', { json: {properties: customProperties}, - }).then(function(response) { - console.log(response); - // this.submit(); + }).then(function() { + formElem.submit(); }); }); +function addCustomPropCodeToSelectAttributes(customPropCode, customPropTitleElem) +{ + let selectElements = customPropTitleElem.closest('.custom-property-row').find('td select'); + let catalogId = customPropTitleElem.closest('.iblockExportTable').data('type'); + + selectElements.each(function (index, element) { + let selectElem = $(element); + let newSelectIdValue = selectElem.attr('id').match(/^[^_]*_/)[0] + customPropCode + catalogId; + let newSelectNameValue = selectElem.attr('name').match(/^[^_]*_/)[0] + customPropCode + `[${catalogId}]`; + + selectElem.attr('id', newSelectIdValue); + selectElem.attr('name', newSelectNameValue); + selectElem.data('type', customPropCode); + triggerSelectChange(selectElem); + }); +} + +function triggerSelectChange(selectElem) +{ + if (selectElem.val().length > 0) { + console.log('был') + selectElem.trigger('change', [self]); + } +} + function setCustomProperties() { - let customPropertiesRaws = $('tr[data-type="custom-property-raw"]'); + let customPropertiesRows = $('.custom-property-row'); + let customPropertyCatalogId; let customPropertyTitle = ''; let customPropertyCode = ''; let productPropertyMatch = ''; let offerPropertyMatch = ''; - customPropertiesRaws.each(function (rawElem) { - let raw = $(rawElem); - console.log(raw.find('input[name=custom-property-title]').val()); - customPropertyTitle = raw.find('input[name=custom-property-title]').value; + let catalogIds = []; + customPropertiesRows.each(function (index, propertyRow) { + let propertyRowObj = $(propertyRow); + customPropertyCatalogId = propertyRowObj.closest('.iblockExportTable').data('type'); + + customPropertyTitle = propertyRowObj.find('input[name="custom-property-title"]').val(); customPropertyCode = getUniquePropertyCode(customPropertyTitle); - productPropertyMatch = raw.find('select[name=custom-product-property-select]').value; - offerPropertyMatch = raw.find('select[name=custom-offer-property-select]').value; + productPropertyMatch = propertyRowObj.find('select[name=custom-product-property-select]').val(); + offerPropertyMatch = propertyRowObj.find('select[name=custom-offer-property-select]').val(); - customProperties.push({ + let values = { 'title': customPropertyTitle, 'code': customPropertyCode, 'productProperty': productPropertyMatch, 'offerProperty': offerPropertyMatch - }); + }; + + if (catalogIds.indexOf(customPropertyCatalogId) === -1) { + customProperties[customPropertyCatalogId] = [values]; + } else { + customProperties[customPropertyCatalogId].push(values); + } + catalogIds.push(customPropertyCatalogId); }); } function getUniquePropertyCode(customPropertyTitle) { - const setupFieldsListValues = setupFieldsListElement.val().split(','); - let uniqueValue = customPropertyTitle; - console.log(uniqueValue); + let uniqueValue = transliterate(customPropertyTitle).replace(/ /g, '_'); let counter = 0; + const setupFieldsListValues = setupFieldsListElement.val().split(','); while (setupFieldsListValues.includes(uniqueValue)) { uniqueValue = `${customPropertyTitle}${++counter}`; } - console.log(uniqueValue); return uniqueValue; } -function addNewParametersToSetupFieldsList() +function addParamsToSetupFieldsList() { let newParams = ''; let parametersToFill = [ @@ -73,22 +112,97 @@ function addNewParametersToSetupFieldsList() 'highloadblock_producteshop_brand_reference_' ]; - customProperties.forEach(function (property) { - parametersToFill.forEach(function (param) { - newParams += param + property.code; + for (let propertiesByCatalogId of Object.values(customProperties)) { + propertiesByCatalogId.forEach(function (values) { + parametersToFill.forEach(function (param) { + newParams += ',' + param + values.code; + }); }); + } + + let newValue = setupFieldsListElement.val() + newParams; + setupFieldsListElement.val(newValue); +} + +function createCustomPropsRaw(addRowButton) +{ + let templateRow = $($('#custom-property-template-row').html()); + let templateSelectElements = templateRow.find('select'); + + let prevTableRow = $(addRowButton).prev('table').find('tbody tr:last-child'); + let lastRawSelectElements = prevTableRow.find('td select'); + + lastRawSelectElements.each(function (index, element) { + let selectElement = $(element); + let templateSelectElement = templateSelectElements[index]; + fillTemplateSelect(selectElement, templateSelectElement); + prevTableRow.after(templateRow); }); +} - setupFieldsListElement.value = setupFieldsListElement.value + ',' + newParams; +function deleteCustomPropsRaw(buttonEvent) +{ + buttonEvent.closest('tr').remove(); +} + +function fillTemplateSelect(sourceSelectElement, templateSelectElement) +{ + let selectOptions = sourceSelectElement.find('option'); + selectOptions.each(function (index, element) { + let optionElem = $(element); + let value = $(optionElem).val(); + let text = $(optionElem).text(); + + $('