<?php
class NewsAction extends GlobalAction{
	
    /**
	 * 栏目列表
	 *
	 */
    public function index(){

		$News = D("News");
		import("@.ORG.Page"); //导入分页类
		$count = $News->count();//计算总数
		$p = new page($count,15);
		$list = $News->limit($p->firstRow.','.$p->listRows)->findAll();
		$page = $p->show();
		$this->assign("page",$page);
		$this->assign('list',$list);
		$this->display();
    }
	
    /**
     * 添加信息
	 *
     */
    public function add()
	{
		//表单提交
		if($this->isPost()){
			$News = D('News');
			if($News->create()){
				$News->posttime = $_POST['posttime']?strtotime($_POST['posttime']):time();
				if(false !== $News->add()){
					$this->success('添加成功');
				}
				else{
					$this->error('添加失败');
				}
			}
			else{
				$this->error($News->getError());
			}
		}
		else{
			//添加信息
			$this->display();
		}
    }
	
    /**
     * 修改
	 *
     */
    public function edit(){
		
		//表单提交
		if($this->isPost()){
			$News = D('News');
			if($News->create()){
				$News->posttime = $_POST['posttime']?strtotime($_POST['posttime']):time();
				if(false !== $News->save()){
					$this->assign('jumpUrl',$_POST['returnurl']);
					$this->success('更新成功');					
				}
				else{
					$this->error('没有任何更新');
				}
			}
			else{
				$this->error($News->getError());
			}
		}
		else{
			//修改栏目
			if(!empty($_GET['id'])){
				$News = D('News');
				$data= $News->getById($_GET['id']);
				if($data){
					$this->assign('returnurl',$_SERVER['HTTP_REFERER']);
					$this->assign('data',$data);
					$this->display();
				}
				else{
					exit('编辑项不存在');
				}		
			}
			else{
				exit('编辑项不存在');
			}
		}
    }

    /**
     * 删除
	 *
     */
	 public function delete(){
		
		if(!empty($_GET['id'])){
			$News = D('News');
			if($News->delete($_GET['id'])) {
				$this->success('删除成功!');
			}
			else{
				$this->error('删除出错!');
			}
		}
		else{
			$this->error('删除项不存在');
		}
	 }
}
?>