<?php
/**
 * [LJCMS] (C)2012-2099 LJdev,Inc.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2014.04.03
 */
class control extends adminbase
{
    private $mod = NULL;
    public function action_run()
    {
        $model = parent::model('category', 'am');
        $data = $model->getForAdmMenu();
        unset($model);
        $var_array = array(
            'adminname'=>parent::$wrap_admin['adminname'],
            'category'=>$data,
        );
        TPL::assign($var_array);
        TPL::display($this->cptpl . 'ui_set.tpl');
    }

    public function action_gat_name(){
        $type=$_REQUEST['type'];
        if($type==''){
            $arr = array(
                'response' => 0,
                'result'   => '参数错误'
            );
            echo json_encode($arr);
            die;
        }
        $id=$_REQUEST['id'];
        if($id==''){
            $arr = array(
                'response' => 0,
                'result'   => '参数错误'
            );
            echo json_encode($arr);
            die;
        }
        if($type=='category'){
            $model = parent::model('category', 'am');
            $data = $model->getData($id);
            $arr['name']=$data['catname'];
        }elseif ($type=='friendlink'){
            $model = parent::model('friendlink', 'am');
            $data = $model->getData($id);
            $arr['name']=$data['name'];
        }elseif ($type=='setting'){
            $model = parent::model('setting', 'am');
            $optionname='site_base';
            $lang= parent::$lang;
            // echo $lang;
            if($lang!='cn'){
                $optionname=$optionname."_".$_SESSION['lang'];
            }
            $data = $model->getOptions($optionname);
            $arr['name']=$data[$id];
        }elseif ($type=='language'){
            $model = parent::model('language', 'am');
            $data = $model->get_lang_data($id);
            $arr['name']=$data['value'];
        }

        $arr['response']=1;
        echo json_encode($arr);
        die;
    }

    public function action_save_name(){
        $type=$_REQUEST['type'];
        if($type==''){
            $arr = array(
                'response' => 0,
                'result'   => '参数错误'
            );
            echo json_encode($arr);
            die;
        }
        $id=$_REQUEST['id'];
        if($id==''){
            $arr = array(
                'response' => 0,
                'result'   => '参数错误'
            );
            echo json_encode($arr);
            die;
        }
        $value=$_REQUEST['text'];
        if($type=='category'){
            $arr['catname']=$value;
            $model = parent::model('category', 'am');
            $res = $model->doEditNodeList($id,$arr);
        }elseif ($type=='friendlink') {
            $arr['name'] = $value;
            $model = parent::model('friendlink', 'am');
            $res = $model->doEdit($id, $arr);
        } elseif ($type=='setting'){
            $arr['name']=$value;
            $model = parent::model('setting', 'am');
            //  print_r($args);die;
            $optionname='site_base';
            $lang= parent::$lang;
            if($lang!='cn'){
                $optionname=$optionname."_".$_SESSION['lang'];
            }
            $args = $model->getOptions($optionname);
            $tjcode=$args['tjcode'];
            unset($args['tjcode']);
            $args[$id]=$value;
            $res = $model->doSave($optionname, $args,$tjcode);
        }elseif ($type=='language'){
            $arr['value']=$value;
            $model = parent::model('language', 'am');
            $res = $model->doEdit_lang($id, $arr);
        }
        if($res){
            $data = array(
                'response' => 1,
                'result'   => '操作成功'
            );
            echo json_encode($data);
            die;
        }else{
            $data = array(
                'response' => 0,
                'result'   => '操作失败'
            );
            echo json_encode($data);
            die;
        }
    }


    public function action_save_img(){
        $type=$_REQUEST['type'];
        $id=$_REQUEST['id'];
        if ($type=='setting'){
            $model = parent::model('setting', 'am');
            $optionname='site_base';
            $lang= parent::$lang;
            // echo $lang;
            if($lang!='cn'){
                $optionname=$optionname."_".$_SESSION['lang'];
            }
            $data = $model->getOptions($optionname);
            $value=$data[$id];
        }
        $var_array = array(
            'value'=>$value,
            'type'=>$type,
            'id'=>$id
        );
        unset($model);
        TPL::assign($var_array);
        TPL::display($this->cptpl . 'save_img.tpl');
    }
    public function action_save_ajax_img(){
        $type=$_REQUEST['type'];
        if($type==''){
            $arr = array(
                'response' => 0,
                'result'   => '参数错误'
            );
            echo json_encode($arr);
            die;
        }
        $id=$_REQUEST['id'];
        if($id==''){
            $arr = array(
                'response' => 0,
                'result'   => '参数错误'
            );
            echo json_encode($arr);
            die;
        }
        $value=$_REQUEST['text'];
        if($type=='category'){

        }elseif ($type=='friendlink') {

        } elseif ($type=='setting'){
            $model = parent::model('setting', 'am');
            //  print_r($args);die;
            $optionname='site_base';
            $lang= parent::$lang;
            if($lang!='cn'){
                $optionname=$optionname."_".$_SESSION['lang'];
            }
            $args = $model->getOptions($optionname);
            $tjcode=$args['tjcode'];
            unset($args['tjcode']);
            $args[$id]=$value;
           // print_r($args);die;
            $res = $model->doSave($optionname, $args,$tjcode);
        }
        if($res){
            $data = array(
                'response' => 1,
                'result'   => '操作成功'
            );
            echo json_encode($data);
            die;
        }else{
            $data = array(
                'response' => 0,
                'result'   => '操作失败'
            );
            echo json_encode($data);
            die;
        }
    }



//写ini文件
        function write_ini_file($assoc_arr, $path, $has_sections = FALSE)
        {
            $content = "";
            if ($has_sections) {
                foreach ($assoc_arr as $key => $elem) {
                    $content .= "[" . $key . "]\n";
                    foreach ($elem as $key2 => $elem2) {
                        if (is_array($elem2)) {
                            for ($i = 0; $i < count($elem2); $i++) {
                                $content .= $key2 . "[] = \"" . $elem2[$i] . "\"\n";
                            }
                        } else if ($elem2 == "") $content .= $key2 . " = \n";
                        else $content .= $key2 . " = \"" . $elem2 . "\"\n";
                    }
                }
            } else {
                foreach ($assoc_arr as $key => $elem) {
                    if (is_array($elem)) {
                        for ($i = 0; $i < count($elem); $i++) {
                            $content .= $key2 . "[] = \"" . $elem[$i] . "\"\n";
                        }
                    } else if ($elem == "") $content .= $key2 . " = \n";
                    else $content .= $key2 . " = \"" . $elem . "\"\n";
                }
            }
            if (!$handle = fopen($path, 'w')) {
                return false;
            }
            if (!fwrite($handle, $content)) {
                return false;
            }
            fclose($handle);
            return true;
        }



}
?>