<?php
/*
 * $RCSfile: Admin.inc,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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
/**
 * 
 * @package Checkout
 * @author Daniel Leaberry <leaberry@gmail.com>
 * @author Matthew Turnbull <matthew.turnbull@messagescan.net>
 */

/**
 * Required classes
 */
GalleryCoreApi::requireOnce('modules/checkout/classes/GeneralHelper.class');

/**
 * @package Checkout
 * @subpackage UserInterface
 */
class AdminController extends GalleryController {

    /**
     * @see GalleryController::handleRequest
     */
  function handleRequest($form) {
	global $gallery;

	$ret = GalleryCoreApi::assertUserIsSiteAdministrator();
	if ($ret) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}

	$error = array();
	$status = array();

	if (isset($form['action']['save'])) {
	    
	    $numProducts=0;
	    for($i=0;$i<count($form['product']);$i++) {
		/* Explicitly erase whatever was previously set */
		$ret = GalleryCoreApi::removePluginParameter('module', 'checkout', 'product'.$i);
		if ($ret) {
		    return array($ret->wrap(__FILE__, __LINE__), null);
		}
		$ret = GalleryCoreApi::removePluginParameter('module', 'checkout', 'price'.$i);
		if ($ret) {
		    return array($ret->wrap(__FILE__, __LINE__), null);
		}
	        if (!empty($form['product'][$i])) {
		    /* Only write real products to the module parameters */
		    $ret = GalleryCoreApi::setPluginParameter(
                    'module', 'checkout', 'product'.$numProducts, $form['product'][$i]);
		    if ($ret) {
			return array($ret->wrap(__FILE__, __LINE__), null);
		    }
		    if ( ((string)(float)$form['price'][$i]) == $form['price'][$i] ) {
		    	$newprice = $form['price'][$i];
		    } else {
		    	$newprice = (float)$form['price'][$i];
		    }
		    $ret = GalleryCoreApi::setPluginParameter(
					'module', 'checkout', 'price'.$numProducts, $newprice);
		    if ($ret) {
			return array($ret->wrap(__FILE__, __LINE__), null);
		    }
		    $numProducts++;
		}
	    }
	    $ret = GalleryCoreApi::setPluginParameter(
                'module', 'checkout', 'numProducts', $numProducts);
	    if ($ret) {
		return array($ret->wrap(__FILE__, __LINE__), null);
	    }	    
	    $numPapers=0;
	    for($i=0;$i<count($form['paper']);$i++) {
		$ret = GalleryCoreApi::removePluginParameter(
                'module', 'checkout', 'paper'.$i);
		if ($ret) {
		    return array($ret->wrap(__FILE__, __LINE__), null);
		}
	        if (!empty($form['paper'][$i])) {
		    /* Only write real papers to the module parameters */
		    $ret = GalleryCoreApi::setPluginParameter(
                    'module', 'checkout', 'paper'.$numPapers, $form['paper'][$i]);
		    if ($ret) {
			return array($ret->wrap(__FILE__, __LINE__), null);
		    }					
		    $numPapers++;
		}
	    }
	    $ret = GalleryCoreApi::setPluginParameter(
                'module', 'checkout', 'numPapers', $numPapers);
	    if ($ret) {
		return array($ret->wrap(__FILE__, __LINE__), null);
	    }	    
	    $ret = GalleryCoreApi::setPluginParameter(
                'module', 'checkout', 'busname', $form['busname']);
	    if ($ret) {
		return array($ret->wrap(__FILE__, __LINE__), null);
	    }	
	    $ret = GalleryCoreApi::setPluginParameter(
                'module', 'checkout', 'csymbol', $form['csymbol']);
	    if ($ret) {
		return array($ret->wrap(__FILE__, __LINE__), null);
	    }
    	    $ret = GalleryCoreApi::setPluginParameter(
                'module', 'checkout', 'postage', $form['postage']);
	    if ($ret) {
		return array($ret->wrap(__FILE__, __LINE__), null);
	    }			
        $ret = GalleryCoreApi::setPluginParameter(
                'module', 'checkout', 'selecttype', $form['selecttype']);
        if ($ret) {
            return array($ret->wrap(__FILE__, __LINE__), null);
        }
        if ( !isset($form['cartredirect']) ) {
            $form['cartredirect'] = 0;
        } else {
            $form['cartredirect'] = 1;
        }
        $ret = GalleryCoreApi::setPluginParameter(
                'module', 'checkout', 'cartredirect', $form['cartredirect']);
        if ($ret) {
            return array($ret->wrap(__FILE__, __LINE__), null);
        }           

        list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'checkout');
        if ($ret) {
            return array($ret->wrap(__FILE__, __LINE__), null);
        }
        list ($ret, $isActive) = $module->isActive();
        if ($ret) {
            return array($ret->wrap(__FILE__, __LINE__), null);
        }

