<?php
class AlbumController extends Zend_Controller_Action 
{
 	
	public function init()
    {	
		
		$this->smarty=Zend_Registry::get('smarty');
		$this->smarty->assign('baseUrl',$this->_request->getBaseUrl());
    }
	
	public function indexAction()
    {	
  		$album=new Album();
		$page=$this->_request->getParam('page');
		$db = $album->getAdapter();
		$query="SELECT  count( `".TABLE_PRE."photo`.`id` ) AS nums, `".TABLE_PRE."album`.* FROM `".TABLE_PRE."album` , `".TABLE_PRE."photo`";
		$condition="`".TABLE_PRE."album`.id = `".TABLE_PRE."photo`.`albumid` AND `".TABLE_PRE."album`.show = '1' GROUP BY `".TABLE_PRE."album`.id ";
		$sortby="".TABLE_PRE."album.id DESC";
		Zend_Loader::loadClass('Custom_Controller_Plugin_Pager');
		$basePageIndex=0;
		$Pagers = new Custom_Controller_Plugin_Pager($db,$page,'16',$condition,$sortby,$basePageIndex,$query) ;
		
		$pageInfo = $Pagers->getNavBar(WEB_ROOT.'/album/index/');
		$this->smarty->albums = $Pagers->findAll();
    	$this->smarty->assign('arrAlbums',$this->smarty->albums);
    	$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);
		//取到最热日志
		$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('title','default');
		$this->smarty->display('header.phtml');  
		$this->smarty->display('album/index.phtml');
		$this->smarty->display('footer.phtml');
    }
    
    public function authAction()
    {
    	$album=new Album();
  		$db = $album->getAdapter();
  		if($this->_request->isPost()){
    		Zend_Loader::loadClass('Zend_Filter_StripTags');
    		$filter=new Zend_Filter_StripTags();
    		$password=MD5($filter->filter($this->_request->getPost('password')));
    		$albumid=$filter->filter($this->_request->getPost('albumid'));
			$where = $db->quoteInto(' id= ?', $albumid)
			       . $db->quoteInto(' AND password = ?', $password);
			//$row =$album->fetchRow($where)->toArray();
			$row = $album->fetchAll($where)->toArray();
			
			Zend_Loader::loadClass('Custom_Controller_Plugin_Message');
			$Message = new Custom_Controller_Plugin_Message();
			$this->smarty->display('header.phtml');
    		if(count($row)=='1'){
    			$Message->showMsg('<font color=\"red\">恭喜您!密码正确!</font>',WEB_ROOT.'album/photo/id/'.$albumid.'/password/'.$password.'/');
    		}else{
    			$Message->showMsg('<font color=\"red\">对不起!您输入的密码错误!</font>',WEB_ROOT.'album/index/');
    		}
			$this->smarty->display('footer.phtml');
    		
  		}else{
  			$id=$this->_request->getParam('id');
  			$this->smarty->assign('id',$id);
  			$this->smarty->display('album/auth.phtml');
  		}	
  		
    }
    
	public function photoAction()
    {	
    	
    	
  		$photo=new Photo();
  		$album=new Album();
  		$db = $album->getAdapter();
  		
  		
  		Zend_Loader::loadClass('Zend_Filter_StripTags');
		$filter=new Zend_Filter_StripTags();
		$id=$filter->filter(($this->_request->getParam('id')));
		$password=$filter->filter(($this->_request->getParam('password')));
		
		
		$page=$this->_request->getParam('page');
		$condition="".TABLE_PRE."photo.albumid=$id";
		$sortby="".TABLE_PRE."photo.id DESC";
		Zend_Loader::loadClass('Custom_Controller_Plugin_Pager');
		$basePageIndex=0;
		$Pagers = new Custom_Controller_Plugin_Pager($photo,$page,'12',$condition,$sortby,$basePageIndex) ;
		
		$pageInfo = $Pagers->getNavBar(WEB_ROOT.'album/photo/id/'.$id.'/');
		$this->smarty->photos = $Pagers->findAll();
		$where="".TABLE_PRE."album.id=$id";
		$arrAlbum= $album->fetchAll($where)->toArray();
		//$arrAlbum=array('0'=>$arrAlbum);
		//print_r($arrAlbum[0]['password']);
		
		$this->smarty->display('header.phtml');  
		
		if($password==$arrAlbum[0]['password'])
		{
			 //取到友情链接
			Zend_Loader::loadClass('Weblink');
			$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('arrPhotos',$this->smarty->photos->toArray());
	    	$this->smarty->assign("pageInfo",$pageInfo);
	        $this->smarty->assign("arrAlbum",$arrAlbum);
			$this->smarty->assign('title','default');
			$this->smarty->display('album/photo.phtml');
		}else{
			Zend_Loader::loadClass('Custom_Controller_Plugin_Message');
			$Message = new Custom_Controller_Plugin_Message();
			$Message->showMsg('<font color=\"red\">对不起!您访问的相册需要密码!</font>',WEB_ROOT.'album/index/');
    	
		}
		
		
		$this->smarty->display('footer.phtml');
    }
}
