<?php
require_once 'Zend/Session/Namespace.php';
class IndexController 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()
    {	
  		
    	//$cache=Zend_Registry::get('cache');
		/*
		// 传递一个唯一标识符给start()方法
		if(!$result = $cache->load('myresult')) {
			
		    $result=time();
			$cache->save($result, 'myresult');
		    
		   
		}else {
		   
		    echo "This one is from cache!\n\n";

	
		}

		print_r($result);
		*/
  		
  		
  		
    	$count=new Count();
    	$countdb=$count->getAdapter();
    	$times=time();
    	Zend_Loader::loadClass('Custom_Controller_Plugin_Ipaddress');
    	$Ipaddress=new Custom_Controller_Plugin_Ipaddress();
    	$IP=$Ipaddress->getIP();
    	$sqlcount=" SELECT MAX( time ) AS maxtime FROM `".TABLE_PRE."count` WHERE `".TABLE_PRE."count`.ip = '$IP' ";
    	$Nums=$countdb->fetchAll($sqlcount);
    	$mintime=time()-$Nums[0]['maxtime'];
    	if($mintime>=3600)
    	{
	    	$data=array(
						'ip'=>$IP,
						'time'=>$times
	    				);
			$count->insert($data);
    	}
		$SeeNums=count($count->fetchAll()->toArray());
		$this->smarty->assign('SeeNums',$SeeNums);
		
		
		
    	$article=new Article();
		$db = $article->getAdapter();
	
       // $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 ";
		
       $query="SELECT `".TABLE_PRE."article`. * , count( ".TABLE_PRE."comment.id ) AS numbers,".TABLE_PRE."articleclass.name AS `classname`
FROM ".TABLE_PRE."article
LEFT JOIN ".TABLE_PRE."comment ON ".TABLE_PRE."article.id = ".TABLE_PRE."comment.commentid
INNER JOIN ".TABLE_PRE."articleclass ON ".TABLE_PRE."article.class = ".TABLE_PRE."articleclass.id ";
        
        $page=$this->_request->getParam('page');
		//$condition=NULL;
		$condition="`".TABLE_PRE."article`.show = '1' GROUP BY ".TABLE_PRE."article.id ";
		$sortby="  `".TABLE_PRE."article`.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.'index/index/');
		$this->smarty->articles = $Pagers->findAll();
		
		//print_r($arrTags);
		
		$weblink=new Weblink();
		$arrLinks=$weblink->fetchAll(TABLE_PRE."weblink.show='1'",TABLE_PRE."weblink.id DESC",null)->toArray();
		$this->smarty->assign('arrLinks',$arrLinks);
		//取到最热日志
		$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('arrArticles',$this->smarty->articles);
    	$this->smarty->assign("pageInfo",$pageInfo);
		$this->smarty->assign('title','default');
		$this->smarty->display('header.phtml');  
		$this->smarty->display('index/index.phtml');
		$this->smarty->display('footer.phtml');
    }
    
   
   	public function rssAction()
    {	
    	//header ("content-type: text/xml");
    	//@header('Content-type:text/xml;charset=UTF-8');
    	header("Content-Type: application/xml");
	    require_once 'Zend/Feed.php';
	   
	    $feedArray = array(
	                'title' => 'OOPHP开源博客日志RSS',
	                'link' => 'http://www.oophp.cn/blog/index/rss',
	                'description' => '热爱PHP技术.更愿意为开源而努力!',
	                'language' => 'zh-cn',
	                'charset' => 'UTF-8',
	                 'pubDate' => time(),
	                'generator' => 'OOPHP开源博客日志RSS',
	                'entries' => array()
	    );
	    $article=new Article();
		$db = $article->getAdapter();
	
        $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 and `".TABLE_PRE."article`.show = '1' ORDER BY `".TABLE_PRE."article`.`id` DESC
LIMIT 0 , 10 ";
	
		$arrArticle=$db->fetchAll($query);
		 foreach($arrArticle as $article){
		    $feedArray['entries'][] = array(
		                'title' => $article['title'],
		                'link' =>'http://www.oophp.cn/article/article/id/'.$article['id'],
		                'guid' =>'http://www.oophp.cn/article/article/id/'.$article['id'],
		                'description' => $article['content'],
		                'pubDate' => strtotime($article['addtime'])
		                );
		 }
		
		$feed = Zend_Feed::importArray($feedArray, 'rss');
		//$feed->saveXML();
		echo $feed->saveXML();

		//echo $feed->send();
	
    }
}