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

Доработки после демо MVP

This commit is contained in:
a.belikin 2024-11-07 18:30:22 +03:00
parent ee02f45637
commit 0ceee2c0d7
3 changed files with 238 additions and 65 deletions

View File

@ -592,6 +592,7 @@ if ($STEP === 1) {
</form> </form>
<?php CJSCore::Init(['jquery']);?> <?php CJSCore::Init(['jquery']);?>
<?php CUtil::InitJSCore(['intaro_custom_props']); ?>
<script type="text/javascript"> <script type="text/javascript">
function checkLoadStatus(object) function checkLoadStatus(object)
{ {
@ -796,22 +797,73 @@ if ($STEP === 1) {
deleteCustomPropsRaw($(this)); deleteCustomPropsRaw($(this));
}); });
$('#submit-form').submit(function (formEvent) { $(document).on('blur', 'input[name="custom-property-title"]', function () {
let formElem = formEvent.currentTarget; let inputElem = $(this);
formEvent.preventDefault(); let newPropertyTitle = inputElem.val();
setCustomProperties();
addParamsToSetupFieldsList();
BX.ajax.runAction('intaro:retailcrm.api.customexportprops.save', { if (!newPropertyTitle) {
json: {properties: customProperties}, return;
}).then(function(response) { }
formElem.submit();
}); let newPropertyCode = getUniquePropertyCode(newPropertyTitle);
addCustomPropCodeToSelectAttributes(newPropertyCode, inputElem);
}); });
$('#submit-form').submit(function (formEvent) {
formEvent.preventDefault();
let formElem = formEvent.currentTarget;
let profileId = $($('input[name="PROFILE_ID"]')).val();
setCustomProperties();
if (customProperties.length > 0) {
addParamsToSetupFieldsList();
BX.ajax.runAction('intaro:retailcrm.api.customexportprops.save', {
json: {
properties: customProperties,
profileId: profileId
},
}).then(function() {
formElem.submit();
});
} else {
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() function setCustomProperties()
{ {
let customPropertiesRows = $('.custom-property-row'); let customPropertiesRows = $('.custom-property-row');
if (customPropertiesRows.length === 0) {
return;
}
let customPropertyCatalogId; let customPropertyCatalogId;
let customPropertyTitle = ''; let customPropertyTitle = '';
let customPropertyCode = ''; let customPropertyCode = '';
@ -824,6 +876,11 @@ if ($STEP === 1) {
customPropertyCatalogId = propertyRowObj.closest('.iblockExportTable').data('type'); customPropertyCatalogId = propertyRowObj.closest('.iblockExportTable').data('type');
customPropertyTitle = propertyRowObj.find('input[name="custom-property-title"]').val(); customPropertyTitle = propertyRowObj.find('input[name="custom-property-title"]').val();
if (!customPropertyTitle) {
return true;
}
customPropertyCode = getUniquePropertyCode(customPropertyTitle); customPropertyCode = getUniquePropertyCode(customPropertyTitle);
productPropertyMatch = propertyRowObj.find('select[name=custom-product-property-select]').val(); productPropertyMatch = propertyRowObj.find('select[name=custom-product-property-select]').val();
offerPropertyMatch = propertyRowObj.find('select[name=custom-offer-property-select]').val(); offerPropertyMatch = propertyRowObj.find('select[name=custom-offer-property-select]').val();
@ -876,27 +933,28 @@ if ($STEP === 1) {
for (let propertiesByCatalogId of Object.values(customProperties)) { for (let propertiesByCatalogId of Object.values(customProperties)) {
propertiesByCatalogId.forEach(function (values) { propertiesByCatalogId.forEach(function (values) {
parametersToFill.forEach(function (param) { parametersToFill.forEach(function (param) {
newParams += param + values.code; newParams += ',' + param + values.code;
}); });
}); });
} }
setupFieldsListElement.value = setupFieldsListElement.value + ',' + newParams; let newValue = setupFieldsListElement.val() + newParams;
setupFieldsListElement.val(newValue);
} }
function createCustomPropsRaw(buttonEvent) function createCustomPropsRaw(addRowButton)
{ {
let rawTemplate = $($('#custom-property-template-row').html()); let templateRow = $($('#custom-property-template-row').html());
let templateSelectElements = rawTemplate.find('select'); let templateSelectElements = templateRow.find('select');
let lastTableRow = $(buttonEvent).prev('table').find('tbody tr:last-child'); let prevTableRow = $(addRowButton).prev('table').find('tbody tr:last-child');
let lastRawSelectElements = lastTableRow.find('td select'); let lastRawSelectElements = prevTableRow.find('td select');
lastRawSelectElements.each(function (index, element) { lastRawSelectElements.each(function (index, element) {
let selectElement = $(element); let selectElement = $(element);
let templateSelectElement = templateSelectElements[index]; let templateSelectElement = templateSelectElements[index];
fillTemplateSelect(selectElement, templateSelectElement); fillTemplateSelect(selectElement, templateSelectElement);
lastTableRow.after(rawTemplate); prevTableRow.after(templateRow);
}); });
} }
@ -907,13 +965,6 @@ if ($STEP === 1) {
function fillTemplateSelect(sourceSelectElement, templateSelectElement) function fillTemplateSelect(sourceSelectElement, templateSelectElement)
{ {
let name = sourceSelectElement.attr('name');
let id = sourceSelectElement.attr('id');
let type = sourceSelectElement.data('type');
templateSelectElement.setAttribute('name', name);
templateSelectElement.setAttribute('id', id);
templateSelectElement.setAttribute('data-type', type);
let selectOptions = sourceSelectElement.find('option'); let selectOptions = sourceSelectElement.find('option');
selectOptions.each(function (index, element) { selectOptions.each(function (index, element) {
let optionElem = $(element); let optionElem = $(element);
@ -926,6 +977,12 @@ if ($STEP === 1) {
function transliterate(titleToTransliterate) function transliterate(titleToTransliterate)
{ {
const hasCyrillicChars = /[\u0400-\u04FF]/.test(titleToTransliterate);
if (!hasCyrillicChars) {
return titleToTransliterate;
}
translitedText = ''; translitedText = '';
for (var i = 0; i < titleToTransliterate.length; i++) { for (var i = 0; i < titleToTransliterate.length; i++) {
switch (titleToTransliterate[i]) { switch (titleToTransliterate[i]) {
@ -965,7 +1022,6 @@ if ($STEP === 1) {
default: translitedText += titleToTransliterate[i]; break; default: translitedText += titleToTransliterate[i]; break;
} }
} }
return translitedText; return translitedText;
} }
</script> </script>

View File

@ -1,63 +1,102 @@
const setupFieldsListElement = $('input[name="SETUP_FIELDS_LIST"]'); $(document).on('blur', 'input[name="custom-property-title"]', function () {
let customProperties = [ let inputElem = $(this);
{code: 'newProperty2', title: 'Новое свойство'}, let newPropertyTitle = inputElem.val();
{code: 'newProperty3', title: 'Новое свойство 2'},
];
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(); formEvent.preventDefault();
// setCustomProperties(); setCustomProperties();
// addNewParametersToSetupFieldsList(); addParamsToSetupFieldsList();
BX.ajax.runAction('intaro:retailcrm.api.customexportprops.save', { BX.ajax.runAction('intaro:retailcrm.api.customexportprops.save', {
json: {properties: customProperties}, json: {properties: customProperties},
}).then(function(response) { }).then(function() {
console.log(response); formElem.submit();
// this.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() function setCustomProperties()
{ {
let customPropertiesRaws = $('tr[data-type="custom-property-raw"]'); let customPropertiesRows = $('.custom-property-row');
let customPropertyCatalogId;
let customPropertyTitle = ''; let customPropertyTitle = '';
let customPropertyCode = ''; let customPropertyCode = '';
let productPropertyMatch = ''; let productPropertyMatch = '';
let offerPropertyMatch = ''; let offerPropertyMatch = '';
customPropertiesRaws.each(function (rawElem) { let catalogIds = [];
let raw = $(rawElem); customPropertiesRows.each(function (index, propertyRow) {
console.log(raw.find('input[name=custom-property-title]').val()); let propertyRowObj = $(propertyRow);
customPropertyTitle = raw.find('input[name=custom-property-title]').value; customPropertyCatalogId = propertyRowObj.closest('.iblockExportTable').data('type');
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({ customPropertyTitle = propertyRowObj.find('input[name="custom-property-title"]').val();
customPropertyCode = getUniquePropertyCode(customPropertyTitle);
productPropertyMatch = propertyRowObj.find('select[name=custom-product-property-select]').val();
offerPropertyMatch = propertyRowObj.find('select[name=custom-offer-property-select]').val();
let values = {
'title': customPropertyTitle, 'title': customPropertyTitle,
'code': customPropertyCode, 'code': customPropertyCode,
'productProperty': productPropertyMatch, 'productProperty': productPropertyMatch,
'offerProperty': offerPropertyMatch 'offerProperty': offerPropertyMatch
}); };
if (catalogIds.indexOf(customPropertyCatalogId) === -1) {
customProperties[customPropertyCatalogId] = [values];
} else {
customProperties[customPropertyCatalogId].push(values);
}
catalogIds.push(customPropertyCatalogId);
}); });
} }
function getUniquePropertyCode(customPropertyTitle) function getUniquePropertyCode(customPropertyTitle)
{ {
const setupFieldsListValues = setupFieldsListElement.val().split(','); let uniqueValue = transliterate(customPropertyTitle).replace(/ /g, '_');
let uniqueValue = customPropertyTitle;
console.log(uniqueValue);
let counter = 0; let counter = 0;
const setupFieldsListValues = setupFieldsListElement.val().split(',');
while (setupFieldsListValues.includes(uniqueValue)) { while (setupFieldsListValues.includes(uniqueValue)) {
uniqueValue = `${customPropertyTitle}${++counter}`; uniqueValue = `${customPropertyTitle}${++counter}`;
} }
console.log(uniqueValue);
return uniqueValue; return uniqueValue;
} }
function addNewParametersToSetupFieldsList() function addParamsToSetupFieldsList()
{ {
let newParams = ''; let newParams = '';
let parametersToFill = [ let parametersToFill = [
@ -73,22 +112,97 @@ function addNewParametersToSetupFieldsList()
'highloadblock_producteshop_brand_reference_' 'highloadblock_producteshop_brand_reference_'
]; ];
customProperties.forEach(function (property) { for (let propertiesByCatalogId of Object.values(customProperties)) {
parametersToFill.forEach(function (param) { propertiesByCatalogId.forEach(function (values) {
newParams += param + property.code; parametersToFill.forEach(function (param) {
newParams += ',' + param + values.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);
} }
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);
});
}
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();
$('<option>', { value: value, text: text }).appendTo(templateSelectElement);
});
}
function transliterate(titleToTransliterate)
{
const hasCyrillicChars = /[\u0400-\u04FF]/.test(titleToTransliterate);
if (!hasCyrillicChars) {
return titleToTransliterate;
}
translitedText = '';
for (var i = 0; i < titleToTransliterate.length; i++) {
switch (titleToTransliterate[i]) {
case 'а': case 'А': translitedText += 'a'; break;
case 'б': case 'Б': translitedText += 'b'; break;
case 'в': case 'В': translitedText += 'v'; break;
case 'г': case 'Г': translitedText += 'g'; break;
case 'д': case 'Д': translitedText += 'd'; break;
case 'е': case 'Е': translitedText += 'e'; break;
case 'ё': case 'Ё': translitedText += 'yo'; break;
case 'ж': case 'Ж': translitedText += 'zh'; break;
case 'з': case 'З': translitedText += 'z'; break;
case 'и': case 'И': translitedText += 'i'; break;
case 'й': case 'Й': translitedText += 'y'; break;
case 'к': case 'К': translitedText += 'k'; break;
case 'л': case 'Л': translitedText += 'l'; break;
case 'м': case 'М': translitedText += 'm'; break;
case 'н': case 'Н': translitedText += 'n'; break;
case 'о': case 'О': translitedText += 'o'; break;
case 'п': case 'П': translitedText += 'p'; break;
case 'р': case 'Р': translitedText += 'r'; break;
case 'с': case 'С': translitedText += 's'; break;
case 'т': case 'Т': translitedText += 't'; break;
case 'у': case 'У': translitedText += 'u'; break;
case 'ф': case 'Ф': translitedText += 'f'; break;
case 'х': case 'Х': translitedText += 'h'; break;
case 'ц': case 'Ц': translitedText += 'c'; break;
case 'ч': case 'Ч': translitedText += 'ch'; break;
case 'ш': case 'Ш': translitedText += 'sh'; break;
case 'щ': case 'Щ': translitedText += 'sch'; break;
case 'ъ': case 'Ъ': translitedText += ''; break;
case 'ы': case 'Ы': translitedText += 'y'; break;
case 'ь': case 'Ь': translitedText += ''; break;
case 'э': case 'Э': translitedText += 'e'; break;
case 'ю': case 'Ю': translitedText += 'yu'; break;
case 'я': case 'Я': translitedText += 'ya'; break;
default: translitedText += titleToTransliterate[i]; break;
}
}
return translitedText;
} }

View File

@ -24,7 +24,9 @@ class CustomExportProps extends Controller
$response->setStatus(new Error('Ошибка')); $response->setStatus(new Error('Ошибка'));
} }
$properties = json_decode($this->getRequest()->getInput(), true)['properties']; $requestData = json_decode($this->getRequest()->getInput(), true);
$properties = $requestData['properties'];
$profileId = $requestData['profileId'];
foreach ($properties as $catalogId => $propertyArray) { foreach ($properties as $catalogId => $propertyArray) {
$newPropertiesString = ''; $newPropertiesString = '';
@ -32,9 +34,10 @@ class CustomExportProps extends Controller
$newPropertiesString .= PHP_EOL . $property['code'] . ' = ' . $property['title']; $newPropertiesString .= PHP_EOL . $property['code'] . ' = ' . $property['title'];
} }
$filePath = sprintf( $filePath = sprintf(
'%s/%s_%s.txt', '%s/%s_%s_%s.txt',
$_SERVER['DOCUMENT_ROOT'] . '/local', $_SERVER['DOCUMENT_ROOT'] . '/local',
'icml_property_retailcrm', 'icml_property_retailcrm',
$profileId,
$catalogId $catalogId
); );
$saveResult = file_put_contents($filePath, $newPropertiesString, FILE_APPEND); $saveResult = file_put_contents($filePath, $newPropertiesString, FILE_APPEND);