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