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 {
 
  22   if ($::form->{report_generator_pdf_options_set}) {
 
  23     my $saved_form = save_form();
 
  25     $self->report_generator_do('PDF');
 
  27     if ($::form->{report_generator_printed}) {
 
  28       restore_form($saved_form);
 
  29       $::form->{MESSAGE} = $::locale->text('The list has been printed.');
 
  30       $self->report_generator_do('HTML');
 
  36   my @form_values = $::form->flatten_variables(grep { ($_ ne 'login') && ($_ ne 'password') } keys %{ $::form });
 
  38   $::form->get_lists('printers' => 'ALL_PRINTERS');
 
  39   map { $_->{selected} = $::myconfig{default_printer_id} == $_->{id} } @{ $::form->{ALL_PRINTERS} };
 
  41   $::form->{copies} = max $::myconfig{copies} * 1, 1;
 
  42   $::form->{title} = $::locale->text('PDF export -- options');
 
  44   print $::form->parse_html_template('report_generator/pdf_export_options', {
 
  45     'HIDDEN'               => \@form_values,
 
  46     'ALLOW_FONT_SELECTION' => SL::ReportGenerator->check_for_pdf_api, });
 
  49 sub action_report_generator_export_as_csv {
 
  51   if ($::form->{report_generator_csv_options_set}) {
 
  52     $self->report_generator_do('CSV');
 
  56   my @form_values = $::form->flatten_variables(grep { ($_ ne 'login') && ($_ ne 'password') } keys %{ $::form });
 
  58   $::form->{title} = $::locale->text('CSV export -- options');
 
  60   print $::form->parse_html_template('report_generator/csv_export_options', { 'HIDDEN' => \@form_values });
 
  63 sub action_report_generator_back {
 
  64   $_[0]->report_generator_do('HTML');
 
  67 sub report_generator_do {
 
  68   my ($self, $format)  = @_;
 
  70   my $nextsub = $::form->{report_generator_nextsub};
 
  72     $::form->error($::locale->text('report_generator_nextsub is not defined.'));
 
  75   foreach my $key (split m/ +/, $::form->{report_generator_variable_list}) {
 
  76     $::form->{$key} = $::form->{"report_generator_hidden_${key}"};
 
  79   $::form->{report_generator_output_format} = $format;
 
  81   delete @{$::form}{map { "report_generator_$_" } qw(nextsub variable_list)};
 
  83   $self->_run_action($nextsub);
 
  86 sub report_generator_list_objects {
 
  87   my ($self, %params) = @_;
 
  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');
 
  93   my $column_defs = $params{report}->{columns};
 
  94   my @columns     = $params{report}->get_visible_columns;
 
  96   for my $obj (@{ $params{objects} || [] }) {
 
  98       my $def = $column_defs->{$_};
 
 100         raw_data => $def->{raw_data} ? $def->{raw_data}->($obj) : '',
 
 101         data     => $def->{sub}      ? $def->{sub}->($obj)
 
 102                   : $obj->can($_)    ? $obj->$_
 
 104         link     => $def->{obj_link} ? $def->{obj_link}->($obj) : '',
 
 108     $params{data_callback}->(\%data) if $params{data_callback};
 
 110     $params{report}->add_data(\%data);
 
 113   return $params{report}->generate_with_headers(%{ $params{options} || {}});
 
 125 SL::Controller::Helper::ReportGenerator - Mixin for controllers that
 
 126 use the L<SL::ReportGenerator> class
 
 130   package SL::Controller::Unicorn;
 
 132   use SL::Controller::Helper::ReportGenerator;
 
 137     # Set up the report generator instance. In this example this is
 
 138     # hidden in "prepare_report".
 
 139     my $report = $self->prepare_report;
 
 141     # Get objects from database.
 
 142     my $orders = SL::DB::Manager::Order->get_all(...);
 
 144     # Let report generator create the output.
 
 145     $self->report_generator_list_objects(
 
 155 =item C<action_report_generator_back>
 
 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'
 
 161 It is never called from a controller manually and should just work
 
 164 =item C<action_report_generator_export_as_csv>
 
 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
 
 171 It is never called from a controller manually and should just work
 
 174 =item C<action_report_generator_export_as_pdf>
 
 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
 
 181 It is never called from a controller manually and should just work
 
 184 =item C<report_generator_do>
 
 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.
 
 192 It is never called from a controller manually and should just work
 
 195 =item C<report_generator_list_objects %params>
 
 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
 
 201 C<%params> can contain the following values:
 
 207 Mandatory. An instance of L<SL::ReportGenerator> that has been set up
 
 208 already (column definitions, title, sort handling etc).
 
 212 Mandatory. An array reference of RDBO models to output.
 
 214 =item C<data_callback>
 
 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>.
 
 224 An optional hash reference that's passed verbatim to the function
 
 225 L<SL::ReportGenerator/generate_with_headers>.
 
 237 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>