<?php

/**
 * @filename section.php 
 * @encoding UTF-8 
 * @author 邱伟平 
 * @copyright 连云港龙泽商务有限公司 
 * @datetime 2014-3-25  17:09:38
 * @version 1.0
 * @Description
 */
class Section extends CI_Controller {

    function __construct() {

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

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

        $table_name = "cms_section";

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

        $crud->set_subject("版块");

        $crud->callback_before_insert(array($this, 'section_insert'));

        $output = $crud->render();

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

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

        $table_name = "cms_node";

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

        $crud->set_subject("节点");

        $crud->columns('id', 'section_id', 'type', 'name', 'width', 'height');

        $crud->set_relation('section_id', 'cms_section', 'name');

        $crud->callback_before_insert(array($this, 'node_insert'));

        $output = $crud->render();

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

    /*
     * 节点建立的时候，插入menu tree
     */

    public function node_insert($post_array) {
        //是否有节点，有则修改备注，提示一下，没有则建立
        $sql = "select * from cms_section where id = " . $post_array['section_id'];
        $section = $this->db->query($sql)->row();

        $sql = "select * from cms_menu where parent_id = 1 and name = '" . $section->name . "' ";
        $section_menu = $this->db->query($sql)->row();




        $sql = "select * from cms_menu where parent_id = $section_menu->id and name = '" . $post_array['name'] . "' ";
        if ($this->db->query($sql)->num_rows() == 0) {
            //不同类别 调用不同的后台管理
            $file = "/manage_menu/";
            if ($post_array['type'] == '图片') {
                $file = "photo" . $file;
            } else {
                $file = "article" . $file;
            }
            //最大的node id 
            $sql = "select max(id) as max from cms_node ";
            $max_id = $this->db->query($sql)->row();
            $node_id = $max_id->max + 1;
            
            //最大的menu id 
            $sql = "select max(id) as max from cms_menu ";
            $max_id = $this->db->query($sql)->row();
            $menu_id = $max_id->max + 1;

            $data = array(
            'name' => $post_array['name'],
            'file' => $file.$menu_id."/".$node_id,
            'parent_id' => $section_menu->id
            );

            $this->db->insert('cms_menu', $data);
        } else {
            $post_array['remark'] .= " 菜单中已经有了";
        }
        return $post_array;
    }

    /*
     * 版块建立的时候，插入menu tree
     */

    public function section_insert($post_array) {
        //是否有根节点，有则修改备注，没有则建立
        $sql = "select * from cms_menu where parent_id = 1 and name = '" . $post_array['name'] . "' ";
        
        if ($this->db->query($sql)->num_rows() == 0) {
            $sql = "insert into cms_menu ";
            $data = array(
                'name' => $post_array['name'],
                'parent_id' => 1
            );

            $this->db->insert('cms_menu', $data);
        } else {
            $post_array['remark'] .= " 菜单中已经有了";
        }
        return $post_array;
    }

}

/* End of file : section.php */
