<?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) {

    require_once(MUDDER_ROOT.'include/editor/fckeditor.php');
    $fck = new FCKeditor('gift[description]');
    $fck->ToolbarSet = 'Admin';
    $fckhtml = $fck->CreateHtml();
    include cptpl('gift_edit', MOD_ROOT.'./admin/templates/');

} else {

    require_once MUDDER_ROOT.'./include/upload.class.php';
    $upimg =& new Upload_Image_calss($_FILES['picture']);

    $isedit = false;
    $isedit && (!isset($giftid) || !is_numeric($giftid) || $giftid < 0) && cpmsg('无效的礼品信息。');

    if(!$gift['name']) {
        $upimg->deletetmp();
        cpmsg('未填写礼品名称，请返回填写。');
    } elseif($gift['price'] < 0) {
        $upimg->deletetmp();
        cpmsg('价格必须大于0，请返回修改。');
    } elseif($gift['num'] < 0) {
        $upimg->deletetmp();
        cpmsg('库存数量必须大于0，请返回修改。');
    }

    !$isedit && $upimg->error && cpmsg($upimg->error.'请返回修改。');
    $isedit && $_FILES['picture'] && $upimg->error && cpmsg($upimg->error.'请返回修改。');
    if($_FILES['picture'] && !$upimg->error) {
        $upimg->folder = 'gifts';
        $upimg->userWatermark = $_config['watermark'];
        $upimg->max_size = $_config['picture_upload_size'];
        $MOD['thumb_width'] = $MOD['thumb_width'] < 10 ? 124 : $MOD['thumb_width'];
        $upimg->thumb = array ( 
            array ( 
                'prefix' => 's_',  
                'width' => $MOD['thumb_width'],
                'height' => $MOD['thumb_height'],
            ),
        );

        $upimg->upload();
        $upimg->error && cpmsg($upimg->error.'请返回修改。');

        $gift['thumb'] = addslashes(serialize($upimg->thumb_filenames[0]));
        $gift['picture'] = addslashes(serialize($upimg->filenames));

    }

    $gift['name'] = html_special_chars_ex($gift['name']);
    $gift['displayorder'] = intval($gift['displayorder']);
    $gift['price'] = is_numeric($gift['price']) && $gift['price'] > 0 ? $gift['price'] : -1;
    $gift['num'] = is_numeric($gift['num']) && $gift['num'] >= 0 ? $gift['num'] : -1;
    $gift['description'] = trim($gift['description']);
    $gift['available'] = intval($gift['available']) > 0 ? 1 : 0;

    $db->query("INSERT INTO {$dbpre}gifts (name,available,displayorder,description,price,num,thumb,picture,salevolume) VALUES ('{$gift['name']}','{$gift['available']}','{$gift['displayorder']}','{$gift['description']}','{$gift['price']}','{$gift['num']}','{$gift['thumb']}','{$gift['picture']}','0')");
    $msg = '礼品增加完毕。';

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

}

?>
