Revision 0f491583
Von Sven Schöling vor etwa 11 Jahren hinzugefügt
SL/Controller/Helper/GetModels.pm | ||
---|---|---|
7 | 7 |
use SL::Controller::Helper::GetModels::Sorted; |
8 | 8 |
use SL::Controller::Helper::GetModels::Paginated; |
9 | 9 |
|
10 |
use Scalar::Util qw(weaken); |
|
11 |
|
|
10 | 12 |
use Rose::Object::MakeMethods::Generic ( |
11 |
scalar => [ qw(controller model query with_objects filtered sorted paginated) ], |
|
12 |
'scalar --get_set_init' => [ qw(handlers) ], |
|
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) ], |
|
13 | 16 |
); |
14 | 17 |
|
15 | 18 |
use constant PRIV => '__getmodelshelperpriv'; |
16 | 19 |
|
17 |
#my $registered_handlers = {}; |
|
20 |
|
|
21 |
# official interface |
|
22 |
|
|
23 |
sub get { |
|
24 |
my ($self) = @_; |
|
25 |
my %params = $self->finalize; |
|
26 |
%params = $self->_run_handlers('get_models', %params); |
|
27 |
|
|
28 |
return $self->manager->get_all(%params); |
|
29 |
} |
|
30 |
|
|
31 |
sub disable_plugin { |
|
32 |
my ($self, $plugin) = @_; |
|
33 |
die 'cannot change internal state after finalize was called' if $self->finalized; |
|
34 |
die 'unsupported plugin' unless $self->can($plugin) && $self->$plugin && $self->$plugin->isa('SL::Controller::Helper::GetModels::Base'); |
|
35 |
$self->$plugin->disabled(1); |
|
36 |
} |
|
37 |
|
|
38 |
sub enable_plugin { |
|
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); |
|
43 |
} |
|
44 |
|
|
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; |
|
49 |
} |
|
50 |
|
|
51 |
# TODO: get better delegation |
|
52 |
sub set_report_generator_sort_options { |
|
53 |
my ($self, %params) = @_; |
|
54 |
$self->finalize; |
|
55 |
|
|
56 |
$self->sorted->set_report_generator_sort_options(%params); |
|
57 |
} |
|
58 |
|
|
59 |
sub get_paginate_args { |
|
60 |
my ($self) = @_; |
|
61 |
my %params = $self->finalize; |
|
62 |
|
|
63 |
$self->paginated->get_current_paginate_params(%params); |
|
64 |
} |
|
18 | 65 |
|
19 | 66 |
sub init { |
20 | 67 |
my ($self, %params) = @_; |
21 | 68 |
|
22 |
# for my $plugin (qw(filtered sorted paginated)) { |
|
23 |
# next unless $params{$plugin}; |
|
24 |
# $self->${ \"make_$plugin" }(%{ delete $params{$plugin} || {} }); |
|
25 |
# } |
|
26 |
# |
|
27 | 69 |
# TODO: default model |
28 | 70 |
$self->model(delete $params{model}); |
29 | 71 |
|
72 |
my @plugins; |
|
30 | 73 |
for my $plugin (qw(filtered sorted paginated)) { |
31 | 74 |
next unless my $spec = delete $params{$plugin} // {}; |
32 | 75 |
my $plugin_class = "SL::Controller::Helper::GetModels::" . ucfirst $plugin; |
33 |
$self->$plugin($plugin_class->new(%$spec, get_models => $self)); |
|
76 |
push @plugins, $self->$plugin($plugin_class->new(%$spec, get_models => $self));
|
|
34 | 77 |
} |
78 |
$self->plugins(@plugins); |
|
35 | 79 |
|
36 | 80 |
$self->SUPER::init(%params); |
81 |
|
|
82 |
$_->read_params for $self->plugins; |
|
83 |
|
|
84 |
weaken $self->controller if $self->controller; |
|
85 |
} |
|
86 |
|
|
87 |
sub finalize { |
|
88 |
my ($self, %params) = @_; |
|
89 |
|
|
90 |
return %{ $self->final_params } if $self->finalized; |
|
91 |
|
|
92 |
push @{ $params{query} ||= [] }, @{ $self->query || [] }; |
|
93 |
push @{ $params{with_objects} ||= [] }, @{ $self->with_objects || [] }; |
|
94 |
|
|
95 |
%params = $_->finalize(%params) for $self->plugins; |
|
96 |
|
|
97 |
$self->finalized(1); |
|
98 |
$self->final_params(\%params); |
|
99 |
|
|
100 |
return %params; |
|
37 | 101 |
} |
38 | 102 |
|
39 | 103 |
sub register_handlers { |
40 | 104 |
my ($self, %additional_handlers) = @_; |
41 | 105 |
|
42 |
# my $only = delete($additional_handlers{ONLY}) || []; |
|
43 |
# $only = [ $only ] if !ref $only; |
|
44 |
# my %hook_params = @{ $only } ? ( only => $only ) : (); |
|
45 |
|
|
46 | 106 |
my $handlers = $self->handlers; |
47 | 107 |
map { push @{ $handlers->{$_} }, $additional_handlers{$_} if $additional_handlers{$_} } keys %$handlers; |
48 | 108 |
} |
49 | 109 |
|
110 |
# TODO fix this |
|
50 | 111 |
sub get_models_url_params { |
51 | 112 |
my ($class, $sub_name_or_code) = @_; |
52 | 113 |
|
... | ... | |
71 | 132 |
return $self->controller->url_for(%default_params, %override_params); |
72 | 133 |
} |
73 | 134 |
|
74 |
sub get { |
|
75 |
my ($self, %params) = @_; |
|
76 |
|
|
77 |
push @{ $params{query} ||= [] }, @{ $self->query || [] }; |
|
78 |
push @{ $params{with_objects} ||= [] }, @{ $self->with_objects || [] }; |
|
79 |
|
|
80 |
%params = $self->_run_handlers('get_models', %params); |
|
81 |
|
|
82 |
return $self->manager->get_all(%params); |
|
83 |
} |
|
84 |
|
|
85 |
sub get_paginate_args { |
|
86 |
my ($self, %params) = @_; |
|
87 |
|
|
88 |
push @{ $params{query} ||= [] }, @{ $self->query || [] }; |
|
89 |
push @{ $params{with_objects} ||= [] }, @{ $self->with_objects || [] }; |
|
90 |
|
|
91 |
$self->paginated->get_current_paginate_params(%params); |
|
92 |
} |
|
93 |
|
|
94 | 135 |
sub manager { |
95 | 136 |
die "No 'model' to work on" unless $_[0]->model; |
96 | 137 |
"SL::DB::Manager::" . $_[0]->model; |
... | ... | |
123 | 164 |
} |
124 | 165 |
} |
125 | 166 |
|
167 |
sub init_source { |
|
168 |
$::form |
|
169 |
} |
|
170 |
|
|
126 | 171 |
1; |
127 | 172 |
__END__ |
128 | 173 |
|
Auch abrufbar als: Unified diff
weitere umstrukturierung