Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 3d1ae2d9

Von Moritz Bunkus vor fast 12 Jahren hinzugefügt

  • ID 3d1ae2d96b65e88a5b12dde2b2524dc10f7932fa
  • Vorgänger f46bab53
  • Nachfolger 23a596b9

Refactoring: list_objects() aus Controllern in ReportGenerator-Helfer verschieben

Unterschiede anzeigen:

SL/Controller/DeliveryPlan.pm
51 51

  
52 52
  $self->prepare_report;
53 53

  
54
  $self->{orderitems} = $self->get_models(%{ $self->db_args });
54
  my $orderitems = $self->get_models(%{ $self->db_args });
55 55

  
56
  $self->list_objects;
56
  $self->report_generator_list_objects(report => $self->{report}, objects => $orderitems);
57 57
}
58 58

  
59 59
# private functions
......
190 190
  $self->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
191 191

  
192 192
  $self->disable_pagination if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
193

  
194
  $self->{report_data} = {
195
    column_defs        => \%column_defs,
196
    columns            => \@columns,
197
  };
198
}
199

  
200
sub list_objects {
201
  my ($self) = @_;
202
  my $column_defs = $self->{report_data}{column_defs};
203
  for my $obj (@{ $self->{orderitems} || [] }) {
204
    $self->{report}->add_data({
205
      map {
206
        $_ => {
207
          data => $column_defs->{$_}{sub} ? $column_defs->{$_}{sub}->($obj)
208
                : $obj->can($_)           ? $obj->$_
209
                :                           $obj->{$_},
210
          link => $column_defs->{$_}{obj_link} ? $column_defs->{$_}{obj_link}->($obj) : '',
211
        },
212
      } @{ $self->{report_data}{columns} || {} }
213
    });
214
  }
215

  
216
  return $self->{report}->generate_with_headers;
217 193
}
218 194

  
219 195
sub make_filter_summary {
SL/Controller/Helper/ReportGenerator.pm
1
#=====================================================================
2
# LX-Office ERP
3
# Copyright (C) 2004
4
# Based on SQL-Ledger Version 2.1.9
5
# Web http://www.lx-office.org
6
######################################################################
7
#
8
# Mixin for controllers to use ReportGenerator things
9
#
10
######################################################################
1
package SL::Controller::Helper::ReportGenerator;
11 2

  
12 3
use strict;
13 4

  
5
use Carp;
14 6
use List::Util qw(max);
15 7

  
16 8
use SL::Form;
......
22 14
our @EXPORT = qw(
23 15
  action_report_generator_export_as_pdf action_report_generator_export_as_csv
24 16
  action_report_generator_back report_generator_do
17
  report_generator_list_objects
25 18
);
26 19

  
27 20
sub action_report_generator_export_as_pdf {
......
71 64
  $_[0]->report_generator_do('HTML');
72 65
}
73 66

  
74
sub report_generator_set_default_sort {
75
  my ($default_sortorder, $default_sortdir) = @_;
76

  
77
  $::form->{sort}         ||= $default_sortorder;
78
  $::form->{sortdir}        = $default_sortdir unless (defined $::form->{sortdir});
79
  $::form->{sortdir}        = $::form->{sortdir} ? 1 : 0;
80
}
81

  
82 67
sub report_generator_do {
83 68
  my ($self, $format)  = @_;
84 69

  
......
98 83
  $self->_run_action($nextsub);
99 84
}
100 85

  
86
sub report_generator_list_objects {
87
  my ($self, %params) = @_;
88

  
89
  croak "Parameter 'objects' must exist and be an array reference"                if                      ref($params{objects}) ne 'ARRAY';
90
  croak "Parameter 'report' must exist and be an instance of SL::ReportGenerator" if                      ref($params{report})  ne 'SL::ReportGenerator';
91
  croak "Parameter 'options', if exists, must be a hash reference"                if $params{options} && (ref($params{options}) ne 'HASH');
92

  
93
  my $column_defs = $params{report}->{columns};
94
  my @columns     = $params{report}->get_visible_columns;
95

  
96
  for my $obj (@{ $params{objects} || [] }) {
97
    my %data = map {
98
      my $def = $column_defs->{$_};
99
      $_ => {
100
        raw_data => $def->{raw_data} ? $def->{raw_data}->($obj) : '',
101
        data     => $def->{sub}      ? $def->{sub}->($obj)
102
                  : $obj->can($_)    ? $obj->$_
103
                  :                    $obj->{$_},
104
        link     => $def->{obj_link} ? $def->{obj_link}->($obj) : '',
105
      },
106
    } @columns;
107

  
108
    $params{data_callback}->(\%data) if $params{data_callback};
109

  
110
    $params{report}->add_data(\%data);
111
  }
112

  
113
  return $params{report}->generate_with_headers(%{ $params{options} || {}});
114
}
115

  
101 116
1;
117
__END__
118

  
119
=pod
120

  
121
=encoding utf8
122

  
123
=head1 NAME
124

  
125
SL::Controller::Helper::ReportGenerator - Mixin for controllers that
126
use the L<SL::ReportGenerator> class
127

  
128
=head1 SYNOPSIS
129

  
130
  package SL::Controller::Unicorn;
131

  
132
  use SL::Controller::Helper::ReportGenerator;
133

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

  
137
    # Set up the report generator instance. In this example this is
138
    # hidden in "prepare_report".
139
    my $report = $self->prepare_report;
140

  
141
    # Get objects from database.
142
    my $orders = SL::DB::Manager::Order->get_all(...);
143

  
144
    # Let report generator create the output.
145
    $self->report_generator_list_objects(
146
      report  => $report,
147
      objects => $orders,
148
    );
149
  }
