introduced Utils.pm, with paginate function


			
			
				1.2@1701
			
			
		
Daniel Tiefnig 18 years ago
parent 20d12b9233
commit 9334e91503

@ -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

@ -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}};
}
}

@ -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;

@ -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 {

@ -30,7 +30,7 @@
<h3>Customers</h3>
<div class="p1">
<table>
<table class="inc_vspace">
<tr class="table_header"><td>CustomerID</td><td>Name</td><td>Firma</td></tr>
[% FOREACH customer = customer_list %]
@ -54,6 +54,49 @@
[% END %]
</table>
[% IF pagination %]
<div class="pagination">
<ul>
[% IF offset == 0 %]
<li class="disablepage">&#171; prev</li>
[% ELSE %]
<li class="nextpage">
<a href="/customer/search?use_session=1&amp;offset=[% offset - 1 %]">&#171; prev</a>
</li>
[% END %]
[% FOREACH pagine = pagination %]
[% IF pagine.offset == offset %]
<li class="currentpage">
[% pagine.offset + 1 %]
</li>
[% ELSIF pagine.offset == -1 %]
...
[% ELSE %]
<li>
<a href="/customer/search?use_session=1&amp;offset=[% pagine.offset %]">[% pagine.offset + 1 %]</a>
</li>
[% END %]
[% END %]
[% IF offset >= max_offset %]
<li class="disablepage">
next &#187;
</li>
[% ELSE %]
<li class="nextpage">
<a href="/customer/search?use_session=1&amp;offset=[% offset + 1 %]">next &#187;</a>
</li>
[% END %]
</ul>
</div>
[% END %]
</div>
[% ELSIF searched %]
<div class="p1">
No matching customers found.
</div>
[% END %]

Loading…
Cancel
Save