/[Frey]/trunk/lib/Frey/ORM/Pager.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/lib/Frey/ORM/Pager.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 969 - (hide annotations)
Fri Jan 9 16:02:33 2009 UTC (15 years, 4 months ago) by dpavlin
File size: 3010 byte(s)
start refactoring of Fey RDBMS support into Frey::ORM
1 dpavlin 969 package Frey::ORM::Pager;
2 dpavlin 143 use Moose;
3     use MooseX::AttributeHelpers;
4    
5     use Data::Page;
6     use Data::Dump qw/dump/;
7    
8     has 'pager' => (
9     is => 'rw',
10     isa => 'Data::Page',
11     default => sub { Data::Page->new },
12     );
13    
14     has 'fey_class' => (
15     is => 'rw',
16     isa => 'Str',
17     required => 1,
18     );
19    
20     has 'item_constructor' => (
21     is => 'rw',
22     isa => 'CodeRef',
23     required => 1,
24     );
25    
26     has 'items' => (
27     metaclass => 'Collection::Array',
28     is => 'rw',
29 dpavlin 969 isa => 'ArrayRef[Frey::ORM::Item]',
30 dpavlin 143 default => sub { [] },
31     provides => {
32     'push' => 'add_item',
33     },
34     );
35    
36 dpavlin 207 has 'range_around' => (
37     is => 'rw',
38     isa => 'Int',
39     default => 10,
40     );
41    
42 dpavlin 969 has 'page' => (
43     is => 'rw',
44     isa => 'Int',
45     required => 1,
46     default => 1,
47     );
48    
49     has 'per_page' => (
50     is => 'rw',
51     isa => 'Int',
52     required => 1,
53     default => 20,
54     );
55    
56     =head1 METHODS
57    
58     =cut
59    
60 dpavlin 149 sub first_item {
61     my $self = shift;
62     $self->items->[0];
63     }
64    
65 dpavlin 143 sub last_item {
66     my $self = shift;
67     my $last = $#{ $self->items };
68     $self->update_collection if $last < 0;
69     $self->items->[ $#{ $self->items } ];
70     }
71    
72     =head2 update_collection
73    
74     $o->update_collection;
75    
76     =cut
77    
78     sub update_collection {
79     my ( $self ) = @_;
80    
81     return if $#{ $self->items } >= 0;
82    
83     warn "## update_collection from iterator";
84    
85 dpavlin 149 if ( ! $self->fey_class->can('collection') ) {
86     warn "ERROR: ", $self->fey_class, " can't do collection";
87     return;
88     }
89    
90 dpavlin 143 my $i = $self->fey_class->collection( $self->pager );
91     while ( my $u = $i->next ) {
92     $self->add_item(
93     $self->item_constructor->(
94     fey => $u,
95     )
96     );
97     }
98     }
99    
100     =head2 items_in_layout
101    
102 dpavlin 969 my $html = $o->items_in_layout;
103 dpavlin 143
104     =cut
105    
106     sub items_in_layout {
107 dpavlin 969 my ( $self ) = @_;
108 dpavlin 143
109     # FIXME somehow, this seem cludgy
110 dpavlin 969 $self->pager->current_page( $self->page );
111     $self->items( [] );
112     $self->update_collection;
113 dpavlin 143
114     my $html = join('', map {
115 dpavlin 969 $_->process
116 dpavlin 143 } @{ $self->items } );
117    
118     return $html;
119     }
120    
121     =head2 render_pager
122    
123     my $html = $o->render_pager;
124    
125     =cut
126    
127     sub render_pager {
128     my $self = shift;
129    
130     my $pager = $self->pager;
131    
132 dpavlin 207 my @show_pages;
133     my $after_current = 0;
134    
135     if ( $pager->current_page <= $self->range_around + 2 ) {
136     @show_pages = ( $pager->first_page .. $pager->current_page );
137     $after_current = $self->range_around - $pager->current_page;
138     } else {
139     @show_pages = ( $pager->first_page, '', $pager->current_page - $self->range_around .. $pager->current_page );
140     }
141    
142     if ( $pager->current_page + $self->range_around + 1 >= $pager->last_page ) {
143     push @show_pages, ( $pager->current_page + 1 .. $pager->last_page );
144     } else {
145     push @show_pages, ( $pager->current_page + 1 .. $pager->current_page + $after_current + $self->range_around, '', $pager->last_page );
146     }
147    
148     warn "## show_pages = ",dump( @show_pages );
149    
150 dpavlin 143 return join( ' ',
151     qq|<div class="notice">|,
152     'Showing',
153     $pager->first, '-', $pager->last,
154     'of',
155     $pager->total_entries,
156     'results<br>',
157     # 'page', $pager->current_page, '/', $pager->last_page,
158 dpavlin 969 Frey::ORM::Links->new(
159 dpavlin 143 name => 'page',
160     current => $pager->current_page,
161 dpavlin 207 values => \@show_pages,
162     empty => '...',
163     current_tag => 'b',
164 dpavlin 143 )->links,
165     qq|</div>|
166     );
167     }
168    
169     1;

  ViewVC Help
Powered by ViewVC 1.1.26