<?php

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

    private $class_model;

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

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

    /**
     * 新增/修改栏目页面
     */
    public function add()
    {
        $class_id = intval($this->getParam('class_id'));
        $button = '添加分类';
        if ($class_id > 0) {
            $editinfo = $this->class_model->getClassInfo($class_id);
            $button = '保存修改';
        } else {
            $editinfo = array(
                'class_id' => 0,
                'class_name' => '',
            );
        }
        $this->assign('editinfo', $editinfo);
        $this->assign('button', $button);
        //TODO　载入资讯类别添加表单
        $this->V('article_class');
    }

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

        $class_id = (int) $_POST['class_id'];
        $sortname = $_POST['sortname'];
        if ($class_id > 0) {
            //update save article_class
            $where = 'class_id = ' . $class_id . '';
            $rs = $this->class_model->addClass(trim($sortname), 'UPDATE', $where);
            if ($rs) {
                $this->redirect('修改新分类成功', PHP_SELF . '?m=article_class');
            } else {
                $this->redirect('修改新分类失败');
            }
        } else {
            //insert save article_class
            $rs = $this->class_model->addClass(trim($sortname));
            if ($rs) {
                $this->redirect('添加新分类成功', PHP_SELF . '?m=article_class');
            } else {
                $this->redirect('添加新分类失败');
            }
        }
    }

    /**
     * del
     * @param int $class_id
     */
    public function del()
    {        
        $class_id = intval($this->getParam('class_id'));        
        
        $rs = $this->class_model->delClass($class_id);
        // TODO DEL该分类下的所有资讯
        if ($rs) {
            $this->redirect('删除分类成功', PHP_SELF . '?m=article_class');
        } else {
            $this->redirect('删除分类失败，请重试！');
        }
    }

    /**
     * order
     */
    public function order()
    {
        //TODO  取出所有资讯栏目
        $url = PHP_SELF . '?m=article_class.order&page=';
        $rs = $this->class_model->getClass($url);

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

    /**
     * ordersave todo ajax
     * return boole
     */
    public function ordersave()
    {
        $class_id = intval($this->getParam('class_id'));
        $orderid = intval($this->getParam('orderid'));

        $rs = $this->class_model->changeClassOrder($class_id, $orderid);
        if ($rs) {
            $result = '1';
        } else {
            $result = '0';
        }
        exit($result);
    }

}