Add better support for user profile fields for addresses to the Cart and the EMS.

This commit is contained in:
Colin Kuskie 2011-08-11 16:27:29 -07:00
parent 39049e1c7c
commit 428ea58327
21 changed files with 1657 additions and 62 deletions

View file

@ -153,6 +153,19 @@ sub getId {
return $self->get("addressId");
}
#-------------------------------------------------------------------
=head2 isProfile ()
Returns 1 if the address is linked to the user's profile.
=cut
sub isProfile {
my $self = shift;
return ($self->get("isProfile") eq 1);
}
#-------------------------------------------------------------------
@ -192,7 +205,6 @@ sub new {
return $self;
}
#-------------------------------------------------------------------
=head2 update ( properties )
@ -259,16 +271,20 @@ The organization or company that this user is a part of.
The address book that this address belongs to.
=head4 isProfile
Whether or not this address is linked to the user profile. Defaults to 0
=cut
sub update {
my ($self, $newProperties) = @_;
my $id = id $self;
foreach my $field (qw(addressBookId email organization address1 address2 address3 state code city label firstName lastName country phoneNumber)) {
foreach my $field (qw(addressBookId email organization address1 address2 address3 state code city label firstName lastName country phoneNumber isProfile)) {
$properties{$id}{$field} = (exists $newProperties->{$field}) ? $newProperties->{$field} : $properties{$id}{$field};
}
$self->addressBook->session->db->setRow("address","addressId",$properties{$id});
}
1;

View file

@ -8,6 +8,7 @@ use WebGUI::Asset::Template;
use WebGUI::Exception::Shop;
use WebGUI::Form;
use WebGUI::International;
use WebGUI::Shop::Admin;
use WebGUI::Shop::Address;
use Scalar::Util qw/blessed/;
@ -195,6 +196,11 @@ C<update> method.
sub get {
my ($self, $name) = @_;
if($name eq "profileAddressId" && !$properties{id $self}{$name}) {
$properties{id $self}{$name} = $self->session->db->quickScalar(q{
select addressId from address where addressBookId=? and isProfile=1
},[$self->getId]);
}
if (defined $name) {
return $properties{id $self}{$name};
}
@ -295,6 +301,55 @@ sub getDefaultAddress {
#-------------------------------------------------------------------
=head2 getProfileAddress ()
Returns the profile address for this address book if there is one. Otherwise throws a WebGUI::Error::ObjectNotFound exception.
=cut
sub getProfileAddress {
my ($self) = @_;
my $id = $self->get('profileAddressId');
if ($id ne '') {
my $address = eval { $self->getAddress($id) };
my $e;
if ($e = WebGUI::Error->caught('WebGUI::Error::ObjectNotFound')) {
$e->rethrow;
}
elsif ($e = WebGUI::Error->caught) {
$e->rethrow;
}
else {
return $address;
}
}
WebGUI::Error::ObjectNotFound->throw(error=>"No profile address.");
}
#-------------------------------------------------------------------
=head2 getProfileAddressMappings ( )
Class or object method which returns the profile address field mappings
=cut
sub getProfileAddressMappings {
return {
homeAddress => 'address1',
homeCity => 'city',
homeState => 'state',
homeZip => 'code',
homeCountry => 'country',
homePhone => 'phoneNumber',
email => 'email',
firstName => 'firstName',
lastName => 'lastName'
}
}
#-------------------------------------------------------------------
=head2 getId ()
Returns the unique id for this addressBook.
@ -419,7 +474,7 @@ sub newByUserId {
}
else {
# nope create one for the user
return $class->create($session);
return $class->create($session,$userId);
}
}
@ -442,19 +497,19 @@ sub processAddressForm {
$prefix ||= '';
my $form = $self->session->form;
my %addressData = (
label => $form->get($prefix . "label"),
firstName => $form->get($prefix . "firstName"),
lastName => $form->get($prefix . "lastName"),
address1 => $form->get($prefix . "address1"),
address2 => $form->get($prefix . "address2"),
address3 => $form->get($prefix . "address3"),
city => $form->get($prefix . "city"),
state => $form->get($prefix . "state"),
code => $form->get($prefix . "code", "zipcode"),
country => $form->get($prefix . "country", "country"),
phoneNumber => $form->get($prefix . "phoneNumber", "phone"),
email => $form->get($prefix . "email", "email"),
organization => $form->get($prefix . "organization"),
label => $form->get($prefix . "label") || '',
firstName => $form->get($prefix . "firstName") || '',
lastName => $form->get($prefix . "lastName") || '',
address1 => $form->get($prefix . "address1") || '',
address2 => $form->get($prefix . "address2") || '',
address3 => $form->get($prefix . "address3") || '',
city => $form->get($prefix . "city") || '',
state => $form->get($prefix . "state") || '',
code => $form->get($prefix . "code", "zipcode") || '',
country => $form->get($prefix . "country", "country") || '',
phoneNumber => $form->get($prefix . "phoneNumber", "phone") || '',
email => $form->get($prefix . "email", "email") || '',
organization => $form->get($prefix . "organization") || '',
);
##Label is optional in the form, but required for the UI and API.
@ -489,9 +544,26 @@ sub update {
foreach my $field (qw(userId defaultAddressId)) {
$properties{$id}{$field} = (exists $newProperties->{$field}) ? $newProperties->{$field} : $properties{$id}{$field};
}
$self->session->db->setRow("addressBook","addressBookId",$properties{$id});
my %postProperties = %{$properties{$id}};
delete $postProperties{profileAddressId};
$self->session->db->setRow("addressBook","addressBookId",\%postProperties);
}
#-------------------------------------------------------------------
=head2 uncache ( )
Deletes the addressBook cache
=cut
sub uncache {
my $self = shift;
delete $addressCache{id $self};
}
#-------------------------------------------------------------------
=head2 www_ajaxGetAddress ( )
@ -536,6 +608,80 @@ sub www_ajaxSave {
#-------------------------------------------------------------------
=head2 www_ajaxSearch ( )
Gets a JSON object with addresses returned based on the search
parameters from the form.
=cut
sub www_ajaxSearch {
my $self = shift;
my $session = $self->session;
my $form = $session->form;
my $name = $form->get('name');
my $fields = {
firstName => (split(" ",$name))[0] || "",
lastName => (split(" ",$name))[1] || "",
organization => $form->get('organization') || "",
address1 => $form->get('address1') || "",
address2 => $form->get('address2') || "",
address3 => $form->get('address3') || "",
city => $form->get('city') || "",
state => $form->get('state') || "",
code => $form->get('zipcode') || "",
country => $form->get('country') || "",
email => $form->get('email') || "",
phoneNumber => $form->get('phone') || "",
};
my $clause = [];
my $params = [];
foreach my $field (keys %$fields) {
my $field_value = $fields->{$field};
if($field_value) {
$field = $session->db->dbh->quote_identifier($field);
$field_value = $field_value."%";
push(@$clause,qq{$field like ?});
push(@$params,$field_value);
}
}
my $admin = WebGUI::Shop::Admin->new($session);
unless ($session->user->isAdmin || $admin->canManage || $admin->isCashier) {
push(@$clause,qq{users.userId=?});
push(@$params,$session->user->getId);
}
my $where = "";
$where = "where ".join(" and ",@$clause) if scalar(@$clause);
my $query = qq{
select
address.*,
users.username
from
address
join addressBook on address.addressBookId = addressBook.addressBookId
join users on addressBook.userId = users.userId
$where
limit 3
};
my $sth = $session->db->read($query,$params);
my $var = [];
while (my $hash = $sth->hashRef) {
push(@$var,$hash);
}
$session->http->setMimeType('text/plain');
return JSON->new->encode($var);
}
#-------------------------------------------------------------------
=head2 www_deleteAddress ( )
Deletes an address from the book.
@ -544,7 +690,10 @@ Deletes an address from the book.
sub www_deleteAddress {
my $self = shift;
$self->getAddress($self->session->form->get("addressId"))->delete;
my $address = $self->getAddress($self->session->form->get("addressId"));
if (defined $address && !$address->isProfile) {
$address->delete;
}
return $self->www_view;
}
@ -700,8 +849,20 @@ sub www_editAddressSave {
$self->addAddress(\%addressData);
}
else {
$self->getAddress($form->get('addressId'))->update(\%addressData);
my $addressId = $form->get('addressId');
my $address = $self->getAddress($addressId);
$address->update(\%addressData);
if($address->isProfile) {
my $u = WebGUI::User->new($self->session, $self->get("userId"));
my $address_mappings = $self->getProfileAddressMappings;
foreach my $field (keys %$address_mappings) {
my $addr_field = $address_mappings->{$field};
$u->profileField($field,$address->get($addr_field));
}
}
}
#profile fields updated in WebGUI::Shop::Address->update
return $self->www_view;
}
@ -732,12 +893,12 @@ sub www_view {
return $self->www_editAddress;
}
foreach my $address (@availableAddresses) {
push(@addresses, {
%{$address->get},
address => $address->getHtmlFormatted,
isDefault => ($self->get('defaultAddressId') eq $address->getId),
deleteButton =>
WebGUI::Form::formHeader( $session )
deleteButton => $address->get("isProfile") ? undef : WebGUI::Form::formHeader( $session )
. WebGUI::Form::hidden( $session, { name => 'shop', value => 'address' } )
. WebGUI::Form::hidden( $session, { name => 'method', value => 'deleteAddress' } )
. WebGUI::Form::hidden( $session, { name => 'addressId', value => $address->getId } )

View file

@ -1054,6 +1054,13 @@ sub www_view {
$self->update({shippingAddressId=>''});
}
#get the billing address
my $billingAddress = eval { $self->getBillingAddress };
if (my $e = WebGUI::Error->caught("WebGUI::Error::ObjectNotFound") && $self->get('billingAddressId')) {
# choose another address cuz we've got a problem
$self->update({billingAddressId=>''});
}
# generate template variables for the items in the cart
my @items = ();
tie my %addressOptions, 'Tie::IxHash';
@ -1215,9 +1222,11 @@ sub www_view {
$addressBook->appendAddressFormVars(\%var, 'shipping_', $shippingAddressData);
$addressBook->appendAddressFormVars(\%var, 'billing_', $billingAddressData);
my $has_billing_addr - $self->get('billingAddressId') ? 1 : 0;
$var{sameShippingAsBilling} = WebGUI::Form::yesNo($session, {
name => 'sameShippingAsBilling',
value => $self->get('billingAddressId') && $self->get('billingAddressId') eq $self->get('shippingAddressId'),
value => (($has_billing_addr && $self->get('billingAddressId') eq $self->get('shippingAddressId')) || !$has_billing_addr),
});
}