<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Model;
use Think\Model;

/**
 * 分类模型 推广员广告模型
 * 鹿文学
 */
class SiteAdvModel extends Model{

    protected $_validate = array(
        array('title', 'require', '广告标题不能为空',        self::MUST_VALIDATE,  'regex',  self::MODEL_BOTH),
        array('data', 'require', '广告图片不能为空',        self::MUST_VALIDATE,  'regex',  self::MODEL_BOTH),
    );

    protected $_auto = array(
        array('start_time',  'time_deal', self::MODEL_BOTH, 'callback'),
        array('end_time',  'time_deal', self::MODEL_BOTH, 'callback'),
        array('create_time',  'time', self::MODEL_INSERT, 'function'),
        array('promote_id',  PID, self::MODEL_INSERT),
        array('promote_account',  PROMOTE_ACCOUNT, self::MODEL_INSERT),
    );

    public function __construct($name = '', $tablePrefix = '', $connection = '') {
        /* 设置默认的表前缀 */
        $this->tablePrefix ='tab_';
        /* 执行构造方法 */
        parent::__construct($name, $tablePrefix, $connection);
    }
		
		public function time_deal($time){
        if(empty($time)){
            $time = 0;
        }else{
            $time = strtotime($time);
        }
        return $time;
    }
		
		/**
		 * 广告列表
		 */
		public function lists($p=1,$order='create_time desc',$row=10) {
			$page = intval($p);
			$page = $page ? $page : 1; //默认显示第一页数据
			
			$map['promote_id'] = PID;
			
			$data = $this->where($map)->order($order)->page($page,$row)->select();
			
			$count = $this->where($map)->count();
			
			return array('data'=>$data,'count'=>$count);
		}
		
		/**
		 * 广告详情
		 */
		public function detail($id) {
			
			if (!is_numeric($id) || $id <1) {return null;}
			
			$map['promote_id'] = PID;
			
			$map['id']= $id;
			
			return $this->where($map)->find();
			
		}
		
		/**
		 * 可用广告位
		 */
		public function position() {
			
			return M('AdvPos','tab_')->field('id,name,title,width,height')->where(array('name'=>array('like','union_index_%','status'=>1)))->select();
			
		}
		
		/**
		 * 新增或更新一个广告
		 */
		public function update($data = null){
        /* 获取数据对象 */
        $data = $this->token(false)->create($data);
        if(empty($data)){
            return false;
        }     
        /* 添加或新增基础内容 */
        if(empty($data['id'])){ //新增数据
            $map['id'] = $data['pos_id'];
            $pos = M("adv_pos","tab_")->where($map)->field("type")->find();
            if($pos['type'] == 1){
                $map1['promote_id'] = PID;
                $map1['pos_id']= $data['pos_id'];
                $adv = $this->where($map1)->field("id")->find();
                if($adv){
                    $this->error = '单图类型只允许加一次!';
                    return false;
                }
            }
            $id = $this->add($data); //添加基础内容

            if(!$id){
                $this->error = '新增基础内容出错!';
                return false;
            }
        } else { //更新数据
            $status = $this->save(); //更新基础内容
            if(false === $status){
                $this->error = '更新基础内容出错!';
                return false;
            }
        }
        
        return $data;
    }
		
		/**
		 * 删除广告
		 */
		public function del($id) {

			$ids = array_unique((array)I('id',null));

			if (empty($ids)) {
					$this->error = '请选择要操作的数据!';
					return false;
			}
			
			$map = array('id' => array('in', $ids) );
			
			$map['promote_id'] = PID;
			
			if($this->where($map)->delete()){
				return true;	
			} else {
				$this->error = '删除失败';
				return false;	
			}
		}
		
		
		
}