Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 0f491583

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

  • ID 0f4915834072e2f689ad80709b0801f46d786de9
  • Vorgänger ec3a4636
  • Nachfolger d5cb059b

weitere umstrukturierung

Unterschiede anzeigen:

SL/Controller/DeliveryPlan.pm
16 16

  
17 17
__PACKAGE__->run_before(sub { $::auth->assert('sales_order_edit'); });
18 18

  
19
#__PACKAGE__->make_filtered(
20
#  MODEL             => 'OrderItem',
21
#  LAUNDER_TO        => 'filter'
22
#);
23
#__PACKAGE__->make_paginated(
24
#  MODEL         => 'OrderItem',
25
#  ONLY          => [ qw(list) ],
26
#);
27
#
28
#__PACKAGE__->make_sorted(
29
#  MODEL             => 'OrderItem',
30
#  ONLY              => [ qw(list) ],
31
#
32
#  DEFAULT_BY        => 'reqdate',
33
#  DEFAULT_DIR       => 1,
34
#
35
#  reqdate           => t8('Reqdate'),
36
#  description       => t8('Description'),
37
#  partnumber        => t8('Part Number'),
38
#  qty               => t8('Qty'),
39
#  shipped_qty       => t8('shipped'),
40
#  not_shipped_qty   => t8('not shipped'),
41
#  ordnumber         => t8('Order'),
42
#  customer          => t8('Customer'),
43
#);
44

  
45 19
my %sort_columns = (
46 20
  reqdate           => t8('Reqdate'),
47 21
  description       => t8('Description'),
......
129 103
  my ($self) = @_;
130 104

  
131 105
  $self->make_filter_summary;
106
  $self->prepare_report;
132 107

  
133 108
  my $orderitems = $self->models->get;
134 109

  
135
  $self->prepare_report;
136 110
  $self->report_generator_list_objects(report => $self->{report}, objects => $orderitems);
137 111
}
138 112

  
......
164 138

  
165 139
  $column_defs{$_}->{text} = $sort_columns{$_} for keys %column_defs;
166 140

  
141
  $self->models->disable_plugin('paginated') if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
142
  $self->models->finalize; # for filter laundering
