<?php

class FeedController extends Zend_Controller_Action
{	
   
	public function init()
	{	
		$this->smarty=Zend_Registry::get('smarty');
		$this->config=Zend_Registry::get('config');
		
		$this->smarty->assign('baseUrl',$this->_request->getBaseUrl());
		
	}
	
	public function indexAction()
	{
				
    	$feed=new Feed();
		$db = $feed->getAdapter();
	
       $query="SELECT * FROM `".TABLE_PRE."feed`";
        
        $page=$this->_request->getParam('page');
	
		$condition=Null;
		$sortby="  `".TABLE_PRE."feed`.id DESC ";
		Zend_Loader::loadClass('Custom_Controller_Plugin_Pager');
		$basePageIndex=0;
		$Pagers = new Custom_Controller_Plugin_Pager($db,$page,'10',$condition,$sortby,$basePageIndex,$query) ;
		$pageInfo = $Pagers->getNavBar(WEB_ROOT.'feed/index/');
		$this->smarty->feeds = $Pagers->findAll();
		
		//取到友情链接
		$weblink=new Weblink();
		$arrLinks=$weblink->fetchAll(TABLE_PRE."weblink.show='1'",TABLE_PRE."weblink.id DESC",null)->toArray();
		$this->smarty->assign('arrLinks',$arrLinks);
		//取到最热日志
		$article=new Article();
		$topArticle=$article->fetchAll(TABLE_PRE."article.show='1'",TABLE_PRE."article.viewnum  DESC",10)->toArray();
		$ArticleNums=count($article->fetchAll()->toArray());
		$this->smarty->assign('ArticleNums',$ArticleNums);
		$this->smarty->assign('topArticle',$topArticle);
		
		//取到最新留言
		$message=new Message();
		$topMessage=$message->fetchAll(TABLE_PRE."message.show='1' and ".TABLE_PRE."message.pid='0' ","id DESC",10)->toArray();
		$MessageNums=count($message->fetchAll()->toArray());
		$this->smarty->assign('MessageNums',$MessageNums);
		$this->smarty->assign('topMessage',$topMessage);
		
		//取到最新评论
		$comment=new Comment();
		$topComment=$comment->fetchAll(TABLE_PRE."comment.show='1'",TABLE_PRE."comment.id  DESC",10)->toArray();
		$CommentNums=count($comment->fetchAll()->toArray());
		$this->smarty->assign('CommentNums',$CommentNums);
		$this->smarty->assign('topComment',$topComment);
		//取到日志分类
		$articleclass=new Articleclass();
		$topClass=$articleclass->fetchAll(TABLE_PRE."articleclass.pid='0'")->toArray();
		$this->smarty->assign('topClass',$topClass);
		
		//取到标签数量
		$taguery="SELECT ".TABLE_PRE."itemtags . * , ".TABLE_PRE."tags.name FROM ".TABLE_PRE."itemtags LEFT JOIN ".TABLE_PRE."tags ON ".TABLE_PRE."tags.id = ".TABLE_PRE."itemtags.tagid WHERE ".TABLE_PRE."itemtags.type = 'article'";
    	$itemtags=new Itemtags();		
		$dbtags = $itemtags->getAdapter();
		$arrTags=$dbtags->fetchAll($taguery);
		$this->smarty->assign('arrTags',$arrTags);
		$this->smarty->assign('arrFeeds',$this->smarty->feeds);
    	$this->smarty->assign("pageInfo",$pageInfo);
		$this->smarty->assign('title','default');
		$this->smarty->display('header.phtml');  
		$this->smarty->display('feed/index.phtml');
		$this->smarty->display('footer.phtml');
	
	}
	
