<?php
// +----------------------------------------------------------------------
// | WindSNS 国内领先企业级社交开源SNS框架系统
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://windsns.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 常州微诺信息科技有限公司 http://www.weinuo.net
// +----------------------------------------------------------------------
// | E-mail: open@weinuo.net
// +----------------------------------------------------------------------
namespace app\index\controller;
use app\common\controller\Frontend;
use think\Config;
use think\Hook;
use think\Db;
use think\Request;
use think\Session;


class Circle extends Frontend
{

    protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }
	/**
	 * 协议
	 * @author   Devil
	 * @blog     http://www.weinuo.net
	 * @version  0.0.1
	 * @datetime 2020-03-04
	 */
    public function index()
    {
    	$this->view->assign('title', __('圈子'));

        $circle_grouplist = Db::name('circle_group')-> where(['circle_state'=>1,'circle_open_state'=>1])-> order('wscircleid', 'desc')->paginate(10); //圈子列表
        $page = $circle_grouplist->render();
    	$this->assign('circle_grouplist', $circle_grouplist);
        return $this->view->fetch();
    }


    public function show()
    {
    	$this->view->assign('title', __('圈子频道'));

        $getcirclestateype  =  input('get.stateype'); 
    	$getcircleid  =  input('get.circleid'); 

        $sqtypeid   = Db::name('circle_group')->where(['wscircleid'=>$getcircleid])->find();
        $this->assign('sqtypeid', $sqtypeid);

    	$sqkaiqiid = Db::name('circle_group')->where(['circle_open_state'=>$getcirclestateype])->find();



        $windsns_contentlunsum = Db::name('circlecontent')
            ->where('circleid', $getcircleid)
            ->count();
           $this->assign('windsns_contentlunsum', $windsns_contentlunsum);




	$windsns_circlegroup = Db::name('circle_group')->where(['wscircleid'=>$getcircleid])->select();
        $this->assign('windsns_circlegroup', $windsns_circlegroup);

        $circlecontentlists = Db::name('circlecontent')->alias('a')->join('ws_circle_group w','a.circleid = w.wscircleid')-> join('ws_user b','a.user_id = b.id')-> where(['circleid'=>$getcircleid,'ws_circlecontent.circle_state'=>1])-> order('aid', 'desc')->paginate(50,false,['query' => request()->param()]); //帖子列表
        $page = $circlecontentlists->render();
    	$this->assign('circlecontentlists', $circlecontentlists);
        $this->assign('page', $page);





        if(!$sqkaiqiid['circle_open_state'] == 1){

    	echo'抱歉！该圈子已经关闭！';
    	exit;
        }elseif(!$sqtypeid){

        	echo'抱歉！该圈子不存在！';
        	exit;
        }elseif($windsns_contentlunsum > 1 or $windsns_contentlunsum == 1){


    

        return $this->view->fetch('windsns_circle');

        }else{
        	
        	return $this->view->fetch('windsns_nocircle');
        }


    }




 public function content()
    {


        $getcircleid  =  input('get.circleid');
        $getcontentid  =  input('get.contentid'); 
        $this->assign('getcontentid', $getcontentid); 



        $sqtypeid   = Db::name('circle_group')->where(['wscircleid'=>$getcircleid])->find();//查询该圈子信息
        $this->assign('sqtypeid', $sqtypeid);

        $sqconteninfos   = Db::name('circlecontent')->where(['aid'=>$getcontentid])->find();//查询该帖子信息
        $this->assign('sqconteninfos', $sqconteninfos);
        $tiezilittle = $sqconteninfos['subject'];
        $this->view->assign('title', __($tiezilittle));


         $windsns_contentinfos = Db::name('circlecontent')->alias('a')-> join('ws_user b','a.user_id = b.id')-> where(['circleid'=>$getcircleid,'ws_circlecontent.circle_state'=>1,'ws_circlecontent.aid'=>$getcontentid])-> select(); //帖子内容打印


		$content = Db::name('circlecontent') -> where('aid', $getcontentid) -> find();//打印帖子内容
		$content = htmlspecialchars_decode($content['content']);

		$this->assign('content', $content);
		$this->assign('windsns_contentinfos', $windsns_contentinfos);
		
		

        $sqlliulan = Db::name('circlecontent')->where('aid',$getcontentid)->setInc('circle_infosliulanshu', 1);//浏览次数+1


        $tiezi_userid = Db::name('circlecontent') -> where('aid', $getcontentid) ->value('user_id');//打印这个帖子用户ID

        
        
        $thistieziuserid   = Db::name('user')-> where('id',$tiezi_userid)->select();
        $this->assign('thistieziuserid', $thistieziuserid);//赋值到页面
        $tiezisum = Db::name('circlecontent')->where(['user_id' => $tiezi_userid,'circle_state' => 1])->count();// 这个用户发布发帖子总数,发帖状态1正常
  	    $this->assign('tiezisum', $tiezisum);

        $user_content = Db::name('circlecontent') -> where('user_id', $tiezi_userid) -> order('aid', 'desc')->paginate(10); ;//打印这个用户下的帖子内容
        $page = $user_content->render();
    	$this->assign('user_content', $user_content);
        $this->assign('page', $page);

		$mysqlReplies = Db::name('circlehuifu')
		    ->where('contentid', $getcontentid)
		    ->alias('a')-> join('ws_user b','a.user_id = b.id')
		    ->order('circlehuifu_id', 'asc')
		    ->select();

		$replies = [];
		foreach ($mysqlReplies as $row) {
		    $key = $row['circlehuifu_id'];
		    if ($row['user_fid'] == 0) {
		        $replies[$key] = $row;
		    } else {
		        $replies[$row['user_fid']]['floor'][$key] = $row;
		    }
		}

		$this->assign('replies', $replies);
		
		

		
		$huihuauserid = $this->auth->getUserinfo()['user_id'];



        if(!$sqconteninfos){

        	echo'抱歉！该帖子不存在！';
        	exit;
        }elseif(!$sqtypeid){

        	echo'抱歉！该圈子不存在！';
        	exit;
        }elseif($tiezi_userid == $huihuauserid){  

           return $this->view->fetch('windsns_usercontent');
        }else{
        	
        	
        	return $this->view->fetch('windsns_content');
        }

    }




  //帖子删除执行--同时删除这个帖子的附件表
  
  
  
     
    public function user_deldo() 
    {
        $gettieziid  =  input('get.circletiezi'); 
        $tiezi_circleid = Db::name('circlecontent') -> where('aid', $gettieziid) ->value('circleid');
         
  
       $shanchua = Db::name('circlecontent')->where('aid',$gettieziid)->delete(); 
       $shanchub = Db::name('circleattachment')->where('aid',$gettieziid)->delete(); 
       $shanchuc = Db::name('circlehuifu')->where('contentid',$gettieziid)->delete(); 
    
    
    
         if($shanchua){
               return $this->view->fetch('index@circlepost/circlepost_delok');

               
         }else{

echo 'no';
			exit;
         }
         
         
         
         
   }
  
  























}
