<?php

/**
 * @filename model_common.php 
 * @encoding UTF-8 
 * @author 邱伟平 
 * @copyright 连云港龙泽商务有限公司 
 * @datetime 2013-6-9  10:00:06
 * @version 1.0
 * @Description
 */
class Model_cmf extends CI_Model {

    /**
     * 设置字段标签
     * 执行结果通过 $crud 带回去了
     */
    public function setDisplayTag($crud, $tableName) {
        $crud->set_table($tableName);
        $where = "where table_name='" . $tableName . "' and TABLE_SCHEMA = '" . $this->db->database . "'";
        $sql = "select TABLE_COMMENT from information_schema.tables " . $where;
        lz_log($sql);
        $query = $this->db->query($sql);
        $row = $query->row();
        lz_log($row);
        $crud->set_subject("");

        $sql = "select COLUMN_NAME,COLUMN_COMMENT from INFORMATION_SCHEMA.Columns " . $where;
        $query = $this->db->query($sql);
        foreach ($query->result() as $row) {
//            lz_log($row->COLUMN_NAME . ":" . $row->COLUMN_COMMENT);
            $crud->display_as($row->COLUMN_NAME, $row->COLUMN_COMMENT);
        }
        $crud->display_as('id', 'id');
    }

    public function get_template_data($data) {
        $path = explode('/', $this->uri->uri_string());

        $template_file_name = $path[0] . "/" . $path[1];

        $sql = "select * from cms_template where filename = '{$template_file_name}'";
        $template = $this->db->query($sql)->row();

        $sql = "select * from cms_template_data where template_id = {$template->id}";
        foreach ($this->db->query($sql)->result() as $template_data) {

            if ($template_data->menu_id == 0) {
                //特定函数取值
                $tmp = explode("->", $template_data->remark);
                $this->load->model($tmp[0]);
                $data[$template_data->index] = call_user_func(array($this->$tmp[0], $tmp[1]), $template_data);
                continue;
            }

            $menu = $this->find_by_id("cms_menu", $template_data->menu_id);

            if ($menu->file == "") {
                //例如 新闻频道 所有文章
                $from = "from " . $menu->remark;
                $where = " where 1=1";
            } else {
                $url = explode("/", $menu->file);
                $from = " from " . $url[2];
                $where = " where menu_id = " . $url[3];
            }

            lz_log($template_data);

            $sql = "select {$template_data->fields} " .
                    $from .
                    $where .
                    " and state = '正常' " .
                    " order by id desc " .
                    " limit {$template_data->start},{$template_data->num}";

            $data[$template_data->index] = $this->db->query($sql)->result();

//            if ($template_data->pages == "分页") {
//                $data['分页'] = $this->pages($where);
//            }
//            if ($template_data->crumbs == "需要") {
//                $tmp = "select * from params where id = ".$type_id;
//                $param = $this->db->query($tmp)->row();
//                $data['面包屑'] = $param->value;
//            }
        }

        return $data;
    }

    /*
     * 分页
     */

    public function pages($from_where, $config = array()) {

        $this->load->library('pagination');

        $sql = "select count(*) as total " . $from_where;
        $config['total_rows'] = $this->db->query($sql)->row()->total;

//        $config['total_rows'] = 100;
        if (!isset($config['per_page'])) {
            $config['per_page'] = 20;
        }

        if (!isset($config['uri_segment'])) {
            $config['uri_segment'] = 4;
        }

        if (!isset($config['base_url'])) {
            $this->load->helper('url');
            $config['base_url'] = base_url();

            for ($i = 1; $i < $this->uri->total_segments(); $i++) {
                $config['base_url'].= "/" . $this->uri->segment($i);
            }
        }
//        lz_log($config['base_url']);
        $config['first_link'] = '<<';
        $config['last_link'] = '>>';
        $config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['cur_tag_open'] = '<b>';
        $config['cur_tag_close'] = '</b>';
        
//        $config['anchor_class'] = "class = 'pages'";


        $this->pagination->initialize($config);

        return $this->pagination->create_links();
    }

    public function find_by_id($table, $id) {
        $sql = "select * from {$table} where id = {$id} ";
        return $this->db->query($sql)->row();
    }

    public function sql($string) {
        return $this->db->query($string)->result();
    }

    public function article($menu_id, $limit) {
        return $this->db->query("select * from cms_article where $menu_id $limit")->result();
    }

    public function photo($menu_id, $limit) {
        return $this->db->query("select * from cms_photo where $menu_id $limit")->result();
    }

    public function summary($articles, $num) {
        foreach ($articles as $article) {
            if ($article->summary == "") {
                $article->summary = strip_tags($article->content);
                
                $article->summary = mb_substr($article->summary,0,$num) . "......";
            }
        }
        return $articles;
    }


}

/* End of file : model_common.php */
