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::RequirementSpec;
 
  19 use SL::Presenter::RequirementSpecItem;
 
  20 use SL::Presenter::RequirementSpecTextBlock;
 
  21 use SL::Presenter::SepaExport;
 
  22 use SL::Presenter::Text;
 
  23 use SL::Presenter::Tag;
 
  25 use Rose::Object::MakeMethods::Generic (
 
  26   scalar => [ qw(need_reinit_widgets) ],
 
  30   return $::request->presenter;
 
  36   my ($options, %locals) = (@_ && ref($_[0])) ? @_ : ({ }, @_);
 
  38   # Set defaults for all available options.
 
  43   $options->{$_} //= $defaults{$_} for keys %defaults;
 
  44   $options->{type} = lc $options->{type};
 
  46   # Check supplied options for validity.
 
  47   foreach (keys %{ $options }) {
 
  48     croak "Unsupported option: $_" unless $defaults{$_};
 
  51   # Only certain types are supported.
 
  52   croak "Unsupported type: " . $options->{type} unless $options->{type} =~ m/^(?:html|js|json|text)$/;
 
  54   # The "template" argument must be a string or a reference to one.
 
  55   $template = ${ $template }                                       if ((ref($template) || '') eq 'REF') && (ref(${ $template }) eq 'SL::Presenter::EscapedText');
 
  56   croak "Unsupported 'template' reference type: " . ref($template) if ref($template) && (ref($template) !~ m/^(?:SCALAR|SL::Presenter::EscapedText)$/);
 
  58   # Look for the file given by $template if $template is not a reference.
 
  61     my $ext = $options->{type} eq 'text' ? 'txt' : $options->{type};
 
  62     $source = "templates/webpages/${template}.${ext}";
 
  63     croak "Template file ${source} not found" unless -f $source;
 
  65   } elsif (ref($template) eq 'SCALAR') {
 
  66     # Normal scalar reference: hand over to Template
 
  70     # Instance of SL::Presenter::EscapedText. Get reference to its content.
 
  71     $source = \$template->{text};
 
  74   # If no processing is requested then return the content.
 
  75   if (!$options->{process}) {
 
  76     # If $template is a reference then don't try to read a file.
 
  77     my $ref = ref $template;
 
  78     return $template                                                                if $ref eq 'SL::Presenter::EscapedText';
 
  79     return SL::Presenter::EscapedText->new(text => ${ $template }, is_escaped => 1) if $ref eq 'SCALAR';
 
  81     # Otherwise return the file's content.
 
  82     my $file    = IO::File->new($source, "r") || croak("Template file ${source} could not be read");
 
  83     my $content = do { local $/ = ''; <$file> };
 
  86     return SL::Presenter::EscapedText->new(text => $content, is_escaped => 1);
 
  89   # Processing was requested. Set up all variables.
 
  90   my %params = ( %locals,
 
  92                  FLASH         => $::form->{FLASH},
 
  94                  INSTANCE_CONF => $::instance_conf,
 
  96                  LXCONFIG      => \%::lx_office_conf,
 
  97                  LXDEBUG       => $::lxdebug,
 
  98                  MYCONFIG      => \%::myconfig,
 
 103   my $parser = $self->get_template;
 
 104   $parser->process($source, \%params, \$output) || croak $parser->error;
 
 106   return SL::Presenter::EscapedText->new(text => $output, is_escaped => 1);
 
 112   $self->{template} ||=
 
 113     Template->new({ INTERPOLATE  => 0,
 
 117                     PLUGIN_BASE  => 'SL::Template::Plugin',
 
 118                     INCLUDE_PATH => '.:templates/webpages',
 
 119                     COMPILE_EXT  => '.tcc',
 
 120                     COMPILE_DIR  => $::lx_office_conf{paths}->{userspath} . '/templates-cache',
 
 121                     ERROR        => 'templates/webpages/generic/exception.html',
 
 125   return $self->{template};
 
 129   my ($self, $text) = @_;
 
 131   return SL::Presenter::EscapedText->new(text => $text);
 
 135   my ($self, $text) = @_;
 
 137   return SL::Presenter::EscapedText->new(text => $text, is_escaped => 1);
 
 141   my ($self, $text) = @_;
 
 143   $text =~ s|\\|\\\\|g;
 
 144   $text =~ s|\"|\\\"|g;
 
 147   return SL::Presenter::EscapedText->new(text => $text, is_escaped => 1);
 
 156 SL::Presenter - presentation layer class
 
 161   my $presenter = SL::Presenter->get;
 
 163   # Lower-level template parsing:
 
 164   my $html = $presenter->render(
 
 165     'presenter/dir/template.html',
 
 169   # Higher-level rendering of certain objects:
 
 170   use SL::DB::Customer;
 
 172   my $linked_customer_name = $presenter->customer($customer, display => 'table-cell');
 
 174   # Render a list of links to sales/purchase records:
 
 177   my $quotation = SL::DB::Manager::Order->get_first(where => { quotation => 1 });
 
 178   my $records   = $quotation->linked_records(direction => 'to');
 
 179   my $html      = $presenter->grouped_record_list($records);
 
 181 =head1 CLASS FUNCTIONS
 
 187 Returns the global presenter object and creates it if it doesn't exist
 
 192 =head1 INSTANCE FUNCTIONS
 
 196 =item C<render $template, [ $options, ] %locals>
 
 198 Renders the template C<$template>. Provides other variables than
 
 199 C<Form::parse_html_template> does.
 
 201 C<$options>, if present, must be a hash reference. All remaining
 
 202 parameters are slurped into C<%locals>.
 
 204 This is the backend function that L<SL::Controller::Base/render>
 
 205 calls. The big difference is that the presenter's L<render> function
 
 206 always returns the input and never sends anything to the browser while
 
 207 the controller's function usually sends the result to the
 
 208 controller. Therefore the presenter's L<render> function does not use
 
 209 all of the parameters for controlling the output that the controller's
 
 212 What is rendered and how C<$template> is interpreted is determined
 
 213 both by C<$template>'s reference type and by the supplied options.
 
 215 If C<$template> is a normal scalar (not a reference) then it is meant
 
 216 to be a template file name relative to the C<templates/webpages>
 
 217 directory. The file name to use is determined by the C<type> option.
 
 219 If C<$template> is a reference to a scalar then the referenced
 
 220 scalar's content is used as the content to process. The C<type> option
 
 221 is not considered in this case.
 
 223 C<$template> can also be an instance of L<SL::Presenter::EscapedText>
 
 224 or a reference to such an instance. Both of these cases are handled
 
 225 the same way as if C<$template> were a reference to a scalar: its
 
 226 content is processed, and C<type> is not considered.
 
 228 Other reference types, unknown options and unknown arguments to the
 
 229 C<type> option cause the function to L<croak>.
 
 231 The following options are available:
 
 237 The template type. Can be C<html> (the default), C<js> for JavaScript,
 
 238 C<json> for JSON and C<text> for plain text content. Affects only the
 
 239 extension that's added to the file name given with a non-reference
 
 240 C<$template> argument.
 
 244 If trueish (which is also the default) it causes the template/content
 
 245 to be processed by the Template toolkit. Otherwise the
 
 246 template/content is returned as-is.
 
 250 If template processing is requested then the template has access to
 
 251 the following variables:
 
 255 =item * C<AUTH> -- C<$::auth>
 
 257 =item * C<FLASH> -- the flash instance (C<$::form-E<gt>{FLASH}>)
 
 259 =item * C<FORM> -- C<$::form>
 
 261 =item * C<INSTANCE_CONF> -- C<$::instance_conf>
 
 263 =item * C<LOCALE> -- C<$::locale>
 
 265 =item * C<LXCONFIG> -- all parameters from C<config/kivitendo.conf>
 
 266 with the same name they appear in the file (first level is the
 
 267 section, second the actual variable, e.g. C<system.language>,
 
 268 C<features.webdav> etc)
 
 270 =item * C<LXDEBUG> -- C<$::lxdebug>
 
 272 =item * C<MYCONFIG> -- C<%::myconfig>
 
 274 =item * C<SELF> -- the controller instance
 
 276 =item * C<PRESENTER> -- the presenter instance the template is
 
 279 =item * All items from C<%locals>
 
 283 The function will always return the output and never send anything to
 
 286 Example: Render a HTML template with a certain title and a few locals
 
 288   $presenter->render('todo/list',
 
 289                      title      => 'List TODO items',
 
 290                      TODO_ITEMS => SL::DB::Manager::Todo->get_all_sorted);
 
 292 Example: Render a string and return its content for further processing
 
 293 by the calling function.
 
 295   my $content = $presenter->render(\'[% USE JavaScript %][% JavaScript.replace_with("#someid", "js/something") %]');
 
 297 Example: Return the content of a JSON template file without processing
 
 300   my $template_content = $presenter->render(
 
 302     { type => 'json', process => 0 }
 
 305 =item C<escape $text>
 
 307 Returns an HTML-escaped version of C<$text>. Instead of a string an
 
 308 instance of the thin proxy-object L<SL::Presenter::EscapedText> is
 
 311 It is safe to call C<escape> on an instance of
 
 312 L<SL::Presenter::EscapedText>. This is a no-op (the same instance will
 
 315 =item C<escaped_text $text>
 
 317 Returns an instance of L<SL::Presenter::EscapedText>. C<$text> is
 
 318 assumed to be a string that has already been HTML-escaped.
 
 320 It is safe to call C<escaped_text> on an instance of
 
 321 L<SL::Presenter::EscapedText>. This is a no-op (the same instance will
 
 324 =item C<escape_js $text>
 
 326 Returns a JavaScript-escaped version of C<$text>. Instead of a string
 
 327 an instance of the thin proxy-object L<SL::Presenter::EscapedText> is
 
 330 It is safe to call C<escape> on an instance of
 
 331 L<SL::Presenter::EscapedText>. This is a no-op (the same instance will
 
 334 =item C<get_template>
 
 336 Returns the global instance of L<Template> and creates it if it
 
 337 doesn't exist already.
 
 343 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>