1 package SL::Controller::Helper::Paginated;
5 use Exporter qw(import);
6 our @EXPORT = qw(make_paginated get_paginate_spec get_current_paginate_params _save_current_paginate_params _get_models_handler_for_paginated _callback_handler_for_paginated);
8 use constant PRIV => '__paginatedhelper_priv';
10 my $controller_paginate_spec;
13 my ($class, %specs) = @_;
15 $specs{MODEL} ||= $class->controller_name;
16 $specs{MODEL} =~ s{ ^ SL::DB:: (?: .* :: )? }{}x;
17 $specs{PER_PAGE} ||= "SL::DB::Manager::$specs{MODEL}"->default_objects_per_page;
18 $specs{FORM_PARAMS} ||= [ qw(page per_page) ];
20 $specs{ONLY} = [ $specs{ONLY} ] if !ref $specs{ONLY};
22 $controller_paginate_spec = \%specs;
24 my %hook_params = @{ $specs{ONLY} } ? ( only => $specs{ONLY} ) : ();
25 $class->run_before('_save_current_paginate_params', %hook_params);
27 SL::Controller::Helper::GetModels::register_get_models_handlers(
29 callback => '_callback_handler_for_paginated',
30 get_models => '_get_models_handler_for_paginated',
34 # $::lxdebug->dump(0, "CONSPEC", \%specs);
37 sub get_paginate_spec {
38 my ($class_or_self) = @_;
40 return $controller_paginate_spec;
43 sub get_current_paginate_params {
44 my ($self, %params) = @_;
46 my $spec = $self->get_paginate_spec;
48 my $priv = $self->{PRIV()} || {};
49 $params{page} = $priv->{page} unless defined $params{page};
50 $params{per_page} = $priv->{per_page} unless defined $params{per_page};
52 my %paginate_params = (
53 page => ($params{page} * 1) || 1,
54 per_page => ($params{per_page} * 1) || $spec->{PER_PAGE},
57 my $paginate_args = ref($spec->{PAGINATE_ARGS}) eq 'CODE' ? $spec->{PAGINATE_ARGS}->($self)
58 : $spec->{PAGINATE_ARGS} ? do { my $sub = $spec->{PAGINATE_ARGS}; $self->$sub() }
60 my $calculated_params = "SL::DB::Manager::$spec->{MODEL}"->paginate(%paginate_params, args => $paginate_args);
63 num_pages => $calculated_params->{max},
64 common_pages => $calculated_params->{common},
67 # $::lxdebug->dump(0, "get_current_paginate_params: ", \%paginate_params);
69 return %paginate_params;
76 sub _save_current_paginate_params {
79 my $paginate_spec = $self->get_paginate_spec;
81 page => $::form->{ $paginate_spec->{FORM_PARAMS}->[0] } || 1,
82 per_page => $::form->{ $paginate_spec->{FORM_PARAMS}->[1] } * 1,
85 # $::lxdebug->message(0, "saving current paginate params to " . $self->{PRIV()}->{page} . ' / ' . $self->{PRIV()}->{per_page});
88 sub _callback_handler_for_paginated {
89 my ($self, %params) = @_;
90 my $priv = $self->{PRIV()} || {};
93 my $paginate_spec = $self->get_paginate_spec;
94 $params{ $paginate_spec->{FORM_PARAMS}->[0] } = $priv->{page};
95 $params{ $paginate_spec->{FORM_PARAMS}->[1] } = $priv->{per_page} if $priv->{per_page};
98 # $::lxdebug->dump(0, "CB handler for paginated; params nach modif:", \%params);
103 sub _get_models_handler_for_paginated {
104 my ($self, %params) = @_;
105 $params{model} ||= $self->get_paginate_spec->{MODEL};
107 "SL::DB::Manager::$params{model}"->paginate($self->get_current_paginate_params, args => \%params);
109 # $::lxdebug->dump(0, "GM handler for paginated; params nach modif:", \%params);
123 SL::Controller::Helper::Paginated - A helper for semi-automatic handling
124 of paginating lists of database models in a controller
130 use SL::Controller::Helper::GetModels;
131 use SL::Controller::Helper::Paginated;
133 __PACKAGE__->make_paginated(
134 MODEL => 'BackgroundJobHistory',
135 ONLY => [ qw(list) ],
136 FORM_PARAMS => [ qw(page per_page) ],
142 my $paginated_models = $self->get_models;
143 $self->render('controller/list', ENTRIES => $paginated_models);
158 [% FOREACH entry = ENTRIES %]
166 [% L.paginate_controls %]
170 This specialized helper module enables controllers to display a
171 paginatable list of database models with as few lines as possible. It
172 can also be combined trivially with the L<SL::Controller::Sorted>
173 helper for sortable lists.
175 For this to work the controller has to provide the information which
176 indexes are eligible for paginateing etc. by a call to
177 L<make_paginated> at compile time.
179 The underlying functionality that enables the use of more than just
180 the paginate helper is provided by the controller helper
181 C<GetModels>. See the documentation for L<SL::Controller::Sorted> for
182 more information on it.
184 A template can use the method C<paginate_controls> from the layout
185 helper module C<L> which renders the links for navigation between the
188 This module requires that the Rose model managers use their C<Paginated>
191 The C<Paginated> helper hooks into the controller call to the action via
192 a C<run_before> hook. This is done so that it can remember the paginate
193 parameters that were used in the current view.
195 =head1 PACKAGE FUNCTIONS
199 =item C<make_paginated %paginate_spec>
201 This function must be called by a controller at compile time. It is
202 uesd to set the various parameters required for this helper to do its
205 The hash C<%paginate_spec> can include the following parameters:
211 Optional. A string: the name of the Rose database model that is used
212 as a default in certain cases. If this parameter is missing then it is
213 derived from the controller's package (e.g. for the controller
214 C<SL::Controller::BackgroundJobHistory> the C<MODEL> would default to
215 C<BackgroundJobHistory>).
217 =item * C<PAGINATE_ARGS>
219 Optional. Either a code reference or the name of function to be called
220 on the controller importing this helper.
222 If this funciton is given then the paginate helper calls it whenever
223 it has to count the total number of models for calculating the number
224 of pages to display. The function must return a hash reference with
225 elements suitable for passing to a Rose model manager's C<get_all>
228 This can be used e.g. when filtering is used.
232 Optional. An integer: the number of models to return per page.
234 Defaults to the underlying database model's default number of models
237 =item * C<FORM_PARAMS>
239 Optional. An array reference with exactly two strings that name the
240 indexes in C<$::form> in which the current page's number (the first
241 element in the array) and the number of models per page (the second
242 element in the array) are stored.
244 Defaults to the values C<page> and C<per_page> if missing.
248 Optional. An array reference containing a list of action names for
249 which the paginate parameters should be saved. If missing or empty then
250 all actions invoked on the controller are monitored.
256 =head1 INSTANCE FUNCTIONS
258 These functions are called on a controller instance.
262 =item C<get_paginate_spec>
264 Returns a hash containing the currently active paginate
265 parameters. The following keys are returned:
271 The currently active page number (numbering starts at 1).
275 Number of models per page (at least 1).
279 Number of pages to display (at least 1).
281 =item * C<common_pages>
283 An array reference with one hash reference for each possible
284 page. Each hash ref contains the keys C<active> (C<1> if that page is
285 the currently active page), C<page> (the page number this hash
286 reference describes) and C<visible> (whether or not it should be
291 =item C<get_current_paginate_params>
293 Returns a hash reference to the paginate spec structure given in the call
294 to L<make_paginated> after normalization (hash reference construction,
295 applying default parameters etc).
305 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>