Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 26813507

Von Sven Schöling vor etwa 11 Jahren hinzugefügt

  • ID 26813507465a6fd209cd902c33a16974627e9670
  • Vorgänger 8a5a3e9c
  • Nachfolger 91175699

Doku cleanup

Unterschiede anzeigen:

SL/Controller/Helper/GetModels/Filtered.pm
140 140
    filtered => 0,
141 141
  );
142 142

  
143

  
144 143
=head1 OVERVIEW
145 144

  
146 145
This C<GetModels> plugin enables use of the
SL/Controller/Helper/GetModels/Paginated.pm
122 122

  
123 123
In a controller:
124 124

  
125
  use SL::Controller::Helper::GetModels;
126
  use SL::Controller::Helper::Paginated;
127

  
128
  __PACKAGE__->make_paginated(
129
    MODEL       => 'BackgroundJobHistory',
130
    ONLY        => [ qw(list) ],
131
    FORM_PARAMS => [ qw(page per_page) ],
125
  SL::Controller::Helper::GetModels->new(
126
    ..
127
    paginated => {
128
      form_params => [ qw(page per_page) ],
129
      per_page    => 20,
130
    }
132 131
  );
133 132

  
134
  sub action_list {
135
    my ($self) = @_;
136

  
137
    my $paginated_models = $self->get_models;
138
    $self->render('controller/list', ENTRIES => $paginated_models);
139
  }
140

  
141 133
In said template:
142 134

  
143
  [% USE L %]
144

  
145
  <table>
146
   <thead>
147
    <tr>
148
     ...
149
    </tr>
150
   </thead>
151

  
152
   <tbody>
153
    [% FOREACH entry = ENTRIES %]
154
     <tr>
155
      ...
156
     </tr>
157
    [% END %]
158
   </tbody>
159
  </table>
160

  
161 135
  [% L.paginate_controls %]
162 136

  
163 137
=head1 OVERVIEW
164 138

  
165
This specialized helper module enables controllers to display a
166
paginatable list of database models with as few lines as possible. It
167
can also be combined trivially with the L<SL::Controller::Sorted>
168
helper for sortable lists.
139
This C<GetModels> plugin enables controllers to display a
140
paginatable list of database models with as few lines as possible.
169 141

  
170 142
For this to work the controller has to provide the information which
171
indexes are eligible for paginateing etc. by a call to
172
L<make_paginated> at compile time.
143
indexes are eligible for paginateing etc. during C<GetModels> creation.
173 144

  
174 145
The underlying functionality that enables the use of more than just
175 146
the paginate helper is provided by the controller helper
176
C<GetModels>. See the documentation for L<SL::Controller::Sorted> for
177
more information on it.
147
C<GetModels>. See the documentation for L<SL::Controller::Helper::GetModels>
148
for more information on it.
178 149

  
179 150
A template can use the method C<paginate_controls> from the layout
180 151
helper module C<L> which renders the links for navigation between the
......
183 154
This module requires that the Rose model managers use their C<Paginated>
184 155
helper.
185 156

  
186
The C<Paginated> helper hooks into the controller call to the action via
187
a C<run_before> hook. This is done so that it can remember the paginate
188
parameters that were used in the current view.
189

  
190
=head1 PACKAGE FUNCTIONS
157
=head1 OPTIONS
191 158

  
192 159
=over 4
193 160

  
194
=item C<make_paginated %paginate_spec>
195

  
196
This function must be called by a controller at compile time. It is
197
uesd to set the various parameters required for this helper to do its
198
magic.
199

  
200
The hash C<%paginate_spec> can include the following parameters:
201

  
202
=over 4
203

  
204
=item * C<MODEL>
205

  
206
Optional. A string: the name of the Rose database model that is used
207
as a default in certain cases. If this parameter is missing then it is
208
derived from the controller's package (e.g. for the controller
209
C<SL::Controller::BackgroundJobHistory> the C<MODEL> would default to
210
C<BackgroundJobHistory>).
211

  
212
=item * C<PAGINATE_ARGS>
213

  
214
Optional. Either a code reference or the name of function to be called
215
on the controller importing this helper.
216

  
217
If this funciton is given then the paginate helper calls it whenever
218
it has to count the total number of models for calculating the number
219
of pages to display. The function must return a hash reference with
220
elements suitable for passing to a Rose model manager's C<get_all>
221
function.
222

  
223
This can be used e.g. when filtering is used.
224

  
225
=item * C<PER_PAGE>
161
=item * C<per_page>
226 162

  
227
Optional. An integer: the number of models to return per page.
163
Optional. The number of models to return per page.
228 164

  
229 165
Defaults to the underlying database model's default number of models
230 166
per page.
231 167

  
232
=item * C<FORM_PARAMS>
168
=item * C<form_params>
233 169

  
234 170
Optional. An array reference with exactly two strings that name the
235 171
indexes in C<$::form> in which the current page's number (the first
......
238 174

  
239 175
Defaults to the values C<page> and C<per_page> if missing.
240 176

  
241
=item * C<ONLY>
242

  
243
Optional. An array reference containing a list of action names for
244
which the paginate parameters should be saved. If missing or empty then
245
all actions invoked on the controller are monitored.
246

  
247
=back
248

  
249 177
=back
250 178

  
251 179
=head1 INSTANCE FUNCTIONS
252 180

  
253
These functions are called on a controller instance.
181
These functions are called on a C<GetModels> instance and delegated here.
254 182

  
255 183
=over 4
256 184

  
......
285 213

  
286 214
=item C<get_current_paginate_params>
287 215

  
288
Returns a hash reference to the paginate spec structure given in the call
289
to L<make_paginated> after normalization (hash reference construction,
216
Returns a hash reference to the paginate spec structure given in the
217
configuration after normalization (hash reference construction,
290 218
applying default parameters etc).
291 219

  
292
=item C<disable_pagination>
293

  
294
Disable pagination for the duration of the current action. Can be used
295
when using the attribute C<ONLY> to L<make_paginated> does not
296
cover all cases.
297

  
298 220
=back
299 221

  
300 222
=head1 BUGS
301 223

  
302
Nothing here yet.
224
C<common_pages> generates an array with an entry for every page, which gets
225
slow if there are a lot of entries. Current observation holds that up to about
226
1000 pages there is no noticable slowdown, but at about 10000 it gets
227
noticable. At 100k-500k pages it's takes way too long and should be remodelled.
228

  
229
This case currently only applies for databases with very large amounts of parts
230
that get paginated, but BackgroundJobHistory can also accumulate.
303 231

  
304 232
=head1 AUTHOR
305 233

  
306 234
Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
307 235

  
236
Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>
237

  
308 238
=cut
SL/Controller/Helper/GetModels/Sorted.pm
143 143

  
144 144
In a controller:
145 145

  
146
  use SL::Controller::Helper::GetModels;
147
  use SL::Controller::Helper::Sorted;
148

  
149
  __PACKAGE__->make_sorted(                                     # update this
150
    DEFAULT_BY   => 'run_at',
151
    DEFAULT_DIR  => 1,
152
    MODEL        => 'BackgroundJobHistory',
153
    ONLY         => [ qw(list) ],
154

  
155
    error        => $::locale->text('Error'),
156
    package_name => $::locale->text('Package name'),
157
    run_at       => $::locale->text('Run at'),
146
  SL::Controller::Helper::GetModels->new(
147
    ...
148
    sorted => {
149
      _default => {
150
        by  => 'run_at',
151
        dir => 1,
152
      },
153
      error        => $::locale->text('Error'),
154
      package_name => $::locale->text('Package name'),
155
      run_at       => $::locale->text('Run at'),
156
    },
158 157
  );
159 158

  
160
  sub action_list {
161
    my ($self) = @_;
162

  
163
    my $sorted_models = $self->get_models;
164
    $self->render('controller/list', ENTRIES => $sorted_models);
165
  }
166

  
167
In said template:
159
In template:
168 160

  
169 161
  [% USE L %]
170 162

  
171 163
  <table>
172 164
   <tr>
173
    <th>[% L.sortable_table_header('package_name') %]</th>         # models
165
    <th>[% L.sortable_table_header('package_name') %]</th>
174 166
    <th>[% L.sortable_table_header('run_at') %]</th>
175 167
    <th>[% L.sortable_table_header('error') %]</th>
176 168
   </tr>
......
186 178

  
187 179
=head1 OVERVIEW
188 180

  
189
This specialized helper module enables controllers to display a
181
This C<GetModels> plugin enables controllers to display a
190 182
sortable list of database models with as few lines as possible.
191 183

  
192 184
For this to work the controller has to provide the information which
193
indexes are eligible for sorting etc. by a call to L<make_sorted> at  #not compiletime
194
compile time.
195

  
196
The underlying functionality that enables the use of more than just
197
the sort helper is provided by the controller helper C<GetModels>. It
198
provides mechanisms for helpers like this one to hook into certain
199
calls made by the controller (C<get_callback> and C<get_models>) so
200
that the specialized helpers can inject their parameters into the
201
calls to e.g. C<SL::DB::Manager::SomeModel::get_all>.
202

  
203
A template on the other hand can use the method
204
C<sortable_table_header> from the layout helper module C<L>.
185
indexes are eligible for sorting etc. through it's configuration of
186
C<GetModels>.
205 187

  
206
This module requires that the Rose model managers use their C<Sorted>
207
helper.
188
A template can then use the method C<sortable_table_header> from the layout
189
helper module C<L>.
208 190

  
209
The C<Sorted> helper hooks into the controller call to the action via
210
a C<run_before> hook. This is done so that it can remember the sort
211
parameters that were used in the current view.
191
This module requires that the Rose model managers use their
192
C<SL::DB::Helper::Sorted> helper.
212 193

  
213
=head1 PACKAGE FUNCTIONS
194
=head1 OPTIONS
214 195

  
215 196
=over 4
216 197

  
217
=item C<make_sorted %sort_spec>                                         # meh complete rewrite
218

  
219
This function must be called by a controller at compile time. It is
220
uesd to set the various parameters required for this helper to do its
221
magic.
222

  
223
There are two sorts of keys in the hash C<%sort_spec>. The first kind
224
is written in all upper-case. Those parameters are control
225
parameters. The second kind are all lower-case and represent indexes
226
that can be used for sorting (similar to database column names). The
227
second kind are also the indexes you use in a template when calling
228
C<[% L.sorted_table_header(...) %]>.
229

  
230
Control parameters include the following:
231

  
232
=over 4
198
=item * C<_default HASHREF>
233 199

  
234
=item * C<MODEL>
200
Optional. If it exists, it is expected to contain the keys C<by> and C<dir> and
201
will be used to set the default sorting if nothing is found in C<source>.
235 202

  
236
Optional. A string: the name of the Rose database model that is used
237
as a default in certain cases. If this parameter is missing then it is
238
derived from the controller's package (e.g. for the controller
239
C<SL::Controller::BackgroundJobHistory> the C<MODEL> would default to
240
C<BackgroundJobHistory>).
203
Defaults to the underlying database model's default.
241 204

  
242
=item * C<DEFAULT_BY>
243

  
244
Optional. A string: the index to sort by if the user hasn't clicked on
245
any column yet (meaning: if the C<$::form> parameters for sorting do
246
not contain a valid index).
247

  
248
Defaults to the underlying database model's default sort column name.
249

  
250
=item * C<DEFAULT_DIR>
251

  
252
Optional. Default sort direction (ascending for trueish values,
253
descrending for falsish values).
254

  
255
Defaults to the underlying database model's default sort direction.
256

  
257
=item * C<FORM_PARAMS>
205
=item * C<form_params>
258 206

  
259 207
Optional. An array reference with exactly two strings that name the
260
indexes in C<$::form> in which the sort index (the first element in
208
indexes in C<source> in which the sort index (the first element in
261 209
the array) and sort direction (the second element in the array) are
262 210
stored.
263 211

  
264 212
Defaults to the values C<sort_by> and C<sort_dir> if missing.
265 213

  
266
=item * C<ONLY>
267

  
268
Optional. An array reference containing a list of action names for
269
which the sort parameters should be saved. If missing or empty then
270
all actions invoked on the controller are monitored.
271

  
272 214
=back
273 215

  
274
All keys that are written in all lower-case name indexes that can be
275
used for sorting. Each value to such a key can be either a string or a
276
hash reference containing certain elements. If the value is only a
277
string then such a hash reference is constructed, and the string is
216
All other keys can be used for sorting. Each value to such a key can be either
217
a string or a hash reference containing certain elements. If the value is only
218
a string then such a hash reference is constructed, and the string is
278 219
used as the value for the C<title> key.
279 220

  
280 221
These possible elements are:
......
311 252

  
312 253
=head1 INSTANCE FUNCTIONS
313 254

  
314
These functions are called on a controller instance.
255
These functions are called on a C<GetModels> instance and delegating to this plugin.
315 256

  
316 257
=over 4
317 258

  
......
320 261
Returns a hash containing the currently active sort parameters.
321 262

  
322 263
The key C<by> contains the active sort index referring to the
323
C<%sort_spec> given to L<make_sorted>.
264
C<%sort_spec> given by the configuration.
324 265

  
325 266
The key C<dir> is either C<1> or C<0>.
326 267

  
327 268
=item C<get_current_sort_params>
328 269

  
329
Returns a hash reference to the sort spec structure given in the call
330
to L<make_sorted> after normalization (hash reference construction,
331
applying default parameters etc).
270
Returns a hash reference to the sort spec structure given in the configuration
271
after normalization (hash reference construction, applying default parameters
272
etc).
332 273

  
333 274
=item C<set_report_generator_sort_options %params>
334 275

  
......
342 283
=item 2. it sets the the links for those column headers that are
343 284
sortable and
344 285

  
345
=item 3. it adds the C<FORM_PARAMS> fields to the list of variables in
286
=item 3. it adds the C<form_params> fields to the list of variables in
346 287
the report generator's export options.
347 288

  
348 289
=back
......
365 306

  
366 307
Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
367 308

  
309
Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>
310

  
368 311
=cut

Auch abrufbar als: Unified diff