Korusol
9/13/2016 - 1:50 PM

Добавление пользовательских свойств UF для

Добавление пользовательских свойств UF для

public function commit()
    {
        $info = array();
        global $APPLICATION;

        function oGetMessage($key, $fields) {
            $messages = array(
                'USER_TYPE_UPDATE' => 'Пользовательское свойство #FIELD_NAME# [#ENTITY_ID#] успешно обновлено',
                'USER_TYPE_UPDATE_ERROR' => 'Ошибка обновления пользовательского свойства #FIELD_NAME# [#ENTITY_ID#]: #ERROR#',
                'USER_TYPE_ADDED' => 'Пользовательское свойство #FIELD_NAME# [#ENTITY_ID#] успешно добавлено',
                'USER_TYPE_ADDED_ERROR' => 'Ошибка добавления пользовательского свойства #FIELD_NAME# [#ENTITY_ID#]: #ERROR#',
                'USER_TYPE_ENUMS_SET_ERROR' => 'Ошибка установки значений пользовательского свойства #FIELD_NAME# [#ENTITY_ID#]: #ERROR#',
            );
            return isset($messages[$key])
                ? str_replace(array_keys($fields), array_values($fields), $messages[$key])
                : '';
        }

// @TODO edit
        $aUserFields = array(
            array(
                'ENTITY_ID' => 'CAT_STORE',
                'FIELD_NAME' => 'UF_STORE_TYPE',
                'USER_TYPE_ID' => 'enumeration',
                'SORT' => 500,
                'MULTIPLE' => 'N',
                'MANDATORY' => 'N',
                'IS_SEARCHABLE' => 'N',
                'SETTINGS' => array(
                    'DEFAULT_VALUE' => '',
                    'SIZE' => '40',
                    'ROWS' => '3',
                ),
                'EDIT_FORM_LABEL' => array(
                    'ru' => 'Тип склада',
                    'en' => 'type of store',
                ),
                'LIST_COLUMN_LABEL' => array(
                    'ru' => 'Тип склада',
                    'en' => 'type of store',
                ),
                'LIST_FILTER_LABEL' => array(
                    'ru' => 'Тип склада',
                    'en' => 'type of store',
                ),
                'VALUES' => array(
                    array(
                        'VALUE'     => 'Метро',
                        'XML_ID'    => 'n1',
                        'DEF'       => 'Y',
                        'SORT'      => 100,
                    ),
                    array(
                        'VALUE'     => 'ПВЗ Maxima',
                        'XML_ID'    => 'n2',
                        'DEF'       => 'N',
                        'SORT'      => 200,
                    ),
                    array(
                        'VALUE'     => 'ПВЗ PickPoint',
                        'XML_ID'    => 'n3',
                        'DEF'       => 'N',
                        'SORT'      => 200,
                    ),
                ),
            ),
        );

        $oUserTypeEntity = new CUserTypeEntity();

        foreach ($aUserFields as $aUserField) {

            $resProperty = CUserTypeEntity::GetList(
                array(),
                array('ENTITY_ID' => $aUserField['ENTITY_ID'], 'FIELD_NAME' => $aUserField['FIELD_NAME'])
            );

            if ($aUserHasField = $resProperty->Fetch()) {
                $idUserTypeProp = $aUserHasField['ID'];
                if ($oUserTypeEntity->Update($idUserTypeProp, $aUserField)) {
                    $info[] = oGetMessage('USER_TYPE_UPDATE', array(
                        '#FIELD_NAME#' => $aUserHasField['FIELD_NAME'],
                        '#ENTITY_ID#' => $aUserHasField['ENTITY_ID'],
                    ));
                } else {
                    if (($ex = $APPLICATION->GetException())) {
                        throw new \Bitrix\Main\SystemException(oGetMessage('USER_TYPE_UPDATE_ERROR', array(
                            '#FIELD_NAME#' => $aUserHasField['FIELD_NAME'],
                            '#ENTITY_ID#' => $aUserHasField['ENTITY_ID'],
                            '#ERROR#' => $ex->GetString(),
                        )));
                    }
                }
            } else {
                if ($idUserTypeProp = $oUserTypeEntity->Add($aUserField)) {
                    $info[] = oGetMessage('USER_TYPE_ADDED', array(
                        '#FIELD_NAME#' => $aUserField['FIELD_NAME'],
                        '#ENTITY_ID#' => $aUserField['ENTITY_ID'],
                    ));
                } else {
                    if (($ex = $APPLICATION->GetException())) {
                        throw new \Bitrix\Main\SystemException(oGetMessage('USER_TYPE_ADDED_ERROR', array(
                            '#FIELD_NAME#' => $aUserField['FIELD_NAME'],
                            '#ENTITY_ID#' => $aUserField['ENTITY_ID'],
                            '#ERROR#' => $ex->GetString(),
                        )));
                    }
                }
            }

            $obEnum = new CUserFieldEnum;

            $valuesEnums = array();
            foreach ($aUserField['VALUES'] as $arUserFieldEnum) {
                $valuesEnums[] = $arUserFieldEnum + array('USER_FIELD_ID' => $idUserTypeProp);
            }

            $userTypeEnumsIterator = CUserFieldEnum::GetList(array('SORT' => 'ASC'), array('USER_FIELD_ID' => $idUserTypeProp));
            if ($userTypeEnumsIterator->SelectedRowsCount()) {
                $valuesEnumsNews = array();
                foreach ($valuesEnums as $idValueEnum => $valueEnum) {
                    $userTypeEnumsHasIterator = CUserFieldEnum::GetList(array(), array('USER_FIELD_ID' => $idUserTypeProp, 'VALUE' => $valueEnum['VALUE']));
                    if ($arTypeEnumsHasIterator = $userTypeEnumsHasIterator->Fetch()) {
                        $valuesEnumsNews[$arTypeEnumsHasIterator['ID']] = $valueEnum;
                    } else {
                        $valuesEnumsNews['n' . $idValueEnum] = $valueEnum;
                    }
                }
            } else {
                $valuesEnumsNews = array();
                foreach ($valuesEnums as $idValueEnum => $valueEnum) {
                    $valuesEnumsNews['n' . $idValueEnum] = $valueEnum;
                }
            }

            if (!$obEnum->SetEnumValues($idUserTypeProp, $valuesEnumsNews)) {
                $strError = '';
                if ($ex = $APPLICATION->GetException()) {
                    $strError = $ex->GetString();
                }
                throw new \Bitrix\Main\SystemException(oGetMessage('USER_TYPE_ENUMS_SET_ERROR', array(
                    '#FIELD_NAME#' => $aUserField['FIELD_NAME'],
                    '#ENTITY_ID#' => $aUserField['ENTITY_ID'],
                    '#ERROR#' => $strError,
                )));
            }
        }
    }

    /**
     * Write action by rollback scenario. Use method `getData` for getting commit saved data
     **/
    public function rollback()
    {
        /**
         * Удаление свойства
         */
        $arrUserFields = [
            1 => [
                'ENTITY_ID' => 'CAT_STORE',
                'FIELD_NAME' => 'UF_STORE_TYPE',
            ]
        ];
        foreach ($arrUserFields as $UserField) {
            $result = CUserTypeEntity::GetList(
                [],
                [
                    'ENTITY_ID' => $UserField['ENTITY_ID'],
                    'FIELD_NAME' => $UserField['FIELD_NAME'],
                ]
            );

            while ($row = $result->Fetch()) {
                if ($row['ID'] > 0) {
                    (new CUserTypeEntity())->Delete($row['ID']);
                }
            }
        }

    }