5 use parent qw(Rose::Object);
10 use SL::Presenter::CustomerVendor;
11 use SL::Presenter::DeliveryOrder;
12 use SL::Presenter::EscapedText;
13 use SL::Presenter::Invoice;
14 use SL::Presenter::Order;
15 use SL::Presenter::Project;
16 use SL::Presenter::Record;
19 return $::request->presenter;
25 my ($options, %locals) = (@_ && ref($_[0])) ? @_ : ({ }, @_);
27 # Set defaults for all available options.
32 $options->{$_} //= $defaults{$_} for keys %defaults;
33 $options->{type} = lc $options->{type};
35 # Check supplied options for validity.
36 foreach (keys %{ $options }) {
37 croak "Unsupported option: $_" unless $defaults{$_};
40 # Only certain types are supported.
41 croak "Unsupported type: " . $options->{type} unless $options->{type} =~ m/^(?:html|js|json)$/;
43 # The "template" argument must be a string or a reference to one.
44 $template = ${ $template } if ((ref($template) || '') eq 'REF') && (ref(${ $template }) eq 'SL::Presenter::EscapedText');
45 croak "Unsupported 'template' reference type: " . ref($template) if ref($template) && (ref($template) !~ m/^(?:SCALAR|SL::Presenter::EscapedText)$/);
47 # Look for the file given by $template if $template is not a reference.
50 $source = "templates/webpages/${template}." . $options->{type};
51 croak "Template file ${source} not found" unless -f $source;
53 } elsif (ref($template) eq 'SCALAR') {
54 # Normal scalar reference: hand over to Template
58 # Instance of SL::Presenter::EscapedText. Get reference to its content.
59 $source = \$template->{text};
62 # If no processing is requested then return the content.
63 if (!$options->{process}) {
64 # If $template is a reference then don't try to read a file.
65 my $ref = ref $template;
66 return $template if $ref eq 'SL::Presenter::EscapedText';
67 return SL::Presenter::EscapedText->new(text => ${ $template }, is_escaped => 1) if $ref eq 'SCALAR';
69 # Otherwise return the file's content.
70 my $file = IO::File->new($source, "r") || croak("Template file ${source} could not be read");
71 my $content = do { local $/ = ''; <$file> };
74 return SL::Presenter::EscapedText->new(text => $content, is_escaped => 1);
77 # Processing was requested. Set up all variables.
78 my %params = ( %locals,
80 FLASH => $::form->{FLASH},
82 INSTANCE_CONF => $::instance_conf,
84 LXCONFIG => \%::lx_office_conf,
85 LXDEBUG => $::lxdebug,
86 MYCONFIG => \%::myconfig,
91 my $parser = $self->get_template;
92 $parser->process($source, \%params, \$output) || croak $parser->error;
94 return SL::Presenter::EscapedText->new(text => $output, is_escaped => 1);
100 $self->{template} ||=
101 Template->new({ INTERPOLATE => 0,
105 PLUGIN_BASE => 'SL::Template::Plugin',
106 INCLUDE_PATH => '.:templates/webpages',
107 COMPILE_EXT => '.tcc',
108 COMPILE_DIR => $::lx_office_conf{paths}->{userspath} . '/templates-cache',
109 ERROR => 'templates/webpages/generic/exception.html',
112 return $self->{template};
116 my ($self, $text) = @_;
118 return SL::Presenter::EscapedText->new(text => $text);
122 my ($self, $text) = @_;
124 return SL::Presenter::EscapedText->new(text => $text, is_escaped => 1);
128 my ($self, $text) = @_;
130 $text =~ s|\\|\\\\|g;
131 $text =~ s|\"|\\\"|g;
134 return SL::Presenter::EscapedText->new(text => $text, is_escaped => 1);
143 SL::Presenter - presentation layer class
148 my $presenter = SL::Presenter->get;
150 # Lower-level template parsing:
151 my $html = $presenter->render(
152 'presenter/dir/template.html',
156 # Higher-level rendering of certain objects:
157 use SL::DB::Customer;
159 my $linked_customer_name = $presenter->customer($customer, display => 'table-cell');
161 # Render a list of links to sales/purchase records:
164 my $quotation = SL::DB::Manager::Order->get_first(where => { quotation => 1 });
165 my $records = $quotation->linked_records(direction => 'to');
166 my $html = $presenter->grouped_record_list($records);
168 =head1 CLASS FUNCTIONS
174 Returns the global presenter object and creates it if it doesn't exist
179 =head1 INSTANCE FUNCTIONS
183 =item C<render $template, [ $options, ] %locals>
185 Renders the template C<$template>. Provides other variables than
186 C<Form::parse_html_template> does.
188 C<$options>, if present, must be a hash reference. All remaining
189 parameters are slurped into C<%locals>.
191 This is the backend function that L<SL::Controller::Base/render>
192 calls. The big difference is that the presenter's L<render> function
193 always returns the input and never sends anything to the browser while
194 the controller's function usually sends the result to the
195 controller. Therefore the presenter's L<render> function does not use
196 all of the parameters for controlling the output that the controller's
199 What is rendered and how C<$template> is interpreted is determined
200 both by C<$template>'s reference type and by the supplied options.
202 If C<$template> is a normal scalar (not a reference) then it is meant
203 to be a template file name relative to the C<templates/webpages>
204 directory. The file name to use is determined by the C<type> option.
206 If C<$template> is a reference to a scalar then the referenced
207 scalar's content is used as the content to process. The C<type> option
208 is not considered in this case.
210 C<$template> can also be an instance of L<SL::Presenter::EscapedText>
211 or a reference to such an instance. Both of these cases are handled
212 the same way as if C<$template> were a reference to a scalar: its
213 content is processed, and C<type> is not considered.
215 Other reference types, unknown options and unknown arguments to the
216 C<type> option cause the function to L<croak>.
218 The following options are available:
224 The template type. Can be C<html> (the default), C<js> for JavaScript
225 or C<json> for JSON content. Affects only the extension that's added
226 to the file name given with a non-reference C<$template> argument.
230 If trueish (which is also the default) it causes the template/content
231 to be processed by the Template toolkit. Otherwise the
232 template/content is returned as-is.
236 If template processing is requested then the template has access to
237 the following variables:
241 =item * C<AUTH> -- C<$::auth>
243 =item * C<FORM> -- C<$::form>
245 =item * C<LOCALE> -- C<$::locale>
247 =item * C<LXCONFIG> -- all parameters from C<config/kivitendo.conf>
248 with the same name they appear in the file (first level is the
249 section, second the actual variable, e.g. C<system.dbcharset>,
250 C<features.webdav> etc)
252 =item * C<LXDEBUG> -- C<$::lxdebug>
254 =item * C<MYCONFIG> -- C<%::myconfig>
256 =item * C<SELF> -- the controller instance
258 =item * All items from C<%locals>
262 The function will always return the output and never send anything to
265 Example: Render a HTML template with a certain title and a few locals
267 $presenter->render('todo/list',
268 title => 'List TODO items',
269 TODO_ITEMS => SL::DB::Manager::Todo->get_all_sorted);
271 Example: Render a string and return its content for further processing
272 by the calling function.
274 my $content = $presenter->render(\'[% USE JavaScript %][% JavaScript.replace_with("#someid", "js/something") %]');
276 Example: Return the content of a JSON template file without processing
279 my $template_content = $presenter->render(
281 { type => 'json', process => 0 }
284 =item C<escape $text>
286 Returns an HTML-escaped version of C<$text>. Instead of a string an
287 instance of the thin proxy-object L<SL::Presenter::EscapedText> is
290 It is safe to call C<escape> on an instance of
291 L<SL::Presenter::EscapedText>. This is a no-op (the same instance will
294 =item C<escaped_text $text>
296 Returns an instance of L<SL::Presenter::EscapedText>. C<$text> is
297 assumed to be a string that has already been HTML-escaped.
299 It is safe to call C<escaped_text> on an instance of
300 L<SL::Presenter::EscapedText>. This is a no-op (the same instance will
303 =item C<escape_js $text>
305 Returns a JavaScript-escaped version of C<$text>. Instead of a string
306 an instance of the thin proxy-object L<SL::Presenter::EscapedText> is
309 It is safe to call C<escape> on an instance of
310 L<SL::Presenter::EscapedText>. This is a no-op (the same instance will
313 =item C<get_template>
315 Returns the global instance of L<Template> and creates it if it
316 doesn't exist already.
322 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>