Apache2-PageKit

 view release on metacpan or  search on metacpan

lib/Apache2/PageKit/View.pm  view on Meta::CPAN

  my ($view) = @_;

  # load record containing HTML::Template object
  my $record = $view->{record};

  my $tmpl = $record->{html_template};

  # Fill in (compiled) <PKIT_SELFURL> tags
  my $exclude_params_set = $record->{exclude_params_set};
  if($exclude_params_set && @$exclude_params_set){
    my $input_param_object = $view->{input_param_object};
    my $orig_uri = $input_param_object->notes->get('orig_uri');
    foreach my $exclude_params (@$exclude_params_set){
      my @exclude_params = split(" ",$exclude_params);
      my $query_string = Apache2::PageKit::params_as_string($input_param_object, \@exclude_params);

      #remove empty parameters as arised from http://ka.zyx.de/galerie?show=abc& or <PKIT_SELFURL>
      $query_string =~ s![?&]$!!;
      if($query_string){
	$tmpl->param("pkit_selfurl$exclude_params", ($orig_uri . '?' . $query_string) . '&');
      } else {
	$tmpl->param("pkit_selfurl$exclude_params", $orig_uri . '?');
      }
    }
  }
  # fill in data from associated objects (for example from the Apache request
  # object if $apr is set
  foreach my $object (@{$view->{associated_objects}}){
    foreach my $key ($object->param){
      # note that we only fill in MODEL_VARs, to avoid errors when setting
      # loops in HTML::Template
      my $type = $tmpl->query(name => $key);
      if ( $type && $type eq 'VAR' ) {
        $view->{pkit_pk}->{browser_cache} = 'no';
        # we need a separate variable for value to force scalar context
	# for multivalued params http://www.xx.yy/a?foo=12&foo=13
        my $value = $object->param($key);
	$tmpl->param($key, $value);
      }
    }
  }

  # finally, we use the $output_param_object object to fill in template
  # get params from $view object
  # note that in this case we allow for MODEL_LOOPs as well as MODEL_VARs
  my $output_param_object = $view->{output_param_object};
  foreach my $key ($output_param_object->param){
    my $value = $output_param_object->param($key);
    $view->{pkit_pk}->{browser_cache} = 'no';
    $tmpl->param($key, $value);
  }

  my $output = $tmpl->output;

  if($record->{has_form}){
    # if fillinform_objects is set, then we use that to fill in any HTML
    # forms in the template.
    my $fif;
    if(@{$view->{fillinform_objects}}){
      $view->{pkit_pk}->{browser_cache} = 'no';
      Encode::_utf8_off($output);
      $fif = HTML::FillInForm->new();
      $output = $fif->fill(scalarref => \$output,
                           fobject   => $view->{fillinform_objects},
			   ignore_fields => $view->{ignore_fillinform_fields}
			  );
      Encode::_utf8_on($output);
    }
  }
  if($view->{can_edit} eq 'yes'){
    $view->{pkit_pk}->{browser_cache} = 'no';
    Apache2::PageKit::Edit::add_edit_links($view, $record, \$output);
  }
  return \$output;
}

# gets static gzipped file, creating it if necessary
sub get_static_gzip {
  my ($view, $filename) = @_;
  my ($gzip_mtime, $gzipped_content);

  (my $relative_filename = $filename) =~ s!^$view->{view_dir}/!!;
  my $gzipped_filename = "$view->{cache_dir}/$relative_filename.gz";

  # is the cache entry valid or changed on disc?
  if(-f "$gzipped_filename"){
    open my $fh, "<$gzipped_filename" or return undef;
    binmode $fh;
    # read mtime from first line
    chomp($gzip_mtime = <$fh>);

    # read rest of gzipped content
    local $/;
    $gzipped_content = <$fh>;
    close $fh;
    if($view->{reload} ne 'no'){
      # is the cache entry valid or changed on disc?
      my $mtime = ( stat($filename) )[9];
      if($mtime != $gzip_mtime){
	$gzipped_content = $view->_create_static_zip($filename, $gzipped_filename);
      }
    }
  } else {
    $gzipped_content = $view->_create_static_zip($filename, $gzipped_filename);
  }
  return $gzipped_content;
}

