<?php
/*
+-----------------------------------------------------------------------------+
| $Id: set_config.php 2010-05-24 12:37:44Z Bleakwind $
| Manage config option
| Copyright (c) 2003-2010 Bleakwind (www.weaverdream.com)
| http://www.weaverdream.com/
| Release under the GNU Lesser General Public License Version 3 (LGPLv3):
|   http://www.gnu.org/licenses/lgpl.html
+-----------------------------------------------------------------------------+
*/

if (!defined( 'ENTRY_INDEX')) {
    echo "<h1>Forbidden</h1><p>You don't have permission to access on this server.</p>";
    exit;
}

// Admin group add and amend ajax
function submit_form($type, $value, $submit)
{
    global $LANGUAGE,$LANGLIST,$SETTING,$CONFIGURE,$MEMBER,$ADMIN,$db,$sys,$c,$setting_cover,$setting_attach,$sys,$ac;
    $ajax_response = new xajaxResponse();
    $error = false;
    //$ajax_response->alert(print_r($value, true)); $ajax_response->assign($submit,"disabled",false); return $ajax_response;

    $submit_value   = "编辑";

    if(!empty($value['config'])){
        foreach($value['config'] as $name => $option ){
            $config_info = func::db_select(DB_TABLE_CONFIG, "subject, if_multi, param", "name='".$name."'");
            $config_info[0]['param'] = unserialize($config_info[0]['param']);
            if ($config_info[0]['if_multi'] != "1") {
                if (!preg_match($config_info[0]['param'][0]['check'], $option)){
                    $error = true;
                    if(empty($anchor)) { $anchor = $name; }
                    $ajax_response->assign($name."_0_return", "innerHTML", "<span class=\"prompt_failed\">不符合要求,请返回重新填写!<!-- ".$name." error --></span>");
                } else {
                    $ajax_response->assign($name."_0_return", "innerHTML", "");
                }
            } else {
                foreach($config_info[0]['param'] as $k => $v ){
                    if (!preg_match($v['check'], $option[$v['name']])){
                        $error = true;
                        if(empty($anchor)) { $anchor = $name; }
                        $ajax_response->assign($name."_".$k."_return", "innerHTML", "<span class=\"prompt_failed\">不符合要求,请返回重新填写!<!-- ".$name." error --></span>");
                    } else {
                        $ajax_response->assign($name."_".$k."_return", "innerHTML", "");
                    }
                }
            }
        }
    }
    if($error){
        $ajax_response->assign($submit,"value",$submit_value);
        $ajax_response->assign($submit,"disabled",false);
        $ajax_response->redirect("#".$anchor);
    }else{
        if(!empty($value['config'])){
            foreach($value['config'] as $name => $option ){
                $config_info = func::db_select(DB_TABLE_CONFIG, "subject, if_multi, param", "name='".$name."'");
                $config_info[0]['param'] = unserialize($config_info[0]['param']);
                if ($config_info[0]['if_multi'] != "1") {
                    $sql = "UPDATE ".DB_TABLE_CONFIG."
                            SET value='".addslashes(trim(func::str_entag($option)))."'
                            WHERE name='".$name."'";
                    $result = &$db->Execute($sql);
                    if (!$result) {
                        $ajax_response->alert($db->ErrorMsg());
                        $ajax_response->assign($submit,"value",$submit_value);
                        $ajax_response->assign($submit,"disabled",false);
                    }
                } else {
                    $sql = "UPDATE ".DB_TABLE_CONFIG."
                            SET value='".addslashes(serialize($option))."'
                            WHERE name='".$name."'";
                    $result = &$db->Execute($sql);
                    if (!$result) {
                        $ajax_response->alert($db->ErrorMsg());
                        $ajax_response->assign($submit,"value",$submit_value);
                        $ajax_response->assign($submit,"disabled",false);
                    }
                }
            }
        }
        $sys->set_prompt(array(
            "prompt_type"       => "'prompt'",
            "prompt_if"         => "1",
            "prompt_message"    => "'配置文件修改完毕'",
            "prompt_returnurl"  => "'".$CONFIGURE['common']['control_admin']."?act=index'",
            "prompt_returntext" => "'返回首页'",
            "prompt_image"      => "'succeed.gif'",
            "prompt_act"        => "'prompt'",
        ));
        $ajax_response->redirect($CONFIGURE['common']['control_admin']."?act=prompt");
    }
    return $ajax_response;
}
$bwajax->register(XAJAX_FUNCTION, "submit_form");
//////

$config_sort_sub    = array();
$config_sort_info   = array();
$config_list        = array();

if (preg_match("/^[0-9]+$/",$sys->get['cs_id'])) {
    $config_sort_info  = func::db_select(DB_TABLE_CONFIG_SORT, "*", "cs_id=".$sys->get['cs_id']);
    $config_sort_info  = $config_sort_info[0];
    if (!empty($config_sort_info)) {
        $sql = "SELECT *
                FROM ".DB_TABLE_CONFIG."
                WHERE cs_id=".$sys->get['cs_id']."
                ORDER BY rank";
        $result = &$db->Execute($sql);
        if (!$result) {
            echo $db->ErrorMsg();
        }else {
            while(!$result->EOF){
                if(!empty($result->fields['param'])){
                    $param = unserialize($result->fields['param']);
                }
                if ($result->fields['if_multi'] == "1"){
                    $value_array = unserialize($result->fields['value']);
                }
                $config_list[] = array(
                                "name"          => $result->fields['name'],
                                "value"         => $result->fields['value'],
                                "value_array"   => $value_array,
                                "unit"          => $result->fields['unit'],
                                "subject"       => $result->fields['subject'],
                                "brief"         => $result->fields['brief'],
                                "if_multi"      => $result->fields['if_multi'],
                                "param"         => $param,
                                "rank"          => $result->fields['rank'],
                            );
                $result->MoveNext();
            }
        }
    }
}
if (empty($config_sort_info) ){
    $config_sort_sub    = func::db_select(DB_TABLE_CONFIG_SORT, "*", "", "", "cs_rank");
}

$t->assign(array(
    "config_sort_sub"   => $config_sort_sub,
    "config_sort_info"  => $config_sort_info,
    "config_list"       => $config_list,
));
?>