        $redirect['view'] = 'core.SiteAdmin';
        if ($isActive) {
            $redirect['subView'] = 'checkout.Admin';
            $status['saved'] = 1;
        } else {
            $redirect['subView'] = 'core.AdminModules';
            $status['configured'] = 'checkout';
        }

	} else if (isset($form['action']['cancel'])) {
	    $redirect['view'] = 'core.SiteAdmin';
	    $redirect['subView'] = 'checkout.Admin';
	}

	if (!empty($redirect)) {
	    $results['redirect'] = $redirect;
	} else {
	    $results['delegate']['view'] = 'core.SiteAdmin';
	    $results['delegate']['subView'] = 'checkout.Admin';
	}
	$results['status'] = $status;
	$results['error'] = $error;

	return array(null, $results);
    }
}

/**
 * @package Checkout
 * @subpackage UserInterface
 */
class AdminView extends GalleryView {

    /**
     * @see GalleryView::loadTemplate
     */
    function loadTemplate(&$template, &$form) {
	global $gallery;

	if ($form['formName'] != 'AdminCheckout') {
        /* Get module options */
        list ($ret, $form) = GeneralHelper::getModuleParameters('checkout', array('busname',
                                                                                  'csymbol',
                                                                                  'postage',
                                                                                  'selecttype',
                                                                                  'cartredirect'));
        if ($ret) {
                return array($ret->wrap(__FILE__, __LINE__), null);
        }
        if ( !isset($form['cartredirect']) ) {
            $form['cartredirect'] = 1;
        }
        if ( $form['cartredirect'] == 1 ) {
            $form['cartredirect'] = true;
        } else {
            $form['cartredirect'] = false;
        }       

        if ( !isset($form['selecttype']) ) {
            $form['selecttype'] = 'combo';
        }
        
	    $form['formName'] = 'AdminCheckout';  
	    
	    /* How many products do we have? */
	    list ($ret, $numProducts) = GalleryCoreApi::getPluginParameter(
                'module', 'checkout', 'numProducts');
	    if ($ret) {
	        return array($ret->wrap(__FILE__, __LINE__), null);
	    }
	    
	    /* Create the relevant number of product entry areas.
	     * Minimum will be 8 items, or at least 4 more than
	     * we currently have configured products
	     */
	    for($i=0;$i<max(8, $numProducts+4);$i++) {
	    	list ($ret, $form['product'][$i]) =
                    GalleryCoreApi::getPluginParameter('module', 'checkout', 'product'.$i);
		    if ($ret) {
					return array($ret->wrap(__FILE__, __LINE__), null);
		    }					
		    list ($ret, $form['price'][$i]) =
                    GalleryCoreApi::getPluginParameter('module', 'checkout', 'price'.$i);
		    if ($ret) {
					return array($ret->wrap(__FILE__, __LINE__), null);
		    }	    
	    }	    

	    /* How many papers do we have? */
	    list ($ret, $numPapers) = GalleryCoreApi::getPluginParameter(
                'module', 'checkout', 'numPapers');
	    if ($ret) {
	        return array($ret->wrap(__FILE__, __LINE__), null);
	    }
	    
	    /* Create the relevant number of paper entry areas.
	     * Minimum will be 3 items, or at least 2 more than
	     * we currently have configured papers
	     */
	    for($i=0;$i<max(3, $numPapers+2);$i++) {
	    	list ($ret, $form['paper'][$i]) =
                    GalleryCoreApi::getPluginParameter('module', 'checkout', 'paper'.$i);
		    if ($ret) {
					return array($ret->wrap(__FILE__, __LINE__), null);
		    }	    	
	    }    
	}

	$template->setVariable('controller', 'checkout.Admin');
	return array(null,
		     array('body' => 'modules/checkout/templates/Admin.tpl'));
    }
}
?>
