* @license PHP License * @package WB * @subpackage base */ WBClass::load('WBAjax'); /** * Simple AJAX stub: Shop Cart * * * * @version 0.1.0 * @package WB * @subpackage base */ class WBAjax_Shop_Cart extends WBAjax { /** * cart * @var WBShop_Cart */ private $cart; /** * second construcotr * * Called by constructer of super class. * * @see include/WB/WBAjax#init() */ protected function init() { $this->cart = WBClass::create('WBShop_Cart'); $this->cart->setUser($this->user->getId()); $this->cart->loadActive(); } /** * add item to cart * * @see WBShop_Cart::add() * @return string comment list HTML */ protected function add() { $item = $this->req->get('item', ''); $quantity = intval($this->req->get('quantity', 1)); $comment = $this->req->get('comment', ''); // no changes if (empty($item) || 1 > $quantity) { return $this->getGaugeHtml(); } $this->cart->add($item, $quantity, $comment); $this->cart->save(); return $this->getGaugeHtml(); } /** * set item in cart * * @see WBShop_Cart::set() * @return string comment list HTML */ protected function set() { $item = $this->req->get('item', ''); $quantity = intval($this->req->get('quantity', 1)); $comment = $this->req->get('comment', ''); // no changes if (empty($item) || 1 > $quantity) { return $this->getGaugeHtml(); } $this->cart->set($item, $quantity, $comment); $this->cart->save(); return $this->getGaugeHtml(); } /** * remove item from cart * * @see WBShop_Cart::remove() * @return string comment list HTML */ protected function remove() { $item = $this->req->get('item', ''); // no changes if (empty($item)) { return $this->getGaugeHtml(); } if (!is_array($item)) { $item = array($item); } foreach ($item as $i) { $this->cart->remove($i); } $this->cart->save(); return $this->getGaugeHtml(); } /** * Rendert cart gauge * * Load template, add cart information and render HTML * * @return string html snippet */ private function getGaugeHtml() { $tmpl = $this->req->get('tmpl', ''); if (empty($tmpl)) { $tmpl = 'display'; } else { $tmpl = 'display/' . $tmpl; } $this->loadTemplates($tmpl); if ($this->tmpl->exists('item_entry')) { $this->tmpl->addRows('item_entry', $this->cart->getItems()); } $this->tmpl->addGlobalVars($this->cart->get()); return $this->tmpl->getParsedTemplate('snippet'); } } ?>