<?php
/**
 * 微动CMS
 * ============================================================================
 * * 版权所有 青岛微动一百网络科技有限公司，并保留所有权利。
 * 网站地址: http://bbs.weidong100.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 更多系统：shopNR本地电商系统、微速卖-云外卖系统
 * 
*/
class IndexAction extends Action {

	public function _initialize(){
		//读取通用导航栏和友情链接
		//导航栏读取
		$Nav = M('Nav');
		$nav_data = $Nav->order('sort desc')->where('parent_id=0')->select();
		foreach ($nav_data as $key => $value) {
			$nav_data[$key]['sub'] = $Nav->order('sort desc')->where(array('parent_id'=>$value['nav_id']))->select();
		}
		$this->assign('nav',$nav_data);
		//友情链接
		$Link = M('Link');
		$link_data = $Link->order('sort desc')->select();
		$this->assign('link',$link_data);
		//站点配置相关
		$Config= M('config');
		$confi_data = $Config->select();
		foreach($confi_data as $ck=>$cv){
		    if($cv['name']=='site_admin'  or $cv['name']=="site_password"){
		        
		    }else{
		       $cdata[$cv['name']]=$cv['value']; 
		    }
		}
		define('article_list_num',$cdata['article_list_num']);   //文章列表每页数量
		define('photo_list_num',$cdata['photo_list_num']);       //相册列表每页数量
		define('photo_num',$cdata['photo_num']);                 //照片列表每页数量
		define('video_list_num',$cdata['video_list_num']);       //视频列表每页数量
		define('index_album_num',$cdata['index_album_num']);       //首页相册数量
		$this->assign('config_data',$cdata);
		//联系QQ客服
		$Cus = M('Cus');
		$cus_data = $Cus->order('sort desc')->select();
		$this->assign('cus',$cus_data);
		
	}
	/**
	 * 首页
	 */
	public function index(){
		//获取幻灯片
		$Slide = M('Slide');
		if(is_mobile()){
	      $slide_type = 1;
	    }else{
	      $slide_type = 0;
	    }
		$slide_data = $Slide->where('is_mobile = "'.$slide_type.'"')->order('sort desc')->select();
		$this->assign('slides',$slide_data);
		//公司简介
		$Page = M('Page');
		$profile = $Page->where('title="公司简介"')->find();
		$this->assign('profile',$profile);
		//文章列表
		$Article = M('Article');
		$article_data = $Article->order('sort desc')->limit(12)->select();
		
    	foreach($article_data as $k2=>$v2){
    	           $article_data[$k2]['content']=$this->cutArticle($article_data[$k2]['content'],300);
    	}
		$this->assign('article',$article_data);
		//推荐相册展示
		$Photo = M('Photo');
		$album = $Photo->order('sort desc')->where('category=0 and is_recommend=1')->limit(index_album_num)->select();
		$this->assign('album',$album);
		//推荐照片展示
		$photo = $Photo->order('sort desc')->where('category>0 and is_recommend=1')->limit(20)->select();
		$this->assign('photo',$photo);
		$this->assign_ur_here('首页',array(array('title'=>'首页','url'=>U('Index/index'))));
		$this->display();
	}
    //文章列表
	public function articlelist(){
	    $Cate = M('Cate');
		$Article = M('Article');
		if($_GET['cate_id']){
		   $title = $Cate->where('cate_id='.$_GET['cate_id'])->getField('title');
		   $map['cate']  = array('eq',$_GET['cate_id']);
		   $this->assign('cate_id',$_GET['cate_id']);
	    }else{
	       $map['cate']  = array('neq','');
	       $title = '最新文章';
	    }
		import('ORG.Util.Page');
	    $count  = $Article->where($map)->count();
	    $Page  = new Page($count,article_list_num);
	    $this->page_type($Page,'篇文章');
	    $show   = $Page->show();
	    $list = $Article->where($map)->order('time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
	    foreach($list as $k=>$v){
	       $list[$k]['content']=$this->cutArticle($v['content'],270);
	    }
		$this->assign('list',$list);
		$article_cate = $Cate->order('sort desc')->select();
		$data=$this->cate($article_cate);
		$this->assign('article_cate',$data);
        $this->assign_ur_here($title,array(array('title'=>$title,'url'=>U('Index/articlelist'))));
        $this->assign('page',$show);
        $this->assign('title',$title);
		$this->display();
	}
	public function marticlelist(){
	    
	    if(I('get.cate_id')){
	        $Cate = M('Cate');
	        $title = $Cate->where(array('cate_id'=>I('get.cate_id')))->getField('title');
	        $Article = M('Article');
	        $map['cate']  = array('eq',$_GET['cate_id']);
	        import('ORG.Util.Page');
	        $count  = $Article->where($map)->count();
	        $Page  = new Page($count,article_list_num);
	        $this->page_type($Page,'篇文章');
	        $show   = $Page->show();
	        $list = $Article->where($map)->limit($Page->firstRow.','.$Page->listRows)->select();
		    $this->assign('data',$list);
		    $this->assign('page',$show);
	    }
	    $this->assign_ur_here($title,array(array('title'=>$title,'url'=>U('Index/marticlelist'))));
	    $this->display();
	    
	    
	}
	//列表拼接输入sql结果,返回拼接好的分类列表
    public function cate($article_cate){
        
		foreach($article_cate as $k =>$v){
		    if($v['parent_id']!=0){
	
		        $data[$v['parent_id']]['sub'][]=$v;
		    }else{
		        $data[$k]=$v;
		    }
		    
		}
	
		foreach($data as $kk=>$vv){
		    if(!is_array($vv['sub'])){
		        $data[$kk]['sub']=$data[$vv['cate_id']]['sub'];
		        unset($data[$vv['cate_id']]);
		    }
		}
		return $data;
        
    }

	public function article(){
	    $Cate = M('Cate');
		if(I('get.id')){
			if(!PosInt(I('get.id'))){
				$this->assign('article','查询出错');
				exit();
			}
			$Article = M('Article');
			if($article_data = $Article->where(array('article_id'=>I('get.id')))->find()){
			    $this->assign('article',$article_data);
			    $cate1=$Cate->where(array('cate_id'=>$article_data['cate']))->find();
			    $cate2=$Cate->where(array('cate_id'=>$cate1['parent_id']))->find();

			    //其他文章
			    $data1= $Article->limit(8)->field('article_id,title')->select();
    		    $this->assign('data1',$data1);
    		    //文章点击数增加1
    		    $Article->where(array('article_id'=>I('get.id')))->setInc('number'); 
			}else{
				$this->assign('article','文章不存在！');
			}
		}else{
			$this->assign('article','来源错误！');
			exit;
		}
		$article_cate = $Cate->where('parent_id=0')->select();
		$this->assign('article_cate',$article_cate);
		$this->assign_ur_here($article_data['title'],array(array('title'=>$cate2['title'],'url'=>U('Index/articlelist',array('cate_id'=>$cate2['cate_id']))),array('title'=>$cate1['title'],'url'=>U('Index/articlelist',array('cate_id'=>$cate1['cate_id']))),array('title'=>$article_data['title'],'url'=>U('Index/article',array('article_id'=>$article_data['article_id'])))));
		$this->assign('seo_keywords',$article_data['seo_keywords']);
		$this->assign('seo_description',$article_data['seo_description']);
		$this->display();
	}

    //相册列表
	public function photolist(){
		$Photo = M('Photo');
		$map['category']  = array('eq',0); 
        import('ORG.Util.Page');
        $count  = $Photo->where($map)->count();
        $Page  = new Page($count,photo_list_num);
        $this->page_type($Page,'个相册');
        $show   = $Page->show();
        $list = $Photo->where($map)->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('photolist',$list);
		$this->assign('page',$show);
   		$this->assign_ur_here('相册列表',array(array('title'=>'相册列表','url'=>U('Index/photolist'))));
		$this->display();
	}

    //相册展示
	public function photo(){
		if(I('get.pid')){
			if(!PosInt(I('get.pid'))){
				$result['title'] = '抱歉，找不到内容！';
			}else{
				$Photo = M('Photo');
				$result = $Photo->where('photo_id='.I('get.pid'))->find();
				$this->assign('result',$result);
				
                $map['category']  = array('eq',I('get.pid')); 
                import('ORG.Util.Page');
                $count  = $Photo->where($map)->count();
                $Page  = new Page($count,photo_list_num);
                $this->page_type($Page,'张照片');
                $show   = $Page->show();
                $list = $Photo->where($map)->limit($Page->firstRow.','.$Page->listRows)->select();
        		$this->assign('list',$list);
        		$this->assign('page',$show);
                
				//相册分类输出
				$photo_cate = $Photo->where('category=0')->select();
				$this->assign('photo_cate',$photo_cate);
			}
		}else{
			$result['title'] = '无内容！';
			$this->assign('result',$result);
		}
		$this->assign_ur_here($result['title'],array(array('title'=>'相册列表','url'=>U('Index/photolist')),array('title'=>$result['title'],'url'=>U('Index/photo',array('pid'=>$result['photo_id'])))));
		$this->display();
	}
	
	//视频列表
	public function videolist(){
		$Video = M('Video');
        import('ORG.Util.Page');
        $count  = $Video->count();
        $Page  = new Page($count,photo_list_num);
        $this->page_type($Page,'个视频');
        $show   = $Page->show();
        $list = $Video->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('videolist',$list);
		$this->assign('page',$show);
		$this->assign_ur_here('视频列表',array(array('title'=>'视频列表','url'=>U('Index/videolist'))));
		$this->display();
	}
	//视频展示
	public function video(){
		if(I('get.vid')){
			if(!PosInt(I('get.vid'))){
				$result['title'] = '抱歉，找不到内容！';
			}else{
				$Video = M('Video');
				$video_data = $Video->where('video_id='.I('get.vid'))->find();
				$this->assign('video',$video_data);
				//视频列表输出
				$video_cate = $Video->Limit(5)->select();
				$this->assign('video_cate',$video_cate);
			}
		}else{
			$result['title'] = '无内容！';
			$this->assign('result',$result);
		}
		$this->assign_ur_here($video_data['title'],array(array('title'=>'视频列表','url'=>U('Index/videolist')),array('title'=>$video_data['title'],'url'=>U('Index/video',array('vid'=>$video_data['video_id'])))));
		$this->display();
	}
	 
	
	
	//单页展示
	public function page(){
	    $Pagesql=M('Page');
	    if(empty($_GET['page_id'])){
	        $page_data= $Pagesql->where('type=0')->order('sort desc')->select();
		    $this->assign('page_data',$page_data);
	    	$this->assign_ur_here('单页',array(array('title'=>'单页','url'=>U('Index/page'))));
	    	$this->display();
	    }else{
	        $page_data= $Pagesql->where('page_id='.$_GET['page_id'])->find();
		    $this->assign('page_data',$page_data);
	    	$this->assign_ur_here($page_data['title'],array(array('title'=>$page_data['title'],'url'=>U('Index/page',array('page_id'=>$page_data['page_id'])))));
	    	$this->assign('seo_keywords',$page_data['seo_keywords']);
	    	$this->assign('seo_description',$page_data['seo_description']);
	    	$this->display('subject');
	    }
	}
	//手机的单页展示
	public function mpage(){
	    if(I('get.pid')){
			if(!PosInt(I('get.pid'))){
				$result['title'] = '抱歉，找不到内容！';
			}else{
				$Pagesql = M('Page');
				$page_data = $Pagesql->where(array('page_id'=>I('get.pid')))->find();
				$this->assign('page_data',$page_data);
			}
		}else{
			$result['title'] = '无内容！';
			$this->assign('result',$result);
		}
		$this->assign_ur_here('单页',array(array('title'=>'单页','url'=>U('Index/page'))));
		$this->display();
	}

	/**
	 * 文章摘要截取方法
	 */
	private function cutArticle($data,$cut=0,$str="...."){

		$data=strip_tags($data);	//去除html标记
		$pattern = "/&[a-zA-Z]+;/";	//去除特殊符号
		$data=preg_replace($pattern,'',$data);
		if(!is_numeric($cut)){
			return $data;
		}
		if($cut>0){
			$data=mb_strimwidth($data,0,$cut,$str,'UTF-8');
			return $data;
		}
		
	}
	
	//获取面包屑标题
	protected function assign_ur_here($title,$bread){
	   //$bread  是一个数组   array   $bread = array('title'=>'ThinkPHP','url'=>'ThinkPHP@gmail.com');
	   $breadcrumb = "<a href=".U('Index/index').">首页</a>";
	   foreach($bread as $key=>$v){
	      $breadcrumb.= " >> <a href='".$v['url']."'>".$v['title']."</a>";
	   }
	   $this->assign('breadcrumb',$breadcrumb);
	   $this->assign('seo_title',$title);
    }
    
    //切换手机和电脑端的分页效果
	protected function page_type($Page,$header){
	   if(is_mobile()){
	      $Page->setConfig('theme'," %upPage% %nowPage%/%totalPage% 页 %downPage% %totalRow% ".$header);
	   }else{
	      $Page->setConfig('header',$header);
	   }
    }
}