1 package SL::Helper::PrintOptions;
5 use List::MoreUtils qw(any);
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
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.
16 # the inline options is untested, but intended to be used later in metatemplating
17 sub get_print_options {
18 my ($class, %params) = @_;
20 my $form = $params{form} || $::form;
21 my $myconfig = $params{myconfig} || \%::myconfig;
22 my $locale = $params{locale} || $::locale;
23 my $options = $params{options};
25 my $prefix = $options->{dialog_name_prefix} || '';
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) = ();
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} : "";
39 $form->{PD}{ $form->{formname} } = "selected";
40 $form->{DF}{ $form->{format} } = "selected";
41 $form->{OP}{ $form->{media} } = "selected";
42 $form->{SM}{ $form->{sendmode} } = "selected";
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'))
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')),
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')),
64 ($form->{type} eq 'invoice' && $form->{storno}) ? (
65 opthash("storno_invoice", $form->{PD}{storno_invoice}, $locale->text('Storno Invoice')),
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')),
71 ($form->{type} =~ /^letter$/) ? (
72 opthash('letter', $form->{PD}{letter}, $locale->text('Letter')),
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');
80 my $printable_templates = any { $::lx_office_conf{print_templates}->{$_} } qw(latex opendocument);
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');
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;
106 map { opthash($_->{id}, ($_->{id} eq $form->{language_id} ? 'selected' : ''), $_->{description}) } +{}, @{ $form->{languages} }
107 if (ref $form->{languages} eq 'ARRAY');
110 map { opthash($_->{id}, ($_->{id} eq $form->{printer_id} ? 'selected' : ''), $_->{printer_description}) } +{}, @{ $form->{printers} }
111 if ((ref $form->{printers} eq 'ARRAY') && scalar @{ $form->{printers } });
116 show => !$options->{"hide_" . $_->[1]} && scalar @{ $_->[0]},
117 hname => $locale->text($_->[2])
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' ];
126 my %dont_display_groupitems = (
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" : ''
140 return $form->parse_html_template("generic/print_options", { SELECTS => \@SELECTS, %template_vars } );
153 SL::Helper::PrintOptions - A helper for generating the print options for
158 # render your template with print_options
159 $self->render('letter/edit',
162 print_options => SL::Helper::PrintOptions->get_print_options (
163 options => { no_postscript => 1,
164 no_opendocument => 1,
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.
178 =item C<get_print_options %params>
180 Parses the template C<generic/print_options>. It does some guessings
181 and settings according to the params, (namely C<form>).
184 The recognized parameters are:
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>, ...
193 =item * C<myconfig>: defaults to %::myconfig
195 =item * C<locale>: defaults to $::locale
197 =item * C<options>: Options can be:
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}
203 * C<show_header>: render headings for the input elements
205 * C<no_queue>: if set, do not show option for printing to queue
207 * C<no_opendocument>: if set, do not show option for printing
210 * C<no_postscript>: if set, do not show option for printing
213 * C<no_html>: if set, do not show option for printing
216 * C<no_opendocument_pdf>
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)
235 incomplete documentation