<?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;

/**
 * 分类模型
 */
class SiteGiftModel extends SiteModel{

    protected $_validate = array(
			array('gift_name', 'check_gift', '该礼包已经存在', self::EXISTS_VALIDATE, 'callback', self::MODEL_BOTH),
    );

    protected $_auto = array(
        array('create_time', NOW_TIME, self::MODEL_INSERT),
        array('promote_id', PID, self::MODEL_BOTH),
        array('start_time', 'strtotime', self::MODEL_BOTH, 'function'),
        array('end_time', 'strtotime', self::MODEL_BOTH, 'function'),
    );
		
		/**
     * 检查礼包是否存在
     * @return bool
     */
    public function check_gift(){
        
        $map['tab_site_gift.id'] = ['neq',I('id')];
        $map['tab_site_gift.promote_id'] = PID;
        $map['tab_site_gift.gift_name'] = I('gift_name');
        $map['tab_site_game.game_name'] = I('game_name');
        $map['tab_site_gift.server_name'] = I('server_name');
				$map['tab_site_gift.giftbag_version'] = I('giftbag_version');
        $data = $this->join('tab_site_game on(tab_site_gift.site_game_id=tab_site_game.id)','inner')->where($map)->find();
        if(empty($data)){
            return true;
        }else{
            return false;
        }
    }

    /**
     * @param int $promote_id
     * @return mixed
     */
    public function get_promote_data($promote_id=PID){
        $map['promote_id'] = $promote_id;
        $map['status'] = 1;
        $data = $this->where($map)->select();
        return $data;
    }

    public function saveData($data = "")
    {
        $data = I('post.');
        $data['novice'] = str_replace(array("\r\n", "\r", "\n"), ",", trim($data['novice']));
        $novice = explode(',',$data['novice']);

        $count = count($novice);
        if(empty(array_pop($novice))){
            $count--;
        }
        $data['novice_num'] = $count;
        $data['game_name'] = get_site_game_name($data['site_game_id']);
        return parent::saveData($data); // TODO: Change the autogenerated stub
    }

}