<?php
/**
* 积分兑换后台处理操作
* @author moufer<moufer@163.com>
* @copyright www.modoer.com
* @version $Id gifts.inc.php 1 2008-2-23 20:57 $
*/
(!defined('IN_ADMIN') || !defined('IN_MUDDER')) && exit('Access Denied');

if(!$dosubmit) {

    $query = $db->query("SELECT * FROM {$dbpre}gifts ORDER BY displayorder,giftid");
    while($row = $db->fetch_array($query)) {
        $gifts[] = $row;
    }

    include cptpl('gifts', MOD_ROOT.'./admin/templates/');

} else {

    foreach($gifts as $id => $val) {

        if($giftsids && is_array($giftsids) && in_array($id, $giftsids)) {
            $db->query("DELETE FROM {$dbpre}gifts WHERE giftid='$id'");
        } else {
            $name = html_special_chars_ex($val['name']);
            $displayorder = intval($val['displayorder']);
            $price = is_numeric($val['price']) && $val['price'] > 0 ? $val['price'] : -1;
            $num = is_numeric($val['num']) && $val['num'] >= 0 ? $val['num'] : -1;
            $description = html_special_chars_ex($val['description']);
            $available = intval($val['available']) > 0 ? 1 : 0;
            if($price < 0) {
                cpmsg($name.'：价格必须大于0，请返回修改');
            } elseif($num < 0) {
                cpmsg($name.'：库存数量必须大于等于0，请返回修改');
            } else {
                $db->query("UPDATE {$dbpre}gifts SET name='{$name}',available='{$available}',displayorder='{$displayorder}',price='{$price}',num='{$num}' WHERE giftid='$id'");
            }
        }

    }

    $msg = '礼品更新完毕。';
    //$cacheclass->Read_Gifts(TRUE);
    cpmsg('礼品更新完毕。','admincp.php?action='.$action.'&file=gifts');

}

?>
