1
0
Fork 0
mirror of synced 2025-04-11 05:00:55 +00:00
Поддержка разных типов кастомных полей для клиента
This commit is contained in:
Ivan Chaplygin 2023-12-07 16:04:46 +03:00
parent 20695708d8
commit 5c46fad8a2
5 changed files with 136 additions and 38 deletions

View file

@ -468,12 +468,13 @@ class RCrmActions
public static function customUserFieldList()
{
$userFields = UserFieldTable::getList([
'select' => ['ID', 'FIELD_NAME'],
'select' => ['ID', 'FIELD_NAME', 'USER_TYPE_ID'],
'filter' => [
['ENTITY_ID' => 'USER'],
['?FIELD_NAME' => '~%INTARO%'],
['!=FIELD_NAME' => 'UF_SUBSCRIBE_USER_EMAIL'],
['USER_TYPE_ID' => 'string']
['?USER_TYPE_ID' => 'string | date | datetime | integer | double | boolean'],
['MULTIPLE' => 'N'],
]
])->fetchAll();
@ -488,12 +489,81 @@ class RCrmActions
]
])->fetch();
$resultList[$userField['FIELD_NAME']] = $label['EDIT_FORM_LABEL'];
$resultList[strtoupper($userField['USER_TYPE_ID']) . '_TYPE'][$userField['FIELD_NAME']] = $label['EDIT_FORM_LABEL'];
}
return $resultList;
}
public static function getTypeUserField()
{
$userFields = UserFieldTable::getList([
'select' => ['FIELD_NAME', 'USER_TYPE_ID'],
'filter' => [
['ENTITY_ID' => 'USER'],
['?FIELD_NAME' => '~%INTARO%'],
['!=FIELD_NAME' => 'UF_SUBSCRIBE_USER_EMAIL'],
['?USER_TYPE_ID' => 'string | date | datetime | integer | double | boolean'],
['MULTIPLE' => 'N'],
]
])->fetchAll();
$result = [];
foreach ($userFields as $userField) {
$result[$userField['FIELD_NAME']] = $userField['USER_TYPE_ID'];
}
return $result;
}
public static function convertFieldToCrmValue($value, $type)
{
$result = $value;
if ($type === 'boolean') {
$result = $value === 'Y' ? 1 : 0;
}
return $result;
}
public static function convertCrmValueToFieldUser($crmValue, $type)
{
$result = $crmValue;
if ($type === 'boolean') {
$result = $crmValue == 1 ? 'Y' : 'N';
}
if ($type === 'date') {
if (empty($crmValue)) {
return '';
}
try {
$result = date('d.m.Y', strtotime($crmValue));
} catch (\Exception $exception) {
$result = '';
}
}
if ($type === 'datetime') {
if (empty($crmValue)) {
return '';
}
try {
$result = date('d.m.Y H:i:s', strtotime($crmValue));
} catch (\Exception $exception) {
$result = '';
}
}
return $result;
}
public static function convertPropToCrmValue($prop)
{
$result = $prop['VALUE'][0];
@ -516,7 +586,7 @@ class RCrmActions
if ($typeField === 'DATE') {
if (empty($crmValue)) {
return $crmValue;
return '';
}
try {

View file

@ -67,6 +67,7 @@ class RetailCrmHistory
$matchedCustomFields = RetailcrmConfigProvider::getMatchedUserFields() ?? [];
$matchedCustomFields = array_flip($matchedCustomFields);
self::$CUSTOM_FIELDS_IS_ACTIVE = RetailcrmConfigProvider::getCustomFieldsStatus();
$customUserFieldTypes = RCrmActions::getTypeUserField();
if ($historyStart && $historyStart > 0) {
$historyFilter['sinceId'] = $historyStart;
@ -129,7 +130,7 @@ class RetailCrmHistory
$customerBuilder->setDataCrm($customer)->build();
$customFields = self::getCustomUserFields($customer, $matchedCustomFields);
$customFields = self::getCustomUserFields($customer, $matchedCustomFields, $customUserFieldTypes);
if (!isset($customer['externalId'])) {
if (!isset($customer['id'])) {
@ -278,7 +279,7 @@ class RetailCrmHistory
self::$CUSTOM_FIELDS_IS_ACTIVE = RetailcrmConfigProvider::getCustomFieldsStatus();
$customUserFieldTypes = RCrmActions::getTypeUserField();
$api = new RetailCrm\ApiClient(RetailcrmConfigProvider::getApiUrl(), RetailcrmConfigProvider::getApiKey());
$page = 1;
@ -478,7 +479,7 @@ class RetailCrmHistory
$newUser = new CUser();
$customerArray = $corporateCustomerBuilder->getCustomer()->getObjectToArray();
$customFields = self::getCustomUserFields($userData, $matchedCustomUserFields);
$customFields = self::getCustomUserFields($userData, $matchedCustomUserFields, $customUserFieldTypes);
$customerArray = array_merge($customerArray, $customFields);
if (!array_key_exists('UF_SUBSCRIBE_USER_EMAIL', $customerArray)) {
@ -947,7 +948,7 @@ class RetailCrmHistory
}
if ($registerNewUser === true) {
$customFields = self::getCustomUserFields($response['customer'], $matchedCustomUserFields);
$customFields = self::getCustomUserFields($response['customer'], $matchedCustomUserFields, $customUserFieldTypes);
$registeredUserID = $newUser->Add(self::getDataUser($customerBuilder, $customFields));
if ($registeredUserID === false) {
@ -2169,7 +2170,7 @@ class RetailCrmHistory
return self::convertBooleanFields($customerArray);
}
private static function getCustomUserFields($customer, $matchedCustomFields)
private static function getCustomUserFields($customer, $matchedCustomFields, $customUserFieldTypes)
{
$customFields = [];
@ -2178,8 +2179,9 @@ class RetailCrmHistory
&& is_array($customer['customFields'])
) {
foreach ($customer['customFields'] as $code => $value) {
if (isset($matchedCustomFields[$code])) {
$customFields[$matchedCustomFields[$code]] = $value;
if (isset($matchedCustomFields[$code]) && !empty($customUserFieldTypes[$matchedCustomFields[$code]])) {
$type = $customUserFieldTypes[$matchedCustomFields[$code]];
$customFields[$matchedCustomFields[$code]] = RCrmActions::convertCrmValueToFieldUser($value, $type);
}
}
}

View file

@ -163,11 +163,13 @@ class RetailCrmUser
private static function getCustomFields(array $arFields)
{
$customUserFields = RetailcrmConfigProvider::getMatchedUserFields();
$typeList = RCrmActions::getTypeUserField();
$result = [];
foreach ($customUserFields as $code => $codeCrm) {
if (!empty($arFields[$code])) {
$result[$codeCrm] = $arFields[$code];
$type = $typeList[$code] ?? '';
$result[$codeCrm] = RCrmActions::convertFieldToCrmValue($arFields[$code], $type);
}
}

View file

@ -191,6 +191,8 @@ $MESS ['NUMERIC_TYPE'] = 'Число';
$MESS ['Y/N_TYPE'] = 'Булев тип';
$MESS ['BOOLEAN_TYPE'] = 'Булев тип';
$MESS ['DATE_TYPE'] = 'Дата';
$MESS ['DATETIME_TYPE'] = 'Дата и время';
$MESS ['DOUBLE_TYPE'] = 'Число';

View file

@ -996,7 +996,7 @@ if (isset($_POST['Update']) && ($_POST['Update'] === 'Y')) {
SITE_CHARSET
);
$arResult['crmCustomUserFields'] = $APPLICATION->ConvertCharsetArray(
$api->customFieldsList(['entity' => 'customer', 'type' => [0 => 'string', 1 => 'text']], 250)->customFields,
$api->customFieldsList(['entity' => 'customer', 'type' => ['string', 'text', 'integer', 'numeric', 'boolean', 'date']], 250)->customFields,
'utf-8',
SITE_CHARSET
);
@ -1016,13 +1016,20 @@ if (isset($_POST['Update']) && ($_POST['Update'] === 'Y')) {
}
$crmCustomOrderFieldsList = [];
$crmCustomUserFieldsList = [];
foreach ($arResult['crmCustomOrderFields'] as $customField){
foreach ($arResult['crmCustomOrderFields'] as $customField) {
$crmCustomOrderFieldsList[strtoupper($customField['type']) . '_TYPE'][] = ['name' => $customField['name'], 'code' => $customField['code']];
}
foreach ($arResult['crmCustomUserFields'] as $customField) {
$crmCustomUserFieldsList[strtoupper($customField['type']). '_TYPE'][] = ['name' => $customField['name'], 'code' => $customField['code']];
}
$arResult['crmCustomOrderFields'] = $crmCustomOrderFieldsList;
unset($crmCustomOrderFieldsList);
$arResult['crmCustomUserFields'] = $crmCustomUserFieldsList;
unset($crmCustomOrderFieldsList, $crmCustomUserFieldsList);
$arResult['matchedOrderProps'] = ConfigProvider::getMatchedOrderProps();
$arResult['matchedUserFields'] = ConfigProvider::getMatchedUserFields();
@ -2332,13 +2339,17 @@ if (isset($_POST['Update']) && ($_POST['Update'] === 'Y')) {
onchange="changeSelectValue(this, 'bitrixUserField_', 'crmUserField_');"
>
<option value=""></option>
<?php foreach ($arResult['bitrixCustomUserFields'] as $code => $prop) {?>
<option
value="<?php echo $code ?>"
<?php if ($bitrixProp === $code) echo 'selected'; ?>
>
<?php echo $prop ?>
</option>
<?php foreach ($arResult['bitrixCustomUserFields'] as $type => $mass) {?>
<optgroup label="<?php echo GetMessage($type); ?>">
<?php foreach ($mass as $code => $prop) {?>
<option
value="<?php echo $code ?>"
<?php if ($bitrixProp === $code) echo 'selected'; ?>
>
<?php echo $prop ?>
</option>
<?php } ?>
</optgroup>
<?php } ?>
</select>
</td>
@ -2349,13 +2360,17 @@ if (isset($_POST['Update']) && ($_POST['Update'] === 'Y')) {
name="crmUserField_<?php echo $bitrixProp ?>"
>
<option value=""></option>
<?php foreach ($arResult['crmCustomUserFields'] as $crmProp) {?>
<option
value="<?php echo $crmProp['code'] ?>"
<?php if ($crmField === $crmProp['code']) echo 'selected'; ?>
>
<?php echo $crmProp['name'] ?>
</option>
<?php foreach ($arResult['crmCustomUserFields'] as $type => $mass) {?>
<optgroup label="<?php echo GetMessage($type); ?>">
<?php foreach ($mass as $crmProp) {?>
<option
value="<?php echo $crmProp['code'] ?>"
<?php if ($crmField === $crmProp['code']) echo 'selected'; ?>
>
<?php echo $crmProp['name'] ?>
</option>
<?php } ?>
</optgroup>
<?php } ?>
</select>
&nbsp;
@ -2383,7 +2398,6 @@ if (isset($_POST['Update']) && ($_POST['Update'] === 'Y')) {
onchange="changeSelectValue(this, 'bitrixOrderProp_', 'crmOrderField_');"
>
<option value=""></option>
<?php foreach ($arResult['bitrixOrdersCustomProp'] as $type => $mass) {?>
<optgroup label="<?php echo GetMessage($type); ?>">
<?php foreach ($mass as $code => $prop) {?>
@ -2432,10 +2446,14 @@ if (isset($_POST['Update']) && ($_POST['Update'] === 'Y')) {
onchange="changeSelectValue(this, 'bitrixUserField_', 'crmUserField_');"
>
<option value=""></option>
<?php foreach ($arResult['bitrixCustomUserFields'] as $code => $prop) {?>
<option value="<?php echo $code ?>">
<?php echo $prop ?>
</option>
<?php foreach ($arResult['bitrixCustomUserFields'] as $type => $mass) {?>
<optgroup label="<?php echo GetMessage($type); ?>">
<?php foreach ($mass as $code => $prop) {?>
<option value="<?php echo $code ?>">
<?php echo $prop ?>
</option>
<?php } ?>
</optgroup>
<?php } ?>
</select>
</td>
@ -2446,10 +2464,14 @@ if (isset($_POST['Update']) && ($_POST['Update'] === 'Y')) {
name="crmUserField"
>
<option value=""></option>
<?php foreach ($arResult['crmCustomUserFields'] as $crmProp) {?>
<option value="<?php echo $crmProp['code'] ?>">
<?php echo $crmProp['name'] ?>
</option>
<?php foreach ($arResult['crmCustomUserFields'] as $type => $mass) {?>
<optgroup label="<?php echo GetMessage($type); ?>">
<?php foreach ($mass as $crmProp) {?>
<option value="<?php echo $crmProp['code'] ?>">
<?php echo $crmProp['name'] ?>
</option>
<?php } ?>
</optgroup>
<?php } ?>
</select>
&nbsp;