<?php/* * $RCSfile: module.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/CheckoutHelper.class');/** * Cart plugin to Checkout the files via paypal * * @package Checkout */class CheckoutModule extends GalleryModule {    function CheckoutModule() {	global $gallery;	$this->setId('checkout');	$this->setName('Checkout');	$this->setDescription($gallery->i18n('Shopping cart module supporting different products and payment'));	$this->setVersion('0.1.14');	$this->setGroup('commerce', $gallery->i18n('Commerce'));	$this->setCallbacks('registerEventListeners|getSiteAdminViews|getItemLinks');	$this->setRequiredCoreApi(array(7, 2));	$this->setRequiredModuleApi(array(3, 0));    }    /**     * @see GalleryModule::performFactoryRegistrations()     */    function performFactoryRegistrations() {	/* Register the GalleryCheckoutTransaction class */	$ret = GalleryCoreApi::registerFactoryImplementation(	    'GalleryEntity', 'GalleryCheckoutTransaction', 'GalleryCheckoutTransaction',	    'modules/checkout/classes/GalleryCheckoutTransaction.class', 'checkout', null);	if ($ret) {	    return $ret->wrap(__FILE__, __LINE__);	}	/* Register the GalleryCheckoutItem class */	$ret = GalleryCoreApi::registerFactoryImplementation(	    'GalleryEntity', 'GalleryCheckoutItem', 'GalleryCheckoutItem',	    'modules/checkout/classes/GalleryCheckoutItem.class', 'checkout', null);	if ($ret) {	    return $ret->wrap(__FILE__, __LINE__);	}	return null;    }    /**     * @see GalleryModule::registerEventListeners()     */    function registerEventListeners() {        GalleryCoreApi::registerEventListener('GalleryCheckout::setflagCleared', new CheckoutModule());        GalleryCoreApi::registerEventListener('GalleryCheckout::emptyCart', new CheckoutModule());    }    /**     * @see GalleryModule::upgrade()     */    function upgrade($currentVersion) {	global $gallery;	if (!empty($currentVersion)) {	    $storage =& $gallery->getStorage();	    switch ($currentVersion) {	    case '0.1.1':	    case '0.1.2':	    case '0.1.3':	    case '0.1.4':	    case '0.1.5':		/* First standalone version.  Register our permissions */		$permissions[] = array('purchase', $gallery->i18n('[checkout] Purchase item'), 0, array());		foreach ($permissions as $p) {		    $ret = GalleryCoreApi::registerPermission($this->getId(),							      'checkout.' . $p[0],							      $p[1], $p[2], $p[3]);		    if ($ret) {			return $ret->wrap(__FILE__, __LINE__);		    }		}		/* Do the schema upgrade */		$ret = $storage->configureStore($this->getId(), array('GalleryCheckoutTransaction:1.0'));		if ($ret) {		    return $ret->wrap(__FILE__, __LINE__);		}	    case '0.1.6':		/* Do the schema upgrade */		$ret = $storage->configureStore($this->getId(), array('GalleryCheckoutTransaction:1.1'));		if ($ret) {		    return $ret->wrap(__FILE__, __LINE__);		}		/* Convert the old integer amounts to strings by dividing by 100 */		$query = '	        UPDATE	          [GalleryCheckoutTransaction]	        SET	          [::amount] = [::amount]/100	        ';		$ret = $storage->execute($query);		if ($ret) {		    return $ret->wrap(__FILE__, __LINE__);		}	    	    case '0.1.7':		/* Do the schema upgrade */		$ret = $storage->configureStore($this->getId(), array('GalleryCheckoutTransaction:1.2'));		if ($ret) {		    return $ret->wrap(__FILE__, __LINE__);		}	        /* For transactions with an email address, set flagPaid */	        $query = '	            UPDATE	              [GalleryCheckoutTransaction]	            SET	              [::flagPaid] = 1	            WHERE	              [::email] IS NOT NULL	            ';	        $ret = $storage->execute($query);	        if ($ret) {	            return $ret->wrap(__FILE__, __LINE__);	        }        	        /*	         * For transactions with CheckoutPaypal children, set	         * paymentPlugin=checkoutpaypal	         * Only run this step if CheckoutPaypal is activated	         * 	         */	        list ($ret, $moduleStatus) = GalleryCoreApi::fetchPluginStatus('module', true);	        if ($ret) {	            return array($ret->wrap(__FILE__, __LINE__), null);	        }	        if (!empty($moduleStatus['checkoutpaypal']['active'])) {	            $query = '	                UPDATE	                  [GalleryCheckoutTransaction]	                INNER JOIN	                  [GalleryChildEntity]	                ON	                  [GalleryChildEntity::parentId] = [GalleryCheckoutTransaction::id]	                SET	                  [GalleryCheckoutTransaction::paymentPlugin] = \'checkoutpaypal\'	                ';	            $ret = $storage->execute($query);	            if ($ret) {	                return $ret->wrap(__FILE__, __LINE__);	            }	        }        	        /* For all other transactions, set paymentPlugin=checkoutemail */	        $query = '	            UPDATE	              [GalleryCheckoutTransaction]	            SET	              [::paymentPlugin] = \'checkoutemail\'	            WHERE	              [::paymentPlugin] IS NULL	            ';	        $ret = $storage->execute($query);	        if ($ret) {	            return $ret->wrap(__FILE__, __LINE__);	        }        	    case '0.1.8':		/* Do the schema upgrade */		$ret = $storage->configureStore($this->getId(), array('GalleryCheckoutTransaction:1.3'));		if ($ret) {		    return $ret->wrap(__FILE__, __LINE__);		}	    case '0.1.9':	    case '0.1.10':	    case '0.1.11':	    case '0.1.12':		/* DON'T do the schema upgrade - there is a problem changing the INTEGER size in G2.1		 * $ret = $storage->configureStore($this->getId(), array('GalleryCheckoutItem:1.0'));		 * if ($ret) {		 *     return $ret->wrap(__FILE__, __LINE__);		 * }		 */	    case '0.1.13':	    	    case 'end of upgrade path':		break;	    default:		return GalleryCoreApi::error(ERROR_BAD_PLUGIN, __FILE__, __LINE__,					    sprintf('Unknown module version %s', $currentVersion));	    }	}	if (!isset($currentVersion)) {	    /* Initial install.  Register our permissions */	    $permissions[] = array('purchase', $gallery->i18n('[checkout] Purchase item'), 0, array());	    foreach ($permissions as $p) {		$ret = GalleryCoreApi::registerPermission($this->getId(),							  'checkout.' . $p[0],							  $p[1], $p[2], $p[3]);		if ($ret) {		    return $ret->wrap(__FILE__, __LINE__);		}	    }	}	return null;    }    /* BLOCK REMOVED as not yet stable    /**     * @see GalleryModule::activate     *    function activate($postActivationEvent=true) {	    /* Add the Cart Info block to the sidebars of the installed themes *	    list ($ret, $themes) = GalleryCoreApi::fetchPluginStatus('theme', true);	    if ($ret) {		return array($ret->wrap(__FILE__, __LINE__), null);	    }	    foreach ($themes as $id => $status) {		list ($ret, $theme) = GalleryCoreApi::loadPlugin('theme', $id);		if ($ret) {		    return array($ret->wrap(__FILE__, __LINE__), null);		}		$themeSettings = $theme->getStandardSettings();		if ($ret) {		    return array($ret->wrap(__FILE__, __LINE__), null);		}		$sidebarBlocks = array();		$sidebarBlocks = unserialize($themeSettings['sidebarBlocks']);		if (count($sidebarBlocks) > 0) {	    	    $sidebarBlocks = array(array('checkout.ShowCart', array()),	    				   $sidebarBlocks);		} else {	    	    $sidebarBlocks = array(array('checkout.ShowCart', array()));		}		$themeSettings['sidebarBlocks'] = serialize($sidebarBlocks);		$theme->setStandardSettings($themeSettings);		if ($ret) {		    return array($ret->wrap(__FILE__, __LINE__), null);		}	    }	list ($ret, $redirect) = parent::activate($postActivationEvent);	if ($ret) {	    return array($ret->wrap(__FILE__, __LINE__), null);	}	return array(null, $redirect);    }     * /    /**     * @see GalleryModule::needsConfiguration     */    function needsConfiguration() {	list ($ret, $value) = $this->getParameter('product0');	if ($ret) {	    return array($ret->wrap(__FILE__, __LINE__), null);	}	return array(null, empty($value));    }    /**     * @see GalleryModule::getSiteAdminViews     */    function getSiteAdminViews() {	global $gallery;			return array(null, array(array('name' => $gallery->i18n('Checkout'), 							   'view' => 'checkout.Admin')));    }    /**     * @see GalleryModule::getConfigurationView     */    function getConfigurationView() {	return 'checkout.Admin';    }    /**     * @see GalleryModule::getItemLinks()     */    function getItemLinks($items, $wantsDetailedLinks, $permissions) {	$links = array();	foreach ($items as $item) {        //if (GalleryUtilities::isA($item, 'GalleryAlbumItem')) {            //continue;        //}	    if (isset($permissions[$item->getId()]['checkout.purchase'])) {		$itemTypeNames = $item->itemTypeName();		$links[$item->getId()][] =		    array('text' => $this->translate(array('text' => 'Price List',							   'arg1' => $itemTypeNames[0])),		          'params' => array('controller' => 'checkout.AddToCart',					    'itemId' => $item->getId(),					    'return' => 1));	    }	}	return array(null, $links);    }    /**     * Event handler for GalleryCheckout::* events     *     * @see GalleryEventListener::handleEvent     */    function handleEvent($event) {    $result = null;    if ( substr($event->getEventName(), 0, 24) == 'GalleryCheckout::setFlag') {        $entity = $event->getEntity();        if (GalleryUtilities::isA($entity, 'GalleryCheckoutTransaction')) {                        /* Check the transaction isn't in 'Hold' state */            if ( !$entity->getFlagHold() ) {                                /* Get a write lock and refresh the entity */                list ($ret, $lockId) = GalleryCoreApi::acquireWriteLock(array($entity->getId()));                if ($ret) {                    return array($ret->wrap(__FILE__, __LINE__), null);                }                list ($ret, $entity) = $entity->refresh();                if ($ret) {                    return array($ret->wrap(__FILE__, __LINE__), null);                }                            /* Set the relevant flag and take any appropriate actions */                switch ($event->getEventName()) {                    case 'GalleryCheckout::setFlagPaid':                         $entity->setFlagPaid(TRUE);                        break;                    case 'GalleryCheckout::setFlagCleared':                        $entity->setFlagCleared(TRUE);                        break;                    case 'GalleryCheckout::setFlagFulfil':                        $entity->setFlagFulfil(TRUE);                        break;                    case 'GalleryCheckout::setFlagFulfilled':                        $entity->setFlagFulfilled(TRUE);                        break;                    case 'GalleryCheckout::setFlagCompleted':                        $entity->setFlagCompleted(TRUE);                        break;                    case 'GalleryCheckout::setFlagHold':                        $entity->setFlagHold(TRUE);                        break;                    case 'GalleryCheckout::setFlagProblem':                        $entity->setFlagProblem(TRUE);                        break;                }                /* Save the modified transaction and release our lock */                $ret = $entity->save();                if ($ret) {                    return array($ret->wrap(__FILE__, __LINE__), null);                }                $ret = GalleryCoreApi::releaseLocks($lockId);                if ($ret) {                    return array($ret->wrap(__FILE__, __LINE__), null);                }            }        }    } elseif ($event->getEventName() == 'GalleryCheckout::emptyCart') {        $ret = CheckoutHelper::emptyCart();        if ($ret) {            return array($ret->wrap(__FILE__, __LINE__), null);        }    }    return array(null, $result);    }}?>