Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 5e45f456

Von Moritz Bunkus vor etwa 12 Jahren hinzugefügt

  • ID 5e45f456a6790d3ecdf358edf76b3679a5cacd05
  • Vorgänger a99a31d6
  • Nachfolger f40865cb

Paginate-Controller-Helper: Paginaten innerhalb einer Action abstellen können

Unterschiede anzeigen:

SL/Controller/Helper/Paginated.pm
3 3
use strict;
4 4

  
5 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);
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 disable_pagination);
7 7

  
8 8
use constant PRIV => '__paginatedhelper_priv';
9 9

  
......
18 18
  $specs{FORM_PARAMS}     ||= [ qw(page per_page) ];
19 19
  $specs{ONLY}            ||= [];
20 20
  $specs{ONLY}              = [ $specs{ONLY} ] if !ref $specs{ONLY};
21
  $specs{ONLY_MAP}          = @{ $specs{ONLY} } ? { map { ($_ => 1) } @{ $specs{ONLY} } } : { '__ALL__' => 1 };
21 22

  
22 23
  $controller_paginate_spec = \%specs;
23 24

  
......
45 46

  
46 47
  my $spec              = $self->get_paginate_spec;
47 48

  
48
  my $priv              = $self->{PRIV()} || {};
49
  my $priv              = _priv($self);
49 50
  $params{page}         = $priv->{page}     unless defined $params{page};
50 51
  $params{per_page}     = $priv->{per_page} unless defined $params{per_page};
51 52

  
......
54 55
    per_page            => ($params{per_page} * 1) || $spec->{PER_PAGE},
55 56
  );
56 57

  
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() }
59
                        :                                         {};
60
  my $calculated_params = "SL::DB::Manager::$spec->{MODEL}"->paginate(%paginate_params, args => $paginate_args);
58
  my %paginate_args     = ref($spec->{PAGINATE_ARGS}) eq 'CODE' ? %{ $spec->{PAGINATE_ARGS}->($self) }
59
                        :     $spec->{PAGINATE_ARGS}            ? do { my $sub = $spec->{PAGINATE_ARGS}; %{ $self->$sub() } }
60
                        :                                         ();
61
  my $calculated_params = "SL::DB::Manager::$spec->{MODEL}"->paginate(%paginate_params, args => \%paginate_args);
61 62
  %paginate_params      = (
62 63
    %paginate_params,
63 64
    num_pages    => $calculated_params->{max},
......
69 70
  return %paginate_params;
70 71
}
71 72

  
73
sub disable_pagination {
74
  my ($self)               = @_;
75
  _priv($self)->{disabled} = 1;
76
}
77

  
72 78
#
73 79
# private functions
74 80
#
......
76 82
sub _save_current_paginate_params {
77 83
  my ($self)        = @_;
78 84

  
85
  return if !_is_enabled($self);
86

  
79 87
  my $paginate_spec = $self->get_paginate_spec;
80 88
  $self->{PRIV()}   = {
81 89
    page            => $::form->{ $paginate_spec->{FORM_PARAMS}->[0] } || 1,
......
87 95

  
88 96
sub _callback_handler_for_paginated {
89 97
  my ($self, %params) = @_;
90
  my $priv            = $self->{PRIV()} || {};
98
  my $priv            = _priv($self);
91 99

  
92
  if ($priv->{page}) {
100
  if (_is_enabled($self) && $priv->{page}) {
93 101
    my $paginate_spec                             = $self->get_paginate_spec;
94 102
    $params{ $paginate_spec->{FORM_PARAMS}->[0] } = $priv->{page};
95 103
    $params{ $paginate_spec->{FORM_PARAMS}->[1] } = $priv->{per_page} if $priv->{per_page};
......
102 110

  
103 111
sub _get_models_handler_for_paginated {
104 112
  my ($self, %params)    = @_;
105
  $params{model}       ||= $self->get_paginate_spec->{MODEL};
113
  my $spec               = $self->get_paginate_spec;
114
  $params{model}       ||= $spec->{MODEL};
106 115

  
107
  "SL::DB::Manager::$params{model}"->paginate($self->get_current_paginate_params, args => \%params);
116
  "SL::DB::Manager::$params{model}"->paginate($self->get_current_paginate_params, args => \%params) if _is_enabled($self);
108 117

  
109
  # $::lxdebug->dump(0, "GM handler for paginated; params nach modif:", \%params);
118
  # $::lxdebug->dump(0, "GM handler for paginated; params nach modif (is_enabled? " . _is_enabled($self) . ")", \%params);
110 119

  
111 120
  return %params;
112 121
}
113 122

  
123
sub _priv {
124
  my ($self)        = @_;
125
  $self->{PRIV()} ||= {};
126
  return $self->{PRIV()};
127
}
128

  
129
sub _is_enabled {
130
  my ($self) = @_;
131
  return !_priv($self)->{disabled} && ($self->get_paginate_spec->{ONLY_MAP}->{$self->action_name} || $self->get_paginate_spec->{ONLY_MAP}->{'__ALL__'});
132
}
133

  
114 134
1;
115 135
__END__
116 136

  
......
294 314
to L<make_paginated> after normalization (hash reference construction,
295 315
applying default parameters etc).
296 316

  
317
=item C<disable_pagination>
318

  
319
Disable pagination for the duration of the current action. Can be used
320
when using the attribute C<ONLY> to L<make_paginated> does not
321
cover all cases.
322

  
297 323
=back
298 324

  
299 325
=head1 BUGS

Auch abrufbar als: Unified diff