1 package SL::Controller::Helper::ReportGenerator;
 
   6 use List::Util qw(max);
 
  11 use SL::ReportGenerator;
 
  13 use Exporter 'import';
 
  15   action_report_generator_export_as_pdf action_report_generator_export_as_csv
 
  16   action_report_generator_back report_generator_do
 
  17   report_generator_list_objects
 
  20 sub action_report_generator_export_as_pdf {
 
  23   delete $::form->{action_report_generator_export_as_pdf};
 
  25   if ($::form->{report_generator_pdf_options_set}) {
 
  26     my $saved_form = save_form();
 
  28     $self->report_generator_do('PDF');
 
  30     if ($::form->{report_generator_printed}) {
 
  31       restore_form($saved_form);
 
  32       $::form->{MESSAGE} = $::locale->text('The list has been printed.');
 
  33       $self->report_generator_do('HTML');
 
  39   my @form_values = $::form->flatten_variables(grep { ($_ ne 'login') && ($_ ne 'password') } keys %{ $::form });
 
  41   $::form->get_lists('printers' => 'ALL_PRINTERS');
 
  42   map { $_->{selected} = $::myconfig{default_printer_id} == $_->{id} } @{ $::form->{ALL_PRINTERS} };
 
  44   $::form->{copies} = max $::myconfig{copies} * 1, 1;
 
  45   $::form->{title} = $::locale->text('PDF export -- options');
 
  47   print $::form->parse_html_template('report_generator/pdf_export_options', {
 
  48     'HIDDEN'               => \@form_values,
 
  49     'ALLOW_FONT_SELECTION' => SL::ReportGenerator->check_for_pdf_api, });
 
  52 sub action_report_generator_export_as_csv {
 
  55   delete $::form->{action_report_generator_export_as_csv};
 
  57   if ($::form->{report_generator_csv_options_set}) {
 
  58     $self->report_generator_do('CSV');
 
  62   my @form_values = $::form->flatten_variables(grep { ($_ ne 'login') && ($_ ne 'password') } keys %{ $::form });
 
  64   $::form->{title} = $::locale->text('CSV export -- options');
 
  66   print $::form->parse_html_template('report_generator/csv_export_options', { 'HIDDEN' => \@form_values });
 
  69 sub action_report_generator_back {
 
  70   $_[0]->report_generator_do('HTML');
 
  73 sub report_generator_do {
 
  74   my ($self, $format)  = @_;
 
  76   my $nextsub = $::form->{report_generator_nextsub};
 
  78     $::form->error($::locale->text('report_generator_nextsub is not defined.'));
 
  81   foreach my $key (split m/ +/, $::form->{report_generator_variable_list}) {
 
  82     $::form->{$key} = $::form->{"report_generator_hidden_${key}"};
 
  85   $::form->{report_generator_output_format} = $format;
 
  87   delete @{$::form}{map { "report_generator_$_" } qw(nextsub variable_list)};
 
  89   $self->_run_action($nextsub);
 
  92 sub report_generator_list_objects {
 
  93   my ($self, %params) = @_;
 
  95   croak "Parameter 'objects' must exist and be an array reference"                if                      ref($params{objects}) ne 'ARRAY';
 
  96   croak "Parameter 'report' must exist and be an instance of SL::ReportGenerator" if                      ref($params{report})  ne 'SL::ReportGenerator';
 
  97   croak "Parameter 'options', if exists, must be a hash reference"                if $params{options} && (ref($params{options}) ne 'HASH');
 
  98   $params{layout} //= 1;
 
 100   my $column_defs = $params{report}->{columns};
 
 101   my @columns     = $params{report}->get_visible_columns('HTML');
 
 103   for my $obj (@{ $params{objects} || [] }) {
 
 105       my $def = $column_defs->{$_};
 
 107         raw_data => $def->{raw_data} ? $def->{raw_data}->($obj) : '',
 
 108         data     => $def->{sub}      ? $def->{sub}->($obj)
 
 109                   : $obj->can($_)    ? $obj->$_
 
 111         link     => $def->{obj_link} ? $def->{obj_link}->($obj) : '',
 
 115     $params{data_callback}->(\%data) if $params{data_callback};
 
 117     $params{report}->add_data(\%data);
 
 120   if ($params{layout}) {
 
 121     return $params{report}->generate_with_headers(%{ $params{options} || {}});
 
 123     my $html = $params{report}->generate_html_content(%{ $params{options} || {}});
 
 124     $self->render(\$html , { layout => 0, process => 0 });
 
 137 SL::Controller::Helper::ReportGenerator - Mixin for controllers that
 
 138 use the L<SL::ReportGenerator> class
 
 142   package SL::Controller::Unicorn;
 
 144   use SL::Controller::Helper::ReportGenerator;
 
 149     # Set up the report generator instance. In this example this is
 
 150     # hidden in "prepare_report".
 
 151     my $report = $self->prepare_report;
 
 153     # Get objects from database.
 
 154     my $orders = SL::DB::Manager::Order->get_all(...);
 
 156     # Let report generator create the output.
 
 157     $self->report_generator_list_objects(
 
 167 =item C<action_report_generator_back>
 
 169 This is the controller action that's called from the one of the report
 
 170 generator's 'export options' pages when the user clicks on the 'back'
 
 173 It is never called from a controller manually and should just work
 
 176 =item C<action_report_generator_export_as_csv>
 
 178 This is the controller action that's called from the generated report
 
 179 when the user wants to export as CSV. First the CSV export options are
 
 180 shown and afterwards the CSV file is generated and offered for
 
 183 It is never called from a controller manually and should just work
 
 186 =item C<action_report_generator_export_as_pdf>
 
 188 This is the controller action that's called from the generated report
 
 189 when the user wants to export as PDF. First the PDF export options are
 
 190 shown and afterwards the PDF file is generated and offered for
 
 193 It is never called from a controller manually and should just work
 
 196 =item C<report_generator_do>
 
 198 This is a common function that's called from
 
 199 L<action_report_generator_back>,
 
 200 L<action_report_generator_export_as_csv> and
 
 201 L<action_report_generator_export_as_pdf>. It handles common options
 
 202 and report generation after options have been set.
 
 204 It is never called from a controller manually and should just work
 
 207 =item C<report_generator_list_objects %params>
 
 209 Iterates over all objects, creates the actual rows of data, hands them
 
 210 over to the report generator and lets the report generator create the
 
 213 C<%params> can contain the following values:
 
 219 Mandatory. An instance of L<SL::ReportGenerator> that has been set up
 
 220 already (column definitions, title, sort handling etc).
 
 224 Mandatory. An array reference of RDBO models to output.
 
 226 =item C<data_callback>
 
 228 Optional. A callback handler (code reference) that gets called for
 
 229 each row before it is passed to the report generator. The row passed
 
 230 will be the handler's first and only argument (a hash reference). It's
 
 231 the same hash reference that's passed to
 
 232 L<SL::ReportGenrator/add_data>.
 
 236 An optional hash reference that's passed verbatim to the function
 
 237 L<SL::ReportGenerator/generate_with_headers>.
 
 249 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>