sessionization

This commit is contained in:
Colin Kuskie 2006-01-13 23:18:15 +00:00
parent 5c5c8075cf
commit 991f4bea9f
4 changed files with 60 additions and 28 deletions

View file

@ -134,11 +134,12 @@ Returns a reference to an array of all enabled instantiated payment plugins.
=cut =cut
sub getEnabledPlugins { sub getEnabledPlugins {
my ($session) = @_;
my (@enabledPlugins, $plugin, @plugins); my (@enabledPlugins, $plugin, @plugins);
@enabledPlugins = $self->session->db->buildArray("select namespace from commerceSettings where type='Shipping' and fieldName='enabled' and fieldValue='1'"); @enabledPlugins = $session->db->buildArray("select namespace from commerceSettings where type='Shipping' and fieldName='enabled' and fieldValue='1'");
foreach (@enabledPlugins) { foreach (@enabledPlugins) {
$plugin = WebGUI::Commerce::Shipping->load($_); $plugin = WebGUI::Commerce::Shipping->load($session, $_);
push(@plugins, $plugin) if ($plugin); push(@plugins, $plugin) if ($plugin);
} }
@ -158,19 +159,21 @@ The namespace of the plugin.
=cut =cut
sub init { sub init {
my ($class, $namespace, $properties, $shoppingCart); my ($class, $session, $namespace, $properties, $shoppingCart);
$class = shift; $class = shift;
$session = shift;
$namespace = shift; $namespace = shift;
$self->session->errorHandler->fatal('No namespace passed to init.') unless ($namespace); $session->errorHandler->fatal('No namespace passed to init.') unless ($namespace);
$properties = $self->session->db->buildHashRef("select fieldName, fieldValue from commerceSettings where namespace=".$self->session->db->quote($namespace)." and type='Shipping'"); $properties = $session->db->buildHashRef("select fieldName, fieldValue from commerceSettings where namespace=".$session->db->quote($namespace)." and type='Shipping'");
$shoppingCart = WebGUI::Commerce::ShoppingCart->new; $shoppingCart = WebGUI::Commerce::ShoppingCart->new($session);
bless {_properties=>$properties, bless {_properties=>$properties,
_shippingParameters => {}, _shippingParameters => {},
_shoppingCart => $shoppingCart, _shoppingCart => $shoppingCart,
_namespace=>$namespace, _namespace=>$namespace,
_session=>$session,
_enabled=>$properties->{enabled}, _enabled=>$properties->{enabled},
_shippingItems => []}, $class; _shippingItems => []}, $class;
} }
@ -203,16 +206,17 @@ The namespace of the plugin.
sub load { sub load {
my ($class, $namespace, $load, $cmd, $plugin); my ($class, $namespace, $load, $cmd, $plugin);
$class = shift; $class = shift;
my $session = shift;
$namespace = shift; $namespace = shift;
$self->session->errorHandler->fatal('No namespace passed to load.') unless ($namespace); $session->errorHandler->fatal('No namespace passed to load.') unless ($namespace);
$cmd = "WebGUI::Commerce::Shipping::$namespace"; $cmd = "WebGUI::Commerce::Shipping::$namespace";
$load = "use $cmd"; $load = "use $cmd";
eval($load); eval($load);
$self->session->errorHandler->warn("Shipping plugin failed to compile: $cmd.".$@) if($@); $session->errorHandler->warn("Shipping plugin failed to compile: $cmd.".$@) if($@);
$plugin = eval($cmd."->init"); $plugin = eval($cmd."->init");
$self->session->errorHandler->warn("Couldn't instantiate shipping plugin: $cmd.".$@) if($@); $session->errorHandler->warn("Couldn't instantiate shipping plugin: $cmd.".$@) if($@);
return $plugin; return $plugin;
} }
@ -225,7 +229,8 @@ Returns the (display) name of the plugin. You must override this method.
=cut =cut
sub name { sub name {
return $self->session->errorHandler->fatal("You must override the name method in the shipping plugin."); my ($session) = @_;
return $session->errorHandler->fatal("You must override the name method in the shipping plugin.");
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -298,6 +303,19 @@ sub processOptionsForm {
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 session
Returns the cached, local session variable.
=cut
sub session {
my ($self) = @_;
return $self->{_session};
}
#-------------------------------------------------------------------
=head2 setOptions ( options ) =head2 setOptions ( options )
Stores the supplied option hash into the plugin object. Stores the supplied option hash into the plugin object.

View file

@ -16,7 +16,7 @@ shopping cart is tied to the sessionId and, thus, expires when the sessionId exp
=head1 SYNOPSIS =head1 SYNOPSIS
$shoppingCart = WebGUI::Commerce::ShoppingCart->new; $shoppingCart = WebGUI::Commerce::ShoppingCart->new($session);
$shoppingCart->add('myItemId', 'myItem', 3); $shoppingCart->add('myItemId', 'myItem', 3);
$shoppingCart->setQuantity('myItemId', 'myItem', 2); $shoppingCart->setQuantity('myItemId', 'myItem', 2);
@ -243,16 +243,30 @@ of the current user.
=cut =cut
sub new { sub new {
my ($class, $sessionId, $sth, $row, %items); my ($class, $session, $sessionId, $sth, $row, %items);
$class = shift; $class = shift;
$sessionId = shift || $self->session->var->get("sessionId"); $session = shift;
$sessionId = shift || $session->var->get("sessionId");
$sth = $self->session->db->read("select * from shoppingCart where sessionId=".$self->session->db->quote($sessionId)); $sth = $session->db->read("select * from shoppingCart where sessionId=".$session->db->quote($sessionId));
while ($row = $sth->hashRef) { while ($row = $sth->hashRef) {
$items{$row->{itemId}."_".$row->{itemType}} = $row; $items{$row->{itemId}."_".$row->{itemType}} = $row;
} }
bless {_sessionId => $sessionId, _items => \%items}, $class; bless {_session=>$session, _sessionId => $sessionId, _items => \%items}, $class;
}
#-------------------------------------------------------------------
=head2 session
Returns the cached, local session variable.
=cut
sub session {
my ($self) = @_;
return $self->{_session};
} }
1; 1;

View file

@ -70,7 +70,7 @@ sub _shippingSelected {
my $session = shift; my $session = shift;
return 0 unless ($session->scratch->get('shippingMethod')); return 0 unless ($session->scratch->get('shippingMethod'));
my $plugin = WebGUI::Commerce::Shipping->load($session->scratch->get('shippingMethod')); my $plugin = WebGUI::Commerce::Shipping->load($session, $session->scratch->get('shippingMethod'));
if ($plugin) { if ($plugin) {
$plugin->setOptions(Storable::thaw($session->scratch->get('shippingOptions'))) if ($session->scratch->get('shippingOptions')); $plugin->setOptions(Storable::thaw($session->scratch->get('shippingOptions'))) if ($session->scratch->get('shippingOptions'));
return 1 if ($plugin->enabled && $plugin->optionsOk); return 1 if ($plugin->enabled && $plugin->optionsOk);
@ -82,7 +82,7 @@ sub _shippingSelected {
#------------------------------------------------------------------- #-------------------------------------------------------------------
sub www_addToCart { sub www_addToCart {
my $session = shift; my $session = shift;
WebGUI::Commerce::ShoppingCart->new->add($session->form->process("itemId"), $session->form->process("itemType"), $session->form->process("quantity")); WebGUI::Commerce::ShoppingCart->new($session)->add($session->form->process("itemId"), $session->form->process("itemType"), $session->form->process("quantity"));
return WebGUI::Operation::execute('viewCart'); return WebGUI::Operation::execute('viewCart');
} }
@ -134,7 +134,7 @@ sub www_checkoutConfirm {
$var{errorLoop} = [ map {{message => $_}} @{$errors} ] if $errors; $var{errorLoop} = [ map {{message => $_}} @{$errors} ] if $errors;
# Put contents of cart in template vars # Put contents of cart in template vars
$shoppingCart = WebGUI::Commerce::ShoppingCart->new; $shoppingCart = WebGUI::Commerce::ShoppingCart->new($session);
($normal, $recurring) = $shoppingCart->getItems; ($normal, $recurring) = $shoppingCart->getItems;
foreach (@$normal) { foreach (@$normal) {
@ -163,7 +163,7 @@ sub www_checkoutConfirm {
$var{subTotal} = sprintf('%.2f', $total); $var{subTotal} = sprintf('%.2f', $total);
$shipping = WebGUI::Commerce::Shipping->load($session->scratch->get('shippingMethod')); $shipping = WebGUI::Commerce::Shipping->load($session, $session->scratch->get('shippingMethod'));
$shipping->setOptions(Storable::thaw($session->scratch->get('shippingOptions'))) if ($session->scratch->get('shippingOptions')); $shipping->setOptions(Storable::thaw($session->scratch->get('shippingOptions'))) if ($session->scratch->get('shippingOptions'));
$var{shippingName} = $shipping->name; $var{shippingName} = $shipping->name;
@ -213,12 +213,12 @@ sub www_checkoutSubmit {
return WebGUI::Operation::execute('checkout') unless (_paymentSelected && _shippingSelected); return WebGUI::Operation::execute('checkout') unless (_paymentSelected && _shippingSelected);
# Load shipping plugin. # Load shipping plugin.
$shipping = WebGUI::Commerce::Shipping->load($session->scratch->get('shippingMethod')); $shipping = WebGUI::Commerce::Shipping->load($session, $session->scratch->get('shippingMethod'));
$shipping->setOptions(Storable::thaw($session->scratch->get('shippingOptions'))) if ($session->scratch->get('shippingOptions')); $shipping->setOptions(Storable::thaw($session->scratch->get('shippingOptions'))) if ($session->scratch->get('shippingOptions'));
# Load payment plugin. # Load payment plugin.
$plugin = WebGUI::Commerce::Payment->load($session, $session->scratch->get('paymentGateway')); $plugin = WebGUI::Commerce::Payment->load($session, $session->scratch->get('paymentGateway'));
$shoppingCart = WebGUI::Commerce::ShoppingCart->new; $shoppingCart = WebGUI::Commerce::ShoppingCart->new($session);
($normal, $recurring) = $shoppingCart->getItems; ($normal, $recurring) = $shoppingCart->getItems;
# Check if shoppingcart contains any items. If not the user probably clicked reload, so we redirect to the current page. # Check if shoppingcart contains any items. If not the user probably clicked reload, so we redirect to the current page.
@ -359,7 +359,7 @@ sub www_confirmTransaction {
#------------------------------------------------------------------- #-------------------------------------------------------------------
sub www_deleteCartItem { sub www_deleteCartItem {
my $session = shift; my $session = shift;
WebGUI::Commerce::ShoppingCart->new->delete($session->form->process("itemId"), $session->form->process("itemType")); WebGUI::Commerce::ShoppingCart->new($session)->delete($session->form->process("itemId"), $session->form->process("itemType"));
return WebGUI::Operation::execute('viewCart'); return WebGUI::Operation::execute('viewCart');
} }
@ -471,7 +471,7 @@ sub www_editCommerceSettings {
# Shipping plugins... # Shipping plugins...
# Check which payment plugins will compile, and load them. # Check which payment plugins will compile, and load them.
foreach (@{$session->config->get("shippingPlugins")}) { foreach (@{$session->config->get("shippingPlugins")}) {
$plugin = WebGUI::Commerce::Shipping->load($_); $plugin = WebGUI::Commerce::Shipping->load($session, $_);
if ($plugin) { if ($plugin) {
push(@shippingPlugins, $plugin); push(@shippingPlugins, $plugin);
$shippingPlugins{$_} = $plugin->name; $shippingPlugins{$_} = $plugin->name;
@ -713,7 +713,7 @@ sub www_selectShippingMethod {
_clearShippingScratch; _clearShippingScratch;
$i18n = WebGUI::International->new($session, 'Commerce'); $i18n = WebGUI::International->new($session, 'Commerce');
$plugins = WebGUI::Commerce::Shipping->getEnabledPlugins; $plugins = WebGUI::Commerce::Shipping->getEnabledPlugins($session);
if (scalar(@$plugins) > 1) { if (scalar(@$plugins) > 1) {
foreach (@$plugins) { foreach (@$plugins) {
@ -742,7 +742,7 @@ sub www_selectShippingMethod {
#------------------------------------------------------------------- #-------------------------------------------------------------------
sub www_selectShippingMethodSave { sub www_selectShippingMethodSave {
my $session = shift; my $session = shift;
my $shipping = WebGUI::Commerce::Shipping->load($session->form->process("shippingMethod")); my $shipping = WebGUI::Commerce::Shipping->load($session, $session->form->process("shippingMethod"));
$shipping->processOptionsForm; $shipping->processOptionsForm;
return WebGUI::Operation::execute('selectShipping') unless ($shipping->optionsOk); return WebGUI::Operation::execute('selectShipping') unless ($shipping->optionsOk);
@ -766,7 +766,7 @@ sub www_transactionComplete {
#------------------------------------------------------------------- #-------------------------------------------------------------------
sub www_updateCart { sub www_updateCart {
my $session = shift; my $session = shift;
my $shoppingCart = WebGUI::Commerce::ShoppingCart->new; my $shoppingCart = WebGUI::Commerce::ShoppingCart->new($session);
foreach my $formElement (keys(%{$session{form}})) { foreach my $formElement (keys(%{$session{form}})) {
if ($formElement =~ m/^quantity~([^~]*)~([^~]*)$/) { if ($formElement =~ m/^quantity~([^~]*)~([^~]*)$/) {
@ -785,7 +785,7 @@ sub www_viewCart {
$i18n = WebGUI::International->new($session, 'Commerce'); $i18n = WebGUI::International->new($session, 'Commerce');
# Put contents of cart in template vars # Put contents of cart in template vars
$shoppingCart = WebGUI::Commerce::ShoppingCart->new; $shoppingCart = WebGUI::Commerce::ShoppingCart->new($session);
($normal, $recurring) = $shoppingCart->getItems; ($normal, $recurring) = $shoppingCart->getItems;
foreach (@$normal) { foreach (@$normal) {

View file

@ -415,7 +415,7 @@ sub www_listSubscriptions {
#------------------------------------------------------------------- #-------------------------------------------------------------------
sub www_purchaseSubscription { sub www_purchaseSubscription {
my $session = shift; my $session = shift;
WebGUI::Commerce::ShoppingCart->new->add($session->form->process("sid"), 'Subscription'); WebGUI::Commerce::ShoppingCart->new($session)->add($session->form->process("sid"), 'Subscription');
return WebGUI::HTTP::setRedirect($session->url->page('op=checkout')); return WebGUI::HTTP::setRedirect($session->url->page('op=checkout'));
} }