]> wagnertech.de Git - mfinanz.git/blob - SL/Helper/PrintOptions.pm
restart apache2 in postinst
[mfinanz.git] / SL / Helper / PrintOptions.pm
1 package SL::Helper::PrintOptions;
2
3 use strict;
4
5 use List::MoreUtils qw(any);
6
7 sub opthash { +{ value => shift, selected => shift, oname => shift } }
8
9 # generate the printing options displayed at the bottom of oe and is forms.
10 # this function will attempt to guess what type of form is displayed, and will generate according options
11 #
12 # about the coding:
13 # this version builds the arrays of options pretty directly. if you have trouble understanding how,
14 # the opthash function builds hashrefs which are then pieced together for the template arrays.
15 # unneeded options are "undef"ed out, and then grepped out.
16 #
17 # the inline options is untested, but intended to be used later in metatemplating
18 sub get_print_options {
19   my ($class, %params) = @_;
20
21   no warnings 'once';
22
23   my $form     = $params{form}     || $::form;
24   my $myconfig = $params{myconfig} || \%::myconfig;
25   my $locale   = $params{locale}   || $::locale;
26   my $options  = $params{options};
27
28   use warnings 'once';
29
30   my $prefix = $options->{dialog_name_prefix} || '';
31
32   # names 3 parameters and returns a hashref, for use in templates
33   my (@FORMNAME, @LANGUAGE_ID, @FORMAT, @SENDMODE, @MEDIA, @PRINTER_ID, @SELECTS) = ();
34
35   # note: "||"-selection is only correct for values where "0" is _not_ a correct entry
36   $form->{sendmode}   = "attachment";
37   $form->{format}     = $form->{format} || $myconfig->{template_format} || "pdf";
38   $form->{copies}     = $form->{copies} || $myconfig->{copies}          || 3;
39   $form->{media}      = $form->{media}  || $myconfig->{default_media}   || "screen";
40   $form->{printer_id} = defined $form->{printer_id}           ? $form->{printer_id} :
41                         defined $myconfig->{default_printer_id} ? $myconfig->{default_printer_id} : "";
42
43   $form->{PD}{ $form->{formname} } = "selected";
44   $form->{DF}{ $form->{format} }   = "selected";
45   $form->{OP}{ $form->{media} }    = "selected";
46   $form->{SM}{ $form->{sendmode} } = "selected";
47
48   push @FORMNAME, grep $_,
49     ($form->{type} eq 'purchase_order') ? (
50       opthash("purchase_order",      $form->{PD}{purchase_order},      $locale->text('Purchase Order')),
51       opthash("bin_list",            $form->{PD}{bin_list},            $locale->text('Bin List'))
52     ) : undef,
53     ($form->{type} eq 'purchase_order_confirmation') ? (
54       opthash("purchase_order_confirmation", $form->{PD}{purchase_order}, $locale->text('Purchase Order Confirmation')),
55     ) : undef,
56     ($form->{type} eq 'credit_note') ?
57       opthash("credit_note",         $form->{PD}{credit_note},         $locale->text('Credit Note')) : undef,
58     ($form->{type} eq 'sales_order_intake') ? (
59       opthash("sales_order_intake",  $form->{PD}{sales_order_intake},  $locale->text('Sales Order Intake')),
60     ) : undef,
61     ($form->{type} eq 'sales_order') ? (
62       opthash("sales_order",         $form->{PD}{sales_order},         $locale->text('Confirmation')),
63       opthash("proforma",            $form->{PD}{proforma},            $locale->text('Proforma Invoice')),
64       opthash("ic_supply",           $form->{PD}{ic_supply},            $locale->text('Intra-Community supply')),
65     ) : undef,
66     ($form->{type} =~ /sales_quotation$/) ?
67       opthash('sales_quotation',     $form->{PD}{sales_quotation},     $locale->text('Quotation')) : undef,
68     ($form->{type} =~ /request_quotation$/) ?
69       opthash('request_quotation',   $form->{PD}{request_quotation},   $locale->text('Request for Quotation')) : undef,
70     ($form->{type} eq 'purchase_quotation_intake') ? (
71       opthash("purchase_quotation_intake", $form->{PD}{purchase_quotation_intake},  $locale->text('Purchase Quotation Intake')),
72     ) : undef,
73     ($form->{type} eq 'invoice' && $form->{storno} && $form->{storno_id}) ? (
74       opthash("storno_invoice",      $form->{PD}{storno_invoice},      $locale->text('Storno Invoice')),
75     ) : undef,
76     ($form->{type} eq 'invoice' && !($form->{storno} && $form->{storno_id})) ? (
77       opthash("invoice",             $form->{PD}{invoice},             $locale->text('Invoice')),
78       opthash("proforma",            $form->{PD}{proforma},            $locale->text('Proforma Invoice')),
79       opthash("invoice_copy",        $form->{PD}{invoice_copy},        $locale->text('Invoice Copy')),
80     ) : undef,
81     ($form->{type} eq 'invoice_for_advance_payment') ? (
82       opthash("invoice_for_advance_payment", $form->{PD}{invoice_for_advance_payment},      $locale->text('Invoice for Advance Payment')),
83     ) : undef,
84     ($form->{type} eq 'final_invoice') ? (
85       opthash("final_invoice", $form->{PD}{final_invoice},             $locale->text('Final Invoice')),
86     ) : undef,
87     ($form->{type} =~ /^supplier_delivery_order$/) ? (
88       opthash('supplier_delivery_order', $form->{PD}{supplier_delivery_order},  $locale->text('Supplier Delivery Order')),
89     ) : undef,
90     ($form->{type} =~ /^rma_delivery_order$/) ? (
91       opthash('rma_delivery_order', $form->{PD}{rma_delivery_order},  $locale->text('RMA Delivery Order')),
92     ) : undef,
93     ($form->{type} =~ /(sales|purchase)_delivery_order$/) ? (
94       opthash($form->{type},         $form->{PD}{$form->{type}},       $locale->text('Delivery Order')),
95       opthash('pick_list',           $form->{PD}{pick_list},           $locale->text('Pick List')),
96     ) : undef,
97     ($form->{type} =~ /_reclamation$/) ? (
98       opthash($form->{type},         $form->{PD}{$form->{type}},       $locale->text('Reclamation')),
99     ) : undef,
100     ($form->{type} =~ /^letter$/) ? (
101       opthash('letter',              $form->{PD}{letter},              $locale->text('Letter')),
102     ) : undef;
103
104   push @SENDMODE,
105     opthash("attachment",            $form->{SM}{attachment},          $locale->text('Attachment')),
106     opthash("inline",                $form->{SM}{inline},              $locale->text('In-line'))
107       if ($form->{media} eq 'email');
108
109   my $printable_templates = any { $::lx_office_conf{print_templates}->{$_} } qw(latex opendocument);
110   push @MEDIA, grep $_,
111       opthash("screen",              $form->{OP}{screen},              $locale->text('Screen')),
112     ($printable_templates && $form->{printers} && scalar @{ $form->{printers} }) ?
113       opthash("printer",             $form->{OP}{printer},             $locale->text('Printer')) : undef,
114     ($printable_templates && !$options->{no_queue}) ?
115       opthash("queue",               $form->{OP}{queue},               $locale->text('Queue')) : undef
116         if ($form->{media} ne 'email');
117
118   push @FORMAT, grep $_,
119     ($::lx_office_conf{print_templates}->{opendocument} &&     $::lx_office_conf{applications}->{openofficeorg_writer}
120                                                         && (-x $::lx_office_conf{applications}->{openofficeorg_writer})
121                                                         && !$options->{no_opendocument_pdf}) ?
122       opthash("opendocument_pdf",    $form->{DF}{"opendocument_pdf"},  $locale->text("PDF (OpenDocument/OASIS)")) : undef,
123     ($::lx_office_conf{print_templates}->{latex}) ?
124       opthash("pdf",                 $form->{DF}{pdf},                 $locale->text('PDF')) : undef,
125     ($::lx_office_conf{print_templates}->{latex} && !$options->{no_postscript}) ?
126       opthash("postscript",          $form->{DF}{postscript},          $locale->text('Postscript')) : undef,
127     (!$options->{no_html}) ?
128       opthash("html", $form->{DF}{html}, "HTML") : undef,
129     ($::lx_office_conf{print_templates}->{opendocument} && !$options->{no_opendocument}) ?
130       opthash("opendocument",        $form->{DF}{opendocument},        $locale->text("OpenDocument/OASIS")) : undef,
131     ($::lx_office_conf{print_templates}->{excel} && !$options->{no_excel}) ?
132       opthash("excel",               $form->{DF}{excel},               $locale->text("Excel")) : undef;
133
134   push @LANGUAGE_ID,
135     map { opthash($_->{id}, ($_->{id} eq $form->{language_id} ? 'selected' : ''), $_->{description}) } +{}, @{ $form->{languages} }
136       if (ref $form->{languages} eq 'ARRAY');
137
138   push @PRINTER_ID,
139     map { opthash($_->{id}, ($_->{id} eq $form->{printer_id} ? 'selected' : ''), $_->{printer_description}) } +{}, @{ $form->{printers} }
140       if ((ref $form->{printers} eq 'ARRAY') && scalar @{ $form->{printers } });
141
142   @SELECTS = map {
143     sname  => $_->[1],
144     DATA   => $_->[0],
145     show   => !$options->{"hide_" . $_->[1]} && scalar @{ $_->[0]},
146     hname  => $locale->text($_->[2])
147   },
148   [ \@FORMNAME,    'formname',    'Formname' ],
149   [ \@LANGUAGE_ID, 'language_id', 'Language' ],
150   [ \@FORMAT,      'format',      'Format'   ],
151   [ \@SENDMODE,    'sendmode',    'Sendmode' ],
152   [ \@MEDIA,       'media',       'Media'    ],
153   [ \@PRINTER_ID,  'printer_id',  'Printer'  ];
154
155   my %dont_display_groupitems = (
156     'dunning' => 1,
157     );
158
159   my %template_vars = (
160     name_prefix          => $prefix || '',
161     show_headers         => $options->{show_headers},
162     display_copies       => scalar @{ $form->{printers} || [] } && $::lx_office_conf{print_templates}->{latex} && $form->{media} ne 'email',
163     display_remove_draft => (!$form->{id} && $form->{draft_id}),
164     display_groupitems   => !$dont_display_groupitems{$form->{type}},
165     display_bothsided    => $options->{show_bothsided},
166     groupitems_checked   => $form->{groupitems} ? "checked" : '',
167     bothsided_checked    => $form->{bothsided}  ? "checked" : '',
168     remove_draft_checked => $form->{remove_draft} ? "checked" : ''
169   );
170
171   return $form->parse_html_template("generic/print_options", { SELECTS  => \@SELECTS, %template_vars } );
172 }
173
174 1;
175
176 __END__
177
178 =pod
179
180 =encoding utf8
181
182 =head1 NAME
183
184 SL::Helper::PrintOptions - A helper for generating the print options for
185 templates
186
187 =head1 SYNOPSIS
188
189   # render your template with print_options
190   $self->render('letter/edit',
191     %params,
192     letter        => $letter,
193     print_options => SL::Helper::PrintOptions->get_print_options (
194       options => { no_postscript   => 1,
195                    no_opendocument => 1,
196                    no_html         => 1,
197                    no_queue        => 1 }),
198
199   );
200
201 Then, in the template, you can render the options with
202     C<[% print_options %]>. Look at the template
203     C<generic/print_options> to see, which variables you get back.
204
205 =head1 FUNCTIONS
206
207 =over 4
208
209 =item C<get_print_options %params>
210
211 Parses the template C<generic/print_options>. It does some guessings
212     and settings according to the params, (namely C<form>).
213
214
215 The recognized parameters are:
216
217 =over 2
218
219 =item * C<form>: defaults to $::form if not given. There are several
220     keys in C<form> which control the output of the options,
221     e.g. C<format>, C<media>, C<copies>, C<printers>, C<printer_id>,
222     C<type>, C<formname>, ...
223
224 =item * C<myconfig>: defaults to %::myconfig
225
226 =item * C<locale>: defaults to $::locale
227
228 =item * C<options>: Options can be:
229
230 * C<dialog_name_prefix>: a string prefixed to the template
231     variables. E.g. if prefix is C<mypref_> the value for copies
232     returned from the user is in $::form->{mypref_copies}
233
234 * C<show_header>: render headings for the input elements
235
236 * C<no_queue>: if set, do not show option for printing to queue
237
238 * C<no_opendocument>: if set, do not show option for printing
239     opendocument format
240
241 * C<no_postscript>: if set, do not show option for printing
242     postscript format
243
244 * C<no_html>: if set, do not show option for printing
245     html format
246
247 * C<no_opendocument_pdf>
248
249 * C<no_excel>
250
251 * and some more
252
253 =back
254
255 =back
256
257 =head1 AUTHOR
258
259 ?
260
261 Bernd Bleßmann E<lt>bernd@kivitendo-premium.deE<gt> (I just moved
262     it from io.pl to here and did some minor changes)
263
264 =head1 BUGS
265
266 incomplete documentation
267
268 =cut