	public function feedshowAction()
	{
		$article=new Article();
    	$comment=new Comment();
		$db = $article->getAdapter();
		$feed=new Feed();
		$dbfeed = $feed->getAdapter();
		
  		Zend_Loader::loadClass('Zend_Filter_StripTags');
		$filter=new Zend_Filter_StripTags();
		$id=$filter->filter(($this->_request->getParam('id')));
        $query="SELECT * FROM `".TABLE_PRE."feed` WHERE ".TABLE_PRE."feed.id =$id";
		$Articleone=$dbfeed->fetchAll($query);
		//取前一篇,后一篇日志
		$Sqlone=" SELECT id,title  FROM `".TABLE_PRE."feed` WHERE `id` > $id ORDER BY id DESC LIMIT 0 , 1 ";
		$Sqltwo="SELECT id,title FROM `".TABLE_PRE."feed` WHERE `id` < $id ORDER BY id DESC LIMIT 0 , 1 ";
		$ArticleForword=$dbfeed->fetchAll($Sqltwo);
		$this->smarty->assign('ArticleForword',$ArticleForword);
		$ArticleBack=$dbfeed->fetchAll($Sqlone);
		$this->smarty->assign('ArticleBack',$ArticleBack);
		/*取到评论*/
		$page=$this->_request->getParam('page');
		$condition="".TABLE_PRE."comment.commentid=$id and ".TABLE_PRE."comment.type='feed'";
		$sortby="".TABLE_PRE."comment.id DESC";
		Zend_Loader::loadClass('Custom_Controller_Plugin_Pager');
		$basePageIndex=0;
		$Pagers = new Custom_Controller_Plugin_Pager($comment,$page,'8',$condition,$sortby,$basePageIndex) ;
		
		$pageInfo = $Pagers->getNavBar(WEB_ROOT.'feed/feedshow/id/'.$id.'/');
		$arrComments = $Pagers->findAll()->toarray();
		$comentsum=$Pagers->totalCount;
		
		//取到随机日志
		$randArticle=$feed->fetchAll("".TABLE_PRE."feed.id !=$id",'RAND()',8)->toArray();
		$this->smarty->assign('randArticle',$randArticle);
		$ArticleNums=count($article->fetchAll()->toArray());
		$this->smarty->assign('ArticleNums',$ArticleNums);
		
		
		
		
		$this->smarty->assign('comentsum',$comentsum);
		$this->smarty->assign('arrComments',$arrComments);
    	$this->smarty->assign("pageInfo",$pageInfo);
		//取到友情链接
		$weblink=new Weblink();
		$arrLinks=$weblink->fetchAll(TABLE_PRE."weblink.show='1'",TABLE_PRE."weblink.id DESC",10)->toArray();
		$this->smarty->assign('arrLinks',$arrLinks);
		//取到最热日志
		$topArticle=$article->fetchAll(TABLE_PRE."article.show='1'",TABLE_PRE."article.viewnum  DESC",10)->toArray();
		$this->smarty->assign('topArticle',$topArticle);
		
		//取到最新留言
		$message=new Message();
		$topMessage=$message->fetchAll(TABLE_PRE."message.show='1' and ".TABLE_PRE."message.pid='0' ","id DESC",10)->toArray();
		$this->smarty->assign('topMessage',$topMessage);
		$MessageNums=count($message->fetchAll()->toArray());
		$this->smarty->assign('MessageNums',$MessageNums);
		//取到访问量
		$count=new Count();
		$SeeNums=count($count->fetchAll()->toArray());
		$this->smarty->assign('SeeNums',$SeeNums);
		
		//取到最新评论
		$topComment=$comment->fetchAll(TABLE_PRE."comment.show='1'",TABLE_PRE."comment.id  DESC",10)->toArray();
		$this->smarty->assign('topComment',$topComment);
		$CommentNums=count($comment->fetchAll()->toArray());
		$this->smarty->assign('CommentNums',$CommentNums);
		
		//取到日志分类
		$articleclass=new Articleclass();
		$topClass=$articleclass->fetchAll(TABLE_PRE."articleclass.pid='0'")->toArray();
		$this->smarty->assign('topClass',$topClass);
		//取到标签数量
		$taguery="SELECT ".TABLE_PRE."itemtags . * , ".TABLE_PRE."tags.name FROM ".TABLE_PRE."itemtags LEFT JOIN ".TABLE_PRE."tags ON ".TABLE_PRE."tags.id = ".TABLE_PRE."itemtags.tagid WHERE ".TABLE_PRE."itemtags.type = 'article'";
    	$itemtags=new Itemtags();		
		$dbtags = $itemtags->getAdapter();
		$arrTags=$dbtags->fetchAll($taguery);
		$this->smarty->assign('arrTags',$arrTags);
		
		$this->smarty->assign("offsetnums",$Pagers->offsetnums);
    	$this->smarty->assign('Articleone',$Articleone);
		$this->smarty->assign('id',$id);
		$this->smarty->display('header.phtml');  
		$this->smarty->display('feed/feedshow.phtml');
		$this->smarty->display('footer.phtml');
	}
	
