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::SepaExport;
 
  19 use SL::Presenter::Text;
 
  20 use SL::Presenter::Tag;
 
  22 use Rose::Object::MakeMethods::Generic (
 
  23   scalar => [ qw(need_reinit_widgets) ],
 
  27   return $::request->presenter;
 
  33   my ($options, %locals) = (@_ && ref($_[0])) ? @_ : ({ }, @_);
 
  35   # Set defaults for all available options.
 
  40   $options->{$_} //= $defaults{$_} for keys %defaults;
 
  41   $options->{type} = lc $options->{type};
 
  43   # Check supplied options for validity.
 
  44   foreach (keys %{ $options }) {
 
  45     croak "Unsupported option: $_" unless $defaults{$_};
 
  48   # Only certain types are supported.
 
  49   croak "Unsupported type: " . $options->{type} unless $options->{type} =~ m/^(?:html|js|json|text)$/;
 
  51   # The "template" argument must be a string or a reference to one.
 
  52   $template = ${ $template }                                       if ((ref($template) || '') eq 'REF') && (ref(${ $template }) eq 'SL::Presenter::EscapedText');
 
  53   croak "Unsupported 'template' reference type: " . ref($template) if ref($template) && (ref($template) !~ m/^(?:SCALAR|SL::Presenter::EscapedText)$/);
 
  55   # Look for the file given by $template if $template is not a reference.
 
  58     my $ext = $options->{type} eq 'text' ? 'txt' : $options->{type};
 
  59     $source = "templates/webpages/${template}.${ext}";
 
  60     croak "Template file ${source} not found" unless -f $source;
 
  62   } elsif (ref($template) eq 'SCALAR') {
 
  63     # Normal scalar reference: hand over to Template
 
  67     # Instance of SL::Presenter::EscapedText. Get reference to its content.
 
  68     $source = \$template->{text};
 
  71   # If no processing is requested then return the content.
 
  72   if (!$options->{process}) {
 
  73     # If $template is a reference then don't try to read a file.
 
  74     my $ref = ref $template;
 
  75     return $template                                                                if $ref eq 'SL::Presenter::EscapedText';
 
  76     return SL::Presenter::EscapedText->new(text => ${ $template }, is_escaped => 1) if $ref eq 'SCALAR';
 
  78     # Otherwise return the file's content.
 
  79     my $file    = IO::File->new($source, "r") || croak("Template file ${source} could not be read");
 
  80     my $content = do { local $/ = ''; <$file> };
 
  83     return SL::Presenter::EscapedText->new(text => $content, is_escaped => 1);
 
  86   # Processing was requested. Set up all variables.
 
  87   my %params = ( %locals,
 
  89                  FLASH         => $::form->{FLASH},
 
  91                  INSTANCE_CONF => $::instance_conf,
 
  93                  LXCONFIG      => \%::lx_office_conf,
 
  94                  LXDEBUG       => $::lxdebug,
 
  95                  MYCONFIG      => \%::myconfig,
 
 100   my $parser = $self->get_template;
 
 101   $parser->process($source, \%params, \$output) || croak $parser->error;
 
 103   return SL::Presenter::EscapedText->new(text => $output, is_escaped => 1);
 
 109   $self->{template} ||=
 
 110     Template->new({ INTERPOLATE  => 0,
 
 114                     PLUGIN_BASE  => 'SL::Template::Plugin',
 
 115                     INCLUDE_PATH => '.:templates/webpages',
 
 116                     COMPILE_EXT  => '.tcc',
 
 117                     COMPILE_DIR  => $::lx_office_conf{paths}->{userspath} . '/templates-cache',
 
 118                     ERROR        => 'templates/webpages/generic/exception.html',
 
 121   return $self->{template};
 
 125   my ($self, $text) = @_;
 
 127   return SL::Presenter::EscapedText->new(text => $text);
 
 131   my ($self, $text) = @_;
 
 133   return SL::Presenter::EscapedText->new(text => $text, is_escaped => 1);
 
 137   my ($self, $text) = @_;
 
 139   $text =~ s|\\|\\\\|g;
 
 140   $text =~ s|\"|\\\"|g;
 
 143   return SL::Presenter::EscapedText->new(text => $text, is_escaped => 1);
 
 152 SL::Presenter - presentation layer class
 
 157   my $presenter = SL::Presenter->get;
 
 159   # Lower-level template parsing:
 
 160   my $html = $presenter->render(
 
 161     'presenter/dir/template.html',
 
 165   # Higher-level rendering of certain objects:
 
 166   use SL::DB::Customer;
 
 168   my $linked_customer_name = $presenter->customer($customer, display => 'table-cell');
 
 170   # Render a list of links to sales/purchase records:
 
 173   my $quotation = SL::DB::Manager::Order->get_first(where => { quotation => 1 });
 
 174   my $records   = $quotation->linked_records(direction => 'to');
 
 175   my $html      = $presenter->grouped_record_list($records);
 
 177 =head1 CLASS FUNCTIONS
 
 183 Returns the global presenter object and creates it if it doesn't exist
 
 188 =head1 INSTANCE FUNCTIONS
 
 192 =item C<render $template, [ $options, ] %locals>
 
 194 Renders the template C<$template>. Provides other variables than
 
 195 C<Form::parse_html_template> does.
 
 197 C<$options>, if present, must be a hash reference. All remaining
 
 198 parameters are slurped into C<%locals>.
 
 200 This is the backend function that L<SL::Controller::Base/render>
 
 201 calls. The big difference is that the presenter's L<render> function
 
 202 always returns the input and never sends anything to the browser while
 
 203 the controller's function usually sends the result to the
 
 204 controller. Therefore the presenter's L<render> function does not use
 
 205 all of the parameters for controlling the output that the controller's
 
 208 What is rendered and how C<$template> is interpreted is determined
 
 209 both by C<$template>'s reference type and by the supplied options.
 
 211 If C<$template> is a normal scalar (not a reference) then it is meant
 
 212 to be a template file name relative to the C<templates/webpages>
 
 213 directory. The file name to use is determined by the C<type> option.
 
 215 If C<$template> is a reference to a scalar then the referenced
 
 216 scalar's content is used as the content to process. The C<type> option
 
 217 is not considered in this case.
 
 219 C<$template> can also be an instance of L<SL::Presenter::EscapedText>
 
 220 or a reference to such an instance. Both of these cases are handled
 
 221 the same way as if C<$template> were a reference to a scalar: its
 
 222 content is processed, and C<type> is not considered.
 
 224 Other reference types, unknown options and unknown arguments to the
 
 225 C<type> option cause the function to L<croak>.
 
 227 The following options are available:
 
 233 The template type. Can be C<html> (the default), C<js> for JavaScript,
 
 234 C<json> for JSON and C<text> for plain text content. Affects only the
 
 235 extension that's added to the file name given with a non-reference
 
 236 C<$template> argument.
 
 240 If trueish (which is also the default) it causes the template/content
 
 241 to be processed by the Template toolkit. Otherwise the
 
 242 template/content is returned as-is.
 
 246 If template processing is requested then the template has access to
 
 247 the following variables:
 
 251 =item * C<AUTH> -- C<$::auth>
 
 253 =item * C<FLASH> -- the flash instance (C<$::form-E<gt>{FLASH}>)
 
 255 =item * C<FORM> -- C<$::form>
 
 257 =item * C<INSTANCE_CONF> -- C<$::instance_conf>
 
 259 =item * C<LOCALE> -- C<$::locale>
 
 261 =item * C<LXCONFIG> -- all parameters from C<config/kivitendo.conf>
 
 262 with the same name they appear in the file (first level is the
 
 263 section, second the actual variable, e.g. C<system.language>,
 
 264 C<features.webdav> etc)
 
 266 =item * C<LXDEBUG> -- C<$::lxdebug>
 
 268 =item * C<MYCONFIG> -- C<%::myconfig>
 
 270 =item * C<SELF> -- the controller instance
 
 272 =item * C<PRESENTER> -- the presenter instance the template is
 
 275 =item * All items from C<%locals>
 
 279 The function will always return the output and never send anything to
 
 282 Example: Render a HTML template with a certain title and a few locals
 
 284   $presenter->render('todo/list',
 
 285                      title      => 'List TODO items',
 
 286                      TODO_ITEMS => SL::DB::Manager::Todo->get_all_sorted);
 
 288 Example: Render a string and return its content for further processing
 
 289 by the calling function.
 
 291   my $content = $presenter->render(\'[% USE JavaScript %][% JavaScript.replace_with("#someid", "js/something") %]');
 
 293 Example: Return the content of a JSON template file without processing
 
 296   my $template_content = $presenter->render(
 
 298     { type => 'json', process => 0 }
 
 301 =item C<escape $text>
 
 303 Returns an HTML-escaped version of C<$text>. Instead of a string an
 
 304 instance of the thin proxy-object L<SL::Presenter::EscapedText> is
 
 307 It is safe to call C<escape> on an instance of
 
 308 L<SL::Presenter::EscapedText>. This is a no-op (the same instance will
 
 311 =item C<escaped_text $text>
 
 313 Returns an instance of L<SL::Presenter::EscapedText>. C<$text> is
 
 314 assumed to be a string that has already been HTML-escaped.
 
 316 It is safe to call C<escaped_text> on an instance of
 
 317 L<SL::Presenter::EscapedText>. This is a no-op (the same instance will
 
 320 =item C<escape_js $text>
 
 322 Returns a JavaScript-escaped version of C<$text>. Instead of a string
 
 323 an instance of the thin proxy-object L<SL::Presenter::EscapedText> is
 
 326 It is safe to call C<escape> on an instance of
 
 327 L<SL::Presenter::EscapedText>. This is a no-op (the same instance will
 
 330 =item C<get_template>
 
 332 Returns the global instance of L<Template> and creates it if it
 
 333 doesn't exist already.
 
 339 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>