* @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(); } /** * Display Gauge * * Show currently active cart * * @return string comment list HTML */ protected function display() { return $this->getGaugeHtml(); } /** * add item to cart * * @see WBShop_Cart::add() * @return string comment list HTML */ protected function add() { $this->prepare4AddSet($item, $quantity, $comment, $atts); // no changes if (empty($item)) { return $this->getGaugeHtml(); } foreach ($item as $i => $a) { $q = 1; if (isset($quantity[$i])) { $q = intval($quantity[$i]); } $c = ''; if (isset($comment[$i])) { $c = $comment[$i]; } $this->cart->add($a, $q, $c); $this->cart->setItemAttributes($a, $atts[$i]); } $this->cart->save(); return $this->getGaugeHtml(); } /** * Set item in cart * * @see WBShop_Cart::set() * @return string comment list HTML */ protected function set() { $this->prepare4AddSet($item, $quantity, $comment, $atts); // no changes if (empty($item)) { return $this->getGaugeHtml(); } foreach ($item as $i => $a) { $q = 1; if (isset($quantity[$i])) { $q = intval($quantity[$i]); } $c = ''; if (isset($comment[$i])) { $c = $comment[$i]; } $this->cart->set($a, $q, $c); $this->cart->setItemAttributes($a, $atts[$i]); } $this->cart->save(); return $this->getGaugeHtml(); } /** * Prepare request variables * * @param array $item * @param array $quantity * @param array $comment * @param array $atts */ private function prepare4AddSet(&$item, &$quantity, &$comment, &$atts) { $this->cart->setNamespace($this->req->get('namespace', '')); $item = $this->req->get('item', ''); $quantity = $this->req->get('quantity', 1); $comment = $this->req->get('comment', ''); // normalize if (!is_array($item)) { $item = array($item); } if (!is_array($quantity)) { $quantity = array($quantity); } if (!is_array($comment)) { $comment = array($comment); } // extract attributes $expo = $this->req->export(); foreach (array('namespace', 'item', 'quantity', 'comment') as $i) { if (isset($expo[$i])) { unset($expo[$i]); } } // normalize attributes $atts = array(); for ($i = 0; $i < count($item); ++$i) { $a = array(); foreach ($expo as $k => $v) { if (!is_array($v)) { $a[$k] = $v; continue; } if (isset($v[$i])) { $a[$k] = $v[$i]; continue; } $a[$k] = ''; } $atts[] = $a; } } /** * remove item from cart * * @see WBShop_Cart::remove() * @return string comment list HTML */ protected function remove() { $this->cart->setNamespace($this->req->get('namespace', '')); $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'); } }