<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Client extends Mocuz_Controller{

    public $channel = array('1001'=>'外部链接','1003'=>'论坛版块','2001'=>'团购模块','2002'=>'常用号码','2010'=>'二维码扫描','2011'=>'签到','2015'=>'五折卡','2018'=>'帖子详情','2019'=>'积分商城','2020'=>'邀请好友功能','2021'=>'话题帖子详情','2022'=>'话题详情');
    public $icon_size = array(88, 88);
    public $hot_flag = array('new'=>'NEW', 'hot'=>'HOT', 'xin'=>'最新', 're'=>'热门', 'no'=>'无');


    public function __construct()
    {
        parent::__construct();
        $this->load->model('mocuz/setting_model', 'setting');
        $this->load->helper('mocuz_client_helper');
    }

    public function first()
    {
        $this->load->view('common/header');
        $this->load->view('common/left_menu');
    }

    /**
     * [index 首页内容元素显示页面]
     * @author [刘厚望]
     * @return [type] [模板加载]
     */
    public function index()
    {
        $data = array();
        //首页入口
        $data['iconlist'] = get_icon_list();
        $this->load->model('mocuz/Home_channel_model','home_channel');
        $data['channel'] = $this->home_channel->get();
        $data['goto'] = $this->channel;
        $data['hot_flag'] = $this->hot_flag;
//        print_r($data['channel']);die();

        $data['index_hot_block'] = $this->setting->get('index_hot_block');//热点新闻区块
        $data['index_hot_news'] = $this->setting->get('index_hot_news');//热点新闻
        $data['index_last_news'] = $this->setting->get('index_last_news');//新鲜事
        $data['index_block_id'] = $this->setting->get('index_block_id');//新鲜事板块ID

        $this->load->view('common/innerheader');
        $this->load->view('admin/mocuz/home_content',$data);
    }

    //首页频道管理
    public function home_channel()
    {
        $data['iconlist'] = get_icon_list();

        $this->load->model('mocuz/Home_channel_model','home_channel');
        $data['channel'] = $this->home_channel->get();

        $data['goto'] = $this->channel;
        $this->load->view('common/innerheader');
        $this->load->view('admin/mocuz/channel',$data);
    }

    public function get_home_channel()
    {
        $this->load->model('mocuz/Home_channel_model','home_channel');
        $data = $this->home_channel->get_one($this->input->post('id'));
        echo json_encode($data);
    }
    
    public function save_home_channel($id="")
    {
        $title = trim($this->input->post("title"));
        $cid = trim($this->input->post("cid"));
        $goto_value = trim($this->input->post("goto_value"));
        $icon = trim($this->input->post("icon"));
        $requiredlogin = trim($this->input->post("requiredlogin"));

        if(! icon_size($icon, $this->icon_size))
        {
            echo $this->json(0,"图标必须{$this->icon_size[0]}*{$this->icon_size[1]}尺寸");exit;
        }
        if(!preg_match('/^([\x{4e00}-\x{9fa5}]|\w|-){1,5}$/u', $title))
        {
            echo $this->json(0,'请输入正确版块名');exit;
        }

        if(!preg_match('/^\d+$/', $cid))
        {
            echo $this->json(0,'请选择正确跳转分类');exit;
        }

        if($cid == 1001)
        {
            if(!preg_match('/^[a-zA-z]+:\/\/[^\s]*$/', $goto_value))
            {
                echo $this->json(0,'请填写正确ID/链接地址');exit;
            }
        }

        if($cid == 1003 || $cid == 2018)
        {
            if(!preg_match('/^\d+$/', $goto_value))
            {
                echo $this->json(0,'请填写正确ID/链接地址');exit;
            }
        }

        $this->load->model('mocuz/Home_channel_model','home_channel');
        if(empty($id))
        {
            $this->check_Home_channel_cid($cid);
            $this->home_channel->insert($this->input->post());
        }
        else
        {
            $data = $this->home_channel->get_one($id);
            if($data['cid'] != $cid)
            {
                $this->check_Home_channel_cid($cid);
            }
            $this->home_channel->update($this->input->post(),$id);
        }
        echo $this->json(1,'成功');
    }

    public function save_home_channel_flag()
    {
        $this->load->model('mocuz/Home_channel_model', 'home_channel');
        $this->home_channel->update(
            array('flag' => $this->input->post('flag')),
            $this->input->post('id')
        );

        echo $this->json(1,'成功');
    }

    public function check_Home_channel_cid($cid)
    {
        $this->load->model('mocuz/Home_channel_model','home_channel');
        if(! $this->home_channel->check_cid($cid))
        {
            echo $this->json(0,'该入口已存在');exit;
        }
    }
    public function del_home_channel()
    {
        $this->load->model('mocuz/Home_channel_model','home_channel');
        $this->home_channel->del_channel($this->input->post('id'));
        echo $this->json(1,'成功');
    }
    public function sort_home_channel()
    {
        $this->load->model('mocuz/Home_channel_model','home_channel');
        $ordernum = $this->input->post('ordernum');
        $this->home_channel->update_order($ordernum);
        echo $this->json(1,'成功');
    }


    /**
     * [save 首页内容元素保存]
     * @author [刘厚望]
     * @return [type] [description]
     */
    public function save()
    {

        $config = array(
            array(
                'field' => 'index_hot_news',
                'label' => '热点新闻ID',
                'rules' => 'trim|regex_match[/^\d+$/]'
            ),
            array(
                'field' => 'index_block_id',
                'label' => '板块数据',
                'rules' => 'trim|regex_match[/^\d+(,\d+)*$/]'
            )
        );
        $data['title'] = '首页内容元素';

        $this->load->library('form_validation', $config);
        if($this->form_validation->run() == FALSE)
        {

            print_r($this->form_validation->error_array());
        }
        else
        {
            if($this->input->post('index_hot_block') == 0 && $this->input->post('index_last_news' == 0))
            {
                echo "至少要显示一个区块";exit;
            }
            $this->setting->set('index_hot_block', $this->input->post('index_hot_block'));

            $this->setting->set('index_last_news', $this->input->post('index_last_news'));

            //当设置为显示的时候进行保存;
            if($this->input->post('index_hot_block') == 1)
            {
                $this->setting->set('index_hot_news', $this->input->post('index_hot_news'));
            }

            if($this->input->post('index_last_news') == 1)
            {
                $cpost = explode(',', $this->input->post('index_block_id'));
                foreach ($cpost as $key => $value) {
                    $cpost[$key] = intval($value);
                }
                $cpost = count($cpost)>1?implode(',', $cpost):$cpost[0];
                $this->setting->set('index_block_id', $cpost);

            }
        }
        redirect('mocuz/client/index');
    }

    /**
     * [community 社区推荐]
     * @author [刘厚望]
     * @return [type] [description]
     */
    public function community()
    {
        $data['iconlist'] = get_icon_list();

        $data['posts_show_type'] = $this->setting->get('posts_show_type');//热点新闻区块
        $data['index_remove_block_id'] = $this->setting->get('index_remove_block_id');//热点新闻区块
        $data['hot_community_id']=$this->setting->get('hot_community_id');//热门帖bid
        $data['hot_community']=$this->setting->get('hot_community');//热门社区是否开启
        $data['hot_community_name']=$this->setting->get('hot_community_name');//客户端热门社区按钮的名字
        $result = $this->setting->get('recomm_forum');
        $recomm_forum = unserialize($result['value']);//推荐板块 序列化数组

        //重新排序
        $arr_nums=$arr=array();
        foreach($recomm_forum as $k=>$v){
            $arr_nums[$k]=$v['bid'];
        }
        asort($arr_nums);

        foreach($arr_nums as $k=>$v){
            $arr[$k]=$recomm_forum[$k];
        }

        $data['recomm_forum'] = empty($arr)?array():$arr;
		$this->load->view('common/innerheader');
		$this->load->view('admin/mocuz/community',$data);
	}




    /**
     * [delcomm 删除推荐板块]
     * @author [刘厚望]
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function delcomm()
    {
        $result = $this->setting->get('recomm_forum');
        $recomm_forum = unserialize($result['value']);//推荐板块 序列化数组

        $id = $this->input->post('id');
        foreach ($recomm_forum as $k=>$v) {
            if($v['bid'] == $id)
            {
                unset($recomm_forum[$k]);
            }
        }

        $data = serialize(array_values($recomm_forum));
        $this->setting->set('recomm_forum', $data);
        $this->json(1,'成功删除');
    }

    /**
     * [savecomm 社区推荐板块保存]
     * @author [刘厚望]
     * @return [type] [description]
     */
    public function savecomm()
    {
         //表单判断
        $hot_community_id=$this->input->post('hot_community_id');
        if(!preg_match('/^\d+$/', $hot_community_id))
            {
                echo "请输入正确正确热点社区ID";exit;
            }
        $hot_community=$this->input->post('hot_community');
        $hot_community_name=$this->input->post('hot_community_name');
        if(mb_strlen($hot_community_name)>5){
            echo "请输入正确正确热点社区名称";exit;
        }
        $this->setting->set('hot_community_id', $hot_community_id);
        $this->setting->set('hot_community', $hot_community);
        $this->setting->set('hot_community_name', $hot_community_name);
        $blockname = $this->input->post('blockname');
        foreach ( $blockname as $v) {
            if(!preg_match('/^([\x{4e00}-\x{9fa5}]|\w|-){1,6}$/u', $v))
            {
                echo "请输入正确的版块名";exit;
            }
        }
        $target = $this->input->post('target');
        foreach ($target as $k=>$v) {
            if(!preg_match('/^\d+$/', $v))
            {
                echo "请输入正确的目标数";exit;
            }
            $target[$k] = intval($v);
        }

        $bid = $this->input->post('bid');
        $icon = $this->input->post('icon');
        $data = array();
        foreach ($bid as $k => $v) {
            $data[$k]['bid'] = empty($v)?0:$v;
            $data[$k]['blockname'] = $blockname[$k];
            $data[$k]['icon'] = $icon[$k];
            $data[$k]['target'] = $target[$k];
        }

        $data = serialize($data);


        $cpost = explode(',', $this->input->post('index_remove_block_id'));
        foreach ($cpost as $key => $value) {
            $cpost[$key] = intval($value);
        }

        $cpost = array_unique(array_filter($cpost));
        asort($cpost);
        $cpost = !empty($cpost)?implode(',',$cpost):'';
        $this->setting->set('index_remove_block_id', $cpost);
        $this->setting->set('posts_show_type', $this->input->post('posts_show_type'));
        $this->setting->set('recomm_forum', $data);
        redirect('mocuz/client/community');
    }

    public function channel()
    {
        $data['iconlist'] = get_icon_list();

        $this->load->model('mocuz/Set_channel_model','set_channel');
        $data['channel'] = $this->set_channel->get();

        $data['goto'] = $this->channel;
		$this->load->view('common/innerheader');
		$this->load->view('admin/mocuz/channel',$data);
    }

    public function get_channel()
    {
        $this->load->model('mocuz/Set_channel_model','set_channel');
        $data = $this->set_channel->get_one($this->input->post('id'));
        echo json_encode($data);
    }

    public function save_channel($id="")
    {
        $title = trim($this->input->post("title"));
        $cid = trim($this->input->post("cid"));
        $goto_value = trim($this->input->post("goto_value"));
        $icon = trim($this->input->post("icon"));
        $requiredlogin = trim($this->input->post("requiredlogin"));

        if(! icon_size($icon, $this->icon_size))
        {
            echo $this->json(0,"图标必须{$this->icon_size[0]}*{$this->icon_size[1]}尺寸");exit;
        }
        if(!preg_match('/^([\x{4e00}-\x{9fa5}]|\w|-){1,5}$/u', $title))
        {
            echo $this->json(0,'请输入正确版块名');exit;
        }

        if(!preg_match('/^\d+$/', $cid))
        {
            echo $this->json(0,'请选择正确跳转分类');exit;
        }

        if($cid == 1001)
        {
            if(!preg_match('/^[a-zA-z]+:\/\/[^\s]*$/', $goto_value))
            {
                echo $this->json(0,'请填写正确ID/链接地址');exit;
            }
        }

        if($cid == 1003 || $cid == 2018)
        {
            if(!preg_match('/^\d+$/', $goto_value))
            {
               echo $this->json(0,'请填写正确ID/链接地址');exit;
            }
        }

        $this->load->model('mocuz/Set_channel_model','set_channel');
        if(empty($id))
        {
            $this->check_cid($cid);
            $this->set_channel->insert($this->input->post());
        }
        else
        {
           $data = $this->set_channel->get_one($id);
           if($data['cid'] != $cid)
           {
                $this->check_cid($cid);
           }
           $this->set_channel->update($this->input->post(),$id);
        }
        echo $this->json(1,'成功');
    }

    public function get_icon_size($icon)
    {

        if(! icon_size($icon, $this->icon_size))
        {
            echo $this->json(0,"图标必须{$this->icon_size[0]}*{$this->icon_size[1]}尺寸");
        }
        echo $this->json(1,'');
    }

    public function check_cid($cid)
    {
         $this->load->model('mocuz/Set_channel_model','set_channel');
         if(! $this->set_channel->check_cid($cid))
         {
             echo $this->json(0,'该入口已存在');exit;
         }
    }
    public function del_channel()
    {
        $this->load->model('mocuz/Set_channel_model','set_channel');
        $this->set_channel->del_channel($this->input->post('id'));
        echo $this->json(1,'成功');
    }
    public function sort_channel()
    {
        $this->load->model('mocuz/Set_channel_model','set_channel');
        $ordernum = $this->input->post('ordernum');
        $this->set_channel->update_order($ordernum);
        echo $this->json(1,'成功');
    }

    //发现频道管理（新）
    public function new_channel()
    {
        $data = array();
        $floor_channel = array();

        $data['iconlist'] = get_icon_list();

        $this->load->model('mocuz/Floor_model','floor_model');
        $floor = $this->floor_model->get();
        if (!empty($floor)) {
            foreach ($floor as $value) {
                $floor_channel[$value['id']]['id'] = $value['id'];
                $floor_channel[$value['id']]['name'] = $value['name'];
                $floor_channel[$value['id']]['ordernum'] = $value['ordernum'];
            }
            $this->load->model('mocuz/Discovery_channel_model','channel_model');
            $channel = $this->channel_model->get();
            foreach ($channel as $c) {
                if (!isset($floor_channel[$c['fid']]['channel'])) {
                    $floor_channel[$c['fid']]['channel'] = array();
                    $i = 1;
                } else {
                    $i = count($floor_channel[$c['fid']]['channel']) + 1;
                }
                $floor_channel[$c['fid']]['channel'][$i]['id'] = $c['id'];
                $floor_channel[$c['fid']]['channel'][$i]['cid'] = $c['cid'];
                $floor_channel[$c['fid']]['channel'][$i]['title'] = $c['title'];
                $floor_channel[$c['fid']]['channel'][$i]['goto_value'] = $c['goto_value'];
                $floor_channel[$c['fid']]['channel'][$i]['icon'] = $c['icon'];
                $floor_channel[$c['fid']]['channel'][$i]['requiredlogin'] = $c['requiredlogin'];
                $floor_channel[$c['fid']]['channel'][$i]['ordernum'] = $c['ordernum'];
                $floor_channel[$c['fid']]['channel'][$i]['flag'] = $c['flag'];
            }
        }
//        echo '<pre>';
//        print_r($floor_channel);
//        exit();

        $data['floor_channel'] = $floor_channel;
        $data['hot_flag'] = $this->hot_flag;
        $data['goto'] = $this->channel;
        $this->load->view('common/innerheader');
        $this->load->view('admin/mocuz/new_channel',$data);
    }

    /*public function sort_floor()
    {
        $id = $this->input->post('id');
        $data = array();
        $data['ordernum'] = $this->input->post('number');
        $this->load->model('mocuz/Floor_model', 'floor');
        $this->floor->update($data, $id);
        echo $this->json(1,'成功');
    }*/

    public function update_floor_name()
    {
        $this->load->model('mocuz/Floor_model', 'floor');
        $this->floor->update(
            array('name' => $this->input->post('fname')),
            $this->input->post('fid')
        );

        echo $this->json(1,'成功');
    }

    public function save_floor()
    {
        $data = array();
        $param = array();
        $param['name'] = trim($this->input->post("name"));
        $param['dateline'] = time();
        $this->load->model('mocuz/Floor_model','floor');
        $data['id_insert'] = $this->floor->insert($param);
        echo $this->json(1,'成功', $data);
    }

    public function del_floor()
    {
        $id = $this->input->post('id');
        //删除此楼层和该楼层下的所有入口
        $this->load->model('mocuz/Discovery_channel_model','discovery_channel');
        $this->discovery_channel->del_floor_channel($id);
        $this->load->model('mocuz/Floor_model','floor');
        $this->floor->del_floor($id);
        echo $this->json(1,'成功');
    }

    public function get_new_channel()
    {
        $this->load->model('mocuz/Discovery_channel_model','discovery_channel');
        $data = $this->discovery_channel->get_one($this->input->post('id'));
        echo json_encode($data);
    }

    public function save_new_channel($id="")
    {
        $data = array();
        $data['title'] = trim($this->input->post("title"));
        $data['cid'] = trim($this->input->post("cid"));
        $data['goto_value'] = trim($this->input->post("goto_value"));
        $data['icon'] = trim($this->input->post("icon"));
        $data['requiredlogin'] = trim($this->input->post("requiredlogin"));
        $data['fid'] = trim($this->input->post("fid"));

        if(! icon_size($data['icon'], $this->icon_size))
        {
            echo $this->json(0,"图标必须{$this->icon_size[0]}*{$this->icon_size[1]}尺寸");exit;
        }
        if(!preg_match('/^([\x{4e00}-\x{9fa5}]|\w|-){1,5}$/u', $data['title']))
        {
            echo $this->json(0,'请输入正确版块名');exit;
        }


        if(!preg_match('/^\d+$/', $data['cid']))
        {
            echo $this->json(0,'请选择正确跳转分类');exit;
        }

        if($data['cid'] == 1001)
        {
            if(!preg_match('/^[a-zA-z]+:\/\/[^\s]*$/', $data['goto_value']))
            {
                echo $this->json(0,'请填写正确ID/链接地址');exit;
            }
        }

        if($data['cid'] == 1003 || $data['cid'] == 2018)
        {
            if(!preg_match('/^\d+$/', $data['goto_value']))
            {
                echo $this->json(0,'请填写正确ID/链接地址');exit;
            }
        }

        $this->load->model('mocuz/Discovery_channel_model','discovery_channel');
        if(empty($id))
        {
            $this->check_new_cid($data['cid']);
            $this->discovery_channel->insert($data);
        }
        else
        {
            $res = $this->discovery_channel->get_one($id);
            if($res['cid'] != $data['cid'])
            {
                $this->check_new_cid($data['cid']);
            }
            $data['fid'] = $res['fid'];
            $this->discovery_channel->update($data,$id);
        }
        echo $this->json(1,'成功');
    }

    public function check_new_cid($cid)
    {
        $this->load->model('mocuz/Discovery_channel_model','discovery_channel');
        if(! $this->discovery_channel->check_cid($cid))
        {
            echo $this->json(0,'该入口已存在');exit;
        }
    }

    public function del_new_channel()
    {
        $this->load->model('mocuz/Discovery_channel_model','discovery_channel');
        $this->discovery_channel->del_channel($this->input->post('id'));
        echo $this->json(1,'成功');
    }

    public function sort_new_channel()
    {
        $channel_order = $this->input->post('channel_order');
        $floor_order = $this->input->post('floor_order');
        $this->load->model('mocuz/Discovery_channel_model','discovery_channel');
        $this->discovery_channel->update_order($channel_order);
        $this->load->model('mocuz/Floor_model','floor');
        $this->floor->update_order($floor_order);
        echo $this->json(1,'成功');
    }

    public function save_new_channel_flag()
    {
        $this->load->model('mocuz/Discovery_channel_model', 'discovery_channel');
        $this->discovery_channel->update(
            array('flag' => $this->input->post('flag')),
            $this->input->post('id')
        );

        echo $this->json(1,'成功');
    }


    /**
     * [about_us 关于我们]
     * @author  [冯阳]
     * @return [type] [description]
     */
    public function about_us()
    {
        $result = $this->setting->get('about_us');
        $data['about_us']=$result['value'];
        $data['title'] = '关于我们';
        $this->data['now'] = 'about_us';
        $this->load->view('common/innerheader',$data);
        $this->load->view('admin/mocuz/about_us',$data);
    }

    /**
     * [about_us_set 关于我们设置]
     * @author [冯阳]
     * @return [type] [description]
     */
    public function about_us_set()
    {
        $header_name=trim($this->input->post('message'));

        $result = $this->setting->set('about_us',$header_name);
        redirect("mocuz/client/about_us");
    }

    /**
     * [disclaimer 免责声明]
     * @author [冯阳]
     * @return [type] [description]
     */
    public function disclaimer()
    {
        $result = $this->setting->get('disclaimer');
        $data['disclaimer']=$result['value'];
        $this->load->view('common/innerheader',$data);
        $this->load->view('admin/mocuz/disclaimer',$data);
    }

    /**
     * [disclaimer_set 免责声明设置]
     * @author [冯阳]
     * @return [type] [description]
     */
    public function disclaimer_set()
    {
        $header_name=$this->input->post('message') ;
        $result = $this->setting->set('disclaimer',$header_name);
        redirect("mocuz/client/disclaimer");
    }
    /**
     * [upload 关于我们图片上传]
     */
    public function upload(){
        if ($_FILES['file']['name']) {
            if (!$_FILES['file']['error']) {
                $name = md5(rand(100, 200));
                $ext = explode('.', $_FILES['file']['name']);
                $filename = $name . '.' . $ext[1];
                $types =array('gif','jpg','png');
                if(!in_array($ext[1],$types))
                {
                    echo  $message = '请上传正确格式图片';exit;
                }
                $destination =FCPATH.'uploads/mocuz/' . $filename; //change this directory
                $location = $_FILES["file"]["tmp_name"];
                move_uploaded_file($location, $destination);

                $url = 'uploads/mocuz/'.$filename;
                echo $url;
            }
            else
            {
              echo  $message = '请上传正确格式图片';
            }
        }

    }


    public function threadsort(){

        $this->load->helper('discuz_helper');
        $data['forums'] = get_forums();

        $this->load->view('common/innerheader');
        $this->load->view('admin/mocuz/threadsort',$data);
    }

    public function threadsort_types($fid){
        $this->load->helper('discuz_helper');
        $result['option'] = get_forums_type($fid);
        $this->load->model('mocuz/Threadsrot_model', 'threadsort');
        $result['data'] = $this->threadsort->get_by_fid($fid);
        echo json_encode($result);
    }

    public function threadsrot_save(){

        $fid = $this->input->post('fid');
        $sortid = $this->input->post('sortid');
        $template = $this->input->post('template');
        $pic_type = $this->input->post('pic_type');
        $fields = $this->input->post('fields');
        $fields['pics'] = $pic_type;

        $this->load->model('mocuz/Threadsrot_model', 'threadsort');
        if($template == 7){
            $this->threadsort->delete_by_fid($fid);
        }

        if($template == 8 || $template == 9){

            $this->threadsort->update_sort($fid, $sortid,$template, $pic_type, $fields);
        }
    }
}
