<?php
/*
 * $RCSfile: SelectProducts.inc,v $
 *
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2004 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

/**
 * @version
 * @package Checkout
 * @author Daniel Leaberry <leaberry@gmail.com>
 * @author Matthew Turnbull <matthew.turnbull@messagescan.net>
 */

/**
 * Required classes
 */
GalleryCoreApi::requireOnce('modules/checkout/classes/CheckoutHelper.class');
GalleryCoreApi::requireOnce('modules/checkout/classes/GeneralHelper.class');

/**
 * This view is for letting the user select the sizes and quantities for ordering.
 * It is the entry point to the user's experience of the module, acting as the
 * 'view my cart' page
 *
 * @package Checkout
 * @subpackage UserInterface
 */
class SelectProductsView extends GalleryView {

    /**
     * @see GalleryView::loadTemplate
     */
    function loadTemplate(&$template, &$form) {
        global $gallery;
        $session =& $gallery->getSession();	
	
	list($ret) = CheckoutHelper::updatePerItemPricing();
        if ($ret) {
            return array($ret->wrap(__FILE__, __LINE__), null);
    	}
    	
        list ($ret, $items) = CheckoutHelper::fetchCheckoutItems();
        if ($ret) {
            return array($ret->wrap(__FILE__, __LINE__), null);
    	}

        list ($ret, $itemList) = CheckoutHelper::loadCheckoutItems();
        if ($ret) {
            return array($ret->wrap(__FILE__, __LINE__), null);
        }

        list ($ret, $itemCount) = CheckoutHelper::countCheckoutItems();
        if ($ret) {
            return array($ret->wrap(__FILE__, __LINE__), null);
        }

        /* Get the thumbnails */
        list ($ret, $thumbnailList) = GalleryCoreApi::fetchThumbnailsByItemIds(array_keys($items));
        if ($ret) {
            return array($ret->wrap(__FILE__, __LINE__), null);
        }
        $thumbnails = array();
        foreach ($thumbnailList as $thumbnail) {
            $thumbnails[$thumbnail->getParentId()] = (array)$thumbnail;
        }

        /* Get the product and pricing details */	
        list ($ret, $product, $price) = CheckoutHelper::fetchProducts();
        if ($ret) {
            return array($ret->wrap(__FILE__, __LINE__), null);
        }

        /* Get the paper options */	
        list ($ret, $paper) = CheckoutHelper::fetchPapers();
        if ($ret) {
            return array($ret->wrap(__FILE__, __LINE__), null);
        }

        /* Get the rest of the module parameters */
        list($ret, $tpl) =
            GeneralHelper::getModuleParameters('checkout', array('csymbol',
                                                                 'postage',
                                                                 'selecttype',
                                                                 'busname',
                                                                 'numProducts'));
        if ($ret) {
            return array($ret->wrap(__FILE__, __LINE__), null);
        }
        
        $returnUrl = $template->getVariable('status');
        
    	/* Add more items to the template variable */
        if (isset($returnUrl['returnUrl'])) {
            $tpl['returnUrl'] = $returnUrl['returnUrl'];
        }
        $tpl['items'] = $items;
        $tpl['thumbnails'] = $thumbnails;
        $tpl['itemCount'] = $itemCount;
        $tpl['product'] = $product;
        $tpl['price'] = $price;
        $tpl['paper'] = $paper;
        $tpl['controller'] = 'checkout.Confirm';

        list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'checkout');
        if ($ret) {
            return array($ret->wrap(__FILE__, __LINE__), null);
        }
        $template->title($tpl['busname'] . '::' . $gallery->i18n('Checkout'));
        $template->setVariable('tpl', $tpl);
	   
        return array(null,
                array('body' => 'modules/checkout/templates/SelectProducts.tpl'));
    }
}
?>
