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::Part;
 
  16 use SL::Presenter::Project;
 
  17 use SL::Presenter::Record;
 
  18 use SL::Presenter::RequirementSpecItem;
 
  19 use SL::Presenter::RequirementSpecTextBlock;
 
  20 use SL::Presenter::SepaExport;
 
  21 use SL::Presenter::Text;
 
  22 use SL::Presenter::Tag;
 
  24 use Rose::Object::MakeMethods::Generic (
 
  25   scalar => [ qw(need_reinit_widgets) ],
 
  29   return $::request->presenter;
 
  35   my ($options, %locals) = (@_ && ref($_[0])) ? @_ : ({ }, @_);
 
  37   # Set defaults for all available options.
 
  42   $options->{$_} //= $defaults{$_} for keys %defaults;
 
  43   $options->{type} = lc $options->{type};
 
  45   # Check supplied options for validity.
 
  46   foreach (keys %{ $options }) {
 
  47     croak "Unsupported option: $_" unless $defaults{$_};
 
  50   # Only certain types are supported.
 
  51   croak "Unsupported type: " . $options->{type} unless $options->{type} =~ m/^(?:html|js|json|text)$/;
 
  53   # The "template" argument must be a string or a reference to one.
 
  54   $template = ${ $template }                                       if ((ref($template) || '') eq 'REF') && (ref(${ $template }) eq 'SL::Presenter::EscapedText');
 
  55   croak "Unsupported 'template' reference type: " . ref($template) if ref($template) && (ref($template) !~ m/^(?:SCALAR|SL::Presenter::EscapedText)$/);
 
  57   # Look for the file given by $template if $template is not a reference.
 
  60     my $ext = $options->{type} eq 'text' ? 'txt' : $options->{type};
 
  61     $source = "templates/webpages/${template}.${ext}";
 
  62     croak "Template file ${source} not found" unless -f $source;
 
  64   } elsif (ref($template) eq 'SCALAR') {
 
  65     # Normal scalar reference: hand over to Template
 
  69     # Instance of SL::Presenter::EscapedText. Get reference to its content.
 
  70     $source = \$template->{text};
 
  73   # If no processing is requested then return the content.
 
  74   if (!$options->{process}) {
 
  75     # If $template is a reference then don't try to read a file.
 
  76     my $ref = ref $template;
 
  77     return $template                                                                if $ref eq 'SL::Presenter::EscapedText';
 
  78     return SL::Presenter::EscapedText->new(text => ${ $template }, is_escaped => 1) if $ref eq 'SCALAR';
 
  80     # Otherwise return the file's content.
 
  81     my $file    = IO::File->new($source, "r") || croak("Template file ${source} could not be read");
 
  82     my $content = do { local $/ = ''; <$file> };
 
  85     return SL::Presenter::EscapedText->new(text => $content, is_escaped => 1);
 
  88   # Processing was requested. Set up all variables.
 
  89   my %params = ( %locals,
 
  91                  FLASH         => $::form->{FLASH},
 
  93                  INSTANCE_CONF => $::instance_conf,
 
  95                  LXCONFIG      => \%::lx_office_conf,
 
  96                  LXDEBUG       => $::lxdebug,
 
  97                  MYCONFIG      => \%::myconfig,
 
 102   my $parser = $self->get_template;
 
 103   $parser->process($source, \%params, \$output) || croak $parser->error;
 
 105   return SL::Presenter::EscapedText->new(text => $output, is_escaped => 1);
 
 111   $self->{template} ||=
 
 112     Template->new({ INTERPOLATE  => 0,
 
 116                     PLUGIN_BASE  => 'SL::Template::Plugin',
 
 117                     INCLUDE_PATH => '.:templates/webpages',
 
 118                     COMPILE_EXT  => '.tcc',
 
 119                     COMPILE_DIR  => $::lx_office_conf{paths}->{userspath} . '/templates-cache',
 
 120                     ERROR        => 'templates/webpages/generic/exception.html',
 
 124   return $self->{template};
 
 128   my ($self, $text) = @_;
 
 130   return SL::Presenter::EscapedText->new(text => $text);
 
 134   my ($self, $text) = @_;
 
 136   return SL::Presenter::EscapedText->new(text => $text, is_escaped => 1);
 
 140   my ($self, $text) = @_;
 
 142   $text =~ s|\\|\\\\|g;
 
 143   $text =~ s|\"|\\\"|g;
 
 146   return SL::Presenter::EscapedText->new(text => $text, is_escaped => 1);
 
 155 SL::Presenter - presentation layer class
 
 160   my $presenter = SL::Presenter->get;
 
 162   # Lower-level template parsing:
 
 163   my $html = $presenter->render(
 
 164     'presenter/dir/template.html',
 
 168   # Higher-level rendering of certain objects:
 
 169   use SL::DB::Customer;
 
 171   my $linked_customer_name = $presenter->customer($customer, display => 'table-cell');
 
 173   # Render a list of links to sales/purchase records:
 
 176   my $quotation = SL::DB::Manager::Order->get_first(where => { quotation => 1 });
 
 177   my $records   = $quotation->linked_records(direction => 'to');
 
 178   my $html      = $presenter->grouped_record_list($records);
 
 180 =head1 CLASS FUNCTIONS
 
 186 Returns the global presenter object and creates it if it doesn't exist
 
 191 =head1 INSTANCE FUNCTIONS
 
 195 =item C<render $template, [ $options, ] %locals>
 
 197 Renders the template C<$template>. Provides other variables than
 
 198 C<Form::parse_html_template> does.
 
 200 C<$options>, if present, must be a hash reference. All remaining
 
 201 parameters are slurped into C<%locals>.
 
 203 This is the backend function that L<SL::Controller::Base/render>
 
 204 calls. The big difference is that the presenter's L<render> function
 
 205 always returns the input and never sends anything to the browser while
 
 206 the controller's function usually sends the result to the
 
 207 controller. Therefore the presenter's L<render> function does not use
 
 208 all of the parameters for controlling the output that the controller's
 
 211 What is rendered and how C<$template> is interpreted is determined
 
 212 both by C<$template>'s reference type and by the supplied options.
 
 214 If C<$template> is a normal scalar (not a reference) then it is meant
 
 215 to be a template file name relative to the C<templates/webpages>
 
 216 directory. The file name to use is determined by the C<type> option.
 
 218 If C<$template> is a reference to a scalar then the referenced
 
 219 scalar's content is used as the content to process. The C<type> option
 
 220 is not considered in this case.
 
 222 C<$template> can also be an instance of L<SL::Presenter::EscapedText>
 
 223 or a reference to such an instance. Both of these cases are handled
 
 224 the same way as if C<$template> were a reference to a scalar: its
 
 225 content is processed, and C<type> is not considered.
 
 227 Other reference types, unknown options and unknown arguments to the
 
 228 C<type> option cause the function to L<croak>.
 
 230 The following options are available:
 
 236 The template type. Can be C<html> (the default), C<js> for JavaScript,
 
 237 C<json> for JSON and C<text> for plain text content. Affects only the
 
 238 extension that's added to the file name given with a non-reference
 
 239 C<$template> argument.
 
 243 If trueish (which is also the default) it causes the template/content
 
 244 to be processed by the Template toolkit. Otherwise the
 
 245 template/content is returned as-is.
 
 249 If template processing is requested then the template has access to
 
 250 the following variables:
 
 254 =item * C<AUTH> -- C<$::auth>
 
 256 =item * C<FLASH> -- the flash instance (C<$::form-E<gt>{FLASH}>)
 
 258 =item * C<FORM> -- C<$::form>
 
 260 =item * C<INSTANCE_CONF> -- C<$::instance_conf>
 
 262 =item * C<LOCALE> -- C<$::locale>
 
 264 =item * C<LXCONFIG> -- all parameters from C<config/kivitendo.conf>
 
 265 with the same name they appear in the file (first level is the
 
 266 section, second the actual variable, e.g. C<system.language>,
 
 267 C<features.webdav> etc)
 
 269 =item * C<LXDEBUG> -- C<$::lxdebug>
 
 271 =item * C<MYCONFIG> -- C<%::myconfig>
 
 273 =item * C<SELF> -- the controller instance
 
 275 =item * C<PRESENTER> -- the presenter instance the template is
 
 278 =item * All items from C<%locals>
 
 282 The function will always return the output and never send anything to
 
 285 Example: Render a HTML template with a certain title and a few locals
 
 287   $presenter->render('todo/list',
 
 288                      title      => 'List TODO items',
 
 289                      TODO_ITEMS => SL::DB::Manager::Todo->get_all_sorted);
 
 291 Example: Render a string and return its content for further processing
 
 292 by the calling function.
 
 294   my $content = $presenter->render(\'[% USE JavaScript %][% JavaScript.replace_with("#someid", "js/something") %]');
 
 296 Example: Return the content of a JSON template file without processing
 
 299   my $template_content = $presenter->render(
 
 301     { type => 'json', process => 0 }
 
 304 =item C<escape $text>
 
 306 Returns an HTML-escaped version of C<$text>. Instead of a string an
 
 307 instance of the thin proxy-object L<SL::Presenter::EscapedText> is
 
 310 It is safe to call C<escape> on an instance of
 
 311 L<SL::Presenter::EscapedText>. This is a no-op (the same instance will
 
 314 =item C<escaped_text $text>
 
 316 Returns an instance of L<SL::Presenter::EscapedText>. C<$text> is
 
 317 assumed to be a string that has already been HTML-escaped.
 
 319 It is safe to call C<escaped_text> on an instance of
 
 320 L<SL::Presenter::EscapedText>. This is a no-op (the same instance will
 
 323 =item C<escape_js $text>
 
 325 Returns a JavaScript-escaped version of C<$text>. Instead of a string
 
 326 an instance of the thin proxy-object L<SL::Presenter::EscapedText> is
 
 329 It is safe to call C<escape> on an instance of
 
 330 L<SL::Presenter::EscapedText>. This is a no-op (the same instance will
 
 333 =item C<get_template>
 
 335 Returns the global instance of L<Template> and creates it if it
 
 336 doesn't exist already.
 
 342 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>