1 package SL::Controller::Helper::GetModels;
5 use parent 'Rose::Object';
6 use SL::Controller::Helper::GetModels::Filtered;
7 use SL::Controller::Helper::GetModels::Sorted;
8 use SL::Controller::Helper::GetModels::Paginated;
10 use Scalar::Util qw(weaken);
12 use Rose::Object::MakeMethods::Generic (
13 scalar => [ qw(controller model query with_objects filtered sorted paginated finalized final_params) ],
14 'scalar --get_set_init' => [ qw(handlers source) ],
15 array => [ qw(plugins) ],
18 use constant PRIV => '__getmodelshelperpriv';
25 my %params = $self->finalize;
27 return $self->manager->get_all(%params);
31 my ($self, $plugin) = @_;
32 die 'cannot change internal state after finalize was called' if $self->finalized;
33 die 'unsupported plugin' unless $self->can($plugin) && $self->$plugin && $self->$plugin->isa('SL::Controller::Helper::GetModels::Base');
35 $self->$plugin->disabled(1);
39 my ($self, $plugin) = @_;
40 die 'cannot change internal state after finalize was called' if $self->finalized;
41 die 'unsupported plugin' unless $self->can($plugin) && $self->$plugin && $self->$plugin->isa('SL::Controller::Helper::GetModels::Base');
42 $self->$plugin->disabled(0);
45 sub is_enabled_plugin {
46 my ($self, $plugin) = @_;
47 die 'unsupported plugin' unless $self->can($plugin) && $self->$plugin && $self->$plugin->isa('SL::Controller::Helper::GetModels::Base');
48 $self->$plugin->is_enabled;
51 # TODO: get better delegation
52 sub set_report_generator_sort_options {
53 my ($self, %params) = @_;
56 $self->sorted->set_report_generator_sort_options(%params);
59 sub get_paginate_args {
61 my %params = $self->finalize;
63 $self->paginated->get_current_paginate_params(%params);
72 sub get_current_sort_params {
75 $self->sorted->read_params;
79 my ($self, %params) = @_;
82 $self->model(delete $params{model});
85 for my $plugin (qw(filtered sorted paginated)) {
86 next unless my $spec = delete $params{$plugin} // {};
87 my $plugin_class = "SL::Controller::Helper::GetModels::" . ucfirst $plugin;
88 push @plugins, $self->$plugin($plugin_class->new(%$spec, get_models => $self));
90 $self->plugins(@plugins);
92 $self->SUPER::init(%params);
94 $_->read_params for $self->plugins;
96 weaken $self->controller if $self->controller;
100 my ($self, %params) = @_;
102 return %{ $self->final_params } if $self->finalized;
104 push @{ $params{query} ||= [] }, @{ $self->query || [] };
105 push @{ $params{with_objects} ||= [] }, @{ $self->with_objects || [] };
107 %params = $_->finalize(%params) for $self->plugins;
110 $self->final_params(\%params);
115 sub register_handlers {
116 my ($self, %additional_handlers) = @_;
118 my $handlers = $self->handlers;
119 map { push @{ $handlers->{$_} }, $additional_handlers{$_} if $additional_handlers{$_} } keys %$handlers;
123 sub get_models_url_params {
124 my ($class, $sub_name_or_code) = @_;
126 my $code = (ref($sub_name_or_code) || '') eq 'CODE' ? $sub_name_or_code : sub { shift->$sub_name_or_code(@_) };
128 my ($self, %params) = @_;
129 my @additional_params = $code->($self);
132 (scalar(@additional_params) == 1) && (ref($additional_params[0]) eq 'HASH') ? %{ $additional_params[0] } : @additional_params,
136 push @{ _registered_handlers($class)->{callback} }, $callback;
140 my ($self, %override_params) = @_;
142 my %default_params = $self->_run_handlers('callback', action => $self->controller->action_name);
144 return $self->controller->url_for(%default_params, %override_params);
148 die "No 'model' to work on" unless $_[0]->model;
149 "SL::DB::Manager::" . $_[0]->model;
153 # private/internal functions
157 my ($self, $handler_type, %params) = @_;
159 foreach my $sub (@{ $self->handlers->{$handler_type} }) {
160 if (ref $sub eq 'CODE') {
161 %params = $sub->($self, %params);
162 } elsif ($self->can($sub)) {
163 %params = $self->$sub(%params);
165 die "SL::Controller::Helper::GetModels::get_callback: Cannot call $sub on " . ref($self) . ")";
191 SL::Controller::Helper::GetModels - Base mixin for controller helpers
192 dealing with semi-automatic handling of sorting and paginating lists
196 For a proper synopsis see L<SL::Controller::Helper::Sorted>.
200 For a generic overview see L<SL::Controller::Helper::Sorted>.
202 This base module is the interface between a controller and specialized
203 helper modules that handle things like sorting and paginating. The
204 specialized helpers register themselves with this module via a call to
205 L<register_get_models_handlers> during compilation time (e.g. in the
206 case of C<Sorted> this happens when the controller calls
207 L<SL::Controller::Helper::Sorted::make_sorted>).
209 A controller will later usually call the L<get_models>
210 function. Templates will call the L<get_callback> function. Both
211 functions run the registered handlers handing over control to the
212 specialized helpers so that they may inject their parameters into the
215 The C<GetModels> helper hooks into the controller call to the action
216 via a C<run_before> hook. This is done so that it can remember the
217 action called by the user. This is used for constructing the callback
220 =head1 PACKAGE FUNCTIONS
224 =item C<get_models_url_params $class, $sub>
226 Register one of the controller's subs to be called whenever an URL has
227 to be generated (e.g. for sort and pagination links). This is a way
228 for the controller to add additional parameters to the URL (e.g. for
231 The C<$sub> parameter can be either a code reference or the name of
232 one of the controller's functions.
234 The value returned by this C<$sub> must be either a single hash
235 reference or a hash of key/value pairs to add to the URL.
237 =item C<register_get_models_handlers $class, %handlers>
239 This function should only be called from other controller helpers like
240 C<Sorted> or C<Paginated>. It is not exported and must therefore be
241 called its full name. The first parameter C<$class> must be the actual
242 controller's class name.
244 If C<%handlers> contains a key C<ONLY> then it is passed to the hook
245 registration in L<SL::Controller::Base::run_before>.
247 The C<%handlers> register callback functions in the specialized
248 controller helpers that are called during invocation of
249 L<get_callback> or L<get_models>. Possible keys are C<callback> and
252 Each handler (the value in the hash) can be either a code reference
253 (in which case it is called directly) or the name of an instance
254 function callable on a controller instance. In both cases the handler
255 receives a hash of parameters built during this very call to
256 L<get_callback> or L<get_models> respectively. The handler's return
257 value must be the new hash to be used in calls to further handlers and
258 to the actual database model functions later on.
262 =head1 INSTANCE FUNCTIONS
266 =item C<get_callback [%params]>
268 Return an URL suitable for use as a callback parameter. It maps to the
269 current controller and action. All registered handlers of type
270 'callback' (e.g. the ones by C<Sorted> and C<Paginated>) can inject
271 the parameters they need so that the same list view as is currently
272 visible can be re-rendered.
274 Optional C<%params> passed to this function may override any parameter
275 set by the registered handlers.
277 =item C<get_models [%params]>
279 Query the model manager via C<get_all> and return its result. The
280 parameters to C<get_all> are constructed by calling all registered
281 handlers of type 'models' (e.g. the ones by C<Sorted> and
284 Optional C<%params> passed to this function may override any parameter
285 set by the registered handlers.
287 The return value is the an array reference of C<Rose> models.
297 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>