From 9334e91503da9838f3a58d37851bf03e3f3593d8 Mon Sep 17 00:00:00 2001 From: Daniel Tiefnig Date: Sat, 8 Nov 2008 16:14:34 +0000 Subject: [PATCH] introduced Utils.pm, with paginate function replaced pagination code in subscribers.pm by call to Utils::paginate added pagination support to customer search added new CSS style inc_vspace for increased vertical spacing in tables --- lib/admin/Controller/customer.pm | 37 ++++++++++++++++++++---- lib/admin/Controller/subscriber.pm | 21 ++------------ lib/admin/Utils.pm | 43 ++++++++++++++++++++++++++++ root/css/admin.css | 3 ++ root/tt/customer.tt | 45 +++++++++++++++++++++++++++++- 5 files changed, 125 insertions(+), 24 deletions(-) create mode 100644 lib/admin/Utils.pm diff --git a/lib/admin/Controller/customer.pm b/lib/admin/Controller/customer.pm index bf67dbd..4428457 100644 --- a/lib/admin/Controller/customer.pm +++ b/lib/admin/Controller/customer.pm @@ -37,16 +37,43 @@ sub search : Local { my ( $self, $c ) = @_; $c->stash->{template} = 'tt/customer.tt'; - my $search_string = $c->request->params->{search_string}; - + my $filter; + my $limit = 10; + my $offset = $c->request->params->{offset} || 0; + $offset = 0 if $offset !~ /^\d+$/; + + if($c->request->params->{use_session}) { + $filter = $c->session->{search_filter}; + } else { + $filter = $c->request->params->{search_string} || ''; + $c->session->{search_filter} = $filter; + } + + $c->stash->{search_string} = $filter; + $filter =~ s/\*/\%/; + $filter =~ s/\?/\_/; + my $customer_list; return unless $c->model('Provisioning')->call_prov( $c, 'billing', 'search_customers', - { filter => { anything => '%'.$search_string.'%' } }, + { filter => { anything => '%'.$filter.'%', + limit => $limit, + offset => $limit * $offset, + } }, \$customer_list ); - $c->stash->{customer_list} = $$customer_list{customers} - if ref $$customer_list{customers} eq 'ARRAY'; + $c->stash->{searched} = 1; + if(ref $$customer_list{customers} eq 'ARRAY' and @{$$customer_list{customers}}) { + $c->stash->{customer_list} = $$customer_list{customers}; + $c->stash->{total_count} = $$customer_list{total_count}; + $c->stash->{offset} = $offset; + if($$customer_list{total_count} > @{$$customer_list{customers}}) { + # paginate! + $c->stash->{pagination} = admin::Utils::paginate($c, $customer_list, $offset, $limit); + $c->stash->{max_offset} = $#{$c->stash->{pagination}}; + } + } + } =head2 getbyid diff --git a/lib/admin/Controller/subscriber.pm b/lib/admin/Controller/subscriber.pm index 78417e3..ee7b9c2 100644 --- a/lib/admin/Controller/subscriber.pm +++ b/lib/admin/Controller/subscriber.pm @@ -3,6 +3,7 @@ package admin::Controller::subscriber; use strict; use warnings; use base 'Catalyst::Controller'; +use admin::Utils; =head1 NAME @@ -94,24 +95,8 @@ sub search : Local { $c->stash->{offset} = $offset; if($$subscriber_list{total_count} > @{$$subscriber_list{subscribers}}) { # paginate! - my @pagination; - foreach my $page (0 .. int(($$subscriber_list{total_count} - 1) / $limit)) { - push @pagination, { offset => $page }; - } - $c->stash->{max_offset} = $#pagination; - if($#pagination > 10) { - if($offset <= 5) { - splice @pagination, 9, @pagination - (10), ({offset => -1}); - } else { - if($offset < @pagination - 6) { - splice @pagination, $offset + 4, @pagination - ($offset + 5), ({offset => -1}); - splice @pagination, 1, $offset - 4, ({offset => -1}); - } else { - splice @pagination, 1, @pagination - 10, ({offset => -1}); - } - } - } - $c->stash->{pagination} = \@pagination; + $c->stash->{pagination} = admin::Utils::paginate($c, $subscriber_list, $offset, $limit); + $c->stash->{max_offset} = $#{$c->stash->{pagination}}; } } diff --git a/lib/admin/Utils.pm b/lib/admin/Utils.pm new file mode 100644 index 0000000..947dcdf --- /dev/null +++ b/lib/admin/Utils.pm @@ -0,0 +1,43 @@ +package admin::Utils; +use strict; +use warnings; + +# Takes a search result as returned by the search_subscribers or +# search_customers provisioning functions, an offset and a limit and +# returns an array containing offset values for a pagination link list +# where each page should list $limit elements. +# The array will contain at most 11 entries, the first and last offset +# (0 and n) are always included. Further, the array will contain either: +# if n <= 10: +# * up to 9 elements from 1 .. n-1 +# if n > 10: +# * 8 elements from 1 .. 8 and -1, if offset <= 5 +# * -1 and 8 elements from n-9 .. n-1, if offset >= n-6 +# * -1, 7 elements from o-3 .. o+3 and -1, elsewise +sub paginate { + my ($c, $subscriber_list, $offset, $limit) = @_; + my @pagination; + + foreach my $page (0 .. int(($$subscriber_list{total_count} - 1) / $limit)) { + push @pagination, { offset => $page }; + } + if($#pagination > 10) { + if($offset <= 5) { + # offset at the beginning, include offsets 0 .. 8 + splice @pagination, 9, @pagination - (10), ({offset => -1}); + } else { + if($offset < @pagination - 6) { + # offset somewhere in the midle, include offsets (o-3 .. o+3) + splice @pagination, $offset + 4, @pagination - ($offset + 5), ({offset => -1}); + splice @pagination, 1, $offset - 4, ({offset => -1}); + } else { + #offset at the end, include offsets n-8 .. n + splice @pagination, 1, @pagination - 10, ({offset => -1}); + } + } + } + + return \@pagination; +} + +1; diff --git a/root/css/admin.css b/root/css/admin.css index 315c8cd..9b33ed8 100644 --- a/root/css/admin.css +++ b/root/css/admin.css @@ -80,6 +80,9 @@ h3 { #contentplace td { padding: 3px 5px 3px 5px; } +#contentplace .inc_vspace td { + padding: 3px 10px 3px 10px; +} /* some table alignment aliases */ #contentplace .tdcenter { diff --git a/root/tt/customer.tt b/root/tt/customer.tt index a99d314..4dc5aff 100644 --- a/root/tt/customer.tt +++ b/root/tt/customer.tt @@ -30,7 +30,7 @@

Customers

- +
[% FOREACH customer = customer_list %] @@ -54,6 +54,49 @@ [% END %]
CustomerIDNameFirma
+ + [% IF pagination %] + + [% END %] + +
+ + [% ELSIF searched %] + +
+ No matching customers found.
[% END %]