shipping is closer

This commit is contained in:
JT Smith 2008-03-07 17:04:26 +00:00
parent 14ffef86c1
commit a535104d7a
5 changed files with 103 additions and 81 deletions

View file

@ -128,8 +128,9 @@ sub www_pay {
my $session = shift; my $session = shift;
my $output = undef; my $output = undef;
my $method = "www_".$session->form->get("method"); my $method = "www_".$session->form->get("method");
if ($method ne "www_" && WebGUI::Shop::Pay->can($method)) { my $pay = WebGUI::Shop::Pay->new($session);
$output = WebGUI::Shop::Pay->$method($session); if ($method ne "www_" && $pay->can($method)) {
$output = $pay->$method();
} }
return $output; return $output;
} }
@ -146,8 +147,9 @@ sub www_ship {
my $session = shift; my $session = shift;
my $output = undef; my $output = undef;
my $method = "www_".$session->form->get("method"); my $method = "www_".$session->form->get("method");
if ($method ne "www_" && WebGUI::Shop::Ship->can($method)) { my $ship = WebGUI::Shop::Ship->new($session);
$output = WebGUI::Shop::Ship->$method($session); if ($method ne "www_" && $ship->can($method)) {
$output = $ship->$method($session);
} }
return $output; return $output;
} }

View file

@ -28,7 +28,7 @@ These subroutines are available from this package:
=cut =cut
public session => my %session; readonly session => my %session;
#------------------------------------------------------------------- #-------------------------------------------------------------------

View file

