<?php

/*
'#################################################################
'# 程序名称：中环CMSV2.0
'# 授权方式：免费软件
'# 作者：善
'# 联系QQ:9118847
'# 作者网站:http://www.ps173.com
'# 发布时间：2015-6-10
'# 声明：欢迎广大的PHP爱好者一起共同探讨
'# 声明：各种用途均可免费使用、自由传播，但必须保留程序代码中的版权信息。
'#################################################################
 */

//广告控制类
class AdverAction extends CommonAction{
	private $adver;

	public function __construct(){
		parent::__construct();
		$this->adver = new AdverModel();

	}
		//检测数据是否合法
	public function check($post){
		$rs = true;
		if(!val::check($post['name'])){
			$this->msg('英文名称不能为空');
			$rs = false;
		}

		if(!val::check($post['tit'])){
			$this->msg('中文名称不能为空');
			$rs = false;
		}

		if(empty($post['position'])){
			$this->msg('广告位置不能为空');
			$rs = false;
		}

		if(!val::check($post['code'])){
			$this->msg('广告代码不能为空');
			$rs = false;
		}

		return $rs;
	}
		//删除广告
	public function del(){
		if(isset($_GET['id'])){
			if($this->adver->delete($_GET['id'])){
				$this->msg('广告删除成功');
			}else{
				$this->msg('广告删除失败');
			}
		}
	}
		//批量操作广告
	public function set(){
		if(isset($_POST['id'])){
			if(isset($_POST['audits'])){
				if($this->adver->sets($_POST['id'], "state = '0'")){
					$this->msg('批量审核成功');
				}else{
					$this->msg('批量审核失败');
				}
			}else if(isset($_POST['locks'])){
				if($this->adver->sets($_POST['id'], "state = '1'")){
					$this->msg('批量停用成功');
				}else{
					$this->msg('批量停用失败');
				}
			}else if(isset($_POST['dels'])){
				if($this->adver->delete($_POST['id'])){
					$this->msg('批量删除成功');
				}else{
					$this->msg('批量删除失败');
				}
			}
		}else{
			$this->msg('请选择你要操作的广告');
		}
	}
		//显示广告列表
	public function index(){
		if(isset($_GET['id']) && isset($_GET['state'])){
			$data['id'] = $_GET['id'];
			$data['state'] = $_GET['state'];
			$this->adver->update($data);
		}

		$this->assign("list", $this->adver->num());
		$this->display();
	}
		//检测名称是否存在
	public function checkname($name){
		if($this->adver->checkname($name)){
			$this->msg('该名称已存在，请重新输入');
			return false;
		}

		return $name;
	}
		//添加广告
	public function add(){
		if(isset($_POST['sub'])){
			if($this->check($_POST)){
				if($this->checkname($_POST['name'])){
					if($this->adver->insert($_POST)){
						$this->msg('广告添加成功');
					}else{
						$this->msg('广告添加失败');
					}
				}
			}
		}

		$this->display();
	}
		//修改广告
	public function mod(){
		if(isset($_GET['id'])){
			$list = $this->adver->find($_GET['id']);
			$list['code'] = val::unhtml($list['code']);
			if(!$list){
				$this->msg('该广告不存在，或者已经被删除');
			}
		}

		$this->assign("list", $list);
		$this->display();
	}
		//修改广告
	public function update(){
		if($this->check($_POST)){
			if($this->adver->update($_POST)){
				$this->msg('广告修改成功');
			}else{
				$this->msg('广告修改失败');
			}
		}
	}


}

?>