<?php
/*
 * $RCSfile: ConfirmPhotos.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 confirm their order
 *
 * @package Checkout
 * @subpackage UserInterface
 */
class ConfirmPhotosView extends GalleryView {

    /**
     * @see GalleryView::loadTemplate
     */
    function loadTemplate(&$template, &$form) {
        global $gallery;
        $session =& $gallery->getSession();

        $total = unserialize($session->get('checkout.total'));

        /* Get the items from the cart */
        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);
        }

        /* Get the product and pricing details */	
        list ($ret, $product, $price) = CheckoutHelper::fetchProducts();
        if ($ret) {
            return array($ret->wrap(__FILE__, __LINE__), null);
        }

	/* Remove items with zero quantity selected */
	if (!empty($items) && is_array($items)) {
	    foreach ($itemList as $item) {
	    	$itemNotZero=false;
	    	for($x=0;$x<count($product);$x++) {
		    if ( isset($items[$item->getId()]['quant'][$x]) ) {
		    	if ((int) $items[$item->getId()]['quant'][$x] > 0 ) {
		    	    $itemNotZero=true;
		    	}
		    }
	    	}
	    	if ( !$itemNotZero ) {
	    	    unset($items[$item->getId()]);
	    	}
	    }
	}
	
        /* Get the paper options */	
        list ($ret, $paper) = CheckoutHelper::fetchPapers();
        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);
        }
        foreach ($thumbnailList as $thumbnail) {
            $thumbnails[$thumbnail->getParentId()] = (array)$thumbnail;
        }
	
        /* Find all our payment plugins */
        GalleryCoreApi::requireOnce('modules/checkout/classes/CheckoutPluginInterface_1_0.class');
        list ($ret, $ids) =
            GalleryCoreApi::getAllFactoryImplementationIds('CheckoutPluginInterface_1_0');
        if ($ret) {
            return array($ret->wrap(__FILE__, __LINE__), null);
        }
        $plugins = array();
        foreach ($ids as $id => $className) {
            list ($ret, $instances[$id]) =
            GalleryCoreApi::newFactoryInstance('CheckoutPluginInterface_1_0', $className);
            if ($ret) {
                return array($ret->wrap(__FILE__, __LINE__), null);
            }

            /*
             * Get the plugin parameters
             */
            list ($ret, $plugins[$id]['paymentText']) = $instances[$id]->getPaymentText();
            if ($ret) {
                return array($ret->wrap(__FILE__, __LINE__), null);
            }
            list ($ret, $plugins[$id]['paymentTemplate']) = $instances[$id]->getPaymentTemplate();
            if ($ret) {
                return array($ret->wrap(__FILE__, __LINE__), null);
            }
            list ($ret, $plugins[$id]['paymentVariables']) = $instances[$id]->getPaymentVariables();
            if ($ret) {
                return array($ret->wrap(__FILE__, __LINE__), null);
            }
        }

        list($ret, $tpl) =
            GeneralHelper::getModuleParameters('checkout', array('csymbol',
                                                                 'postage',
                                                                 'busname'));
        if ($ret) {
            return array($ret->wrap(__FILE__, __LINE__), null);
        }
        
        $tpl['items'] = $items;
        $tpl['thumbnails'] = $thumbnails;
        $tpl['price'] = $price;
        $tpl['product'] = $product;
        $tpl['paper'] = $paper;
        $tpl['total'] = $total;
        $tpl['controller'] = 'checkout.Confirm';
        $tpl['payments'] = $plugins;

        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/ConfirmPhotos.tpl'));
    }
}
?>