Пример #1
0
/**
 * Сохранение информации о конкретном параметре пользовательских настроек
 * @return <type>
 */
function nc_customsettings_save_once()
{
    $nc_core = nc_Core::get_object();
    $ClassID = $nc_core->input->fetch_get_post('ClassID');
    $TemplateID = $nc_core->input->fetch_get_post('TemplateID');
    $old_name = $nc_core->input->fetch_get_post('param');
    $new_name = $nc_core->input->fetch_get_post('name');
    $type = $nc_core->input->fetch_get_post('type');
    $subtype = $nc_core->input->fetch_get_post('subtype');
    $caption = $nc_core->input->fetch_get_post('caption');
    $default_value = $nc_core->input->fetch_get_post('default_value');
    if (!preg_match("/^[a-z0-9_]+\$/i", $new_name)) {
        throw new Exception(NETCAT_CUSTOM_ONCE_ERROR_FIELD_NAME);
    }
    if (!$caption) {
        throw new Exception(NETCAT_CUSTOM_ONCE_ERROR_CAPTION);
    }
    if ($ClassID) {
        $custom_settings = $nc_core->component->get_by_id($ClassID, 'CustomSettingsTemplate');
    } else {
        $custom_settings = $nc_core->template->get_by_id($TemplateID, 'CustomSettings');
    }
    if (!$custom_settings) {
        $custom_settings = array();
    } else {
        $a2f = new nc_a2f($custom_settings, '');
        $custom_settings = $a2f->eval_value($custom_settings);
    }
    $keys = empty($custom_settings) ? array() : array_keys($custom_settings);
    if (in_array($new_name, $keys) && $new_name != $old_name) {
        throw new Exception(NETCAT_CUSTOM_ONCE_ERROR_FIELD_EXISTS);
    }
    $param = array('type' => $type, 'subtype' => $subtype, 'caption' => $caption);
    if ($default_value) {
        $param['default_value'] = $default_value;
    }
    $input = $nc_core->input->fetch_get_post();
    // загружаем объект для работы с полем
    $classname = "nc_a2f_field_" . $type . ($subtype ? "_" . $subtype : "");
    if (!class_exists($classname)) {
        return false;
    }
    $fl = new $classname();
    $ex_params = $fl->get_extend_parameters();
    if (!empty($ex_params)) {
        foreach ($ex_params as $k => $v) {
            if ($type == 'select' && $subtype == 'static' && $k == 'values') {
                $param['values'] = array();
                if (!empty($_POST['select_static_key'])) {
                    foreach ($_POST['select_static_key'] as $i => $option_key) {
                        $option_key = trim($option_key);
                        $option_value = trim($_POST['select_static_value'][$i]);
                        if (!strlen($option_key) || !strlen($option_value)) {
                            continue;
                        }
                        $param['values'][$option_key] = $option_value;
                    }
                }
            } else {
                if (isset($input['cs_' . $k])) {
                    $param[$k] = $input['cs_' . $k];
                }
            }
        }
    }
    /*
          foreach ( $input as $k => $v ) {
          if ( nc_preg_match('/cs_([a-z0-9_]+)/i', $k, $match) ) $param[$match[1]] = $v;
          } */
    $custom_settings_new = array();
    if (!empty($custom_settings)) {
        foreach ($custom_settings as $k => $v) {
            if ($k != $old_name && $k != $new_name) {
                $custom_settings_new[$k] = $v;
            }
            if ($k == $old_name || $k == $new_name) {
                $custom_settings_new[$new_name] = $param;
            }
        }
    }
    if (!$old_name) {
        $custom_settings_new[$new_name] = $param;
    }
    $s = nc_a2f::array_to_string($custom_settings_new);
    $s = $custom_settings_new ? '$settings_array = ' . $s . ';' : '';
    if ($ClassID) {
        $nc_core->component->update($ClassID, array('CustomSettingsTemplate' => $s));
    } else {
        $nc_core->template->update($TemplateID, array('CustomSettings' => $s));
    }
    return true;
}