@ -2,6 +2,7 @@ package WebGUI::Shop::Ship;
use strict; use strict;
use Class::InsideOut qw{ :std };
use WebGUI::Exception; use WebGUI::Exception;
use WebGUI::International; use WebGUI::International;
use WebGUI::Pluggable; use WebGUI::Pluggable;
@ -27,17 +28,16 @@ These subroutines are available from this package:
=cut =cut
readonly session => my %session;
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 create ( $session, $class, $options ) =head2 addShipper ( $class, $options )
The interface method for creating new, configured instances of ShipDriver. If the ShipperDriver throws an exception, it is propagated The interface method for creating new, configured instances of ShipDriver. If the ShipperDriver throws an exception, it is propagated
back up to the top. back up to the top.
=head3 $session
A WebGUI::Session object.
=head4 $class =head4 $class
The class of the new ShipDriver object to create. The class of the new ShipDriver object to create.
@ -48,16 +48,14 @@ A list of properties to assign to this ShipperDriver. See C<definition> for det
=cut =cut
sub create { sub addShipper {
my $class = shift; my $self = shift;
my $session = shift; my $session = shift;
WebGUI::Error::InvalidParam->throw(error => q{Must provide a session variable})
unless ref $session eq 'WebGUI::Session';
my $requestedClass = shift; my $requestedClass = shift;
WebGUI::Error::InvalidParam->throw(error => q{Must provide a class to create an object}) WebGUI::Error::InvalidParam->throw(error => q{Must provide a class to create an object})
unless defined $requestedClass; unless defined $requestedClass;
WebGUI::Error::InvalidParam->throw(error => q{The requested class is not enabled in your WebGUI configuration file}, param => $requestedClass) WebGUI::Error::InvalidParam->throw(error => q{The requested class is not enabled in your WebGUI configuration file}, param => $requestedClass)
unless isIn($requestedClass, @{ WebGUI::Shop::Ship->getDrivers($session) } ); unless isIn($requestedClass, keys %{$self->getDrivers($session) } );
my $options = shift; my $options = shift;
WebGUI::Error::InvalidParam->throw(error => q{You must pass a hashref of options to create a new ShipDriver object}) WebGUI::Error::InvalidParam->throw(error => q{You must pass a hashref of options to create a new ShipDriver object})
unless defined($options) and ref $options eq 'HASH' and scalar keys %{ $options }; unless defined($options) and ref $options eq 'HASH' and scalar keys %{ $options };
@ -67,23 +65,19 @@ sub create {
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 getDrivers ( $session ) =head2 getDrivers ( )
This subroutine returns an arrayref of available shipping driver classes This subroutine returns a hash reference of available shipping driver classes as keys with their human readable names as values, read from the WebGUI config file in the shippingDrivers directive.
from the WebGUI config file.
=head3 $session
A WebGUI::Session object. A WebGUI::Error::InvalidParam exception will be thrown if it doesn't get one.
=cut =cut
sub getDrivers { sub getDrivers {
my $class = shift; my $self = shift;
my $session = shift; my %drivers = ();
WebGUI::Error::InvalidParam->throw(error => q{Must provide a session variable}) foreach my $class (@{$self->session->config->get('shippingDrivers')}) {
unless ref $session eq 'WebGUI::Session'; $drivers{$class} = eval { WebGUI::Pluggable::instanciate($class, 'getName', [ $self->session ])};
return $session->config->get('shippingDrivers'); }
return \%drivers;
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -102,11 +96,11 @@ A WebGUI::Shop::Cart object. A WebGUI::Error::InvalidParam exception will be th
=cut =cut
sub getOptions { sub getOptions {
my ($class, $cart) = @_; my ($self, $cart) = @_;
WebGUI::Error::InvalidParam->throw(error => q{Need a cart.}) unless defined $cart and $cart->isa("WebGUI::Shop::Cart"); WebGUI::Error::InvalidParam->throw(error => q{Need a cart.}) unless defined $cart and $cart->isa("WebGUI::Shop::Cart");
my $session = $cart->session; my $session = $cart->session;
my %options = (); my %options = ();
foreach my $shipper (@{$class->getShippers($session)}) { foreach my $shipper (@{$self->getShippers()}) {
$options{$shipper->getId} = { $options{$shipper->getId} = {
label => $shipper->get("label"), label => $shipper->get("label"),
price => $shipper->calculate($cart), price => $shipper->calculate($cart),
@ -117,57 +111,21 @@ sub getOptions {
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 getShippers ( $session ) =head2 getShipper ( )
Returns an array ref of all shipping objects in the db.
=head3 $session
A WebGUI::Session object. A WebGUI::Error::InvalidParam exception will be thrown if it doesn't get one.
=head3
=cut
sub getShippers {
my $class = shift;
my $session = shift;
WebGUI::Error::InvalidParam->throw(error => q{Must provide a session variable})
unless ref $session eq 'WebGUI::Session';
my @drivers = ();
my $sth = $session->db->prepare('select shipperId from shipper');
$sth->execute();
while (my $driver = $sth->hashRef()) {
push @drivers, WebGUI::Shop::Ship->new($session, $driver->{shipperId});
}
$sth->finish;
return \@drivers;
}
#-------------------------------------------------------------------
=head2 new ( $session, $shipperId )
Looks up an existing ShipperDriver in the db by shipperId and returns Looks up an existing ShipperDriver in the db by shipperId and returns
that object. If the ShipperDriver throws an exception, it is propagated that object. If the ShipperDriver throws an exception, it is propagated
back up to the top. back up to the top.
=head3 $session =head3 id
A WebGUI::Session object. The id of the shipper to instanciate.
=head3 $shipperId
The ID of a shipper to look up and instanciate.
=cut =cut
sub new { sub getShipper {
my $class = shift; my ($self, $shipperId) = @_;
my $session = shift; my $session = $self->session;
WebGUI::Error::InvalidParam->throw(error => q{Must provide a session variable})
unless ref $session eq 'WebGUI::Session';
my $shipperId = shift;
WebGUI::Error::InvalidParam->throw(error => q{Must provide a shipperId}) WebGUI::Error::InvalidParam->throw(error => q{Must provide a shipperId})
unless defined $shipperId; unless defined $shipperId;
my $requestedClass = $session->db->quickScalar('select className from shipper where shipperId=?',[$shipperId]); my $requestedClass = $session->db->quickScalar('select className from shipper where shipperId=?',[$shipperId]);
@ -179,6 +137,58 @@ sub new {
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 getShippers ( )
Returns an array ref of all shipping objects in the db.
=head3
=cut
sub getShippers {
my $self = shift;
my @drivers = ();
my $sth = $self->session->db->prepare('select shipperId from shipper');
$sth->execute();
while (my $driver = $sth->hashRef()) {
push @drivers, $self->getShipper($driver->{shipperId});
}
$sth->finish;
return \@drivers;
}
#-------------------------------------------------------------------
=head2 new ( $session )
Constructor.
=head3 $session
A WebGUI::Session object.
=cut
sub new {
my $class = shift;
my $session = shift;
WebGUI::Error::InvalidObject->throw(expected=>"WebGUI::Session", got=>(ref $session), error => q{Must provide a session variable}) unless ref $session eq 'WebGUI::Session';
my $self = register $class;
my $id = id $self;
$session{ $id } = $session;
return $self;
}
#-------------------------------------------------------------------
=head2 session ()
Returns a reference to the current session.
=cut
#-------------------------------------------------------------------
=head2 www_do ( ) =head2 www_do ( )
Let's ship drivers do method calls. Requires a driver param in the post form vars which contains the id of the driver to load. Let's ship drivers do method calls. Requires a driver param in the post form vars which contains the id of the driver to load.
@ -186,9 +196,10 @@ Let's ship drivers do method calls. Requires a driver param in the post form var
=cut =cut
sub www_do { sub www_do {
my ($class, $session) = @_; my ($self) = @_;
my $form = $session->form; my $form = $self->session->form;
my $driver = $class->new($session, $form->get("driver")); WebGUI::Error::InvalidParam->throw(error => q{must have a form var called driver with a driver id }) if ($form->get("driver") eq "");
my $driver = $self->getShipper($form->get("driver"));
my $output = undef; my $output = undef;
my $method = "www_". ( $form->get("do")); my $method = "www_". ( $form->get("do"));
if ($driver->can($method)) { if ($driver->can($method)) {
@ -206,12 +217,21 @@ The main management screen for shippers.
=cut =cut
sub www_manage { sub www_manage {
my ($class, $session) = @_; my ($self) = @_;
my $session = $self->session;
return $session->privilege->adminOnly() unless ($session->user->isInGroup("3")); return $session->privilege->adminOnly() unless ($session->user->isInGroup("3"));
my $admin = WebGUI::Shop::Admin->new($session); my $admin = WebGUI::Shop::Admin->new($session);
my $console = $admin->getAdminConsole;
my $output = "Test";
my $i18n = WebGUI::International->new($session, "Shop"); my $i18n = WebGUI::International->new($session, "Shop");
my $output = WebGUI::Form::formHeader($session)
.WebGUI::Form::hidden($session, {name=>"shop", value=>"ship"})
.WebGUI::Form::hidden($session, {name=>"method", value=>"addDriver"})
.WebGUI::Form::selectBox($session, {name=>"className", options=>$self->getDrivers})
.WebGUI::Form::submit($session, {value=>$i18n->get("add shipper")})
.WebGUI::Form::formFooter($session);
foreach my $shipper (@{$self->getShippers}) {
}
my $console = $admin->getAdminConsole;
return $console->render($output, $i18n->get("shipping methods")); return $console->render($output, $i18n->get("shipping methods"));
} }

View file

@ -240,7 +240,7 @@ sub getId {
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 getName ( ) =head2 getName ( $session )
Return a human readable name for this driver. Never overridden in the Return a human readable name for this driver. Never overridden in the
subclass, instead specified in definition with the name "name". subclass, instead specified in definition with the name "name".
@ -248,8 +248,8 @@ subclass, instead specified in definition with the name "name".
=cut =cut
sub getName { sub getName {
my $self = shift; my ($class, $session) = @_;
my $definition = $self->definition($self->session); my $definition = $class->definition($session);
return $definition->[0]->{name}; return $definition->[0]->{name};
} }

View file

@ -152,7 +152,7 @@ this is not used, it uses the current session user object.
sub canEdit { sub canEdit {
my $self = shift; my $self = shift;
my $user = shift || $session->user; my $user = shift || $self->session->user;
return $user->isInGroup( $self->session->get('groupIdAdminCommerce')); return $user->isInGroup( $self->session->get('groupIdAdminCommerce'));
} }