<?php
/*
 * $RCSfile: CartHelper.class,v $
 *
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2005 Bharat Mediratta
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */

/**
 * @package Cart
 * @version $Revision: 1.11 $ $Date: 2005/09/03 13:44:59 $
 * @author Bharat Mediratta <bharat@menalto.com>
 */

/**
 * A helper class for Carts
 *
 * Utility functions useful in managing Carts
 *
 * @package Cart
 * @subpackage Classes
 */
class CartHelper {

    /**
     * Return the ids of the items in the cart
     *
     * @return array object GalleryStatus a status code
     *               array (itemId => count, itemId => count, ..)
     * @static
     */
    function fetchCartItemCounts() {
	global $gallery;
	$session =& $gallery->getSession();

	$cartItemIds = $session->get('cart.itemIds');
	if (empty($cartItemIds) || !is_array($cartItemIds)) {
	    $cartItemIds = array();
	}

	return array(GalleryStatus::success(), $cartItemIds);
    }

    /**
     * Add the item ids to the user's cart
     * (Note that permissions should be checked in advance; this function does not check)
     *
     * @param array int item ids
     * @return object GalleryStatus a status code
     * @static
     */
    function addItemsToCart($ids) {
	list ($ret, $cartItemIds) = CartHelper::fetchCartItemCounts();
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	foreach ($ids as $id) {
	    if (isset($cartItemIds[$id])) {
		$cartItemIds[$id]++;
	    } else {
		$cartItemIds[$id] = 1;
	    }
	}

	$ret = CartHelper::setCartItemCounts($cartItemIds);
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	return GalleryStatus::success();
    }

    /**
     * Store cart contents
     *
     * @param array (itemId => count, itemId => count, ..)
     * @return object GalleryStatus a status code
     * @static
     */
    function setCartItemCounts($cartItemIds) {
	global $gallery;
	$session =& $gallery->getSession();

	if (empty($cartItemIds)) {
	    $session->remove('cart.itemIds');
	} else {
	    $session->put('cart.itemIds', $cartItemIds);
	}

	return GalleryStatus::success();
    }

    /**
     * Load entities for all cart items.
     * Remove any invalid ids from cart.
     *
     * @return array object GalleryStatus a status code
     *               array of items
     * @static
     */
    function loadCartItems() {
	list ($ret, $cartItemIds) = CartHelper::fetchCartItemCounts();
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}
	$idList = array_keys($cartItemIds);
	if (!empty($cartItemIds)) {
	    $ret = GalleryCoreApi::studyPermissions($idList);
	    if ($ret->isError()) {
		return array($ret->wrap(__FILE__, __LINE__), null);
	    }
	}
	$changed = false;
	$items = array();
	foreach ($idList as $itemId) {
	    list ($ret, $item) = GalleryCoreApi::loadEntitiesById($itemId);
	    if ($ret->isError()) {
		if ($ret->getErrorCode() & ERROR_MISSING_OBJECT) {
		    /* Item deleted from the gallery; remove it from the cart */
		    unset($cartItemIds[$itemId]);
		    $changed = true;
		    continue;
		} else {
		    return array($ret->wrap(__FILE__, __LINE__), null);
		}
	    }
	    list ($ret, $canView) = GalleryCoreApi::hasItemPermission($itemId, 'core.view');
	    if ($ret->isError()) {
		return array($ret->wrap(__FILE__, __LINE__), null);
	    }
	    if ($canView) {
		$items[] = $item;
	    } else {
		unset($cartItemIds[$itemId]);
		$changed = true;
	    }
	}
	if ($changed) {
	    $ret = CartHelper::setCartItemCounts($cartItemIds);
	    if ($ret->isError()) {
		return array($ret->wrap(__FILE__, __LINE__), null);
	    }
	}
    
	return array(GalleryStatus::success(), $items);
    }
}
?>
