SL::Controller::Base wrapper für ReportGenerator
authorSven Schöling <s.schoeling@linet-services.de>
Wed, 14 Dec 2011 15:19:15 +0000 (16:19 +0100)
committerSven Schöling <s.schoeling@linet-services.de>
Wed, 14 Dec 2011 15:29:45 +0000 (16:29 +0100)
SL/Controller/Helper/ReportGenerator.pm [new file with mode: 0644]
SL/ReportGenerator.pm
templates/webpages/report_generator/csv_export_options.html
templates/webpages/report_generator/html_report.html
templates/webpages/report_generator/pdf_export_options.html

diff --git a/SL/Controller/Helper/ReportGenerator.pm b/SL/Controller/Helper/ReportGenerator.pm
new file mode 100644 (file)
index 0000000..3455375
--- /dev/null
@@ -0,0 +1,101 @@
+#=====================================================================
+# 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
+#
+######################################################################
+
+use strict;
+
+use List::Util qw(max);
+
+use SL::Form;
+use SL::Common;
+use SL::MoreCommon;
+use SL::ReportGenerator;
+
+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
+);
+
+sub action_report_generator_export_as_pdf {
+  my ($self) = @_;
+  if ($::form->{report_generator_pdf_options_set}) {
+    my $saved_form = save_form();
+
+    $self->report_generator_do('PDF');
+
+    if ($::form->{report_generator_printed}) {
+      restore_form($saved_form);
+      $::form->{MESSAGE} = $::locale->text('The list has been printed.');
+      $self->report_generator_do('HTML');
+    }
+
+    return;
+  }
+
+  my @form_values = $::form->flatten_variables(grep { ($_ ne 'login') && ($_ ne 'password') } keys %{ $::form });
+
+  $::form->get_lists('printers' => 'ALL_PRINTERS');
+  map { $_->{selected} = $::myconfig{default_printer_id} == $_->{id} } @{ $::form->{ALL_PRINTERS} };
+
+  $::form->{copies} = max $::myconfig{copies} * 1, 1;
+  $::form->{title} = $::locale->text('PDF export -- options');
+  $::form->header;
+  print $::form->parse_html_template('report_generator/pdf_export_options', {
+    'HIDDEN'               => \@form_values,
+    'ALLOW_FONT_SELECTION' => SL::ReportGenerator->check_for_pdf_api, });
+}
+
+sub action_report_generator_export_as_csv {
+  my ($self) = @_;
+  if ($::form->{report_generator_csv_options_set}) {
+    $self->report_generator_do('CSV');
+    return;
+  }
+
+  my @form_values = $::form->flatten_variables(grep { ($_ ne 'login') && ($_ ne 'password') } keys %{ $::form });
+
+  $::form->{title} = $::locale->text('CSV export -- options');
+  $::form->header;
+  print $::form->parse_html_template('report_generator/csv_export_options', { 'HIDDEN' => \@form_values });
+}
+
+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)  = @_;
+
+  my $nextsub = $::form->{report_generator_nextsub};
+  if (!$nextsub) {
+    $::form->error($::locale->text('report_generator_nextsub is not defined.'));
+  }
+
+  foreach my $key (split m/ +/, $::form->{report_generator_variable_list}) {
+    $::form->{$key} = $::form->{"report_generator_hidden_${key}"};
+  }
+
+  $::form->{report_generator_output_format} = $format;
+
+  delete @{$::form}{map { "report_generator_$_" } qw(nextsub variable_list)};
+
+  $self->_run_action($nextsub);
+}
+
+1;
index 6b99901..f81435e 100644 (file)
@@ -23,6 +23,7 @@ sub new {
   $self->{options}  = {
     'std_column_visibility' => 0,
     'output_format'         => 'HTML',
+    'controller_class   '   => '',
     'allow_pdf_export'      => 1,
     'allow_csv_export'      => 1,
     'html_template'         => 'report_generator/html_report',
@@ -389,6 +390,7 @@ sub prepare_html_content {
     'EXPORT_VARIABLE_LIST' => join(' ', @{ $self->{export}->{variable_list} }),
     'EXPORT_NEXTSUB'       => $self->{export}->{nextsub},
     'DATA_PRESENT'         => $self->{data_present},
+    'CONTROLLER_DISPATCH'  => $opts->{controller_class},
   };
 
   return $variables;
@@ -768,6 +770,10 @@ sub _generate_csv_content {
   }
 }
 
+sub check_for_pdf_api {
+  return eval { require PDF::API2; 1; } ? 1 : 0;
+}
+
 1;
 
 __END__
@@ -921,6 +927,12 @@ Used to determine if a button for CSV export should be displayed. Default is yes
 
 The template to be used for HTML reports. Default is 'report_generator/html_report'.
 
+=item controller_class
+
+If this is used from a C<SL::Controller::Base> based controller class, pass the
+class name here and make sure C<SL::Controller::Helper::ReportGenerator> is
+used in the controller. That way the exports stay functional.
+
 =back
 
 =head2 PDF Options
index 0af5560..996adfc 100644 (file)
@@ -1,16 +1,6 @@
 [%- USE T8 %]
 [% USE HTML %]<body>
 
- <script type="text/javascript">
-  <!--
-      function submit_report_generator_form(nextsub) {
-        document.report_generator_form.report_generator_dispatch_to.value = nextsub;
-        document.report_generator_form.submit();
-      }
-
-    -->
- </script>
-
  <div class="listheading" width="100%">[% HTML.escape(title) %]</div>
 
  <form action="[% HTML.escape(script) %]" method="post" name="report_generator_form">
@@ -21,7 +11,6 @@
 
   <input type="hidden" name="report_generator_csv_options_set" value="1">
   <input type="hidden" name="report_generator_dispatch_to" value="">
-  <input type="hidden" name="action" value="report_generator_dispatcher">
 
   <table>
    <tr>
 
   </table>
 
+[%- IF CONTROLLER_DISPATCH %]
+   <p>
+    <input type="hidden" name="action" value="[% CONTROLLER_DISPATCH | html %]/dispatch">
+    <input type="submit" name="action_report_generator_export_as_csv" value="[% 'Export as CSV' | $T8 %]">
+    <input type="submit" name="action_report_generator_back" value="[% 'Back' | $T8 %]">
+    <input type="hidden" name="CONTROLLER_DISPATCH" value="[% CONTROLLER_DISPATCH | html %]">
+   </p>
+[%- ELSE %]
   <p>
+   <input type="hidden" name="action" value="report_generator_dispatcher">
    <input type="submit" class="submit" onclick="submit_report_generator_form('report_generator_export_as_csv')" value="[% 'Export as CSV' | $T8 %]">
    <input type="submit" class="submit" onclick="submit_report_generator_form('report_generator_back')" value="[% 'Back' | $T8 %]">
   </p>
+ <script type="text/javascript"><!--
+      function submit_report_generator_form(nextsub) {
+        document.report_generator_form.report_generator_dispatch_to.value = nextsub;
+        document.report_generator_form.submit();
+      } // -->
+ </script>
+[%- END %]
+
 
  </form>
 </body>
index 32c2bd5..8b2d253 100644 (file)
@@ -1,16 +1,6 @@
 [%- USE T8 %]
 [% USE HTML %]<body>
 
- <script type="text/javascript">
-  <!--
-      function submit_report_generator_form(nextsub) {
-        document.report_generator_form.report_generator_dispatch_to.value = nextsub;
-        document.report_generator_form.submit();
-      }
-
-    -->
- </script>
-
  <style type="text/css">
   <!--
 .top_border {
    [% FOREACH var = EXPORT_VARIABLES %]<input type="hidden" name="report_generator_hidden_[% var.key %]" value="[% HTML.escape(var.value) %]">
    [% END %]
 
+[%- IF CONTROLLER_DISPATCH %]
+   <input type="hidden" name="action" value="[% CONTROLLER_DISPATCH %]/dispatch">
+   <input type="hidden" name="report_generator_nextsub" value="[% HTML.escape(EXPORT_NEXTSUB) %]">
+   <input type="hidden" name="report_generator_variable_list" value="[% HTML.escape(EXPORT_VARIABLE_LIST) %]">
+   <input type="hidden" name="CONTROLLER_DISPATCH" value="[% CONTROLLER_DISPATCH | html %]">
+
+   <p>
+    [% 'List export' | $T8 %]<br>
+    [% IF ALLOW_PDF_EXPORT %]<input type="submit" name="action_report_generator_export_as_pdf" value="[% 'Export as PDF' | $T8 %]">[% END %]
+    [% IF ALLOW_CSV_EXPORT %]<input type="submit" name="action_report_generator_export_as_csv" value="[% 'Export as CSV' | $T8 %]">[% END %]
+   </p>
+[%- ELSE %]
    <input type="hidden" name="report_generator_nextsub" value="[% HTML.escape(EXPORT_NEXTSUB) %]">
    <input type="hidden" name="report_generator_variable_list" value="[% HTML.escape(EXPORT_VARIABLE_LIST) %]">
    <input type="hidden" name="report_generator_dispatch_to" value="">
     [% IF ALLOW_PDF_EXPORT %]<input type="submit" class="submit" onclick="submit_report_generator_form('report_generator_export_as_pdf')" value="[% 'Export as PDF' | $T8 %]">[% END %]
     [% IF ALLOW_CSV_EXPORT %]<input type="submit" class="submit" onclick="submit_report_generator_form('report_generator_export_as_csv')" value="[% 'Export as CSV' | $T8 %]">[% END %]
    </p>
+ <script type="text/javascript"> <!--
+      function submit_report_generator_form(nextsub) {
+        document.report_generator_form.report_generator_dispatch_to.value = nextsub;
+        document.report_generator_form.submit();
+      } // -->
+ </script>
+[%- END %]
+
   </form>
  [% END %]
 
index 41eb6c0..5af4de8 100644 (file)
@@ -3,16 +3,6 @@
 
  [%- SET default_margin = LxERP.format_amount(1.5) %]
 
- <script type="text/javascript">
-  <!--
-      function submit_report_generator_form(nextsub) {
-        document.report_generator_form.report_generator_dispatch_to.value = nextsub;
-        document.report_generator_form.submit();
-      }
-
-    -->
- </script>
-
  <div class="listheading" width="100%">[% HTML.escape(title) %]</div>
 
  <form action="[% HTML.escape(script) %]" method="post" name="report_generator_form">
@@ -23,7 +13,6 @@
 
   <input type="hidden" name="report_generator_pdf_options_set" value="1">
   <input type="hidden" name="report_generator_dispatch_to" value="">
-  <input type="hidden" name="action" value="report_generator_dispatcher">
 
   <table>
    <tr>
    [% END %]
   </table>
 
+[%- IF CONTROLLER_DISPATCH %]
+   <p>
+    <input type="hidden" name="action" value="[% CONTROLLER_DISPATCH | html %]/dispatch">
+    <input type="submit" name="action_report_generator_export_as_pdf" value="[% 'Export as PDF' | $T8 %]">
+    <input type="submit" name="action_report_generator_back" value="[% 'Back' | $T8 %]">
+    <input type="hidden" name="CONTROLLER_DISPATCH" value="[% CONTROLLER_DISPATCH | html %]">
+   </p>
+[%- ELSE %]
   <p>
+   <input type="hidden" name="action" value="report_generator_dispatcher">
    <input type="submit" class="submit" onclick="submit_report_generator_form('report_generator_export_as_pdf')" value="[% 'Export as PDF' | $T8 %]">
    <input type="submit" class="submit" onclick="submit_report_generator_form('report_generator_back')" value="[% 'Back' | $T8 %]">
   </p>
+ <script type="text/javascript"><!--
+      function submit_report_generator_form(nextsub) {
+        document.report_generator_form.report_generator_dispatch_to.value = nextsub;
+        document.report_generator_form.submit();
+      } // -->
+ </script>
+[%- END %]
 
  </form>
 </body>