<?php
/**
 *
 * 發送到購物車
 *
 * @version        $Id: posttocar.php 1 15:38 2010年7月8日Z tianya $
 * @package        DedeCMS.Site
 * @copyright      Copyright (c) 2007 - 2010, DesDev, Inc.
 * @license        http://help.dedecms.com/usersguide/license.html
 * @link           http://www.dedecms.com
 */
require_once (dirname(__FILE__) . "/../include/common.inc.php");
require_once DEDEINC.'/shopcar.class.php';
$cart = new MemberShops();

$do = isset($do) ? trim($do) : 'add';

if($do == 'add')
{
    /*
    function addItem();                add a product to car
    */
    $buynum = isset($buynum) && is_numeric($buynum) ? $buynum : 1;
    $id =empty($id)? "" : intval($id);
    $buynum = ($buynum < 1) ? 1 : $buynum;
    $rs = $dsql->GetOne("SELECT id,channel,title FROM #@__archives WHERE id='$id'");
    if(!is_array($rs))
    {
        ShowMsg("該商品已不存在！","-1");
        exit();
    }
    $cts = GetChannelTable($rs['channel']);
    $rows = $dsql->GetOne("SELECT aid as id,trueprice as price,units FROM `$cts[addtable]` WHERE aid='$id'");
    if(!is_array($rows))
    {
        ShowMsg("該商品已不存在！","-1");
        exit();
    }
    $rows['buynum'] = $buynum;
    $rows['title']     = $rs['title'];
    $cart->addItem($id, $rows);
    ShowMsg("已添加加到購物車,<a href='car.php'>查看購物車</a>","car.php");
    exit();
}
elseif($do == 'del')
{
    /*
    function delItem();                del products from car
    */
    if(!isset($ids))
    {
        ShowMsg("請選擇要刪除的商品！","-1");
        exit;
    }
    if(is_array($ids))
    {
        foreach($ids as $id)
        {
            $id = intval($id);
            $cart->delItem($id);
        }
    }
    else
    {
        $ids = intval($ids);
        $cart->delItem($ids);
    }
    ShowMsg("已成功刪除購物車中的商品,<a href='car.php'>查看購物車</a>","car.php");
    exit;
}
elseif($do == 'clear')
{
    /*
    function clearItem();        clear car products all!
    */
    $cart->clearItem();
    ShowMsg("購物車中商品已全部清空！","car.php");
    exit;
}
elseif($do == 'update')
{
    /*
    function updateItem();        update car products number!
    */
    if(isset($ids) && is_array($ids))
    {
        foreach($ids as $id){
            $id = intval($id);
            $rs = $dsql->GetOne("SELECT id,channel,title FROM #@__archives WHERE id='$id'");
            if(!is_array($rs)) continue;
            $cts = GetChannelTable($rs['channel']);
            $rows = $dsql->GetOne("SELECT aid as id,trueprice as price,units FROM `$cts[addtable]` WHERE aid='$id'");
            if(!is_array($rows)) continue;
            $rows['buynum'] = intval(${'buynum'.$id});
            if($rows['buynum'] < 1)
            {
                //如果設單位數量小於1個時更新,則移出購物車
                $cart->delItem($id);
                continue;
            }
            $rows['title']     = $rs['title'];
            $cart->addItem($id, $rows);
        }
    }
    ShowMsg("購物車中商品已全部更新！","car.php");
    exit;
}