# opens template, each 
sub open_view {
  my ($view, $page_id, $pkit_view, $lang) = @_;

  return if exists $view->{already_loaded}->{$page_id};

  my $record = $view->_fetch_from_file_cache($page_id, $pkit_view, $lang);
  unless($record){
    # template not cached, load now
    $view->_load_page($page_id, $pkit_view);
    $record = $view->_fetch_from_file_cache($page_id, $pkit_view, $lang);
    die "Error loading record for page_id $page_id and view $pkit_view"
      unless $record;
  }

  if($view->{reload} ne 'no'){
    # check for updated files on disk
    unless($view->_is_record_uptodate($record, $pkit_view, $page_id)){
      # one of the included files changed on disk, reload

lib/Apache2/PageKit/View.pm  view on Meta::CPAN


sub fill_in_view {
  my ($view) = @_;

  # load record containing Template Toolkit object
  my $record = $view->{record};

  my %tt_params;

  # Fill in (compiled) <PKIT_SELFURL> tags
  my $exclude_params_set = $record->{exclude_params_set};
  if ( $exclude_params_set && @$exclude_params_set ) {
    my $input_param_object = $view->{input_param_object};
    my $orig_uri           = $input_param_object->notes->get('orig_uri');
    foreach my $exclude_params (@$exclude_params_set) {
      my @exclude_params = split ( " ", $exclude_params );
      my $query_string = Apache2::PageKit::params_as_string( $input_param_object, \@exclude_params );

      #remove empty parameters as arised from http://ka.zyx.de/galerie?show=abc& or <PKIT_SELFURL>
      $query_string =~ s![?&]$!!;
      if ($query_string) {
        $tt_params{"pkit_selfurl$exclude_params"} = ( $orig_uri . '?' . $query_string ) . '&';
      }
      else {
        $tt_params{"pkit_selfurl$exclude_params"} = $orig_uri . '?';
      }
    }
  }

  # fill in data from associated objects (for example from the Apache request
  # object if $apr is set
  foreach my $object ( @{ $view->{associated_objects} } ) {
    foreach my $key ( $object->param ) {  
      $view->{pkit_pk}->{browser_cache} = 'no';
      # we need a separate variable for value to force scalar context
      # for multivalued params http://www.xx.yy/a?foo=12&foo=13
      my $value = $object->param($key);
      $tt_params{$key} = $value;
    }
  }

  # finally, we use the $output_param_object object to fill in template
  # get params from $view object
  # note that in this case we allow for MODEL_LOOPs as well as MODEL_VARs
  my $output_param_object = $view->{output_param_object};
  foreach my $key ( $output_param_object->param ) {
    my $value = $output_param_object->param($key);
    $view->{pkit_pk}->{browser_cache} = 'no';
    $tt_params{$key} = $value;
  }

  my $output = Template::Context->new->process(Template::Document->new( $record->{filtered_html} ), \%tt_params );

  if ( $record->{has_form} ) {

    # if fillinform_objects is set, then we use that to fill in any HTML
    # forms in the template.
    my $fif;
    if ( @{ $view->{fillinform_objects} } ) {
      $view->{pkit_pk}->{browser_cache} = 'no';
      Encode::_utf8_off($output);
      $fif = HTML::FillInForm->new();
      $output = $fif->fill(
                            scalarref     => \$output,
                            fobject       => $view->{fillinform_objects},
                            ignore_fields => $view->{ignore_fillinform_fields}
      );
      Encode::_utf8_on($output);      
    }
  }
  if ( $view->{can_edit} eq 'yes' ) {
    $view->{pkit_pk}->{browser_cache} = 'no';
    Apache2::PageKit::Edit::add_edit_links( $view, $record, \$output );
  }
  return \$output;
}

######################################################

sub _load_page {
  my ( $view, $page_id, $pkit_view ) = @_;

  # solange HTML::Template::XPath noch nichts mit Tempate Toolkit anfangen kann,
  # HTML::Template verwenden.
  my $content_template_class = $view->{template_class};
  $content_template_class = 'HTML::Template' if $content_template_class eq 'Template';

  my $content = $view->{content} ||= Apache2::PageKit::Content->new(
                                                                    content_dir    => $view->{content_dir},
                                                                    view_dir       => $view->{view_dir},
                                                                    default_lang   => $view->{default_lang},
                                                                    relaxed_parser => $view->{relaxed_parser},
                                                                    template_class => $content_template_class,
  );

  $view->{lang_tmpl} = $content->{lang_tmpl} = {};
  $content->{include_mtimes}  = {};
  $view->{component_ids_hash} = {};

  # we add Config.xml to the hash of files to be checked for mtimes,
  # in case default_input_charset or default_output_charset changes!
  ( my $config_file = $view->{view_dir} ) =~ s!/View$!/Config/Config.xml!;
  my $config_mtime = ( stat($config_file) )[9];
  $view->{include_mtimes} = { $config_file => $config_mtime };

  my $template_file = $view->_find_template( $pkit_view, $page_id );
  my $template_ref = $view->_load_component( $page_id, $page_id, $pkit_view );

  # remove PKIT_COMMENT parts.
  my $pkit_comment_re = $re_helper{ $view->{relaxed_parser} eq 'yes' ? 'relaxed_parser' : 'std_parser' }->{pkit_comment_re};
  $$template_ref =~ s/$pkit_comment_re//sgi;

  #  my $template_file = $view->_find_template($pkit_view, $page_id);
  my $lang_tmpl = $content->process_template( $page_id, $template_ref );

  # add used content file(s) to the mtimes hash
  while ( my ( $file, $mtime ) = each( %{ $content->{include_mtimes} } ) ) {
    $view->{include_mtimes}->{$file} = $mtime;
  }

  # go through content files (which have had content filled in)
  while ( my ( $lang, $filtered_html ) = each %$lang_tmpl ) {

    my $exclude_params_set = $view->_preparse_model_tags($filtered_html);
    $view->_html_clean($filtered_html);

    my $has_form  = ( $$filtered_html =~ m!<form!i );
    my $tt_parser = Template::Parser->new;



( run in 1.126 second using v1.01-cache-2.11-cpan-39bf76dae61 )