From 378f53c806b9a9aa0230de5e057f7c29cbaaa1a2 Mon Sep 17 00:00:00 2001 From: Andreas Granig Date: Mon, 26 May 2014 15:37:53 +0200 Subject: [PATCH] MT#7191 Use DT to select contact country. --- debian/control | 2 + .../Panel/Controller/API/CustomerContacts.pm | 2 + lib/NGCP/Panel/Controller/API/Root.pm | 5 ++ .../Panel/Controller/API/SystemContacts.pm | 2 + lib/NGCP/Panel/Controller/Contact.pm | 52 +++++++++++++++++++ lib/NGCP/Panel/Field/Country.pm | 17 ++++++ lib/NGCP/Panel/Form/Contact/Reseller.pm | 5 +- lib/NGCP/Panel/Role/API/CustomerContacts.pm | 4 ++ lib/NGCP/Panel/Role/API/SystemContacts.pm | 4 ++ 9 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 lib/NGCP/Panel/Field/Country.pm diff --git a/debian/control b/debian/control index 624480bfde..eeb5cf68cc 100644 --- a/debian/control +++ b/debian/control @@ -31,6 +31,7 @@ Build-Depends: debhelper (>= 8), libfile-spec-perl (>= 3.4000~), libfile-type-perl, libgd-gd2-perl, + libgeography-countries-perl, libhash-merge-simple-perl, libhash-storediterator-perl, libhtml-formhandler-model-dbic-perl, @@ -109,6 +110,7 @@ Depends: gettext, libfile-spec-perl (>= 3.4000~), libfile-type-perl, libgd-gd2-perl, + libgeography-countries-perl, libhash-merge-simple-perl, libhtml-formhandler-model-dbic-perl, libhtml-formhandler-perl (>= 0.40026), diff --git a/lib/NGCP/Panel/Controller/API/CustomerContacts.pm b/lib/NGCP/Panel/Controller/API/CustomerContacts.pm index eebadd8457..021c7037bf 100644 --- a/lib/NGCP/Panel/Controller/API/CustomerContacts.pm +++ b/lib/NGCP/Panel/Controller/API/CustomerContacts.pm @@ -158,11 +158,13 @@ sub POST :Allow { my $form = $self->get_form($c); $resource->{reseller_id} //= undef; + $resource->{country}{id} = delete $resource->{country}; last unless $self->validate_form( c => $c, resource => $resource, form => $form, ); + $resource->{country} = $resource->{country}{id}; my $reseller = $c->model('DB')->resultset('resellers') ->find($resource->{reseller_id}); diff --git a/lib/NGCP/Panel/Controller/API/Root.pm b/lib/NGCP/Panel/Controller/API/Root.pm index de380cc9b3..d7927944cd 100644 --- a/lib/NGCP/Panel/Controller/API/Root.pm +++ b/lib/NGCP/Panel/Controller/API/Root.pm @@ -185,6 +185,9 @@ sub field_to_json : Private { when(/\+NGCP::Panel::Field::Regex/) { return "String"; } + when(/\+NGCP::Panel::Field::Country/) { + return "String"; + } when(/\+NGCP::Panel::Field::EmailList/) { return "String"; } @@ -232,6 +235,8 @@ sub get_collection_properties { $name = 'primary_number'; } elsif($f->type =~ /AliasNumber/) { $name = 'alias_numbers'; + } elsif($f->type =~ /Country$/) { + $name = 'country'; } elsif($f->type !~ /Regex|EmailList|SubscriberStatusSelect|SubscriberLockSelect|Identifier|PosInteger/) { $name .= '_id'; } diff --git a/lib/NGCP/Panel/Controller/API/SystemContacts.pm b/lib/NGCP/Panel/Controller/API/SystemContacts.pm index 53ea6b41fb..a59422e3f1 100644 --- a/lib/NGCP/Panel/Controller/API/SystemContacts.pm +++ b/lib/NGCP/Panel/Controller/API/SystemContacts.pm @@ -145,12 +145,14 @@ sub POST :Allow { ); last unless $resource; + $resource->{country}{id} = delete $resource->{country}; my $form = $self->get_form($c); last unless $self->validate_form( c => $c, resource => $resource, form => $form, ); + $resource->{country} = $resource->{country}{id}; my $now = NGCP::Panel::Utils::DateTime::current_local; $resource->{create_timestamp} = $now; diff --git a/lib/NGCP/Panel/Controller/Contact.pm b/lib/NGCP/Panel/Controller/Contact.pm index 469a5933dc..62b908d74e 100644 --- a/lib/NGCP/Panel/Controller/Contact.pm +++ b/lib/NGCP/Panel/Controller/Contact.pm @@ -8,6 +8,8 @@ use NGCP::Panel::Form::Contact::Admin; use NGCP::Panel::Utils::Message; use NGCP::Panel::Utils::Navigation; +use Geography::Countries qw/countries country CNT_F_REGULAR CNT_I_FLAG CNT_I_CODE2/; + sub auto :Does(ACL) :ACLDetachTo('/denied_page') :AllowedRole(admin) :AllowedRole(reseller) { my ($self, $c) = @_; $c->log->debug(__PACKAGE__ . '::auto'); @@ -75,6 +77,7 @@ sub create :Chained('list_contact') :PathPart('create') :Args(0) { if($c->user->is_superuser && $no_reseller) { delete $form->values->{reseller}; } + $form->values->{country} = $form->values->{country}{id}; my $contact = $c->stash->{contacts}->create($form->values); delete $c->session->{created_objects}->{reseller}; $c->session->{created_objects}->{contact} = { id => $contact->id }; @@ -121,6 +124,7 @@ sub edit :Chained('base') :PathPart('edit') :Args(0) { my $form; my $params = { $c->stash->{contact}->get_inflated_columns }; $params = $params->merge($c->session->{created_objects}); + $params->{country}{id} = delete $params->{country}; if($c->user->is_superuser && $no_reseller) { $form = NGCP::Panel::Form::Contact::Reseller->new; $params->{reseller}{id} = $c->user->reseller_id; @@ -152,6 +156,7 @@ sub edit :Chained('base') :PathPart('edit') :Args(0) { $form->values->{reseller_id} = $form->values->{reseller}{id}; } delete $form->values->{reseller}; + $form->values->{country} = $form->values->{country}{id}; $c->stash->{contact}->update($form->values); $c->flash(messages => [{type => 'success', text => $c->loc('Contact successfully changed')}]); delete $c->session->{created_objects}->{reseller}; @@ -229,6 +234,53 @@ sub ajax_noreseller :Chained('list_contact') :PathPart('ajax_noreseller') :Args( $c->detach( $c->view("JSON") ); } +sub countries_ajax :Chained('/') :PathPart('contact/country/ajax') :Args(0) { + my ($self, $c) = @_; + + my $from = $c->request->params->{iDisplayStart} // 0; + my $len = $c->request->params->{iDisplayLength} // 4; + my $to = $from + $len - 1; + my $search = $c->request->params->{sSearch}; + my $top = $c->request->params->{iIdOnTop}; + + my $top_entry; + my @aaData = map { + my @c = country($_); + if($c[CNT_I_CODE2]) { + my $e = { name => $_, id => $c[CNT_I_CODE2] }; + if($top && !$top_entry && $top eq $e->{id}) { + $top_entry = $e; + (); # we insert it as top element after the map + } else { + $e; + } + } else { (); } + } countries; + if($top_entry) { + unshift @aaData, $top_entry; + } + + if(defined $search) { + @aaData = map { + if($_->{id} =~ /$search/i || $_->{name} =~ /$search/i) { + $_; + } else { (); } + } @aaData; + } + + my $count = @aaData; + @aaData = @aaData[$from .. ($to < $#aaData ? $to : $#aaData)]; + + $c->stash(aaData => \@aaData, + iTotalRecords => $count, + iTotalDisplayRecords => $count, + sEcho => int($c->request->params->{sEcho} // 1), + ); + + $c->detach( $c->view("JSON") ); +} + + __PACKAGE__->meta->make_immutable; 1; diff --git a/lib/NGCP/Panel/Field/Country.pm b/lib/NGCP/Panel/Field/Country.pm new file mode 100644 index 0000000000..4891cb79af --- /dev/null +++ b/lib/NGCP/Panel/Field/Country.pm @@ -0,0 +1,17 @@ +package NGCP::Panel::Field::Country; +use HTML::FormHandler::Moose; +extends 'HTML::FormHandler::Field::Compound'; + +has_field 'id' => ( + type => '+NGCP::Panel::Field::DataTable', + label => 'Country', + do_label => 0, + do_wrapper => 0, + required => 1, + template => 'helpers/datatables_field.tt', + ajax_src => '/contact/country/ajax', + table_titles => ['#', 'Country'], + table_fields => ['id', 'name'], +); + +1; diff --git a/lib/NGCP/Panel/Form/Contact/Reseller.pm b/lib/NGCP/Panel/Form/Contact/Reseller.pm index 3317efda1a..81624e4e31 100644 --- a/lib/NGCP/Panel/Form/Contact/Reseller.pm +++ b/lib/NGCP/Panel/Form/Contact/Reseller.pm @@ -79,8 +79,9 @@ has_field 'city' => ( ); has_field 'country' => ( - type => 'Text', - maxlength => 2, + type => '+NGCP::Panel::Field::Country', + validate_when_empty => 1, + label => 'Country', element_attr => { rel => ['tooltip'], title => ['The two-letter ISO 3166-1 country code of the contact (e.g. US or DE).'] diff --git a/lib/NGCP/Panel/Role/API/CustomerContacts.pm b/lib/NGCP/Panel/Role/API/CustomerContacts.pm index 442870bba3..97b647fd65 100644 --- a/lib/NGCP/Panel/Role/API/CustomerContacts.pm +++ b/lib/NGCP/Panel/Role/API/CustomerContacts.pm @@ -60,6 +60,7 @@ sub hal_from_contact { relation => 'ngcp:'.$self->resource_name, ); + $resource{country}{id} = delete $resource{country}; $form //= $self->get_form($c); $self->validate_form( c => $c, @@ -67,6 +68,7 @@ sub hal_from_contact { form => $form, run => 0, ); + $resource{country} = $resource{country}{id}; $resource{id} = int($contact->id); $hal->resource({%resource}); @@ -83,6 +85,7 @@ sub contact_by_id { sub update_contact { my ($self, $c, $contact, $old_resource, $resource, $form) = @_; + $resource->{country}{id} = delete $resource->{country}; $form //= $self->get_form($c); # TODO: for some reason, formhandler lets missing reseller_id slip thru $resource->{reseller_id} //= undef; @@ -91,6 +94,7 @@ sub update_contact { form => $form, resource => $resource, ); + $resource->{country} = $resource->{country}{id}; my $now = NGCP::Panel::Utils::DateTime::current_local; $resource->{modify_timestamp} = $now; diff --git a/lib/NGCP/Panel/Role/API/SystemContacts.pm b/lib/NGCP/Panel/Role/API/SystemContacts.pm index 32cc0f97ee..5f75de2c38 100644 --- a/lib/NGCP/Panel/Role/API/SystemContacts.pm +++ b/lib/NGCP/Panel/Role/API/SystemContacts.pm @@ -46,6 +46,7 @@ sub hal_from_contact { relation => 'ngcp:'.$self->resource_name, ); + $resource{country}{id} = delete $resource{country}; $form //= $self->get_form($c); # TODO: i'd expect reseller to be removed automatically @@ -56,6 +57,7 @@ sub hal_from_contact { form => $form, run => 0, ); + $resource{country} = $resource{country}{id}; $resource{id} = int($contact->id); $hal->resource({%resource}); @@ -71,6 +73,7 @@ sub contact_by_id { sub update_contact { my ($self, $c, $contact, $old_resource, $resource, $form) = @_; + $resource->{country}{id} = delete $resource->{country}; $form //= $self->get_form($c); delete $resource->{reseller_id}; return unless $self->validate_form( @@ -78,6 +81,7 @@ sub update_contact { form => $form, resource => $resource, ); + $resource->{country} = $resource->{country}{id}; my $now = NGCP::Panel::Utils::DateTime::current_local; $resource->{modify_timestamp} = $now;