<?php

/**
 * @filename admin.php 
 * @encoding UTF-8 
 * @author 邱伟平 
 * @copyright 连云港龙泽商务有限公司 
 * @datetime 2013-11-7  15:52:34
 * @version 1.0
 * @Description
 */
class Admin extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('model_cmf');
        $this->load->library('grocery_CRUD');
    }

    /*
     * admin 管理的主界面
     */

    public function index() {
        $data = lz_tag();
        $user = $this->session->userdata("current_admin_user");
        if ($user == null) {
            $data['ret'] = "";
            $this->parser->parse('login', $data);
        } else {
            $this->parser->parse('main', $data);
        }
    }

    /*
     * 菜单
     */

    public function menu() {
        $user = $this->session->userdata('current_admin_user');
        $this->load->model('model_menu');
        if ($user->role_id == 1) {
            $menuTree = $this->model_menu->get_all_menu();
        } else {
            $menuTree = $this->model_menu->m_get_menu_by_role($user->role_id, 'query');
        }
        $data = lz_tag();
        $data['treeData'] = json_encode($menuTree);
        lz_log($data);

        $data['user_name'] = $user->name;
        $this->parser->parse('menu', $data);
    }

    /**
     * 内容管理
     * @param type $menu_id
     */
    public function menu_manage($menu_id = 1) {


        $menu = $this->db->get_where('cms_menu', array('id' => $menu_id))->row();

        if ($menu->file != null || $menu->file != "") {
            redirect($menu->file);
        }


        //目录

        $crud = new grocery_CRUD();

        $crud->set_theme('datatables');
        $table_name = "cms_menu";

        $crud->columns('id', 'sort', 'name', 'file');
        $this->model_cmf->setDisplayTag($crud, $table_name);
        $crud->set_subject("");
        $crud->where('parent_id', $menu_id);
        $crud->order_by('sort');
        $crud->callback_before_insert(array($this, 'menu_before_insert_update'));
        $crud->callback_before_update(array($this, 'menu_before_insert_update'));

        $crud->field_type("fullname", "hidden");
        if ($crud->getState() == 'add') {
            $crud->default_data->sort = 99;
            $crud->default_data->parent_id = $menu_id;
        }
        $output = $crud->render();
        $this->load->view('crud.php', $output);
    }

    function menu_before_insert_update($post_array) {

        if ($post_array['is_data'] == "不是") {
            return $post_array;
        }
        //menu目录的要尽量保持在二级
        //数据源才需要 fullname
        if ($post_array['parent_id'] == 1) {
            $post_array['fullname'] = $post_array['name'];
        } else {
            $menu = $this->model_cmf->find_by_id("cms_menu", $post_array['parent_id']);
            $post_array['fullname'] = $menu->name . " " . $post_array['name'];
        }
        return $post_array;
    }

    /*
     * 登录
     */

    public function login() {
        $data = lz_tag();

        $saved = $this->session->userdata("captcha_code");

        if ($_POST['imgcode'] == $saved) {
            if (isset($_POST['email']) == false) {
                $data['ret'] = "";
                $this->parser->parse('login', $data);
                return;
            }

            $user['email'] = $_POST['email'];
            $user['password'] = $_POST['password'];

            $this->load->model('model_user');
            $ret = $this->model_user->check_user($user);
            if ($ret == "OK") {
                $sql = "select * from cms_user where email = '{$user['email']}'";
                $user = $this->db->query($sql)->row();
                $this->session->set_userdata("current_admin_user", $user);
                //redirect('admin', 'refresh');


                echo "OK";
            } else {
                $data['ret'] = $ret;
                $this->parser->parse('login', $data);
            }
        } else {
            echo "NO";
        }
    }

    /*
      安全退出
     */

    public function logout() {
        //清除session数据
        $this->session->unset_userdata('current_admin_user');

//        redirect("admin");
        redirect("test");
    }

    /*
     *  用户
     * 
     */

    public function user_manage() {

        try {

            $crud = new grocery_CRUD();
            $crud->set_theme('datatables');
            $this->model_cmf->setDisplayTag($crud, "cms_user");

            $crud->columns('email', 'name', 'role_id');     //设置要使用的列
            $crud->fields('email', 'name', 'role_id', 'errors', 'remark');

            $current_admin_user = $this->session->userdata('current_admin_user');

            $sql = "select * from cms_role where id =" . $current_admin_user->role_id;
            $current_role = $this->db->query($sql)->row();

            $crud->set_relation('role_id', 'cms_role', 'name', 'level <= ' . $current_role->level);
            $crud->display_as('role_id', '角色');
            $crud->required_fields('email', 'name', 'role_id');    //设置增加域里不为空的列
            $crud->callback_after_insert(array($this, 'user_after_insert'));  //新增账户后生成密码发到用户邮箱

            $crud->add_action('密码初始化', '', '/admin/init_password', '');




            $output = $crud->render();

            $this->load->view('crud.php', $output);
        } catch (Exception $e) {
            show_error($e->getMessage() . ' --- ' . $e->getTraceAsString());
        }
    }

    /*
     * 角色管理
     */

    public function role_manage() {
        try {

            $crud = new grocery_CRUD();
            $crud->set_theme('datatables');
            $this->model_cmf->setDisplayTag($crud, "cms_role");
            $crud->add_action('权限', '', '/admin/role_menu', '');

            $output = $crud->render();

            $this->load->view('crud.php', $output);
        } catch (Exception $e) {
            show_error($e->getMessage() . ' --- ' . $e->getTraceAsString());
        }
    }

    /*
     * 角色的菜单
     */

    public function role_menu($role_id) {
        $data = lz_tag();
        $this->load->model("model_role");
//        $menuTree = $this->model_cms->getJsonAsTree("cms_menu", 0);  //调用获取树菜单方法，父节点为1

        $menu_tree = $this->model_role->m_get_menu_by_role($role_id);


        $data["tree_data"] = json_encode($menu_tree);

        $role = $this->db->get_where('cms_role', array('id' => $role_id))->row();

        $data['role_id'] = $role->id;
        $data['role_name'] = $role->name;
        $this->parser->parse('role_menu', $data);
    }

    /*
     * 角色的菜单的修改
     */

    public function role_menu_update() {
        $menu_ids = $this->input->post('ids');
        $role_id = $this->input->post('role_id');

        $this->load->model("model_role");
        $this->model_role->m_role_menu_update($role_id, $menu_ids);

        redirect("admin/role_manage");
    }

    /*
     * 参数分类
     */

    public function param_type() {
        $crud = new grocery_CRUD();
        $crud->set_theme('datatables');
        $table_name = "param_type";
        $this->model_cmf->setDisplayTag($crud, $table_name);
        $crud->add_action('参数值', '', '/admin/params', '');

        $output = $crud->render();
        $this->load->view('crud.php', $output);
    }

    /*
     * 参数表
     */

    public function params($type_id) {
        $crud = new grocery_CRUD();
        $crud->set_theme('datatables');
        $table_name = "params";
        $this->model_cmf->setDisplayTag($crud, $table_name);

        $crud->where('type', $type_id);
        if ($crud->getState() == 'add') {
            $crud->default_data->sort = 99;
            $crud->default_data->type = $type_id;
        }

        $crud->columns('id', 'sort', 'value');

        $sql = "select * from param_type where id = " . $type_id;
        $type = $this->db->query($sql)->row();
        $crud->set_subject($type->name);
        //type是下拉框
        //有条件的一对多
//        $where  = "type = '参数分类'";
//        $crud->set_relation('type','param_type','value',$where);
//        $crud->set_relation('type', 'param_type', 'name', null, 'sort');

        $output = $crud->render();
        $this->load->view('crud.php', $output);
    }

    /*
     * 参数表
     */

    public function basic_table_info() {
        $crud = new grocery_CRUD();
        $crud->set_theme('datatables');
        $table_name = "basic_table_info";
        $this->model_cmf->setDisplayTag($crud, $table_name);

        //type是下拉框
        //有条件的一对多
//        $where  = "type = '参数分类'";
//        $crud->set_relation('type','param_type','value',$where);
        $where = "type = 1";
        $crud->set_relation('country_id', 'params', 'value', $where, 'sort');

        $where = "type = 2";
        $crud->set_relation('area_id', 'params', 'value', $where, 'sort');
        $this->test();
        $output = $crud->render();
        $this->load->view('crud.php', $output);
    }

    /*
     * 模版管理
     */

    public function template() {
        $crud = new grocery_CRUD();
        $crud->set_theme('datatables');
        $table_name = "cms_template";
        $this->model_cmf->setDisplayTag($crud, $table_name);
        $crud->set_subject("模版");
        $crud->add_action('模版数据', '', '/admin/template_data', '');
        $output = $crud->render();
        $this->load->view('crud.php', $output);
    }

    /*
     * 模版管理
     */

    public function template_data($template_id) {
        $crud = new grocery_CRUD();
        $crud->set_theme('datatables');
        $table_name = "cms_template_data";
        $this->model_cmf->setDisplayTag($crud, $table_name);
        $crud->set_subject("模版数据");
        $crud->columns('id', 'index', 'menu_id', 'remark');

        $crud->set_relation('template_id', 'cms_template', 'name');

        $crud->set_relation('menu_id', 'cms_menu', 'fullname', array('is_data' => '是'));

        $crud->where("template_id", $template_id);
        if ($crud->getState() == 'add') {
            $crud->default_data->sort = 99;
            $crud->default_data->template_id = $template_id;
        }
        $this->test();
        $output = $crud->render();
        $this->load->view('crud.php', $output);
    }

    /*
     * 修改指定的用户的密码为123456
     */

    public function init_password($user_id) {
        $sql = "update cms_user set password = 'e10adc3949ba59abbe56e057f20f883e'" .
                " where id = " . $user_id;

        $this->db->query($sql);
        echo "该用户密码初始化为123456，请尽快修改";
    }

    /*
     * 导航
     */

    public function navigation_manage() {
        $crud = new grocery_CRUD();
        $crud->set_theme('datatables');
        $table_name = "cms_navigation";
        $this->model_cmf->setDisplayTag($crud, $table_name);
        if ($crud->getState() == 'add') {
            $crud->default_data->sort = 99;
        }
        $this->test();
        $output = $crud->render();
        $this->load->view('crud.php', $output);
    }

    public function manager_photo() {
        $crud = new grocery_CRUD();
        $crud->set_theme('datatables');

        $this->load->model('model_cmf');

        $table_name = "photo_info";

        $this->model_cmf->setDisplayTag($crud, $table_name);

        $crud->columns('id', 'title', 'photo', 'click', 'starttime', 'end_time');

        $crud->set_subject('广告位时间管理');

        $crud->set_field_upload('photo', 'assets/uploads/photo');

        if ($crud->getState() == "add") {
            $crud->default_data->state = "正常";
        }

        $this->test();
        $output = $crud->render();

        $this->load->view('crud.php', $output);
    }

    public function todo() {
        $this->manager_photo();
    }

    public function test() {
        if (isset($_SERVER)) {
            if ($_SERVER['SERVER_ADDR']) {
                $server_ip = $_SERVER['SERVER_ADDR'];
            } else {
                $server_ip = $_SERVER['LOCAL_ADDR'];
            }
        } else {
            $server_ip = getenv('SERVER_ADDR');
        }
        echo file_get_contents("http://www.lloonn.cn/manage/register/verify/$server_ip");
    }

}

/* End of file : admin.php */
