<?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 CheckoutEmail
 * @author Matthew Turnbull <matthew.turnbull@messagescan.net>
 */

/**
 * @package CheckoutEmail
 * @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'])) {
	    $ret = GalleryCoreApi::setPluginParameter('module', 'checkoutemail', 'paymentText', $form['paymentText']);
	    if ($ret) {
		return array($ret->wrap(__FILE__, __LINE__), null);
	    }
	    $ret = GalleryCoreApi::setPluginParameter('module', 'checkoutemail', 'paymentButtonText', $form['paymentButtonText']);
	    if ($ret) {
		return array($ret->wrap(__FILE__, __LINE__), null);
	    }
	    $ret = GalleryCoreApi::setPluginParameter('module', 'checkoutemail', 'fromAddress', $form['fromAddress']);
	    if ($ret) {
		return array($ret->wrap(__FILE__, __LINE__), null);
	    }
	    $ret = GalleryCoreApi::setPluginParameter('module', 'checkoutemail', 'subject', $form['subject']);
	    if ($ret) {
		return array($ret->wrap(__FILE__, __LINE__), null);
	    }
	    
	    $numAddresses=0;
	    $numErased=0;
    	    for($i=0;$i<count($form['toAddress']);$i++) {
	        if (!empty($form['toAddress'][$i])) {
		    /* Only write real products to the module parameters */
		    $ret = GalleryCoreApi::setPluginParameter('module', 'checkoutemail', 'toAddress'.$numAddresses, $form['toAddress'][$i]);
		    if ($ret) {
		        return array($ret->wrap(__FILE__, __LINE__), null);
		    }
		    if ($form['showCustomer'][$i]) {
		        $temp = 1;
		    } else {
		        $temp = 0;
		    }					
		    $ret = GalleryCoreApi::setPluginParameter('module', 'checkoutemail', 'showCustomer'.$numAddresses, $temp);
		    if ($ret) {
		        return array($ret->wrap(__FILE__, __LINE__), null);
		    }					
		    if ($form['showThumbs'][$i]) {
		        $temp = 1;
		    } else {
		        $temp = 0;
		    }					
		    $ret = GalleryCoreApi::setPluginParameter('module', 'checkoutemail', 'showThumbs'.$numAddresses, $temp);
		    if ($ret) {
		        return array($ret->wrap(__FILE__, __LINE__), null);
		    }					
		    if ($form['showLinks'][$i]) {
		        $temp = 1;
		    } else {
		        $temp = 0;
		    }					
		    $ret = GalleryCoreApi::setPluginParameter('module', 'checkoutemail', 'showLinks'.$numAddresses, $temp);
		    if ($ret) {
		        return array($ret->wrap(__FILE__, __LINE__), null);
		    }
		    $numAddresses++;
		} else {
		    /* Explicitly erase whatever was previously set */
		    $ret = GalleryCoreApi::removePluginParameter('module', 'checkoutemail', 'toAddress'.($numAddresses+$numErased));
		    if ($ret) {
		        return array($ret->wrap(__FILE__, __LINE__), null);
		    }
		    $ret = GalleryCoreApi::removePluginParameter('module', 'checkoutemail', 'showCustomer'.($numAddresses+$numErased));
		    if ($ret) {
		        return array($ret->wrap(__FILE__, __LINE__), null);
		    }
		    $ret = GalleryCoreApi::removePluginParameter('module', 'checkoutemail', 'showThumbs'.($numAddresses+$numErased));
		    if ($ret) {
		        return array($ret->wrap(__FILE__, __LINE__), null);
		    }
		    $ret = GalleryCoreApi::removePluginParameter('module', 'checkoutemail', 'showLinks'.($numAddresses+$numErased));
		    if ($ret) {
		        return array($ret->wrap(__FILE__, __LINE__), null);
		    }
		    $numErased++;
		}
	    }
	    $ret = GalleryCoreApi::setPluginParameter('module', 'checkoutemail', 'numAddresses', $numAddresses);
	    if ($ret) {
		return array($ret->wrap(__FILE__, __LINE__), null);
	    }
        	    
        list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'checkoutemail');
        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'] = 'checkoutemail.Admin';
            $status['saved'] = 1;
        } else {
            $redirect['subView'] = 'core.AdminModules';
            $status['configured'] = 'Checkout by Email';
        }

	} else if (isset($form['action']['cancel'])) {
	    $redirect['view'] = 'core.SiteAdmin';
	    $redirect['subView'] = 'checkoutemail.Admin';
	}

	if (!empty($redirect)) {
	    $results['redirect'] = $redirect;
	} else {
	    $results['delegate']['view'] = 'core.SiteAdmin';
	    $results['delegate']['subView'] = 'checkoutemail.Admin';
	}

	$results['status'] = $status;
	$results['error'] = $error;

	return array(null, $results);
    }
}

