<?php

/**
 * 后台广告管理模块 Controller
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zhangwentao $  <zwttmac@qq.com>
 * $Id: ad.php 19 2011-11-22 11:00:06Z zhangwentao $
 * http://www.t-mac.org；
 */
class adAction extends Action
{

    private $tmp_model;
    private $ad_model;

    /**
     * _init 方法 在执行任何Action前执行
     */
    public function _init()
    {
        $this->assign('action', $_GET['TMAC_ACTION']);
        $this->tmp_model = Tmac::model('ad');
        $check_model = $this->M('check');
        $check_model->checkLogin();
        $check_model->CheckPurview('zhuna_admin');
    }

    /**
     * 广告管理 首页
     */
    public function index()
    {        
        //TODO  取出所有广告
        $rs = $this->tmp_model->getAdList();
        $this->assign($rs);
        $this->V('ad');
    }

    /**
     * 新增/修改广告页面
     */
    public function add()
    {
        $ad_id = intval($this->getParam('ad_id'));
        if ($ad_id > 0) {
            $editinfo = $this->tmp_model->getAdInfo($ad_id);                
        } else {
            $editinfo = array(
                'ad_id' => 0,
                'ad_title' => '',
                'ad_link' => 'http://',
                'ad_width' => '',
                'ad_height' => '',
                'ad_type_radio' => '',
                'ad_uploadfile' => '',
                'ad_order' => '',
                'ad_starttime' => '',
                'ad_endtime' => '',
                'ad_state_radio' => '',
                'ad_addtime' => 0,                
                'ad_externallinks' => '',
                'ad_area' => '',
                'ad_name' => '',
            );            
        }
        //取广告类型option数组
        $ad_type_radio_ary = Tmac::config('ad.ad_type');
        $ad_type_radio_option = Utility::RadioButton($ad_type_radio_ary, 'type_radio', $editinfo['ad_type_radio'], 'onclick="typechange(this.value);"');

        //取广告状态option数组
        $ad_state_radio_ary = Tmac::config('ad.ad_state');
        $ad_state_radio_option = Utility::RadioButton($ad_state_radio_ary, 'state_radio', $editinfo['ad_state_radio'], 'onclick="statechange(this.value);"');


        //初始化一下    默认state_radio
        $editinfo['ad_starttime'] = !empty($editinfo['ad_starttime']) ? date('Y-m-d', $editinfo['ad_starttime']) : '';
        $editinfo['ad_endtime'] = !empty($editinfo['ad_endtime']) ? date('Y-m-d', $editinfo['ad_endtime']) : '';

        $this->assign('ad_type_radio_option', $ad_type_radio_option);
        $this->assign('ad_state_radio_option', $ad_state_radio_option);
        $this->assign('editinfo', $editinfo);        
        //TODO　载入资讯类别添加表单
        $this->V('ad');
    }

    /**
     * 广告管理 insert update => save()
     */
    public function save()
    {
        if (empty($_POST) || count($_POST) < 1) {
            $this->redirect('don\'t be evil');
            exit;
        }

        /* 初始化变量 */
        $ad_id = !empty($_POST['ad_id']) ? (int) $_POST['ad_id'] : 0;
        $area = !empty($_POST['area']) ? trim($this->H($_POST['area'])) : '';
        $title = !empty($_POST['title']) ? trim($this->H($_POST['title'])) : '';
        $sizewidth = !empty($_POST['sizewidth']) ? $_POST['sizewidth'] : '';
        $sizeheight = !empty($_POST['sizeheight']) ? $_POST['sizeheight'] : '';
        $link = !empty($_POST['link']) ? trim($this->H($_POST['link'])) : '';
        $ad_type_radio = !empty($_POST['type_radio']) ? $_POST['type_radio'] : '';

        $ad_externallinks = !empty($_POST['externallinks']) ? $_POST['externallinks'] : '';
        $ad_order = !empty($_POST['order']) && is_numeric($_POST['order']) ? $_POST['order'] : '0';
        $ad_state_radio = !empty($_POST['state_radio']) ? $_POST['state_radio'] : '';
        $ad_starttime = !empty($_POST['ad_starttime']) ? strtotime($_POST['ad_starttime']) : '';
        $ad_endtime = !empty($_POST['ad_endtime']) ? strtotime($_POST['ad_endtime']) : '';
        $ad_name = $this->H($this->getParam('ad_name'));
        $ad_uploadfile = $this->H($this->getParam('thumb'));        

        //判断ad_name已经存在
        $check_ad_id = $this->tmp_model->checkAdName($ad_name);
        if ($check_ad_id && ($ad_id != $check_ad_id)) {
            $this->redirect('别名 "'.$ad_name.'" 已经存在！');
            exit;
        }        

        /* 验证不能为空 */
        if (empty($area)) {
            $this->redirect('广告位置不能为空!');
            exit;
        }
        if (empty($title)) {
            $this->redirect('广告标题不能为空');
            exit;
        }
        if (empty($ad_name)) {
            $this->redirect('广告位置代号不能为空!');
            exit;
        }
        if (empty($link)) {
            $this->redirect('广告链接不能为空');
            exit;
        }
        // TODO goon to verify

        $info = array(
            'ad_title' => $title,
            'ad_width' => $sizewidth,
            'ad_height' => $sizeheight,
            'ad_link' => $link,
            'ad_type_radio' => $ad_type_radio,
            'ad_uploadfile' => $ad_uploadfile,
            'ad_order' => $ad_order,
            'ad_state_radio' => $ad_state_radio,
            'ad_starttime' => $ad_starttime,
            'ad_endtime' => $ad_endtime,
            'ad_area' => $area,            
            'ad_externallinks' => $ad_externallinks,
            'ad_addtime' => time(),
            'ad_area' => $area,
            'ad_name' => $ad_name            
        );

        if ($ad_id > 0) {
            $ad_page = HttpResponse::getCookie('ad_page');
            //update save article
            $where = 'ad_id = ' . $ad_id . '';
            $rs = $this->tmp_model->addAd($info, 'UPDATE', $where);
            if ($rs) {
                $this->redirect('修改广告内容成功', PHP_SELF . '?m=ad&page=' . $ad_page . '');
            } else {
                $this->redirect('修改广告内容失败');
            }
        } else {
            //insert save article_class
            $rs = $this->tmp_model->addAd($info);
            if ($rs) {
                $this->redirect('添加广告成功', PHP_SELF . '?m=ad');
            } else {
                $this->redirect('添加广告失败');
            }
        }
    }

    /**
     * del
     * @param int $class_id
     */
    public function del()
    {
        $do_id = intval($this->getParam('ad_id'));
        $id_a = $this->getParam('id_a');        

        if (is_array($id_a)) {
            $id = implode(',', $id_a);            
        } elseif (!empty($do_id)) {
            $id = $do_id;
        } else {
            $this->redirect('请选择要删除的...');
            exit;
        }
        $rs = $this->tmp_model->delAd($id);
        // TODO DEL该分类下的所有资讯
        if ($rs) {
            $ad_page = HttpResponse::getCookie('ad_page');
            $this->redirect('删除广告成功', PHP_SELF . '?m=ad&page=' . $ad_page . '');
        } else {
            $this->redirect('删除广告失败，请重试！');
        }
    }

}