<?php

/**
 * 后台友情链接模块 Controller
 * ============================================================================
 * zhuna_php 住哪网酒店分销联盟程序php版　BY Tmac PHP MVC framework
 * $Author: zhangwentao $  <zwttmac@qq.com>
 * $Id: flink.php 19 2011-11-22 11:00:06Z zhangwentao $
 * http://un.zhuna.cn；
 */
class flinkAction extends Action
{

    private $tmp_model;

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

    /**
     * 资讯类别管理 首页
     */
    public function index()
    {
        //TODO  取出所有资讯
        $rs = $this->tmp_model->getFlinkList();
        $this->assign($rs);
        $this->V('flink');
    }

    /**
     * 新增/修改友情链接
     */
    public function add()
    {        
        $flink_id = !empty($_GET['flink_id']) ? (int) $_GET['flink_id'] : 0;
        if ($flink_id > 0) {
            $editinfo = $this->tmp_model->getFlinkInfo($flink_id);
        } else {
            $editinfo = array();
            $editinfo['flink_type_radio'] = '';
            $editinfo['flink_state_radio'] = '';
        }
        //取友情链接类型radiobutton数组
        $flink_type_radio_ary = UtilityConfig::flink_type_radio_ary();
        $flink_type_radio_option = Utility::RadioButton($flink_type_radio_ary, 'flink_type_radio', $editinfo['flink_type_radio'], 'onclick="typechange(this.value);"');

        //取广告状态option数组
        $flink_state_radio_ary = UtilityConfig::ad_state_radio_ary();
        $flink_state_radio_option = Utility::RadioButton($flink_state_radio_ary, 'flink_state_radio', $editinfo['flink_state_radio'], 'onclick="statechange(this.value);"');


        //初始化一下    默认state_radio
        $editinfo['flink_order'] = !empty($editinfo['flink_order']) ? $editinfo['flink_order'] : '0';
        $editinfo['flink_link'] = !empty($editinfo['flink_link']) ? $editinfo['flink_link'] : 'http://';
        $editinfo['flink_starttime'] = !empty($editinfo['flink_starttime']) ? date('Y-m-d', $editinfo['flink_starttime']) : '';
        $editinfo['flink_endtime'] = !empty($editinfo['flink_endtime']) ? date('Y-m-d', $editinfo['flink_endtime']) : '';
        $editinfo['flink_title'] = !empty($editinfo['flink_title']) ? $editinfo['flink_title'] : '';
        $editinfo['flink_id'] = !empty($editinfo['flink_id']) ? $editinfo['flink_id'] : '';
        $editinfo['flink_uploadfile'] = !empty($editinfo['flink_uploadfile']) ? $editinfo['flink_uploadfile'] : '';
        $editinfo['flink_externallinks'] = !empty($editinfo['flink_externallinks']) ? $editinfo['flink_externallinks'] : '';

        $this->assign('flink_type_radio_option', $flink_type_radio_option);
        $this->assign('flink_state_radio_option', $flink_state_radio_option);
        $this->assign('editinfo', $editinfo);

        $this->V('flink');
    }

    /**
     * 新增/修改友情链接页面　保存　
     */
    public function save()
    {
        if (empty($_POST) || count($_POST) < 5) {
            $this->redirect('don\'t be evil');
            exit;
        }

        /* 初始化变量 */
        $flink_id = !empty($_POST['flink_id']) ? (int) $_POST['flink_id'] : 0;
        $flink_title = !empty($_POST['title']) ? trim($this->H($_POST['title'])) : '';
        $flink_link = !empty($_POST['link']) ? trim($this->H($_POST['link'])) : '';
        $flink_type_radio = !empty($_POST['flink_type_radio']) ? $_POST['flink_type_radio'] : '';
        $flink_uploadfile = !empty($_POST['thumb']) ? $_POST['thumb'] : '';
        $flink_externallinks = !empty($_POST['externallinks']) ? trim($this->H($_POST['externallinks'])) : '';
        $flink_order = !empty($_POST['order']) && is_numeric($_POST['order']) ? $_POST['order'] : '0';
        $flink_state_radio = !empty($_POST['flink_state_radio']) ? $_POST['flink_state_radio'] : '';
        $flink_starttime = !empty($_POST['starttime']) ? strtotime($_POST['starttime']) : '';
        $flink_endtime = !empty($_POST['endtime']) ? strtotime($_POST['endtime']) : '';

        /* 验证不能为空 */
        if (empty($flink_title)) {
            $this->redirect('友情链接标题不能为空');
            exit;
        }
        if (empty($flink_link)) {
            $this->redirect('友情链接不能为空');
            exit;
        }
        if (!isset($flink_type_radio)) {
            $this->redirect('选择友情链接类型!');
            exit;
        }
        // TODO goon to verify

        $info = array(
            'flink_title' => $flink_title,
            'flink_link' => $flink_link,
            'flink_type_radio' => $flink_type_radio,
            'flink_uploadfile' => $flink_uploadfile,
            'flink_externallinks' => $flink_externallinks,
            'flink_order' => $flink_order,
            'flink_state_radio' => $flink_state_radio,
            'flink_starttime' => $flink_starttime,
            'flink_endtime' => $flink_endtime,
            'flink_addusername' => $_SESSION['admin'],
            'flink_addtime' => time()
        );

        if ($flink_id > 0) {
            $flink_page = HttpResponse::getCookie('flink_page');
            //update save article
            $where = 'flink_id = ' . $flink_id . '';
            $rs = $this->tmp_model->addFlink($info, 'UPDATE', $where);
            if ($rs) {
                $this->redirect('修改友情链接成功', PHP_SELF . '?m=flink&page= ' . $flink_page . '');
            } else {
                $this->redirect('修改友情链接失败');
            }
        } else {
            //insert save article_class
            $rs = $this->tmp_model->addFlink($info);
            if ($rs) {
                $this->redirect('添加友情链接成功', PHP_SELF . '?m=flink');
            } else {
                $this->redirect('添加友情链接失败');
            }
        }
    }

    /**
     * del
     * @param int $class_id
     */
    public function del()
    {
        $do_id = (int) $_GET['flink_id'];
        $id_a = $_POST['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->delflink($id);
        // TODO DEL该分类下的所有资讯
        if ($rs) {
            $flink_page = HttpResponse::getCookie('flink_page');
            $this->redirect('删除友情链接成功', PHP_SELF . '?m=flink&page=' . $flink_page . '');
        } else {
            $this->redirect('删除友情链接失败，请重试！');
        }
    }

}