X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FController%2FHelper%2FReportGenerator.pm;h=be9b8306de097a622294824a60b6cdbe8c4dcf1c;hb=332e327b6613bf762b34d841442378de255d946b;hp=34553752905a2666e83b0b9c038f8b69db9a2e2a;hpb=1320854c647c77be5befbc2c07332df92afdf4ea;p=kivitendo-erp.git diff --git a/SL/Controller/Helper/ReportGenerator.pm b/SL/Controller/Helper/ReportGenerator.pm index 345537529..be9b8306d 100644 --- a/SL/Controller/Helper/ReportGenerator.pm +++ b/SL/Controller/Helper/ReportGenerator.pm @@ -1,19 +1,11 @@ -#===================================================================== -# LX-Office ERP -# Copyright (C) 2004 -# Based on SQL-Ledger Version 2.1.9 -# Web http://www.lx-office.org -###################################################################### -# -# Mixin for controllers to use ReportGenerator things -# -###################################################################### +package SL::Controller::Helper::ReportGenerator; use strict; +use Carp; use List::Util qw(max); +use Scalar::Util qw(blessed); -use SL::Form; use SL::Common; use SL::MoreCommon; use SL::ReportGenerator; @@ -22,10 +14,34 @@ use Exporter 'import'; our @EXPORT = qw( action_report_generator_export_as_pdf action_report_generator_export_as_csv action_report_generator_back report_generator_do + report_generator_list_objects ); +sub _setup_action_bar { + my ($self, $type) = @_; + + my $key = $::form->{CONTROLLER_DISPATCH} ? 'action' : 'report_generator_form.report_generator_dispatch_to'; + my $value = $::form->{CONTROLLER_DISPATCH} ? $::form->{CONTROLLER_DISPATCH} . "/" : ''; + + for my $bar ($::request->layout->get('actionbar')) { + $bar->add( + action => [ + $type eq 'pdf' ? $::locale->text('PDF export') : $::locale->text('CSV export'), + submit => [ '#report_generator_form', { $key => "${value}report_generator_export_as_${type}" } ], + ], + action => [ + $::locale->text('Back'), + submit => [ '#report_generator_form', { $key => "${value}report_generator_back" } ], + ], + ); + } +} + sub action_report_generator_export_as_pdf { my ($self) = @_; + + delete $::form->{action_report_generator_export_as_pdf}; + if ($::form->{report_generator_pdf_options_set}) { my $saved_form = save_form(); @@ -47,6 +63,9 @@ sub action_report_generator_export_as_pdf { $::form->{copies} = max $::myconfig{copies} * 1, 1; $::form->{title} = $::locale->text('PDF export -- options'); + + _setup_action_bar($self, 'pdf'); # Sub not exported, therefore don't call via object. + $::form->header; print $::form->parse_html_template('report_generator/pdf_export_options', { 'HIDDEN' => \@form_values, @@ -55,6 +74,9 @@ sub action_report_generator_export_as_pdf { sub action_report_generator_export_as_csv { my ($self) = @_; + + delete $::form->{action_report_generator_export_as_csv}; + if ($::form->{report_generator_csv_options_set}) { $self->report_generator_do('CSV'); return; @@ -63,6 +85,9 @@ sub action_report_generator_export_as_csv { my @form_values = $::form->flatten_variables(grep { ($_ ne 'login') && ($_ ne 'password') } keys %{ $::form }); $::form->{title} = $::locale->text('CSV export -- options'); + + _setup_action_bar($self, 'csv'); # Sub not exported, therefore don't call via object. + $::form->header; print $::form->parse_html_template('report_generator/csv_export_options', { 'HIDDEN' => \@form_values }); } @@ -71,14 +96,6 @@ sub action_report_generator_back { $_[0]->report_generator_do('HTML'); } -sub report_generator_set_default_sort { - my ($default_sortorder, $default_sortdir) = @_; - - $::form->{sort} ||= $default_sortorder; - $::form->{sortdir} = $default_sortdir unless (defined $::form->{sortdir}); - $::form->{sortdir} = $::form->{sortdir} ? 1 : 0; -} - sub report_generator_do { my ($self, $format) = @_; @@ -98,4 +115,191 @@ sub report_generator_do { $self->_run_action($nextsub); } +sub report_generator_list_objects { + my ($self, %params) = @_; + + croak "Parameter 'objects' must exist and be an array reference" if ref($params{objects}) ne 'ARRAY'; + croak "Parameter 'report' must exist and be an instance of SL::ReportGenerator" if ref($params{report}) ne 'SL::ReportGenerator'; + croak "Parameter 'options', if exists, must be a hash reference" if $params{options} && (ref($params{options}) ne 'HASH'); + $params{layout} //= 1; + + my $column_defs = $params{report}->{columns}; + my @columns = $params{report}->get_visible_columns('HTML'); + + for my $obj (@{ $params{objects} || [] }) { + my %data; + + if (blessed($obj) && $obj->isa('SL::Controller::Helper::ReportGenerator::ControlRow::Base')) { + $obj->set_data($params{report}); + next; + + } else { + %data = map { + my $def = $column_defs->{$_}; + my $tmp; + $tmp->{raw_data} = $def->{raw_data} ? $def->{raw_data}->($obj) : ''; + $tmp->{data} = $def->{sub} ? $def->{sub}->($obj) + : $obj->can($_) ? $obj->$_ + : $obj->{$_}; + $tmp->{link} = $def->{obj_link} ? $def->{obj_link}->($obj) : ''; + $_ => $tmp; + } @columns; + } + + $params{data_callback}->(\%data) if $params{data_callback}; + + $params{report}->add_data(\%data); + } + + my %options = %{ $params{options} || {} }; + $options{action_bar} //= $params{action_bar} // 1; + + if ($params{layout}) { + return $params{report}->generate_with_headers(%options); + } else { + my $html = $params{report}->generate_html_content(action_bar => 0, %options); + $self->render(\$html , { layout => 0, process => 0 }); + } +} + 1; +__END__ + +=pod + +=encoding utf8 + +=head1 NAME + +SL::Controller::Helper::ReportGenerator - Mixin for controllers that +use the L class + +=head1 SYNOPSIS + + package SL::Controller::Unicorn; + + use SL::Controller::Helper::ReportGenerator; + + sub action_list { + my ($self) = @_; + + # Set up the report generator instance. In this example this is + # hidden in "prepare_report". + my $report = $self->prepare_report; + + # Get objects from database. + my $orders = SL::DB::Manager::Order->get_all(...); + + # Let report generator create the output. + $self->report_generator_list_objects( + report => $report, + objects => $orders, + ); + } + +=head1 FUNCTIONS + +=over 4 + +=item C + +This is the controller action that's called from the one of the report +generator's 'export options' pages when the user clicks on the 'back' +button. + +It is never called from a controller manually and should just work +as-is. + +=item C + +This is the controller action that's called from the generated report +when the user wants to export as CSV. First the CSV export options are +shown and afterwards the CSV file is generated and offered for +download. + +It is never called from a controller manually and should just work +as-is. + +=item C + +This is the controller action that's called from the generated report +when the user wants to export as PDF. First the PDF export options are +shown and afterwards the PDF file is generated and offered for +download. + +It is never called from a controller manually and should just work +as-is. + +=item C + +This is a common function that's called from +L, +L and +L. It handles common options +and report generation after options have been set. + +It is never called from a controller manually and should just work +as-is. + +=item C + +Iterates over all objects, creates the actual rows of data, hands them +over to the report generator and lets the report generator create the +output. + +C<%params> can contain the following values: + +=over 2 + +=item C + +Mandatory. An instance of L that has been set up +already (column definitions, title, sort handling etc). + +=item C + +Mandatory. An array reference of RDBO models to output. + +An element of the array can also be an instance of a control row, i.e. +an instance of a class derived from +C. +See also: + +L +L + +=item C + +Optional. A callback handler (code reference) that gets called for +each row before it is passed to the report generator. The row passed +will be the handler's first and only argument (a hash reference). It's +the same hash reference that's passed to +L. + +=item C + +An optional hash reference that's passed verbatim to the function +L. + +=item C + +If the buttons for exporting PDF and/or CSV variants are included in +the action bar. Otherwise they're rendered at the bottom of the page. + +The value can be either a specific action bar instance or simply 1 in +which case the default action bar is used: +C<$::request-Elayout-Eget('actionbar')>. + +=back + +=back + +=head1 BUGS + +Nothing here yet. + +=head1 AUTHOR + +Moritz Bunkus Em.bunkus@linet-services.deE + +=cut