<?php

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

    private $tmp_model;

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

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

    /**
     * 新增/修改文章页面
     */
    public function add()
    {
        $aid = !empty($_GET['aid']) ? (int) $_GET['aid'] : 0;
        $pid = 0;
        if ($aid > 0) {
            $editinfo = $this->tmp_model->getArticleInfo($aid);
            $cityinfo = $this->M('globals')->getCityinfo($editinfo['CityID'], 'c');
            $pid = $cityinfo['Pid'];
        } else {
            $editinfo = array(
                'title' => '',
                'img' => '',
                'aid' => $aid,
                'class_id' => '0',
                'content' => '',
                'CityID' => '0'
            );
        }
        //取文章栏目
        $class_ary = $this->tmp_model->getClass();
        $class_id_option = Utility::Option($class_ary, $editinfo['class_id'], 'class_id,class_name');
        //取省级名字
        $province_ary = $this->tmp_model->getCity('p');
        //把缩写和名字组合在一起
        $province_array = array();
        foreach ($province_ary AS $k => $v) {
            $province_array[$v{'Pid'}] = $v['Pname'];
        }
        $province_option = Utility::Option($province_array, $pid);

        //状态
        $state_radio_ary = UtilityConfig::state_radio_ary();

        //初始化一下    默认state_radio
        $editinfo['state_radio'] = isset($editinfo['state_radio']) ? $editinfo['state_radio'] : -1;
        $editinfo['author'] = !empty($editinfo['author']) ? $editinfo['author'] : '默认管理员';
        $editinfo['order'] = !empty($editinfo['order']) ? $editinfo['order'] : '0';

        $state_radio = Utility::RadioButton($state_radio_ary, 'state_radio', $editinfo['state_radio'], 'onclick="exviewimg(this.value);"');

        $this->assign('class_id_option', $class_id_option);
        $this->assign('province_option', $province_option);
        $this->assign('state_radio', $state_radio);
        $this->assign('pid', $pid);
        $this->assign('editinfo', $editinfo);
        //TODO　载入资讯类别添加表单
        $this->V('article');
    }

    /**
     * ajax根据省pid 取下级city list
     */
    public function getCity()
    {
        $pid = (int) $_POST['pid'];
        $type = !empty($_POST['type']) ? $_POST['type'] : 'yes';

        $city_ary = $this->tmp_model->getCity('c', $pid);
        //把缩写和名字组合在一起
        $city_array = array();
        foreach ($city_ary AS $k => $v) {
            $cityid = $v['cityid'];
            if ($type == 'yes') {
                $cityname = $v['suoxie'] . ' ' . $v['cityname'];
            } else {
                $cityname = $v['cityname'];
            }
            $city_array[] = array('cityid' => '' . $cityid . '', 'cityname' => '' . $cityname . '');
        }
        $rs = json_encode($city_array);
        exit($rs);
    }

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

        /* 初始化变量 */
        $aid = !empty($_POST['aid']) ? (int) $_POST['aid'] : 0;
        $class_id = !empty($_POST['class_id']) ? (int) $_POST['class_id'] : 0;
        $cityid = !empty($_POST['cityid']) ? $_POST['cityid'] : 0;
        $title = !empty($_POST['title']) ? trim($this->H($_POST['title'])) : '';
        $state_radio = !empty($_POST['state_radio']) ? $_POST['state_radio'] : 0;
        $img = !empty($_POST['thumb']) ? $_POST['thumb'] : '';
        $author = !empty($_POST['author']) ? trim($this->H($_POST['author'])) : '';
        $order = !empty($_POST['order']) && is_numeric($_POST['order']) ? $_POST['order'] : '0';
        $content = !empty($_POST['content']) ? $_POST['content'] : '';

        $info = array(
            'class_id' => $class_id,
            'CityID' => $cityid,
            'title' => $title,
            'state_radio' => $state_radio,
            'author' => $author,
            'order' => $order,
            'img' => $img,
            'time' => time(),
            'content' => $content
        );
        if ($aid > 0) {
            $article_page = HttpResponse::getCookie('article_page');
            //update save article
            $where = 'aid = ' . $aid . '';
            $rs = $this->tmp_model->addArticle($info, 'UPDATE', $where);
            if ($rs) {
                $this->redirect('修改资讯文章成功', PHP_SELF . '?m=article&page= ' . $article_page . '');
            } else {
                $this->redirect('修改资讯文章失败');
            }
        } else {
            //insert save article_class
            $rs = $this->tmp_model->addArticle($info);
            if ($rs) {
                $this->redirect('添加资讯文章成功', PHP_SELF . '?m=article');
            } else {
                $this->redirect('添加资讯文章失败');
            }
        }
    }

    /**
     * del
     * @param int $class_id
     */
    public function del()
    {        
        $aid = intval($this->getParam('aid'));
        $id_a = $this->getParam('id_a');
        if (is_array($id_a)) {
            $id = implode(',', $id_a);
        } elseif (!empty($aid)) {
            $id = $aid;
        } else {
            $this->redirect('请选择要删除的...');
            exit;
        }
        $rs = $this->tmp_model->delArticle($id);
        // TODO DEL该分类下的所有资讯
        if ($rs) {
            $article_page = HttpResponse::getCookie('article_page');
            $this->redirect('删除分类成功', PHP_SELF . '?m=article&page=' . $article_page . '');
        } else {
            $this->redirect('删除分类失败，请重试！');
        }
    }

}