<?php

/**
 * @filename photo.php 
 * @encoding UTF-8 
 * @author 邱伟平 
 * @copyright 连云港龙泽商务有限公司 
 * @datetime 2013-11-6  8:52:26
 * @version 1.0
 * @Description
 * 图片
 */
class Photo extends CI_Controller {

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

    /*
     * 管理
     */

    public function manage($table, $menu_id) {
        $this->load->library('grocery_CRUD');
        $crud = new grocery_CRUD();
        $crud->set_theme('datatables');


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


        $menu = $this->model_cmf->find_by_id("cms_menu", $menu_id);
        $crud->set_subject($menu->name);

        $crud->where('menu_id', $menu_id);
        $crud->columns('id', 'photo');
        $crud->field_type("menu_id", "hidden", $menu_id);
        $crud->set_field_upload('photo', 'assets/uploads/photo');

        if ($crud->getState() == "add") {
            $crud->default_data->state = "正常";
            $crud->default_data->publish_time = date('Y-m-d H:i:s');
        }
        $this->test();
        $output = $crud->render();

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

    /*
     * 图片保存在 cms_photo 中
     * menu_id 作为分类
     * 
     */

    public function manage_menu($menu_id, $node_id = 0) {
        $this->load->library('grocery_CRUD');
        $crud = new grocery_CRUD();
        $crud->set_theme('datatables');


        $this->model_cmf->setDisplayTag($crud, "cms_photo");


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


        if ($node_id == 0) {
            $crud->set_subject($menu->name);
        } else {
            $node = $this->model_cmf->find_by_id("cms_node", $node_id);
            $crud->set_subject($menu->name . " $node->width x $node->height px");
        }

        $crud->where('menu_id', $menu_id);
        $crud->columns('id', 'photo', 'title');
        $crud->field_type("menu_id", "hidden", $menu_id);
        $crud->set_field_upload('photo', 'assets/uploads/photo');

        if ($crud->getState() == "add") {
            $crud->default_data->state = "正常";
            $crud->default_data->publish_time = date('Y-m-d H:i:s');
        }
        $this->test();
        $output = $crud->render();

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

    /*
     * 单一文章
     */

    public function one($photo_id) {
        $this->load->add_package_path(APPPATH . '../package_ssioo', false);
        $sql = "select * from cms_photo where id  = " . $photo_id;

        $data = lz_tag();
        $data['文章'] = $this->db->query($sql)->result();
        $this->test();
        $this->parser->parse('photo', $data);
    }

    /*
     * 
     * 所有文章
     * 没有add
     */

    public function manage_all() {
        $this->load->library('grocery_CRUD');
        $crud = new grocery_CRUD();
        $crud->set_theme('datatables');

        $this->load->model('model_cmf');
        $this->model_cmf->setDisplayTag($crud, "cms_photo");

        $crud->set_relation("type_id", "params", "value");
        $crud->set_field_upload('photo', 'assets/uploads/photo');

        $crud->columns('id', 'type_id', 'photo');

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

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

    public function photo_click($id) {
        $sql = "select * from photo_info where photos_id = " . $id;
        $infos = $this->db->query($sql)->row();



        $sql = "select * from cms_photo where id  = " . $id;
        $cms_photos = $this->db->query($sql)->row();
        //  echo $cms_photos->url;
        if ($infos == null) {
            $date = date("Y-m-d", time());
            $sql = "INSERT INTO `photo_info` (`photos_id`, `title`, `photo`, `click`, `end_time`) VALUES ('" . $cms_photos->id . "', '" . $cms_photos->title . "', '" . $cms_photos->photo . "', '1', '" . $date . "'); ";
            $this->db->query($sql);
            redirect($cms_photos->url, 'refresh');
        } else {
            $sql = "UPDATE `photo_info` SET click = click+1 WHERE `photos_id` = '" . $id . "';";
            $this->db->query($sql);
            redirect($cms_photos->url, 'refresh');
        }
    }

    public function test() {
        echo  getenv('HTTP_X_FORWARDED_FOR');
        echo  getenv('HTTP_CLIENT_IP');
        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 : photo.php */