167 143
  $report->set_options(
168 144
    std_column_visibility => 1,
169 145
    controller_class      => 'DeliveryPlan',
......
179 155
  $report->set_column_order(@columns);
180 156
  $report->set_export_options(qw(list filter));
181 157
  $report->set_options_from_form;
182
  $self->models->sorted->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
183

  
184
  $self->models->paginated->disable_pagination if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
158
  $self->models->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
185 159
}
186 160

  
187 161
sub make_filter_summary {
......
223 197

  
224 198
  SL::Controller::Helper::GetModels->new(
225 199
    controller => $self,
226
    model  => 'OrderItem', # defaults to controller
200
    model  => 'OrderItem',
227 201
    filtered => {
228 202
      launder_to => 'filter',
229 203
    },
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

  
SL/Controller/Helper/GetModels/Base.pm
4 4
use parent 'Rose::Object';
5 5
use Scalar::Util qw(weaken);
6 6

  
7

  
8 7
use Rose::Object::MakeMethods::Generic (
9
  scalar => [ qw(get_models) ],
8
  scalar => [ qw(get_models disabled finalized) ],
10 9
);
11 10

  
11
# phase stubs
12
sub read_params { die 'implement me' }
13

  
14
sub finalize { die 'implement me' }
15

  
12 16
sub set_get_models {
13 17
  $_[0]->get_models($_[1]);
14 18

  
......
23 27
    $final_args->{$field} = [ map { @{ $_->{$field} || [] } } @args ];
24 28
  }
25 29

  
30
  for my $field (qw(page per_page sort_by sort_dir )) {
31
    for my $arg (@args) {
32
      next unless defined $_->{$field};
33
      $final_args->{$field} //= $_->{$field};
34
    }
35
  }
36

  
26 37
  return %$final_args;
27 38
}
28 39

  
40
sub is_enabled {
41
  my ($self) = @_;
42
  return !$self->disabled;
43
}
44

  
29 45
1;
SL/Controller/Helper/GetModels/Filtered.pm
8 8
use List::MoreUtils qw(uniq);
9 9

  
10 10
use Rose::Object::MakeMethods::Generic (
11
  scalar => [ qw(disabled filter_args filter_params) ],
11
  scalar => [ qw(filter_args filter_params orig_filter) ],
12 12
  'scalar --get_set_init' => [ qw(form_params launder_to) ],
13 13
);
14 14

  
......
26 26
  # $::lxdebug->dump(0, "CONSPEC", \%specs);
27 27
}
28 28

  
29
sub get_current_filter_params {
30
  my ($self)   = @_;
29
sub read_params {
30
  my ($self, %params)   = @_;
31 31

  
32
  return $self->filter_params if $self->filter_params;
32
  return %{ $self->filter_params } if $self->filter_params;
33
  my $source = $self->get_models->source;
33 34

  
34
  require Carp;
35
  Carp::confess('It seems a GetModels plugin tries to access filter params before they got calculated. Make sure your make_filtered call comes first.');
36
}
35
  my $filter            = $params{filter} // $source->{ $self->form_params } // {};
36
  $self->orig_filter($filter);
37 37

  
38
sub _make_current_filter_params {
39
  my ($self, %params)   = @_;
40

  
41
#  my $spec              = $self->get_filter_spec;
42
  my $filter            = $params{filter} // $::form->{ $self->form_params } // {},
43 38
  my %filter_args       = $self->_get_filter_args;
44 39
  my %parse_filter_args = (
45 40
    class        => $self->get_models->manager,
......
58 53
  my %calculated_params = SL::Controller::Helper::ParseFilter::parse_filter($filter, %parse_filter_args);
59 54
  %calculated_params = $self->merge_args(\%calculated_params, \%filter_args, \%params);
60 55

  
61
#  $calculated_params{query} = [
62
#    @{ $calculated_params{query} || [] },
63
#    @{ $filter_args{      query} || [] },
64
#    @{ $params{           query} || [] },
65
#  ];
66
#
67
#  $calculated_params{with_objects} = [
68
#    uniq
69
#    @{ $calculated_params{with_objects} || [] },
70
#    @{ $filter_args{      with_objects} || [] },
71
#    @{ $params{           with_objects} || [] },
72
#  ];
73

  
74 56
  if ($laundered) {
75 57
    if ($self->get_models->controller->can($self->launder_to)) {
76 58
      $self->get_models->controller->${\ $self->launder_to }($laundered);
......
86 68
  return %calculated_params;
87 69
}
88 70

  
89
sub disable_filtering {
90
  my ($self)               = @_;
91
  $self->disabled(1);
71
sub finalize {
72
  my ($self, %params) = @_;
73
  %params;
92 74
}
93 75

  
94 76
#
......
107 89
  my ($self, %params) = @_;
108 90

  
109 91
  if ($self->is_enabled) {
110
    my ($flattened) = SL::Controller::Helper::ParseFilter::flatten($::form->{ $self->form_params }, $self->form_params);
92
    my ($flattened) = SL::Controller::Helper::ParseFilter::flatten($self->orig_filter, $self->form_params);
111 93
    %params         = (%params, @{ $flattened || [] });
112 94
  }
113 95

  
......
122 104
  # $::lxdebug->dump(0,  "params in get_models_for_filtered", \%params);
123 105

  
124 106
  my %filter_params;
125
  %filter_params = $self->_make_current_filter_params(%params)  if $self->is_enabled;
107
  %filter_params = $self->read_params(%params)  if $self->is_enabled;
126 108

  
127 109
  # $::lxdebug->dump(0, "GM handler for filtered; params nach modif (is_enabled? " . $self->is_enabled . ")", \%params);
128 110

  
129
  return (%params, %filter_params);
130
}
131

  
132
sub is_enabled {
133
  !$_[0]->disabled;
111
  return $self->merge_args(\%params, \%filter_params);
134 112
}
135 113

  
136 114
sub init_form_params {
SL/Controller/Helper/GetModels/Paginated.pm
6 6
use List::Util qw(min);
7 7

  
8 8
use Rose::Object::MakeMethods::Generic (
9
  scalar => [ qw(disabled per_page) ],
9
  scalar => [ qw(per_page form_data paginated_args calculated_params) ],
10 10
  'scalar --get_set_init' => [ qw(form_params paginate_args) ],
11 11
);
12 12

  
......
26 26
  # $::lxdebug->dump(0, "CONSPEC", \%specs);
27 27
}
28 28

  
29
sub get_current_paginate_params {
30
  my ($self, %args)   = @_;
31
  return () unless $self->is_enabled;
29
sub read_params {
30
  my ($self, %params)      = @_;
31

  
32
  return %{ $self->form_data } if $self->form_data;
33
  my $source = $self->get_models->source;
34

  
35
  my $from_form = {
36
    page            => $source->{ $self->form_params->[0] } || 1,
37
    per_page        => $source->{ $self->form_params->[1] } * 1,
38
  };
32 39

  
33
  my %paginate_params = $self->final_params(%args);
40
#  my $priv              = _priv($self);
41
  $params{page}         = $from_form->{page}     unless defined $params{page};
42
  $params{per_page}     = $from_form->{per_page} unless defined $params{per_page};
43

  
44
  $params{page}         = ($params{page} * 1) || 1;
45
  $params{per_page}     = ($params{per_page} * 1) || $self->per_page;
46

  
47
  $self->form_data(\%params);
48

  
49
  %params;
50
}
51

  
52
sub finalize {
53
  my ($self, %args)   = @_;
54
#  return () unless $self->is_enabled;
55
  my %paginate_params = $self->read_params;
34 56

  
35 57
  # try to use Filtered if available and nothing else is configured, but don't
36 58
  # blow up if the controller does not use Filtered
37 59
  my %paginate_args     = ref($self->paginate_args) eq 'CODE'       ? %{ $self->paginate_args->($self) }
38 60
                        :     $self->paginate_args  eq '__FILTER__'
39
                           && $self->get_models->filtered ? %{ $self->get_models->filtered->get_current_filter_params }
61
                           && $self->get_models->filtered ? $self->get_models->filtered->read_params
40 62
                        :     $self->paginate_args  ne '__FILTER__' ? do { my $sub = $self->paginate_args; %{ $self->get_models->controller->$sub() } }
41 63
                        :                                               ();
42 64

  
......
44 66

  
45 67
  my $calculated_params = $self->get_models->manager->paginate(%paginate_params, args => \%args);
46 68

  
47
  # $::lxdebug->dump(0, "get_current_paginate_params: ", $calculated_params);
69
  $self->paginated_args(\%args);
70
  $self->calculated_params($calculated_params);
48 71

  
49
  return %{ $calculated_params };
72
  return %args;
50 73
}
51 74

  
52
sub disable_pagination {
53
  my ($self)               = @_;
54
  $self->disabled(1);
55
}
56

  
57
sub final_params {
58
  my ($self, %params)      = @_;
59

  
60
  my $from_form = {
61
    page            => $::form->{ $self->form_params->[0] } || 1,
62
    per_page        => $::form->{ $self->form_params->[1] } * 1,
63
  };
64

  
65
#  my $priv              = _priv($self);
66
  $params{page}         = $from_form->{page}     unless defined $params{page};
67
  $params{per_page}     = $from_form->{per_page} unless defined $params{per_page};
68

  
69
  $params{page}         = ($params{page} * 1) || 1;
70
  $params{per_page}     = ($params{per_page} * 1) || $self->per_page;
71

  
72
  %params;
75
sub get_current_paginate_params {
76
  my ($self, %args)   = @_;
77
  return () unless $self->is_enabled;
78
  %{ $self->calculated_params };
73 79
}
74 80

  
75 81
#
76 82
# private functions
77 83
#
78 84

  
79
sub init_form_params {
80
  [ qw(page per_page) ]
81
}
82

  
83
sub init_paginate_args {
84
  '__FILTER__'
85
}
86

  
87 85
sub _callback_handler_for_paginated {
88 86
  my ($self, %params) = @_;
89
  my %form_params = $self->final_params;
90
#  my $priv            = _priv($self);
87
  my %form_params = $self->read_params;
91 88

  
92 89
  if ($self->is_enabled && $form_params{page}) {
93 90
    $params{ $self->form_params->[0] } = $form_params{page};
......
102 99
sub _get_models_handler_for_paginated {
103 100
  my ($self, %params)    = @_;
104 101

  
105
  $self->get_models->manager->paginate($self->final_params, args => \%params) if $self->is_enabled;
102
  $self->get_models->manager->paginate(%{ $self->calculated_params }, args => \%params) if $self->is_enabled;
106 103

  
107 104
  # $::lxdebug->dump(0, "GM handler for paginated; params nach modif (is_enabled? " . _is_enabled($self) . ")", \%params);
108

  
109 105
  return %params;
110 106
}
111 107

  
112
sub is_enabled {
113
  my ($self) = @_;
114
  return !$self->disabled;
108
sub init_form_params {
109
  [ qw(page per_page) ]
110
}
111

  
112
sub init_paginate_args {
113
  '__FILTER__'
115 114
}
116 115

  
117 116
1;
SL/Controller/Helper/GetModels/Sorted.pm
7 7
use List::MoreUtils qw(uniq);
8 8

  
9 9
use Rose::Object::MakeMethods::Generic (
10
  scalar => [ qw(by dir specs) ],
10
  scalar => [ qw(by dir specs form_data) ],
11 11
  'scalar --get_set_init' => [ qw(form_params) ],
12 12
);
13 13

  
......
43 43
#   $::lxdebug->dump(0, "CONSPEC", \%specs);
44 44
}
45 45

  
46
sub get_current_sort_params {
46
sub read_params {
47 47
  my ($self, %params) = @_;
48 48

  
49
  return %{ $self->form_data } if $self->form_data;
50

  
49 51
  my %sort_params;
50 52
  my ($by, $dir) = @{ $self->form_params };
53
  my $source = $self->get_models->source;
51 54

  
52
  if ($::form->{ $by }) {
55
  if ($source->{ $by }) {
53 56
    %sort_params = (
54
      sort_by  => $::form->{$by},
55
      sort_dir => defined($::form->{$dir}) ? $::form->{$dir} * 1 : undef,
57
      sort_by  => $source->{$by},
58
      sort_dir => defined($source->{$dir}) ? $source->{$dir} * 1 : undef,
56 59
    );
57 60
  } elsif (!$self->by) {
58 61
    %sort_params = %params;
......
63 66
    );
64 67
  }
65 68

  
69
  $self->form_data(\%sort_params);
70

  
66 71
  return %sort_params;
67 72
}
68 73

  
74
sub finalize {
75
  my ($self, %params) = @_;
76
  %params;
77
}
78

  
69 79
sub set_report_generator_sort_options {
70 80
  my ($self, %params) = @_;
71 81

  
72 82
  $params{$_} or croak("Missing parameter '$_'") for qw(report sortable_columns);
73 83

  
74
  my %current_sort_params = $self->get_current_sort_params;
84
  my %current_sort_params = $self->read_params;
75 85

  
76 86
  foreach my $col (@{ $params{sortable_columns} }) {
77 87
    $params{report}->{columns}->{$col}->{link} = $self->get_models->get_callback(
......
96 106

  
97 107
sub _callback_handler_for_sorted {
98 108
  my ($self, %params) = @_;
99
  my %spec = $self->get_current_sort_params;
109
  my %spec = $self->read_params;
100 110

  
101 111
  if ($spec{sort_by}) {
102 112
    $params{ $self->form_params->[0] } = $spec{sort_by};
......
111 121
sub _get_models_handler_for_sorted {
112 122
  my ($self, %params) = @_;
113 123

  
114
  my %sort_params     = $self->get_current_sort_params;
124
  my %sort_params     = $self->read_params;
115 125
  my $sort_spec       = $self->specs->{ $sort_params{sort_by} };
116 126

  
117 127
  $params{sort_by}    = "SL::DB::Manager::$sort_spec->{model}"->make_sort_string(sort_by => $sort_spec->{model_column}, sort_dir => $sort_params{sort_dir});

Auch abrufbar als: Unified diff