   public function commentAction()
    {
    	require_once 'Zend/Session/Namespace.php';
        if($this->_request->isPost()){
    		Zend_Loader::loadClass('Zend_Filter_StripTags');
    		$filter=new Zend_Filter_StripTags();
    		$username=$filter->filter($this->_request->getPost('username'));
    		$email=$filter->filter($this->_request->getPost('email'));
    		$content=$filter->filter($this->_request->getPost('content'));
    		$commentid=$filter->filter($this->_request->getPost('articleId'));
    		$headimg=$filter->filter($this->_request->getPost('headimg'));
    		$imgcode=$filter->filter($this->_request->getPost('imgcode'));
    		$authCode = new Zend_Session_Namespace('Auth_Code');
    		
        	$comment=new Comment();
        	$db=$comment->getAdapter();
        	
    		if($authCode->imagecode==$imgcode){
	    		if($username!=''&&$email!=''&&$commentid!=''&&$content!=''){
					require_once 'Zend/Validate/EmailAddress.php';
					$validator = new Zend_Validate_EmailAddress();
					if ($validator->isValid($email)) {
						Zend_Loader::loadClass('Custom_Controller_Plugin_IpLocation');
				    	Zend_Loader::loadClass('Custom_Controller_Plugin_Ipaddress');
				    	$Ipaddress=new Custom_Controller_Plugin_Ipaddress();
				    	$IP=$Ipaddress->getIP();
				    	$dir= ROOT_DIR."/blog/public/QQWry2.Dat";
				    	$iplocation=new Custom_Controller_Plugin_IpLocation($dir);
						$location=$iplocation->getlocation($IP);
						$address=iconv("gb2312","utf-8",$location['country']);
						$data=array(
						'username'=>$username,
		    			'pid'=>'0',
						'email'=>$email,
						'commentid'=>$commentid,
						'headimg'=>$headimg,
						'show'=>'1',
						'content'=>$content,
						'type'=>'feed',
		    			'ip'=>$IP,
		    			'address'=>$address,
						'addtime'=>time(),
						'updatetime'=>time()
						);
						$comment->insert($data);
						$db->lastInsertId();
						unset($data);
						//echo "您的评论发表成功!";
						
						/*取到评论*/
						$comment=new Comment();
						$page=$this->_request->getParam('page');
						$condition="".TABLE_PRE."comment.commentid=$commentid and ".TABLE_PRE."comment.type='feed'" ;
						$sortby="".TABLE_PRE."comment.id DESC";
						Zend_Loader::loadClass('Custom_Controller_Plugin_Pager');
						$basePageIndex=0;
						$Pagers = new Custom_Controller_Plugin_Pager($comment,$page,'8',$condition,$sortby,$basePageIndex) ;
						
						$pageInfo = $Pagers->getNavBar(WEB_ROOT.'feed/feedshow/id/'.$commentid.'/');
						$this->smarty->comments = $Pagers->findAll();
						$this->smarty->assign('arrComments',$this->smarty->comments->toArray());
				    	$this->smarty->assign("pageInfo",$pageInfo);
				    	$this->smarty->assign('flag','0');
				    	$this->smarty->assign('message','您的评论发表成功!');
				    	$this->smarty->display('feed/comment.phtml');
		
					} else {
					    //echo "对不起!您填写有电子邮箱地址有误!";
					    $this->smarty->assign('flag','5');
				    	$this->smarty->assign('message','对不起!您填写有电子邮箱地址有误!');
					    $this->smarty->display('feed/comment.phtml');
					}
						
	    		}elseif($username==''){
	    			//echo "对不起!您的大名不能为空!";
	    			$this->smarty->assign('flag','1');
				    $this->smarty->assign('message','对不起!您的大名不能为空!');
	    			$this->smarty->display('feed/photo.phtml');
	    		}elseif($commentid==''){
	    			//echo "对不起!评论文章编号不能为空!";
	    			$this->smarty->assign('flag','2');
				    $this->smarty->assign('message','对不起!评论文章编号不能为空!');
	    			$this->smarty->display('feed/comment.phtml');
	    		}elseif($content==''){
	    			//echo "对不起!您填写的评论内容不能为空!";
	    			$this->smarty->assign('flag','3');
				    $this->smarty->assign('message','对不起!您填写的评论内容不能为空!');
	    			$this->smarty->display('feed/comment.phtml');
	    		}
    		}else{
    			$this->smarty->assign('flag','4');
				$this->smarty->assign('message','对不起!您填写的验证码错误或超时!');
    			//echo "对不起!您填写的验证码错误或超时!";
    			$this->smarty->display('feed/comment.phtml');
    		}
        }else{
        	
        	$this->smarty->display('login.phtml');
        }
       
    }
    
}