get_models callback entefrnt und in den finalize prozess integriert
[kivitendo-erp.git] / SL / Controller / Helper / GetModels.pm
1 package SL::Controller::Helper::GetModels;
2
3 use strict;
4
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;
9
10 use Scalar::Util qw(weaken);
11
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) ],
16 );
17
18 use constant PRIV => '__getmodelshelperpriv';
19
20
21 # official interface
22
23 sub get {
24   my ($self) = @_;
25   my %params = $self->finalize;
26
27   return $self->manager->get_all(%params);
28 }
29
30 sub disable_plugin {
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');
34   $self->$plugin->disabled(1);
35 }
36
37 sub enable_plugin {
38   my ($self, $plugin) = @_;
39   die 'cannot change internal state after finalize was called' if $self->finalized;
40   die 'unsupported plugin' unless $self->can($plugin) && $self->$plugin && $self->$plugin->isa('SL::Controller::Helper::GetModels::Base');
41   $self->$plugin->disabled(0);
42 }
43
44 sub is_enabled_plugin {
45   my ($self, $plugin) = @_;
46   die 'unsupported plugin' unless $self->can($plugin) && $self->$plugin && $self->$plugin->isa('SL::Controller::Helper::GetModels::Base');
47   $self->$plugin->is_enabled;
48 }
49
50 # TODO: get better delegation
51 sub set_report_generator_sort_options {
52   my ($self, %params) = @_;
53   $self->finalize;
54
55   $self->sorted->set_report_generator_sort_options(%params);
56 }
57
58 sub get_paginate_args {
59   my ($self) = @_;
60   my %params = $self->finalize;
61
62   $self->paginated->get_current_paginate_params(%params);
63 }
64
65 sub init {
66   my ($self, %params) = @_;
67
68   # TODO: default model
69   $self->model(delete $params{model});
70
71   my @plugins;
72   for my $plugin (qw(filtered sorted paginated)) {
73     next unless my $spec = delete $params{$plugin} // {};
74     my $plugin_class = "SL::Controller::Helper::GetModels::" . ucfirst $plugin;
75     push @plugins, $self->$plugin($plugin_class->new(%$spec, get_models => $self));
76   }
77   $self->plugins(@plugins);
78
79   $self->SUPER::init(%params);
80
81   $_->read_params for $self->plugins;
82
83   weaken $self->controller if $self->controller;
84 }
85
86 sub finalize {
87   my ($self, %params) = @_;
88
89   return %{ $self->final_params } if $self->finalized;
90
91   push @{ $params{query}        ||= [] }, @{ $self->query || [] };
92   push @{ $params{with_objects} ||= [] }, @{ $self->with_objects || [] };
93
94   %params = $_->finalize(%params) for $self->plugins;
95
96   $self->finalized(1);
97   $self->final_params(\%params);
98
99   return %params;
100 }
101
102 sub register_handlers {
103   my ($self, %additional_handlers) = @_;
104
105   my $handlers    = $self->handlers;
106   map { push @{ $handlers->{$_} }, $additional_handlers{$_} if $additional_handlers{$_} } keys %$handlers;
107 }
108
109 # TODO fix this
110 sub get_models_url_params {
111   my ($class, $sub_name_or_code) = @_;
112
113   my $code     = (ref($sub_name_or_code) || '') eq 'CODE' ? $sub_name_or_code : sub { shift->$sub_name_or_code(@_) };
114   my $callback = sub {
115     my ($self, %params)   = @_;
116     my @additional_params = $code->($self);
117     return (
118       %params,
119       (scalar(@additional_params) == 1) && (ref($additional_params[0]) eq 'HASH') ? %{ $additional_params[0] } : @additional_params,
120     );
121   };
122
123   push @{ _registered_handlers($class)->{callback} }, $callback;
124 }
125
126 sub get_callback {
127   my ($self, %override_params) = @_;
128
129   my %default_params = $self->_run_handlers('callback', action => $self->controller->action_name);
130
131   return $self->controller->url_for(%default_params, %override_params);
132 }
133
134 sub manager {
135   die "No 'model' to work on" unless $_[0]->model;
136   "SL::DB::Manager::" . $_[0]->model;
137 }
138
139 #
140 # private/internal functions
141 #
142
143 sub _run_handlers {
144   my ($self, $handler_type, %params) = @_;
145
146   foreach my $sub (@{ $self->handlers->{$handler_type} }) {
147     if (ref $sub eq 'CODE') {
148       %params = $sub->($self, %params);
149     } elsif ($self->can($sub)) {
150       %params = $self->$sub(%params);
151     } else {
152       die "SL::Controller::Helper::GetModels::get_callback: Cannot call $sub on " . ref($self) . ")";
153     }
154   }
155
156   return %params;
157 }
158
159 sub init_handlers {
160   {
161     callback => [],
162   }
163 }
164
165 sub init_source {
166   $::form
167 }
168
169 1;
170 __END__
171
172 =pod
173
174 =encoding utf8
175
176 =head1 NAME
177
178 SL::Controller::Helper::GetModels - Base mixin for controller helpers
179 dealing with semi-automatic handling of sorting and paginating lists
180
181 =head1 SYNOPSIS
182
183 For a proper synopsis see L<SL::Controller::Helper::Sorted>.
184
185 =head1 OVERVIEW
186
187 For a generic overview see L<SL::Controller::Helper::Sorted>.
188
189 This base module is the interface between a controller and specialized
190 helper modules that handle things like sorting and paginating. The
191 specialized helpers register themselves with this module via a call to
192 L<register_get_models_handlers> during compilation time (e.g. in the
193 case of C<Sorted> this happens when the controller calls
194 L<SL::Controller::Helper::Sorted::make_sorted>).
195
196 A controller will later usually call the L<get_models>
197 function. Templates will call the L<get_callback> function. Both
198 functions run the registered handlers handing over control to the
199 specialized helpers so that they may inject their parameters into the
200 call chain.
201
202 The C<GetModels> helper hooks into the controller call to the action
203 via a C<run_before> hook. This is done so that it can remember the
204 action called by the user. This is used for constructing the callback
205 in L<get_callback>.
206
207 =head1 PACKAGE FUNCTIONS
208
209 =over 4
210
211 =item C<get_models_url_params $class, $sub>
212
213 Register one of the controller's subs to be called whenever an URL has
214 to be generated (e.g. for sort and pagination links). This is a way
215 for the controller to add additional parameters to the URL (e.g. for
216 filter parameters).
217
218 The C<$sub> parameter can be either a code reference or the name of
219 one of the controller's functions.
220
221 The value returned by this C<$sub> must be either a single hash
222 reference or a hash of key/value pairs to add to the URL.
223
224 =item C<register_get_models_handlers $class, %handlers>
225
226 This function should only be called from other controller helpers like
227 C<Sorted> or C<Paginated>. It is not exported and must therefore be
228 called its full name. The first parameter C<$class> must be the actual
229 controller's class name.
230
231 If C<%handlers> contains a key C<ONLY> then it is passed to the hook
232 registration in L<SL::Controller::Base::run_before>.
233
234 The C<%handlers> register callback functions in the specialized
235 controller helpers that are called during invocation of
236 L<get_callback> or L<get_models>. Possible keys are C<callback> and
237 C<models>.
238
239 Each handler (the value in the hash) can be either a code reference
240 (in which case it is called directly) or the name of an instance
241 function callable on a controller instance. In both cases the handler
242 receives a hash of parameters built during this very call to
243 L<get_callback> or L<get_models> respectively. The handler's return
244 value must be the new hash to be used in calls to further handlers and
245 to the actual database model functions later on.
246
247 =back
248
249 =head1 INSTANCE FUNCTIONS
250
251 =over 4
252
253 =item C<get_callback [%params]>
254
255 Return an URL suitable for use as a callback parameter. It maps to the
256 current controller and action. All registered handlers of type
257 'callback' (e.g. the ones by C<Sorted> and C<Paginated>) can inject
258 the parameters they need so that the same list view as is currently
259 visible can be re-rendered.
260
261 Optional C<%params> passed to this function may override any parameter
262 set by the registered handlers.
263
264 =item C<get_models [%params]>
265
266 Query the model manager via C<get_all> and return its result. The
267 parameters to C<get_all> are constructed by calling all registered
268 handlers of type 'models' (e.g. the ones by C<Sorted> and
269 C<Paginated>).
270
271 Optional C<%params> passed to this function may override any parameter
272 set by the registered handlers.
273
274 The return value is the an array reference of C<Rose> models.
275
276 =back
277
278 =head1 BUGS
279
280 Nothing here yet.
281
282 =head1 AUTHOR
283
284 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
285
286 =cut