Gelangensbestätigung als Weiche in io.pl UND Helper PrintOptions.pm
[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       opthash("ic_supply",           $form->{PD}{ic_supply},            $locale->text('Intra-Community supply')),
55     ) : undef,
56     ($form->{type} =~ /sales_quotation$/) ?
57       opthash('sales_quotation',     $form->{PD}{sales_quotation},     $locale->text('Quotation')) : undef,
58     ($form->{type} =~ /request_quotation$/) ?
59       opthash('request_quotation',   $form->{PD}{request_quotation},   $locale->text('Request for Quotation')) : undef,
60     ($form->{type} eq 'invoice') ? (
61       opthash("invoice",             $form->{PD}{invoice},             $locale->text('Invoice')),
62       opthash("proforma",            $form->{PD}{proforma},            $locale->text('Proforma Invoice')),
63     ) : undef,
64     ($form->{type} eq 'invoice' && $form->{storno}) ? (
65       opthash("storno_invoice",      $form->{PD}{storno_invoice},      $locale->text('Storno Invoice')),
66     ) : undef,
67     ($form->{type} =~ /_delivery_order$/) ? (
68       opthash($form->{type},         $form->{PD}{$form->{type}},       $locale->text('Delivery Order')),
69       opthash('pick_list',           $form->{PD}{pick_list},           $locale->text('Pick List')),
70     ) : undef;
71
72   push @SENDMODE,
73     opthash("attachment",            $form->{SM}{attachment},          $locale->text('Attachment')),
74     opthash("inline",                $form->{SM}{inline},              $locale->text('In-line'))
75       if ($form->{media} eq 'email');
76
77   my $printable_templates = any { $::lx_office_conf{print_templates}->{$_} } qw(latex opendocument);
78   push @MEDIA, grep $_,
79       opthash("screen",              $form->{OP}{screen},              $locale->text('Screen')),
80     ($printable_templates && $form->{printers} && scalar @{ $form->{printers} }) ?
81       opthash("printer",             $form->{OP}{printer},             $locale->text('Printer')) : undef,
82     ($printable_templates && !$options->{no_queue}) ?
83       opthash("queue",               $form->{OP}{queue},               $locale->text('Queue')) : undef
84         if ($form->{media} ne 'email');
85
86   push @FORMAT, grep $_,
87     ($::lx_office_conf{print_templates}->{opendocument} &&     $::lx_office_conf{applications}->{openofficeorg_writer}  &&     $::lx_office_conf{applications}->{xvfb}
88                                                         && (-x $::lx_office_conf{applications}->{openofficeorg_writer}) && (-x $::lx_office_conf{applications}->{xvfb})
89      && !$options->{no_opendocument_pdf}) ?
90       opthash("opendocument_pdf",    $form->{DF}{"opendocument_pdf"},  $locale->text("PDF (OpenDocument/OASIS)")) : undef,
91     ($::lx_office_conf{print_templates}->{latex}) ?
92       opthash("pdf",                 $form->{DF}{pdf},                 $locale->text('PDF')) : undef,
93     ($::lx_office_conf{print_templates}->{latex} && !$options->{no_postscript}) ?
94       opthash("postscript",          $form->{DF}{postscript},          $locale->text('Postscript')) : undef,
95     (!$options->{no_html}) ?
96       opthash("html", $form->{DF}{html}, "HTML") : undef,
97     ($::lx_office_conf{print_templates}->{opendocument} && !$options->{no_opendocument}) ?
98       opthash("opendocument",        $form->{DF}{opendocument},        $locale->text("OpenDocument/OASIS")) : undef,
99     ($::lx_office_conf{print_templates}->{excel} && !$options->{no_excel}) ?
100       opthash("excel",               $form->{DF}{excel},               $locale->text("Excel")) : undef;
101
102   push @LANGUAGE_ID,
103     map { opthash($_->{id}, ($_->{id} eq $form->{language_id} ? 'selected' : ''), $_->{description}) } +{}, @{ $form->{languages} }
104       if (ref $form->{languages} eq 'ARRAY');
105
106   push @PRINTER_ID,
107     map { opthash($_->{id}, ($_->{id} eq $form->{printer_id} ? 'selected' : ''), $_->{printer_description}) } +{}, @{ $form->{printers} }
108       if ((ref $form->{printers} eq 'ARRAY') && scalar @{ $form->{printers } });
109
110   @SELECTS = map {
111     sname  => $_->[1],
112     DATA   => $_->[0],
113     show   => !$options->{"hide_" . $_->[1]} && scalar @{ $_->[0]},
114     hname  => $locale->text($_->[2])
115   },
116   [ \@FORMNAME,    'formname',    'Formname' ],
117   [ \@LANGUAGE_ID, 'language_id', 'Language' ],
118   [ \@FORMAT,      'format',      'Format'   ],
119   [ \@SENDMODE,    'sendmode',    'Sendmode' ],
120   [ \@MEDIA,       'media',       'Media'    ],
121   [ \@PRINTER_ID,  'printer_id',  'Printer'  ];
122
123   my %dont_display_groupitems = (
124     'dunning' => 1,
125     'letter'  => 1,
126     );
127
128   my %template_vars = (
129     name_prefix          => $prefix || '',
130     show_headers         => $options->{show_headers},
131     display_copies       => scalar @{ $form->{printers} || [] } && $::lx_office_conf{print_templates}->{latex} && $form->{media} ne 'email',
132     display_remove_draft => (!$form->{id} && $form->{draft_id}),
133     display_groupitems   => !$dont_display_groupitems{$form->{type}},
134     groupitems_checked   => $form->{groupitems} ? "checked" : '',
135     remove_draft_checked => $form->{remove_draft} ? "checked" : ''
136   );
137
138   return $form->parse_html_template("generic/print_options", { SELECTS  => \@SELECTS, %template_vars } );
139 }
140
141 1;
142
143 __END__
144
145 =pod
146
147 =encoding utf8
148
149 =head1 NAME
150
151 SL::Helper::PrintOptions - A helper for generating the print options for
152 templates
153
154 =head1 SYNOPSIS
155
156   # render your template with print_options
157   $self->render('letter/edit',
158     %params,
159     letter        => $letter,
160     print_options => SL::Helper::PrintOptions->get_print_options (
161       options => { no_postscript   => 1,
162                    no_opendocument => 1,
163                    no_html         => 1,
164                    no_queue        => 1 }),
165
166   );
167
168 Then, in the template, you can render the options with
169     C<[% print_options %]>. Look at the template
170     C<generic/print_options> to see, which variables you get back.
171
172 =head1 FUNCTIONS
173
174 =over 4
175
176 =item C<get_print_options %params>
177
178 Parses the template C<generic/print_options>. It does some guessings
179     and settings according to the params, (namely C<form>).
180
181
182 The recognized parameters are:
183
184 =over 2
185
186 =item * C<form>: defaults to $::form if not given. There are several
187     keys in C<form> which control the output of the options,
188     e.g. C<format>, C<media>, C<copies>, C<printers>, C<printer_id>,
189     C<type>, C<formname>, ...
190
191 =item * C<myconfig>: defaults to %::myconfig
192
193 =item * C<locale>: defaults to $::locale
194
195 =item * C<options>: Options can be:
196
197 * C<dialog_name_prefix>: a string prefixed to the template
198     variables. E.g. if prefix is C<mypref_> the value for copies
199     returned from the user is in $::form->{mypref_copies}
200
201 * C<show_header>: render headings for the input elements
202
203 * C<no_queue>: if set, do not show option for printing to queue
204
205 * C<no_opendocument>: if set, do not show option for printing
206     opendocument format
207
208 * C<no_postscript>: if set, do not show option for printing
209     postscript format
210
211 * C<no_html>: if set, do not show option for printing
212     html format
213
214 * C<no_opendocument_pdf>
215
216 * C<no_excel>
217
218 * and some more
219
220 =back
221
222 =back
223
224 =head1 AUTHOR
225
226 ?
227
228 Bernd Bleßmann E<lt>bernd@kivitendo-premium.deE<gt> (I just moved
229     it from io.pl to here and did some minor changes)
230
231 =head1 BUGS
232
233 incomplete documentation
234
235 =cut