<?php

if (!defined('ROOT')) exit('Can\'t Access !');

class table_type extends table_mode
{
    function vaild()
    {
        if (!front::post('typename') && !front::post('batch_add') ) {
            front::flash(lang_admin('fill_in_the_category_title_etc'));
            return false;
        }
        if (!front::post('htmldir'))
            front::$post['htmldir'] = pinyin::get2(front::post('typename'));
        return true;
    }

    function view_before(&$data = null)
    {
        $rank = new rank();
        $rank = $rank->getrow('typeid=' . front::get('id'));
        if (is_array($rank))
            $data['_ranks'] = unserialize($rank['ranks']);
        else $data['_ranks'] = array();
        unset($data['ranks']);
    }

    function save_after($typeid = '')
    {
        if (front::$post['_ranks']) {
            $_ranks = serialize(front::post('_ranks'));
            $rank = new rank();
            if (is_array($rank->getrow(array('typeid' => front::get('id')))))
                $rank->rec_update(array('ranks' => $_ranks), 'typeid=' . $typeid);
            else
                $rank->rec_insert(array('typeid' => front::get('id'), 'ranks' => $_ranks));
        } else {
            $rank = new rank();
            $rank->rec_delete('typeid=' . $typeid);
        }
    }

    function save_before()
    {
        if (front::$post['htmlrule1'] != '') {
            front::$post['htmlrule'] = front::$post['htmlrule1'];
        }
        if (front::$post['listhtmlrule1'] != '') {
            front::$post['listhtmlrule'] = front::$post['listhtmlrule1'];
        }
        front::$post['typecontent'] = stripcslashes(htmlspecialchars_decode(front::$post['typecontent']));
        front::$post['module'] = 'article';
    }

    function delete_before($id = '')
    {
       /* $typeid = intval(front::$get['id']);
        $where = "typeid = '$typeid'";
        $arc = new archive();
        $arc->rec_delete($where);*/
    }

    function edit_before(){
        //修改模板触发清空可视化缓存
        if (front::post('template') && front::get('id')) {
            $type = new type();
            $type_data = $type->getrow(array("typeid"=>front::get('id')));
            if($type_data['template']!=front::post('template')){
                front::remove(ROOT.'/cache/template_admin');
            }
        }
    }

}