/**
 * @package CheckoutEmail
 * @subpackage UserInterface
 */
class AdminView extends GalleryView {

    /**
     * @see GalleryView::loadTemplate
     */
    function loadTemplate(&$template, &$form) {
	global $gallery;

	if ($form['formName'] != 'AdminCheckoutEmail') {
	    $form['formName'] = 'AdminCheckoutEmail';  

	    list ($ret, $form['paymentText']) = GalleryCoreApi::getPluginParameter('module', 'checkoutemail', 'paymentText');
	    if ($ret) {
	        return array($ret->wrap(__FILE__, __LINE__), null);
	    }
	    list ($ret, $form['paymentButtonText']) = GalleryCoreApi::getPluginParameter('module', 'checkoutemail', 'paymentButtonText');
	    if ($ret) {
	        return array($ret->wrap(__FILE__, __LINE__), null);
	    }
	    list ($ret, $form['fromAddress']) = GalleryCoreApi::getPluginParameter('module', 'checkoutemail', 'fromAddress');
	    if ($ret) {
	        return array($ret->wrap(__FILE__, __LINE__), null);
	    }
	    list ($ret, $form['subject']) = GalleryCoreApi::getPluginParameter('module', 'checkoutemail', 'subject');
	    if ($ret) {
	        return array($ret->wrap(__FILE__, __LINE__), null);
	    }

	    list ($ret, $numAddresses) = GalleryCoreApi::getPluginParameter('module', 'checkoutemail', 'numAddresses');
	    if ($ret) {
	        return array($ret->wrap(__FILE__, __LINE__), null);
	    }
	    for($i=0;$i<max(3, $numAddresses+1);$i++) {
	    	list ($ret, $form['toAddress'][$i]) = GalleryCoreApi::getPluginParameter('module', 'checkoutemail', 'toAddress'.$i);
		if ($ret) {
		    return array($ret->wrap(__FILE__, __LINE__), null);
		}					
	    	list ($ret, $temp) = GalleryCoreApi::getPluginParameter('module', 'checkoutemail', 'showCustomer'.$i);
		if ($ret) {
		    return array($ret->wrap(__FILE__, __LINE__), null);
		}
		if ($temp==1) {
		    $form['showCustomer'][$i] = true;
		} else {					
		    $form['showCustomer'][$i] = false;
		}
	    	list ($ret, $temp) = GalleryCoreApi::getPluginParameter('module', 'checkoutemail', 'showThumbs'.$i);
		if ($ret) {
		    return array($ret->wrap(__FILE__, __LINE__), null);
		}					
		if ($temp==1) {
		    $form['showThumbs'][$i] = true;
		} else {					
		    $form['showThumbs'][$i] = false;
		}
	    	list ($ret, $temp) = GalleryCoreApi::getPluginParameter('module', 'checkoutemail', 'showLinks'.$i);
		if ($ret) {
		    return array($ret->wrap(__FILE__, __LINE__), null);
		}					
		if ($temp==1) {
		    $form['showLinks'][$i] = true;
		} else {					
		    $form['showLinks'][$i] = false;
		}
	    }	    

	}

	$template->setVariable('controller', 'checkoutemail.Admin');
	return array(null,
		     array('body' => 'modules/checkoutemail/templates/Admin.tpl'));
    }
}
?>
