Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- switch ($modx->event->name) {
- case 'msOnBeforeCreateOrder':
- // Убедитесь, что переменная $msOrder определена
- if (!isset($msOrder)) {
- $modx->log(modX::LOG_LEVEL_ERROR, 'Ошибка: $msOrder не определен.');
- return;
- }
- $address = $msOrder->getOne('Address');
- $properties = array();
- // Обработка полей из $_POST
- foreach ($_POST as $key => $value) {
- if (strpos($key, 'extfld_') !== false) {
- // Предотвращаем XSS, обеспечивая очистку входных данных
- $properties[$key] = htmlentities($value, ENT_COMPAT | ENT_HTML401, 'UTF-8');
- }
- }
- if (!empty($properties)) {
- //$modx->log(modX::LOG_LEVEL_ERROR, 'Setting properties: ' . json_encode($properties));
- $address->set('properties', json_encode($properties));
- }
- break;
- case 'msOnBeforeUpdateOrder':
- // Убедитесь, что переменная $msOrder определена
- if (!isset($msOrder)) {
- $modx->log(modX::LOG_LEVEL_ERROR, 'Ошибка: $msOrder не определен.');
- return;
- }
- $address = $msOrder->getOne('Address');
- $properties = array();
- // Собираем данные из правильного источника
- if (isset($_POST['addr_properties'])) {
- $properties = json_decode($_POST['addr_properties'], true);
- }
- // Альтернативный вариант: сбор из полей формы
- foreach ($_POST as $key => $value) {
- if (strpos($key, 'addr_properties_') === 0) {
- $fieldName = str_replace('addr_properties_', '', $key);
- $properties[$fieldName] = $value;
- }
- }
- // Получаем текущие свойства из базы данных
- $currentProperties = json_decode($address->get('properties'), true) ?: array();
- // Обновляем свойства, оставляя пустые значения без изменений
- foreach ($properties as $key => $value) {
- if ($value !== '' && $value !== null && $value !== undefined) {
- $currentProperties[$key] = $value;
- }
- }
- // Сохраняем все свойства, даже те, которые не были изменены
- if (!empty($currentProperties)) {
- $address->set('properties', json_encode($currentProperties));
- }
- break;
- case 'msOnManagerCustomCssJs':
- if (!isset($page) || $page != 'orders') {
- return;
- }
- $modx->controller->addHtml("
- <script type='text/javascript'>
- Ext.onReady(function() {
- console.log('Ext.onReady triggered');
- // Проверяем доступность компонента
- Ext.ComponentMgr.onAvailable('minishop2-window-order-update', function() {
- console.log('minishop2-window-order-update available');
- this.on('beforesubmit', function(form, action) {
- console.log('beforesubmit event triggered');
- console.log('Form:', form);
- console.log('Action:', action);
- // Проверяем, является ли action объектом
- if (typeof action !== 'object') {
- console.error('Action is undefined');
- return;
- }
- var properties = this.record.addr_properties || {};
- // Фильтруем поля формы, оставляя только те, что начинаются с 'addr_properties_'
- form.items.each(function(field) {
- console.log('Field:', field);
- if (field.name && field.name.startsWith('addr_properties_') && typeof field.getValue === 'function') {
- var key = field.name.replace('addr_properties_', '');
- var value = field.getValue();
- console.log('Field value:', key, value);
- if (value !== '' && value !== null && value !== undefined) {
- properties[key] = value;
- console.log('Field value added:', key, properties[key]);
- } else {
- console.log('Ignoring empty field:', key);
- }
- }
- });
- if (!action.extraParams) {
- console.log('action.extraParams is undefined, initializing it');
- action.extraParams = {};
- }
- action.extraParams.addr_properties = JSON.stringify(properties);
- console.log('Extra params:', action.extraParams);
- });
- // Проверяем существование miniShop2 и его конфигурации
- if (typeof miniShop2 !== 'undefined' && miniShop2.config && miniShop2.config['order_address_fields'].includes('properties')) {
- if (this.record.addr_properties) {
- var key;
- for (key in this.record.addr_properties) {
- const value_key = this.record.addr_properties[key];
- if (value_key) {var old_value_key = ': ' + value_key + ' (cкопировать ниже)';} else {var old_value_key = ''}
- // Добавляем редактируемое поле
- this.fields.items[2].items.push(
- {
- xtype: 'textfield',
- name: 'addr_properties_' + key,
- fieldLabel: _('ms2_properties_' + key) + old_value_key,
- anchor: '100%',
- style: 'border:1px solid #efefef;width:95%;padding:5px;',
- value: value_key
- }
- );
- }
- }
- }
- });
- });
- </script>");
- break;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement