Javascript enhancements for cart address handling

This commit is contained in:
Paul Driver 2010-04-28 15:08:55 -07:00
parent aedda01e58
commit 17dd742c08
3 changed files with 279 additions and 0 deletions

View file

@ -224,6 +224,30 @@ sub getAddress {
#-------------------------------------------------------------------
=head2 getAddressByLabel ( label )
Returns an address object.
=head3 id
An address object's label, e.g. 'Home', 'Work'
=cut
sub getAddressByLabel {
my ($self, $label) = @_;
my $sql = q{
SELECT addressId
FROM address
WHERE addressBookId = ?
AND label = ?
};
my $id = $self->session->db->quickScalar($sql, [$self->getId, $label]);
return $id && $self->getAddress($id);
}
#-------------------------------------------------------------------
=head2 getAddresses ( )
Returns an array reference of address objects that are in this book.
@ -470,6 +494,48 @@ sub update {
#-------------------------------------------------------------------
=head2 www_ajaxGetAddress ( )
Gets a JSON object representing the address given by the addressId form
parameter
=cut
sub www_ajaxGetAddress {
my $self = shift;
my $session = $self->session;
$session->http->setMimeType('text/plain');
my $addressId = $session->form->get('addressId');
my $address = $self->getAddress($addressId) or return;
return JSON->new->encode($address->get);
}
#-------------------------------------------------------------------
=head2 www_ajaxSave ( )
Saves an address book entry
=cut
sub www_ajaxSave {
my $self = shift;
my $session = $self->session;
my $address = JSON->new->decode($session->form->get('address'));
my $obj = $self->getAddressByLabel($address->{label});
if ($obj) {
$obj->update($address);
}
else {
$obj = $self->addAddress($address);
}
$session->http->setMimeType('text/plain');
return $obj->getId;
}
#-------------------------------------------------------------------
=head2 www_deleteAddress ( )
Deletes an address from the book.