<?php
class Admin_MessageController extends Zend_Controller_Action 
{
	public function init()
	{
		Zend_Loader::loadClass('Message');
		$this->smarty=Zend_Registry::get('smarty');
		$this->config=Zend_Registry::get('config');
		$this->smarty->assign('baseUrl',$this->_request->getBaseUrl());
	}
	
		
	
	public function indexAction()
	{
			$message=new Message();
			$page=$this->_request->getParam('page');
			//if('0'>=$page) $page=1;
			$condition=NULL;
			$sortby='id DESC';
			Zend_Loader::loadClass('Custom_Controller_Plugin_Pager');
			$basePageIndex=0;
			$Pagers = new Custom_Controller_Plugin_Pager($message,$page,'10',$condition,$sortby,$basePageIndex) ;
			
			$pageInfo = $Pagers->getNavBar(WEB_ROOT.'admin/link/index/');
			$this->smarty->messages = $Pagers->findAll();
	    	$this->smarty->assign('arrArticles',$this->smarty->messages->toArray());
	    	
	    	$this->smarty->assign("pageInfo",$pageInfo);
             //显示模版
			$this->smarty->display('message/index.phtml');
             
	}
	
	public function addAction()
	{	
		if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
			Zend_Loader::loadClass('Zend_Filter_StripTags');
			$filter=new Zend_Filter_StripTags();
			$title=$filter->filter(($this->_request->getPost('title')));
			$content=$filter->filter(($this->_request->getPost('content')));
			$show=$filter->filter(($this->_request->getPost('show')));
			$address=$filter->filter(($this->_request->getPost('address')));
			$id=$filter->filter(($this->_request->getPost('id')));
			$dateFormat =$this->config->Format->dateFormat;
			$message=new Message();
			if($content!=''&&$title!=''){
				$db = $weblink->getAdapter();
				if(''==$id){
					$data=array(
					'title'=>$title,
					'content'=>$content,
					'ip'=>$ip,
					'show'=>$show,
					'addtime'=>date($dateFormat),
					'updatetime'=>date($dateFormat)
					);
					$message->insert($data);
					$typeid = $db->lastInsertId();
					unset($data);
					$messages="您的留言增加成功!";
				}
				Zend_Loader::loadClass('Custom_Controller_Plugin_Message');
				$Message = new Custom_Controller_Plugin_Message();
				$this->smarty->display('header.phtml');
				$Message->showMsg('<font color=\"red\">'.$messages.'</font>',WEB_ROOT.'admin/message/index/');
				$this->smarty->display('footer.phtml');
			}
		}else{
			$this->smarty->display('message/add.phtml');
		}
		
	}

	public function editAction()
	{	
		Zend_Loader::loadClass('Zend_Filter_StripTags');
		$filter=new Zend_Filter_StripTags();
		$id=$filter->filter(($this->_request->getPost('id')));
		$message=new Message();
		$set=array(
		'show'=>$show
		);
		$where = $db->quoteInto('id = ?', $id);
		$rows=$message->update($set, $where);
		unset($set);
		$messages="您的留言编辑成功!";
		Zend_Loader::loadClass('Custom_Controller_Plugin_Message');
		$Message = new Custom_Controller_Plugin_Message();
		$this->smarty->display('header.phtml');
		$Message->showMsg('<font color=\"red\">'.$messages.'</font>',WEB_ROOT.'admin/message/index/');
		$this->smarty->display('footer.phtml');
	}
	
	
	public function delAction()
	{		
		$message=new Message();
		$attachment=new Attachment();
		if (strtolower($_SERVER['REQUEST_METHOD'])=='post') {
            $id =$this->_request->getPost('id');
            
            $StringId = join ( ",", $id );
            $where = 'id IN ('.$StringId.')';
            $message->delete($where);
        } else {
            $id = (int)$this->_request->getParam('id');
        	if ($id > 0) {
                $where = 'id = ' . $id;
                $message->delete($where);
            }
        }
		Zend_Loader::loadClass('Custom_Controller_Plugin_Message');
		$Message = new Custom_Controller_Plugin_Message();
		$this->smarty->display('header.phtml');
		$Message->showMsg('<font color=\"red\">您留言删除成功!</font>',WEB_ROOT.'admin/message/index/');
		$this->smarty->display('footer.phtml');
	}
	
	
}