<?php
/**
* 
* @author moufer<moufer@163.com>
* @copyright www.modoer.com
* @version $Id  $
*/
if(!$dosubmit) {

    $userranks = $db->get_all("SELECT * FROM {$dbpre}userranks ORDER BY rankid");
    include cptpl('userranks', MOD_ROOT.'./admin/templates/');

} else {

    if($newrank && is_array($newrank)) {
        $newrank['rankname'] = html_special_chars_ex($newrank['rankname']);
        if($newrank['rankname']) {
            if(!is_numeric($newrank['point'])) {
                cpmsg('积分格式不正确，请返回修改。');
            } else {
                $db->query("INSERT INTO {$dbpre}userranks(rankname,point) VALUES('{$newrank['rankname']}','{$newrank['point']}')");
            }
        }
    }

    if($userrank && is_array($userrank)) foreach($userrank as $id => $val) {
        if(!is_numeric($id) || $id < 1) cpmsg('RankID不正确。');
        if($rankid && is_array($rankid) && in_array($id, $rankid)) {
            $db->query("DELETE FROM {$dbpre}userranks WHERE rankid='$id'");
        } else {
            $val['rankname'] = html_special_chars_ex($val['rankname']);
            if(!$val['rankname']) cpmsg('未填写等级名称，请返回修改。');
            if(!is_numeric($val['point'])) cpmsg('积分格式不正确，请返回修改。');
            $db->update("UPDATE {$dbpre}userranks SET rankname='{$val['rankname']}', point='{$val['point']}' WHERE rankid='$id'");
        }
    }

    cache('userranks');
    cpmsg('用户等级更新完毕。');
}
?>