*/ /** * Required classes */ GalleryCoreApi::requireOnce('modules/cart/classes/CartHelper.class'); /** * This controller will handle the changing of item quantities in a cart * * @package Cart * @subpackage UserInterface * */ class ModifyCartController extends GalleryController { /** * @see GalleryController::handleRequest() */ function handleRequest($form) { list ($ret, $cartItemIds) = CartHelper::fetchCartItemCounts(); if ($ret) { return array($ret->wrap(__FILE__, __LINE__), null); } $error = array(); $status = array(); $results = array(); if (!isset($form['action']) || isset($form['action']['modify'])) { switch ($form['pluginId']) { case 'updateCart': /* Update counts */ if (isset($form['counts'])) { foreach ($form['counts'] as $itemId => $count) { if ($count <= 0) { unset($cartItemIds[$itemId]); } else { $cartItemIds[$itemId] = (int)$count; } } } /* Delete whatever needs deleting */ if (isset($form['delete'])) { foreach ($form['delete'] as $itemId => $junk) { unset($cartItemIds[$itemId]); } } /* Update the cart values */ $ret = CartHelper::setCartItemCounts($cartItemIds); if ($ret) { return array($ret->wrap(__FILE__, __LINE__), null); } $status['cartModified'] = 1; break; case 'emptyCart': /* Empty the cart */ $ret = CartHelper::setCartItemCounts(array()); if ($ret) { return array($ret->wrap(__FILE__, __LINE__), null); } $status['cartModified'] = 1; break; default: /* Expect it to be one of our plugins */ GalleryCoreApi::requireOnce( 'modules/cart/classes/CartPluginInterface_1_0.class'); list ($ret, $cartPlugin) = GalleryCoreApi::newFactoryInstanceById('CartPluginInterface_1_0', $form['pluginId']); if ($ret) { return array($ret->wrap(__FILE__, __LINE__), null); } if (!isset($cartPlugin)) { break; } /* Pass in only the types the plugin supports */ list ($ret, $itemList) = GalleryCoreApi::loadEntitiesById(array_keys($cartItemIds)); if ($ret) { return array($ret->wrap(__FILE__, __LINE__), null); } $fulfillmentCart = array(); $supportedTypes = $cartPlugin->getSupportedItemTypes(); foreach ($itemList as $item) { foreach ($supportedTypes as $type) { if ($type == '*' || GalleryUtilities::isA($item, $type)) { $fulfillmentCart[$item->getId()] = $cartItemIds[$item->getId()]; break; } } } list ($ret, $redirect) = $cartPlugin->fulfillCart($fulfillmentCart); if ($ret) { return array($ret->wrap(__FILE__, __LINE__), null); } /* Remove the fulfilled items */ foreach (array_keys($fulfillmentCart) as $id) { unset($cartItemIds[$id]); } /* Save the modified cart */ $ret = CartHelper::setCartItemCounts($cartItemIds); if ($ret) { return array($ret->wrap(__FILE__, __LINE__), null); } if (!isset($redirect['href'])) { /* If we're redirecting offsite, don't include the status */ $status['cartModified'] = 1; } /* Redirect according to the wishes of the cart plugin */ $results['redirect'] = $redirect; break; } } /* Prepare our results */ if (!isset($results['redirect'])) { $results['redirect']['view'] = 'cart.ViewCart'; } $results['status'] = $status; $results['error'] = $error; return array(null, $results); } } ?>