<?php

/**
 * @filename ssioo.php 
 * @encoding UTF-8 
 * @author 邱伟平 
 * @copyright 连云港龙泽商务有限公司 
 * @datetime 2013-11-6  9:41:24
 * @version 1.0
 * @Description
 */
class Ssioo extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

    /*
     * 网址
     */

    public function site($type_id = 0) {
        $this->load->model('model_cmf');

        $this->load->library('grocery_CRUD');
        $crud = new grocery_CRUD();
        $crud->set_theme('datatables');
        $this->model_cmf->setDisplayTag($crud, "ssioo_site");
        $crud->set_subject("网址");

        if ($type_id != 0) {
            $crud->where('type_id', $type_id);
            if ($crud->getState() == 'add') {
                $crud->default_data->type_id = $type_id;
            }
        }
        $current_user = $this->session->userdata("current_user");
        $crud->set_relation("type_id", "ssioo_types", "name", array('member_id' => $current_user->id));

        $crud->columns('type_id', 'name', 'url');
        $crud->field_type("member_id", 'hidden', $current_user->id);


        $output = $crud->render();

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

    public function site_all() {
        $this->load->model('model_cmf');

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

        $current_user = $this->session->userdata("current_user");
        $crud->set_relation("type_id", "ssioo_types", "name", array('member_id' => $current_user->id));

        $crud->set_relation("member_id", "cms_user", "name");



        $output = $crud->render();

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

    /*
     * 分类
     */

    public function type() {
        $this->load->model('model_cmf');

        $this->load->library('grocery_CRUD');
        $crud = new grocery_CRUD();
        $crud->set_theme('datatables');
        $this->model_cmf->setDisplayTag($crud, "ssioo_types");
        $crud->set_subject("分类");

        $current_user = $this->session->userdata("current_user");


        if ($current_user->role_id == 5) {
            //会员 只能看到自己的
            $crud->columns('name');
            $crud->where("member_id", $current_user->id);
            $crud->field_type("member_id", 'hidden', $current_user->id);
            if ($crud->getState() == 'add') {
                
            }
        } else {
            $crud->set_relation("member_id", "cms_user", "name");
        }

        $crud->add_action('网址', '', '/ssioo/site', '');

        $output = $crud->render();

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

    /*
     * 单一文章
     */

    public function one($article_id = 1) {
        $this->load->add_package_path(APPPATH . '../package_article', false);

        $data['文章'] = array(
            array('title' => 'Title 1', 'body' => 'Body 1')
        );
        $data['site'] = site_url();
        $this->parser->parse('article', $data);
    }

    /*
     * 展现在插件中的
     */

    public function style($style_id = 1) {
        $this->load->add_package_path(APPPATH . '../package_ssioo', false);
        $sql = "select * from ssioo_style where id = " . $style_id;
//        $data['style'] = $this->db->query($sql)->result();
        $style = $this->db->query($sql)->row();
        $style->li_width = floor($style->width / $style->cols);
        $style->li_height = floor($style->height / $style->rows);

        $data = lz_tag();
        lz_log($style);
        $data['member_id'] = $style->member_id;
        $data['style'] = array($style);
        //单一分类
        $sql = "select * from ssioo_site " .
                "where member_id = {$style->member_id}" .
                " limit " . ($style->rows * $style->cols - 1);
        lz_log($sql);
        $data['sites'] = $this->db->query($sql)->result();
        $this->parser->parse('style_single', $data);
    }

    /*
     * 插件演示
     */

    public function style_demo() {
        $this->load->add_package_path(APPPATH . '../package_ssioo', false);
        $data = lz_tag();
        $this->parser->parse('style_demo', $data);
    }

    /*
     * 保存
     */

    public function style_insert() {

        $style['name'] = $_POST['name'];
        $style['width'] = $_POST['width'];
        $style['height'] = $_POST['height'];
        $style['cols'] = $_POST['cols'];
        $style['rows'] = $_POST['rows'];
        $style['font_size'] = $_POST['font_size'];
        $style['font_color'] = $_POST['font_color'];


        $style['types'] = implode(',', $_POST['types']);


        $current_user = $this->session->userdata("current_user");
        $style['member_id'] = $current_user->id;
        $this->db->insert("ssioo_style", $style);

        redirect("ssioo/style_manage");
    }

    /*
     * 保存
     */

    public function style_update() {
        $id = $_POST['id'];
        $style['name'] = $_POST['name'];
        $style['width'] = $_POST['width'];
        $style['height'] = $_POST['height'];
        $style['cols'] = $_POST['cols'];
        $style['rows'] = $_POST['rows'];
        $style['font_size'] = $_POST['font_size'];
        $style['font_color'] = $_POST['font_color'];


        $style['types'] = implode(',', $_POST['types']);


        $current_user = $this->session->userdata("current_user");
        $style['member_id'] = $current_user->id;

        $this->db->where('id', $id);
        $this->db->update("ssioo_style", $style);

        redirect("ssioo/style_manage");
    }

    /*
     * 插件展示风格管理
     */

    public function style_manage() {
        $this->load->model('model_cmf');

        $this->load->library('grocery_CRUD');
        $crud = new grocery_CRUD();
        $crud->set_theme('datatables');
        $this->model_cmf->setDisplayTag($crud, "ssioo_style");
        $crud->set_subject("展示风格");

        $current_user = $this->session->userdata("current_user");


        if ($current_user->role_id == 5) {
            //会员 只能看到自己的
            $crud->columns('id', 'name');
            $crud->where("member_id", $current_user->id);


            if ($crud->getState() == "add") {
                redirect("ssioo/add_style");
            }
            if ($crud->getState() == "edit") {

                redirect("ssioo/edit_style/" . $this->uri->segment(4));
            }
        } else {
            $crud->set_relation("member_id", "cms_user", "name");
        }



        $crud->add_action("复制", "", "/ssioo/style_copy");
        $crud->add_action("获取插件代码", "", "/ssioo/style_code");


        $output = $crud->render();

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

    /*
     * 复制一个风格
     */

    public function style_copy($style_id) {
        $sql = "select * from ssioo_style where id = " . $style_id;
        $style = $this->db->query($sql)->row();

        $style->name .= " 复制";

        $style->id = null;
        $this->db->insert("ssioo_style", $style);

        redirect("ssioo/style_manage");
    }

    /*
     * 获取插件代码
     */

    public function style_code($style_id) {
        $this->load->add_package_path(APPPATH . '../package_ssioo', false);
        $data = lz_tag();

        $sql = "select * from ssioo_style where id = " . $style_id;
        $data['style'] = $this->db->query($sql)->result();
        $this->parser->parse('style_code', $data);
    }

    /*
     * 调用新增风格的页面
     */

    public function add_style() {
        $this->load->add_package_path(APPPATH . '../package_ssioo', false);
        $data = lz_tag();
        $current_user = $this->session->userdata("current_user");
        $sql = "select id,name from ssioo_types where member_id = " . $current_user->id;
        $data['分类'] = $this->db->query($sql)->result();

        $this->parser->parse('style_new', $data);
    }

    /*
     * 调用编辑风格的页面
     */

    public function edit_style($style_id) {
        $this->load->add_package_path(APPPATH . '../package_ssioo', false);
        $data = lz_tag();
        $current_user = $this->session->userdata("current_user");
        $sql = "select id,name from ssioo_types where member_id = " . $current_user->id;
        $data['分类'] = $this->db->query($sql)->result();


        $sql = "select * from ssioo_style where id = " . $style_id;
        $data['old_style'] = $this->db->query($sql)->result();

        $this->parser->parse('style_edit', $data);
    }

    /*
     * 会员的首页，展现会员的所有网站信息
     */

    public function member($member_id = 4) {
        $this->load->add_package_path(APPPATH . '../package_ssioo', false);
        $data = lz_tag();
        $sql = "select * from cms_user where id = " . $member_id;
        $member = $this->db->query($sql)->row();
        $data['member_name'] = $member->name;
        $this->parser->parse('index', $data);
    }

    /*
     * 编辑展示风格的时候，预览，需要site数据
     */

    public function ajax_get_site() {
        $rows = $_POST['rowsCount'];
        $cols = $_POST['colsCount'];
        $types = $_POST['types'];

        $sql = "select name from ssioo_site where ";

        if (count($types) == 1) {
            $sql .= " type_id = {$types[0]}";
        } else {
            $sql .= " type_id in (" . implode(',', $types) . ")";
            ;
        }
        $sql .= " limit " . ($rows * $cols - 1);


        lz_log($sql);
        $result = $this->db->query($sql)->result();
        echo json_encode($result);
    }

}

/* End of file : ssioo.php */