150

  
151
=head1 FUNCTIONS
152

  
153
=over 4
154

  
155
=item C<action_report_generator_back>
156

  
157
This is the controller action that's called from the one of the report
158
generator's 'export options' pages when the user clicks on the 'back'
159
button.
160

  
161
It is never called from a controller manually and should just work
162
as-is.
163

  
164
=item C<action_report_generator_export_as_csv>
165

  
166
This is the controller action that's called from the generated report
167
when the user wants to export as CSV. First the CSV export options are
168
shown and afterwards the CSV file is generated and offered for
169
download.
170

  
171
It is never called from a controller manually and should just work
172
as-is.
173

  
174
=item C<action_report_generator_export_as_pdf>
175

  
176
This is the controller action that's called from the generated report
177
when the user wants to export as PDF. First the PDF export options are
178
shown and afterwards the PDF file is generated and offered for
179
download.
180

  
181
It is never called from a controller manually and should just work
182
as-is.
183

  
184
=item C<report_generator_do>
185

  
186
This is a common function that's called from
187
L<action_report_generator_back>,
188
L<action_report_generator_export_as_csv> and
189
L<action_report_generator_export_as_pdf>. It handles common options
190
and report generation after options have been set.
191

  
192
It is never called from a controller manually and should just work
193
as-is.
194

  
195
=item C<report_generator_list_objects %params>
196

  
197
Iterates over all objects, creates the actual rows of data, hands them
198
over to the report generator and lets the report generator create the
199
output.
200

  
201
C<%params> can contain the following values:
202

  
203
=over 2
204

  
205
=item C<report>
206

  
207
Mandatory. An instance of L<SL::ReportGenerator> that has been set up
208
already (column definitions, title, sort handling etc).
209

  
210
=item C<objects>
211

  
212
Mandatory. An array reference of RDBO models to output.
213

  
214
=item C<data_callback>
215

  
216
Optional. A callback handler (code reference) that gets called for
217
each row before it is passed to the report generator. The row passed
218
will be the handler's first and only argument (a hash reference). It's
219
the same hash reference that's passed to
220
L<SL::ReportGenrator/add_data>.
221

  
222
=item C<options>
223

  
224
An optional hash reference that's passed verbatim to the function
225
L<SL::ReportGenerator/generate_with_headers>.
226

  
227
=back
228

  
229
=back
230

  
231
=head1 BUGS
232

  
233
Nothing here yet.
234

  
235
=head1 AUTHOR
236

  
237
Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
238

  
239
=cut
SL/Controller/Project.pm
77 77

  
78 78
  $self->prepare_report;
79 79

  
80
  $self->{projects} = $self->get_models(%{ $self->db_args });
80
  my $projects = $self->get_models(%{ $self->db_args });
81 81

  
82
  $self->list_objects;
82
  $self->report_generator_list_objects(report => $self->{report}, objects => $projects);
83 83
}
84 84

  
85 85
sub action_new {
......
274 274
  $self->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
275 275

  
276 276
  $self->disable_pagination if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
277

  
278
  $self->{report_data} = {
279
    column_defs        => \%column_defs,
280
    columns            => \@columns,
281
  };
282
}
283

  
284
sub list_objects {
285
  my ($self)      = @_;
286
  my $column_defs = $self->{report_data}->{column_defs};
287

  
288
  for my $obj (@{ $self->{projects} || [] }) {
289
    my %data = map {
290
      $_ => {
291
        data => $column_defs->{$_}{sub} ? $column_defs->{$_}{sub}->($obj)
292
              : $obj->can($_)           ? $obj->$_
293
              :                           $obj->{$_},
294
        link => $column_defs->{$_}{obj_link} ? $column_defs->{$_}{obj_link}->($obj) : '',
295
      },
296
    } @{ $self->{report_data}{columns} || {} };
297

  
298
    $self->{report}->add_data(\%data);
299
  }
300

  
301
  return $self->{report}->generate_with_headers;
302 277
}
303 278

  
304 279
1;
SL/Controller/SellPriceInformation.pm
32 32
    db_args => $db_args,
33 33
  );
34 34

  
35
  $self->{orderitems} = SL::DB::Manager::OrderItem->get_all(%$db_args);
35
  my $orderitems = SL::DB::Manager::OrderItem->get_all(%$db_args);
36 36

  
37
  $self->list_objects;
37
  $self->report_generator_list_objects(report => $self->{report}, objects => $orderitems, options => { no_layout => 1 });
38 38
}
39 39

  
40 40
# private functions
......
124 124
    title                => $::locale->text('Sales Price information'),
125 125
  );
126 126
  $report->set_options_from_form;
127

  
128
  $self->{report_data} = {
129
    column_defs => $column_defs,
130
    columns     => \@columns,
131
    visible     => \@visible,
132
    sortable    => \@sortable,
133
  };
134
}
135

  
136
sub list_objects {
137
  my ($self) = @_;
138
  my $column_defs = $self->{report_data}{column_defs};
139
  for my $obj (@{ $self->{orderitems} || [] }) {
140
    $self->{report}->add_data({
141
      map {
142
        $_ => {
143
          data => $column_defs->{$_}{sub} ? $column_defs->{$_}{sub}->($obj)
144
                : $obj->can($_)           ? $obj->$_
145
                :                           $obj->{$_},
146
          link => $column_defs->{$_}{obj_link} ? $column_defs->{$_}{obj_link}->($obj) : '',
147
        },
148
      } @{ $self->{report_data}{columns} || {} }
149
    });
150
  }
151

  
152
  return $self->{report}->generate_with_headers(no_layout => 1);
153 127
}
154 128

  
155 129
sub link_to {

Auch abrufbar als: Unified diff