<?php
require_once 'Zend/Controller/Action.php';
class SearchController 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()
    {	
    	
    	
    	Zend_Loader::loadClass('Zend_Filter_StripTags');
		$filter=new Zend_Filter_StripTags();
		$condition="`".TABLE_PRE."article`.show = '1'";
    	if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
    		$search=$filter->filter(($this->getRequest()->getPost('keywords')));
    		if($search!=''){
    			$condition="`".TABLE_PRE."article`.show = '1' AND `".TABLE_PRE."article`.`title` LIKE '%$search%'";
    			
    			$this->smarty->assign('keywords',$search);
    		}
    	}else{
    		$class=$filter->filter(($this->_request->getParam('class')));
    		$year=$filter->filter(($this->_request->getParam('year')));
    		$month=$filter->filter(($this->_request->getParam('month')));
    		if($class!=''){
    			$condition="`".TABLE_PRE."article`.show = '1' AND `".TABLE_PRE."article`.`class` ='$class'";
    		}
    		
    		if($year!=''&&$month!=''){
    			$condition="`".TABLE_PRE."article`.show = '1' AND  year( `addtime` ) = '$year' AND month( `addtime` ) = '$month'";
    		}
    	}
    	
    	$article=new Article();
    	$db = $article->getAdapter();
		
		$type=$filter->filter(($this->_request->getParam('type')));
		$count='35';
		if($type==''){$type='list';}
		if($type=='common'){$count='10';}
		
        $query="SELECT `".TABLE_PRE."article`.*, `".TABLE_PRE."articleclass`.`name` AS `classname` FROM `".TABLE_PRE."article` INNER JOIN `".TABLE_PRE."articleclass` ON ".TABLE_PRE."article.class = `".TABLE_PRE."articleclass`.id ";
		$page=$this->_request->getParam('page');
		//$condition="`".TABLE_PRE."article`.show = '1'";
		$sortby="`".TABLE_PRE."article`.id DESC";
		Zend_Loader::loadClass('Custom_Controller_Plugin_Pager');
		$basePageIndex=0;
		$Pagers = new Custom_Controller_Plugin_Pager($db,$page,$count,$condition,$sortby,$basePageIndex,$query) ;
		$pageInfo = $Pagers->getNavBar(WEB_ROOT.'/search/index/');
		$this->smarty->articles = $Pagers->findAll();
		$this->smarty->assign('SearchNums',$Pagers->count);
    	$this->smarty->assign('arrArticles',$this->smarty->articles);
    	
    	//取到友情链接
		$weblink=new Weblink();
		$arrLinks=$weblink->fetchAll(TABLE_PRE."weblink.show='1'",TABLE_PRE."weblink.id DESC",10)->toArray();
		$this->smarty->assign('arrLinks',$arrLinks);
		//取到最热日志
		$article=new Article();
		$topArticle=$article->fetchAll(TABLE_PRE."article.show='1'",TABLE_PRE."article.viewnum  DESC",10)->toArray();
		$this->smarty->assign('topArticle',$topArticle);
		$ArticleNums=count($article->fetchAll()->toArray());
		$this->smarty->assign('ArticleNums',$ArticleNums);
		
		//取到最新留言
		$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);
		
		//取到最新评论
		$comment=new Comment();
		$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);
		
		//取到访问量
		$count=new Count();
		$SeeNums=count($count->fetchAll()->toArray());
		$this->smarty->assign('SeeNums',$SeeNums);
		//取到标签数量
		$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('type',$type);
		
    	$this->smarty->assign("pageInfo",$pageInfo);
    	$this->smarty->display('header.phtml');  
		$this->smarty->display('search/index.phtml');
		$this->smarty->display('footer.phtml');
    }
}