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