5 use parent qw(Rose::Object);
10 use SL::Presenter::Chart;
11 use SL::Presenter::CustomerVendor;
12 use SL::Presenter::DeliveryOrder;
13 use SL::Presenter::EscapedText;
14 use SL::Presenter::Invoice;
15 use SL::Presenter::GL;
16 use SL::Presenter::Letter;
17 use SL::Presenter::Order;
18 use SL::Presenter::Part;
19 use SL::Presenter::Project;
20 use SL::Presenter::Record;
21 use SL::Presenter::RequirementSpec;
22 use SL::Presenter::RequirementSpecItem;
23 use SL::Presenter::RequirementSpecTextBlock;
24 use SL::Presenter::SepaExport;
25 use SL::Presenter::Text;
26 use SL::Presenter::Tag;
27 use SL::Presenter::BankAccount;
29 use Rose::Object::MakeMethods::Generic (
30 scalar => [ qw(need_reinit_widgets) ],
34 return $::request->presenter;
40 my ($options, %locals) = (@_ && ref($_[0])) ? @_ : ({ }, @_);
42 # Set defaults for all available options.
47 $options->{$_} //= $defaults{$_} for keys %defaults;
48 $options->{type} = lc $options->{type};
50 # Check supplied options for validity.
51 foreach (keys %{ $options }) {
52 croak "Unsupported option: $_" unless $defaults{$_};
55 # Only certain types are supported.
56 croak "Unsupported type: " . $options->{type} unless $options->{type} =~ m/^(?:html|js|json|text)$/;
58 # The "template" argument must be a string or a reference to one.
59 $template = ${ $template } if ((ref($template) || '') eq 'REF') && (ref(${ $template }) eq 'SL::Presenter::EscapedText');
60 croak "Unsupported 'template' reference type: " . ref($template) if ref($template) && (ref($template) !~ m/^(?:SCALAR|SL::Presenter::EscapedText)$/);
62 # Look for the file given by $template if $template is not a reference.
65 my $ext = $options->{type} eq 'text' ? 'txt' : $options->{type};
66 $source = "templates/webpages/${template}.${ext}";
67 croak "Template file ${source} not found" unless -f $source;
69 } elsif (ref($template) eq 'SCALAR') {
70 # Normal scalar reference: hand over to Template
74 # Instance of SL::Presenter::EscapedText. Get reference to its content.
75 $source = \$template->{text};
78 # If no processing is requested then return the content.
79 if (!$options->{process}) {
80 # If $template is a reference then don't try to read a file.
81 my $ref = ref $template;
82 return $template if $ref eq 'SL::Presenter::EscapedText';
83 return SL::Presenter::EscapedText->new(text => ${ $template }, is_escaped => 1) if $ref eq 'SCALAR';
85 # Otherwise return the file's content.
86 my $file = IO::File->new($source, "r") || croak("Template file ${source} could not be read");
87 my $content = do { local $/ = ''; <$file> };
90 return SL::Presenter::EscapedText->new(text => $content, is_escaped => 1);
93 # Processing was requested. Set up all variables.
94 my %params = ( %locals,
96 FLASH => $::form->{FLASH},
98 INSTANCE_CONF => $::instance_conf,
100 LXCONFIG => \%::lx_office_conf,
101 LXDEBUG => $::lxdebug,
102 MYCONFIG => \%::myconfig,
107 my $parser = $self->get_template;
108 $parser->process($source, \%params, \$output) || croak $parser->error;
110 return SL::Presenter::EscapedText->new(text => $output, is_escaped => 1);
116 $self->{template} ||=
117 Template->new({ INTERPOLATE => 0,
121 PLUGIN_BASE => 'SL::Template::Plugin',
122 INCLUDE_PATH => '.:templates/webpages',
123 COMPILE_EXT => '.tcc',
124 COMPILE_DIR => $::lx_office_conf{paths}->{userspath} . '/templates-cache',
125 ERROR => 'templates/webpages/generic/exception.html',
129 return $self->{template};
133 my ($self, $text) = @_;
135 return SL::Presenter::EscapedText->new(text => $text);
139 my ($self, $text) = @_;
141 return SL::Presenter::EscapedText->new(text => $text, is_escaped => 1);
145 my ($self, $text) = @_;
147 $text =~ s|\\|\\\\|g;
148 $text =~ s|\"|\\\"|g;
151 return SL::Presenter::EscapedText->new(text => $text, is_escaped => 1);
160 SL::Presenter - presentation layer class
165 my $presenter = SL::Presenter->get;
167 # Lower-level template parsing:
168 my $html = $presenter->render(
169 'presenter/dir/template.html',
173 # Higher-level rendering of certain objects:
174 use SL::DB::Customer;
176 my $linked_customer_name = $presenter->customer($customer, display => 'table-cell');
178 # Render a list of links to sales/purchase records:
181 my $quotation = SL::DB::Manager::Order->get_first(where => { quotation => 1 });
182 my $records = $quotation->linked_records(direction => 'to');
183 my $html = $presenter->grouped_record_list($records);
185 =head1 CLASS FUNCTIONS
191 Returns the global presenter object and creates it if it doesn't exist
196 =head1 INSTANCE FUNCTIONS
200 =item C<render $template, [ $options, ] %locals>
202 Renders the template C<$template>. Provides other variables than
203 C<Form::parse_html_template> does.
205 C<$options>, if present, must be a hash reference. All remaining
206 parameters are slurped into C<%locals>.
208 This is the backend function that L<SL::Controller::Base/render>
209 calls. The big difference is that the presenter's L<render> function
210 always returns the input and never sends anything to the browser while
211 the controller's function usually sends the result to the
212 controller. Therefore the presenter's L<render> function does not use
213 all of the parameters for controlling the output that the controller's
216 What is rendered and how C<$template> is interpreted is determined
217 both by C<$template>'s reference type and by the supplied options.
219 If C<$template> is a normal scalar (not a reference) then it is meant
220 to be a template file name relative to the C<templates/webpages>
221 directory. The file name to use is determined by the C<type> option.
223 If C<$template> is a reference to a scalar then the referenced
224 scalar's content is used as the content to process. The C<type> option
225 is not considered in this case.
227 C<$template> can also be an instance of L<SL::Presenter::EscapedText>
228 or a reference to such an instance. Both of these cases are handled
229 the same way as if C<$template> were a reference to a scalar: its
230 content is processed, and C<type> is not considered.
232 Other reference types, unknown options and unknown arguments to the
233 C<type> option cause the function to L<croak>.
235 The following options are available:
241 The template type. Can be C<html> (the default), C<js> for JavaScript,
242 C<json> for JSON and C<text> for plain text content. Affects only the
243 extension that's added to the file name given with a non-reference
244 C<$template> argument.
248 If trueish (which is also the default) it causes the template/content
249 to be processed by the Template toolkit. Otherwise the
250 template/content is returned as-is.
254 If template processing is requested then the template has access to
255 the following variables:
259 =item * C<AUTH> -- C<$::auth>
261 =item * C<FLASH> -- the flash instance (C<$::form-E<gt>{FLASH}>)
263 =item * C<FORM> -- C<$::form>
265 =item * C<INSTANCE_CONF> -- C<$::instance_conf>
267 =item * C<LOCALE> -- C<$::locale>
269 =item * C<LXCONFIG> -- all parameters from C<config/kivitendo.conf>
270 with the same name they appear in the file (first level is the
271 section, second the actual variable, e.g. C<system.language>,
272 C<features.webdav> etc)
274 =item * C<LXDEBUG> -- C<$::lxdebug>
276 =item * C<MYCONFIG> -- C<%::myconfig>
278 =item * C<SELF> -- the controller instance
280 =item * C<PRESENTER> -- the presenter instance the template is
283 =item * All items from C<%locals>
287 The function will always return the output and never send anything to
290 Example: Render a HTML template with a certain title and a few locals
292 $presenter->render('todo/list',
293 title => 'List TODO items',
294 TODO_ITEMS => SL::DB::Manager::Todo->get_all_sorted);
296 Example: Render a string and return its content for further processing
297 by the calling function.
299 my $content = $presenter->render(\'[% USE JavaScript %][% JavaScript.replace_with("#someid", "js/something") %]');
301 Example: Return the content of a JSON template file without processing
304 my $template_content = $presenter->render(
306 { type => 'json', process => 0 }
309 =item C<escape $text>
311 Returns an HTML-escaped version of C<$text>. Instead of a string an
312 instance of the thin proxy-object L<SL::Presenter::EscapedText> is
315 It is safe to call C<escape> on an instance of
316 L<SL::Presenter::EscapedText>. This is a no-op (the same instance will
319 =item C<escaped_text $text>
321 Returns an instance of L<SL::Presenter::EscapedText>. C<$text> is
322 assumed to be a string that has already been HTML-escaped.
324 It is safe to call C<escaped_text> on an instance of
325 L<SL::Presenter::EscapedText>. This is a no-op (the same instance will
328 =item C<escape_js $text>
330 Returns a JavaScript-escaped version of C<$text>. Instead of a string
331 an instance of the thin proxy-object L<SL::Presenter::EscapedText> is
334 It is safe to call C<escape> on an instance of
335 L<SL::Presenter::EscapedText>. This is a no-op (the same instance will
338 =item C<get_template>
340 Returns the global instance of L<Template> and creates it if it
341 doesn't exist already.
347 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>