finally got shipping driver management working
This commit is contained in:
parent
1658f3957d
commit
819552ce06
9 changed files with 153 additions and 137 deletions
|
|
@ -18,7 +18,7 @@ use strict;
|
|||
use WebGUI::AdminConsole;
|
||||
use WebGUI::Shop::AddressBook;
|
||||
use WebGUI::Shop::Cart;
|
||||
#use WebGUI::Shop::Pay;
|
||||
use WebGUI::Shop::Pay;
|
||||
use WebGUI::Shop::Ship;
|
||||
use WebGUI::Shop::Tax;
|
||||
|
||||
|
|
|
|||
|
|
@ -136,9 +136,9 @@ sub formHeader {
|
|||
croak "Second parameter must be hash reference"
|
||||
if ref $params ne "HASH";
|
||||
|
||||
my $action = exists $params->{ action } ? $params->{ action } : $session->url->page();
|
||||
my $method = exists $params->{ method } ? $params->{ method } : "post";
|
||||
my $enctype = exists $params->{ enctype } ? $params->{ enctype } : "multipart/form-data";
|
||||
my $action = (exists $params->{action} && $params->{action} ne "") ? $params->{action} : $session->url->page();
|
||||
my $method = (exists $params->{method} && $params->{method} ne "") ? $params->{method} : "post";
|
||||
my $enctype = (exists $params->{enctype} && $params->{enctype} ne "") ? $params->{enctype} : "multipart/form-data";
|
||||
|
||||
# Fix a query string in the action URL
|
||||
my $hidden;
|
||||
|
|
|
|||
|
|
@ -97,6 +97,7 @@ sub www_editSettings {
|
|||
my $ac = $self->getAdminConsole;
|
||||
my $setting = $self->session->setting;
|
||||
my $form = WebGUI::HTMLForm->new($self->session);
|
||||
$form->submit;
|
||||
$form->hidden(name=>"shop", value=>"admin");
|
||||
$form->hidden(name=>"method", value=>"editSettingsSave");
|
||||
$form->template(
|
||||
|
|
|
|||
|
|
@ -190,6 +190,8 @@ Returns a reference to the current session.
|
|||
|
||||
=head2 www_addDriver ()
|
||||
|
||||
Adds a ship driver to the shop, then displays it's edit screen.
|
||||
|
||||
=cut
|
||||
|
||||
sub www_addDriver {
|
||||
|
|
@ -202,18 +204,34 @@ sub www_addDriver {
|
|||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 www_deleteDriver ()
|
||||
|
||||
Deletes a ship driver from the shop.
|
||||
|
||||
=cut
|
||||
|
||||
sub www_deleteDriver {
|
||||
my $self = shift;
|
||||
my $form = $self->session->form;
|
||||
WebGUI::Error::InvalidParam->throw(error => q{must have a form var called driverId with guid }) if ($form->get("driverId") eq "");
|
||||
$self->getShipper($form->get("driverId"))->delete;
|
||||
return $self->www_manage;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=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 driverId param in the post form vars which contains the id of the driver to load.
|
||||
|
||||
=cut
|
||||
|
||||
sub www_do {
|
||||
my ($self) = @_;
|
||||
my $form = $self->session->form;
|
||||
WebGUI::Error::InvalidParam->throw(error => q{must have a form var called driver with a driver id }) if ($form->get("driver") eq "");
|
||||
WebGUI::Error::InvalidParam->throw(error => q{must have a form var called driverId with a driver id }) if ($form->get("driverId") eq "");
|
||||
WebGUI::Error::InvalidParam->throw(error => q{must have a form var called do with a method name in the driver }) if ($form->get("do") eq "");
|
||||
my $driver = $self->getShipper($form->get("driver"));
|
||||
my $driver = $self->getShipper($form->get("driverId"));
|
||||
my $output = undef;
|
||||
my $method = "www_". ( $form->get("do"));
|
||||
if ($driver->can($method)) {
|
||||
|
|
@ -243,7 +261,23 @@ sub www_manage {
|
|||
.WebGUI::Form::submit($session, {value=>$i18n->get("add shipper")})
|
||||
.WebGUI::Form::formFooter($session);
|
||||
foreach my $shipper (@{$self->getShippers}) {
|
||||
|
||||
$output .= '<div style="clear: both;">'
|
||||
.WebGUI::Form::formHeader($session, {extras=>'style="float: left;"'})
|
||||
.WebGUI::Form::hidden($session, {name=>"shop", value=>"ship"})
|
||||
.WebGUI::Form::hidden($session, {name=>"method", value=>"deleteDriver"})
|
||||
.WebGUI::Form::hidden($session, {name=>"driverId", value=>$shipper->getId})
|
||||
.WebGUI::Form::submit($session, {value=>$i18n->get("delete"), extras=>'class="backwardButton"'})
|
||||
.WebGUI::Form::formFooter($session)
|
||||
.WebGUI::Form::formHeader($session, {extras=>'style="float: left;"'})
|
||||
.WebGUI::Form::hidden($session, {name=>"shop", value=>"ship"})
|
||||
.WebGUI::Form::hidden($session, {name=>"method", value=>"do"})
|
||||
.WebGUI::Form::hidden($session, {name=>"do", value=>"edit"})
|
||||
.WebGUI::Form::hidden($session, {name=>"driverId", value=>$shipper->getId})
|
||||
.WebGUI::Form::submit($session, {value=>$i18n->get("edit"), extras=>'class="normalButton"'})
|
||||
.WebGUI::Form::formFooter($session)
|
||||
.' '
|
||||
.$shipper->get("label")
|
||||
.'</div>';
|
||||
}
|
||||
my $console = $admin->getAdminConsole;
|
||||
return $console->render($output, $i18n->get("shipping methods"));
|
||||
|
|
|
|||
|
|
@ -32,34 +32,8 @@ These subroutines are available from this package:
|
|||
=cut
|
||||
|
||||
readonly session => my %session;
|
||||
readonly className => my %className;
|
||||
readonly shipperId => my %shipperId;
|
||||
readonly options => my %options;
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 _buildObj ( )
|
||||
|
||||
Private method used to build objects, shared by new and create.
|
||||
|
||||
=cut
|
||||
|
||||
sub _buildObj {
|
||||
my ($class, $session, $requestedClass, $shipperId, $options) = @_;
|
||||
my $self = {};
|
||||
bless $self, $requestedClass;
|
||||
register $self;
|
||||
|
||||
my $id = id $self;
|
||||
|
||||
$session{ $id } = $session;
|
||||
$shipperId{ $id } = $shipperId;
|
||||
$options{ $id } = $options;
|
||||
$className{ $id } = $requestedClass;
|
||||
|
||||
return $self;
|
||||
}
|
||||
|
||||
private options => my %options;
|
||||
private shipperId => my %shipperId;
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
|
|
@ -76,15 +50,6 @@ sub calculate {
|
|||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 className ( )
|
||||
|
||||
Accessor for the className of the object. This is the name of the driver that is used
|
||||
to do calculations.
|
||||
|
||||
=cut
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 create ( $session, $options )
|
||||
|
||||
Constructor for new WebGUI::Shop::ShipperDriver objects. Returns a WebGUI::Shop::ShipperDriver object.
|
||||
|
|
@ -109,11 +74,9 @@ sub create {
|
|||
WebGUI::Error::InvalidParam->throw(error => q{Must provide a hashref of options})
|
||||
unless ref $options eq 'HASH' and scalar keys %{ $options };
|
||||
my $shipperId = $session->id->generate;
|
||||
my $self = WebGUI::Shop::ShipDriver->_buildObj($session, $class, $shipperId, $options);
|
||||
|
||||
$session->db->write('insert into shipper (shipperId,className) VALUES (?,?)', [$shipperId, $class]);
|
||||
$self->set($options);
|
||||
|
||||
my $self = $class->new($session, $shipperId);
|
||||
$self->update($options);
|
||||
return $self;
|
||||
}
|
||||
|
||||
|
|
@ -167,7 +130,7 @@ Removes this ShipDriver object from the db.
|
|||
|
||||
sub delete {
|
||||
my $self = shift;
|
||||
$self->session->db->write('delete from shipper');
|
||||
$self->session->db->write('delete from shipper where shipperId=?',[$self->getId]);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -190,13 +153,18 @@ return the value from the options hash.
|
|||
sub get {
|
||||
my $self = shift;
|
||||
my $param = shift;
|
||||
my $options = $self->options;
|
||||
if (defined $param) {
|
||||
return $options->{$param};
|
||||
my $opts = $options{id $self};
|
||||
if ($opts eq "") {
|
||||
$opts = {};
|
||||
}
|
||||
else {
|
||||
return $options;
|
||||
$opts = JSON::from_json($opts);
|
||||
}
|
||||
if (defined $param) {
|
||||
return $opts->{$param};
|
||||
}
|
||||
my %copy = %{$opts};
|
||||
return \%copy;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
|
@ -214,13 +182,12 @@ sub getEditForm {
|
|||
my $form = WebGUI::HTMLForm->new($self->session);
|
||||
$form->submit;
|
||||
$form->hidden(
|
||||
name => 'shipperId',
|
||||
name => 'driverId',
|
||||
value => $self->getId,
|
||||
);
|
||||
$form->hidden(
|
||||
name => 'className',
|
||||
value => $self->className,
|
||||
);
|
||||
$form->hidden(name => 'shop',value => "ship");
|
||||
$form->hidden(name => 'method',value => "do");
|
||||
$form->hidden(name => 'do',value => "editSave");
|
||||
$form->dynamicForm($definition, 'properties', $self);
|
||||
return $form;
|
||||
}
|
||||
|
|
@ -235,7 +202,8 @@ since a lot of WebGUI classes have a getId method.
|
|||
=cut
|
||||
|
||||
sub getId {
|
||||
return shift->shipperId;
|
||||
my $self = shift;
|
||||
return $shipperId{id $self};
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
|
@ -275,26 +243,40 @@ sub new {
|
|||
my $properties = $session->db->quickHashRef('select * from shipper where shipperId=?',[$shipperId]);
|
||||
WebGUI::Error::ObjectNotFound->throw(error => q{shipperId not found in db}, id => $shipperId)
|
||||
unless scalar keys %{ $properties };
|
||||
##This check is just to guardband the from_json call below.
|
||||
WebGUI::Error::InvalidParam->throw(
|
||||
error => qq{Options property for $shipperId was broken in the db},
|
||||
param => $properties->{options},
|
||||
) unless $properties->{options}; ##Note, existence is controlled by the columns in the db
|
||||
my $options = from_json($properties->{options});
|
||||
my $self = WebGUI::Shop::ShipDriver->_buildObj($session, $class, $shipperId, $options);
|
||||
my $self = register $class;
|
||||
my $id = id $self;
|
||||
$session{ $id } = $session;
|
||||
$options{ $id } = $properties->{options};
|
||||
$shipperId{ $id } = $shipperId;
|
||||
return $self;
|
||||
}
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 options ( )
|
||||
=head2 processPropertiesFromFormPost ( )
|
||||
|
||||
Accessor for the driver properties. This returns a hashref
|
||||
any driver specific properties. To set the properties, use
|
||||
the C<set> method.
|
||||
Updates ship driver with data from Form.
|
||||
|
||||
=cut
|
||||
|
||||
sub processPropertiesFromFormPost {
|
||||
my $self = shift;
|
||||
my %properties;
|
||||
my $fullDefinition = $self->definition($self->session);
|
||||
foreach my $definition (@{$fullDefinition}) {
|
||||
foreach my $property (keys %{$definition->{properties}}) {
|
||||
$properties{$property} = $self->session->form->process(
|
||||
$property,
|
||||
$definition->{properties}{$property}{fieldType},
|
||||
$definition->{properties}{$property}{defaultValue}
|
||||
);
|
||||
}
|
||||
}
|
||||
$properties{title} = $fullDefinition->[0]{name} if ($properties{title} eq "" || lc($properties{title}) eq "untitled");
|
||||
$self->update(\%properties);
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 session ( )
|
||||
|
|
@ -305,7 +287,7 @@ Accessor for the session object. Returns the session object.
|
|||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 set ( $options )
|
||||
=head2 update ( $options )
|
||||
|
||||
Setter for user configurable options in the ship objects.
|
||||
|
||||
|
|
@ -316,40 +298,52 @@ flattened into JSON and stored in the database as text. There is no content che
|
|||
|
||||
=cut
|
||||
|
||||
sub set {
|
||||
sub update {
|
||||
my $self = shift;
|
||||
my $options = shift;
|
||||
WebGUI::Error::InvalidParam->throw(error => 'set was not sent a hashref of options to store in the database')
|
||||
my $options = shift || {};
|
||||
WebGUI::Error::InvalidParam->throw(error => 'update was not sent a hashref of options to store in the database')
|
||||
unless ref $options eq 'HASH' and scalar keys %{ $options };
|
||||
my $jsonOptions = to_json($options);
|
||||
$self->session->db->write('update shipper set options=? where shipperId=?', [$jsonOptions, $self->shipperId]);
|
||||
return;
|
||||
$options{id $self} = $jsonOptions;
|
||||
$self->session->db->write('update shipper set options=? where shipperId=?', [$jsonOptions, $self->getId]);
|
||||
return undef;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 shipperId ( )
|
||||
|
||||
Accessor for the unique identifier for this shipperDriver. The shipperId is
|
||||
a GUID.
|
||||
|
||||
=cut
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 www_edit ( )
|
||||
|
||||
Generates an edito form.
|
||||
Generates an edit form.
|
||||
|
||||
=cut
|
||||
|
||||
sub www_edit {
|
||||
my $self = shift;
|
||||
my $admin = WebGUI::Shop::Admin->new($self->session);
|
||||
my $i18n = WebGUI::International->new($self->session, "Shop");
|
||||
return $admin->getAdminConsole->render($self->getEditForm->print, $i18n->get("shipping methods"));
|
||||
my $session = $self->session;
|
||||
return $session->privilege->insufficient() unless $session->user->isInGroup(3);
|
||||
my $admin = WebGUI::Shop::Admin->new($session);
|
||||
my $i18n = WebGUI::International->new($session, "Shop");
|
||||
my $form = $self->getEditForm;
|
||||
$form->submit;
|
||||
return $admin->getAdminConsole->render($form->print, $i18n->get("shipping methods"));
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 www_editSave ( )
|
||||
|
||||
Saves the data from the post.
|
||||
|
||||
=cut
|
||||
|
||||
sub www_editSave {
|
||||
my $self = shift;
|
||||
my $session = $self->session;
|
||||
return $session->privilege->insufficient() unless $session->user->isInGroup(3);
|
||||
$self->processPropertiesFromFormPost;
|
||||
$session->http->setRedirect("/?shop=ship;method=manage");
|
||||
return undef;
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
|
|||
|
|
@ -79,8 +79,8 @@ sub definition {
|
|||
},
|
||||
pricePerWeight => {
|
||||
fieldType => 'float',
|
||||
label => $i18n->get('pricePerWeight'),
|
||||
hoverHelp => $i18n->get('pricePerWeight help'),
|
||||
label => $i18n->get('percentageOfWeight'),
|
||||
hoverHelp => $i18n->get('percentageOfWeight help'),
|
||||
defaultValue => 0,
|
||||
},
|
||||
pricePerItem => {
|
||||
|
|
|
|||
|
|
@ -58,6 +58,9 @@ sub handler {
|
|||
}
|
||||
else {
|
||||
if ($output eq "chunked") {
|
||||
if ($session->errorHandler->canShowDebug()) {
|
||||
$session->output->print($session->errorHandler->showDebug(),1);
|
||||
}
|
||||
last;
|
||||
}
|
||||
elsif (defined $output && $output ne "") {
|
||||
|
|
|
|||
|
|
@ -3,6 +3,12 @@ package WebGUI::i18n::English::Shop;
|
|||
use strict;
|
||||
|
||||
our $I18N = {
|
||||
'add shipper' => {
|
||||
message => q|Add Shipping Method|,
|
||||
lastUpdated => 0,
|
||||
context => q|button in shipping manager|
|
||||
},
|
||||
|
||||
'shopping cart template' => {
|
||||
message => q|Cart Template|,
|
||||
lastUpdated => 0,
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ my $session = WebGUI::Test->session;
|
|||
#----------------------------------------------------------------------------
|
||||
# Tests
|
||||
|
||||
my $tests = 42;
|
||||
my $tests = 35;
|
||||
plan tests => 1 + $tests;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
|
|
@ -154,6 +154,7 @@ my $options = {
|
|||
label => 'Slow and dangerous',
|
||||
enabled => 1,
|
||||
};
|
||||
|
||||
$driver = WebGUI::Shop::ShipDriver->create( $session, $options );
|
||||
|
||||
isa_ok($driver, 'WebGUI::Shop::ShipDriver');
|
||||
|
|
@ -162,18 +163,16 @@ isa_ok($driver->session, 'WebGUI::Session', 'session method returns a session ob
|
|||
|
||||
is($session->getId, $driver->session->getId, 'session method returns OUR session object');
|
||||
|
||||
like($driver->shipperId, $session->id->getValidator, 'got a valid GUID for shipperId');
|
||||
is($driver->getId, $driver->shipperId, 'getId returns the same thing as shipperId');
|
||||
like($driver->getId, $session->id->getValidator, 'got a valid GUID for shipperId');
|
||||
|
||||
is($driver->className, ref $driver, 'className property set correctly');
|
||||
|
||||
cmp_deeply($driver->options, $options, 'options accessor works');
|
||||
cmp_deeply($driver->get, $options, 'options accessor works');
|
||||
|
||||
my $dbData = $session->db->quickHashRef('select * from shipper where shipperId=?',[$driver->shipperId]);
|
||||
my $dbData = $session->db->quickHashRef('select * from shipper where shipperId=?',[$driver->getId]);
|
||||
cmp_deeply(
|
||||
$dbData,
|
||||
{
|
||||
shipperId => $driver->shipperId,
|
||||
shipperId => $driver->getId,
|
||||
className => ref($driver),
|
||||
options => q|{"label":"Slow and dangerous","enabled":1}|,
|
||||
},
|
||||
|
|
@ -194,7 +193,6 @@ is (WebGUI::Shop::ShipDriver->getName($session), 'Shipper Driver', 'getName retu
|
|||
#
|
||||
#######################################################################
|
||||
|
||||
cmp_deeply($driver->get, $driver->options, 'get works like the options method with no param passed');
|
||||
is($driver->get('enabled'), 1, 'get the enabled entry from the options');
|
||||
is($driver->get('label'), 'Slow and dangerous', 'get the label entry from the options');
|
||||
|
||||
|
|
@ -215,7 +213,7 @@ my @forms = HTML::Form->parse($html, 'http://www.webgui.org');
|
|||
is (scalar @forms, 1, 'getEditForm generates just 1 form');
|
||||
|
||||
my @inputs = $forms[0]->inputs;
|
||||
is (scalar @inputs, 5, 'getEditForm: the form has 5 controls');
|
||||
is (scalar @inputs, 7, 'getEditForm: the form has 7 controls');
|
||||
|
||||
my @interestingFeatures;
|
||||
foreach my $input (@inputs) {
|
||||
|
|
@ -232,11 +230,19 @@ cmp_deeply(
|
|||
type => 'submit',
|
||||
},
|
||||
{
|
||||
name => 'shipperId',
|
||||
name => 'driverId',
|
||||
type => 'hidden',
|
||||
},
|
||||
{
|
||||
name => 'className',
|
||||
name => 'shop',
|
||||
type => 'hidden',
|
||||
},
|
||||
{
|
||||
name => 'method',
|
||||
type => 'hidden',
|
||||
},
|
||||
{
|
||||
name => 'do',
|
||||
type => 'hidden',
|
||||
},
|
||||
{
|
||||
|
|
@ -295,40 +301,12 @@ cmp_deeply(
|
|||
'new croaks unless the requested shipperId object exists in the db',
|
||||
);
|
||||
|
||||
my $driverCopy = WebGUI::Shop::ShipDriver->new($session, $driver->shipperId);
|
||||
my $driverCopy = WebGUI::Shop::ShipDriver->new($session, $driver->getId);
|
||||
|
||||
is($driver->getId, $driverCopy->getId, 'same id');
|
||||
is($driver->className, $driverCopy->className, 'same className');
|
||||
cmp_deeply($driver->options, $driverCopy->options, 'same options');
|
||||
is(ref $driver, ref $driverCopy, 'same className');
|
||||
cmp_deeply($driver->get, $driverCopy->get, 'same options');
|
||||
|
||||
my $brokenDriver = WebGUI::Shop::ShipDriver->create($session, {label=>'to be broken', enabled=>'0'});
|
||||
$session->db->write('update shipper set options=NULL where shipperId=?',[$brokenDriver->getId]);
|
||||
|
||||
eval { $oldDriver = WebGUI::Shop::ShipDriver->new($session, $brokenDriver->getId); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidParam', 'new croaks if the options column in the db is null');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => re('Options property for \S{22} was broken in the db'),
|
||||
param => undef,
|
||||
),
|
||||
'new croaks if the options column in the db is null',
|
||||
);
|
||||
|
||||
$session->db->write(q{update shipper set options='' where shipperId=?},[$brokenDriver->getId]);
|
||||
|
||||
eval { $oldDriver = WebGUI::Shop::ShipDriver->new($session, $brokenDriver->getId); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidParam', 'new croaks if the options column in the db is empty string');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => re('Options property for \S{22} was broken in the db'),
|
||||
param => '',
|
||||
),
|
||||
'new croaks if the options column in the db is empty string',
|
||||
);
|
||||
|
||||
|
||||
#######################################################################
|
||||
|
|
@ -346,15 +324,15 @@ like ($@, qr/^You must override the calculate method/, 'calculate croaks to forc
|
|||
#
|
||||
#######################################################################
|
||||
|
||||
eval { $driver->set(); };
|
||||
eval { $driver->update(); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidParam', 'set takes exception to not giving it a hashref of options');
|
||||
isa_ok($e, 'WebGUI::Error::InvalidParam', 'update takes exception to not giving it a hashref of options');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => 'set was not sent a hashref of options to store in the database',
|
||||
error => 'update was not sent a hashref of options to store in the database',
|
||||
),
|
||||
'set takes exception to not giving it a hashref of options',
|
||||
'update takes exception to not giving it a hashref of options',
|
||||
);
|
||||
|
||||
#######################################################################
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue