Apache-PageKit

 view release on metacpan or  search on metacpan

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

  }

  # record up to date!
  return 1;
}

# here the usage of "component" also includes page
sub _load_component {
  my ($view, $page_id, $component_id, $pkit_view, $component_params) = @_;

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

  unless($template_file){
    # no template file exists, attempt to generate from XML and XSL files
    # currently only XML::LibXSLT is supported
    $template_ref = $view->{content}->generate_template($page_id, $component_id, $pkit_view, $view->{input_param_object}, $component_params);
  } else {
    open TEMPLATE, "<$template_file" or die "can not read $template_file";
    binmode TEMPLATE;
    local($/) = undef;
    my $template = <TEMPLATE>;
    close TEMPLATE;
    
    # expand PKIT_MACRO tags
    $template =~ s!<\s*PKIT_MACRO$key_value_pattern\s*/?>!$component_params->{uc($+)} || ''!egi;

    $template_ref = \$template;

    my $mtime = (stat(_))[9];
    $view->{include_mtimes}->{$template_file} = $mtime;
  }

  if($view->{can_edit} eq 'yes'){
    Apache::PageKit::Edit::add_component_edit_stubs($view, $page_id, $template_ref, $pkit_view);
  }

  $view->_include_components($page_id,$template_ref,$pkit_view);

  return $template_ref;
}

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

  $Apache::PageKit::Content::PAGE_ID_XSL_PARAMS->{$page_id} = {};

  my $content = $view->{content} ||= Apache::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 => $view->{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, $skip_xpath_content ) = $content->process_template($page_id, $template_ref);

  # find the right converter for perl < 5.8.0
  # if we skip the xpath content, the string is in $default_input_charset.
  # otherwise it is in utf8 ( from libxml2 )
  my $converter;
  my $default_output_charset = $view->{default_output_charset};
  if ( $skip_xpath_content ) {
    my $default_input_charset = $view->{default_input_charset};
    unless ( lc $default_input_charset eq lc $default_output_charset) {
      eval {
        $converter = Text::Iconv->new( $default_input_charset, $default_output_charset );
      };
      if ($@) {
        (my $config_dir = $view->{content_dir}) =~ s!/Content$!/Config!;
        die "The conversion from ($default_input_charset => $default_output_charset) is not supported by Text::Iconv please check file ${config_dir}/Config.xml";
      }
    }
    else {
      unless ( /^utf-?8$/i =~ $default_output_charset) {
        eval {
          $converter = Text::Iconv->new( 'utf8', $default_output_charset);
        };
        if ($@) {
          (my $config_dir = $view->{content_dir}) =~ s!/Content$!/Config!;
          die "The conversion from ('utf8' => $default_output_charset) is not supported by Text::Iconv please check file ${config_dir}/Config.xml";
        }
      }
    }
  }

  # 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){

    if ( $converter ) {
      $$filtered_html = $converter->convert($$filtered_html) || die "Can not convert page from 'something' to $default_output_charset" if $$filtered_html;
    }

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

    my $has_form = ($$filtered_html =~ m!<form!i);
    my $tmpl;
    eval {
      $tmpl =  $view->{template_class}->new(scalarref => $filtered_html,
					   # don't die when we set a parameter that is not in the template
					   die_on_bad_params=>0,
					   # built in __FIRST__, __LAST__, etc vars
					   loop_context_vars=>1,
					   max_includes => 50,
					   global_vars=>1);
    };
    if($@){
      die "Can not load template (MODEL TAGS) for $page_id: $@"
    }
    my $record = {
		  exclude_params_set => $exclude_params_set,
		  filename => $template_file,
		  html_template => $tmpl,
		  include_mtimes => $view->{include_mtimes},
		  component_ids => $view->{component_ids},
		  has_form => $has_form,
		 };

    # make directories, if approriate
    (my $dir = $page_id) =~ s!(/)?[^/]*?$!!;

    if($dir){
      File::Path::mkpath("$view->{cache_dir}/$dir");
    }

    my ($extra_param, $param_hash) = ("", "");
    # get a list of requested params in the *.xsl file
    if (my @xml_params = sort keys %{$Apache::PageKit::Content::PAGE_ID_XSL_PARAMS->{$page_id}}) {
      my $param_obj = $view->{input_param_object};

      for my $xml_param (@xml_params){
        my $value = $param_obj->param($xml_param) || '';
	$extra_param .= "&$xml_param=" . $value;
      }
      $param_hash = Digest::MD5::md5_hex($extra_param);
    }

    # Store record
    Storable::lock_store($record, "$view->{cache_dir}/$page_id.$pkit_view.$lang$param_hash");
  }

  # include mtimes and component_ids are filled in by _include_components
  # and _fill_in_content
  delete $view->{include_mtimes};
  delete $view->{lang_tmpl};
  delete $view->{component_ids_hash};
}

sub _preparse_model_tags {
  use bytes;

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

    }
  }

  # 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';
      $fif = HTML::FillInForm->new();
      $output = $fif->fill(
                            scalarref     => \$output,
                            fobject       => $view->{fillinform_objects},
                            ignore_fields => $view->{ignore_fillinform_fields}
      );
    }
  }
  if ( $view->{can_edit} eq 'yes' ) {
    $view->{pkit_pk}->{browser_cache} = 'no';
    Apache::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} ||= Apache::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, $skip_xpath_content ) = $content->process_template($page_id, $template_ref);

  # find the right converter for perl < 5.8.0
  # if we skip the xpath content, the string is in $default_input_charset.
  # otherwise it is in utf8 ( from libxml2 )
  my $converter;
  my $default_output_charset = $view->{default_output_charset};
  if ( $skip_xpath_content ) {
    my $default_input_charset = $view->{default_input_charset};
    unless ( lc $default_input_charset eq lc $default_output_charset) {
      eval {
        $converter = Text::Iconv->new( $default_input_charset, $default_output_charset );
      };
      if ($@) {
        (my $config_dir = $view->{content_dir}) =~ s!/Content$!/Config!;
        die "The conversion from ($default_input_charset => $default_output_charset) is not supported by Text::Iconv please check file ${config_dir}/Config.xml";
      }
    }
    else {
      unless ( /^utf-?8$/i =~ $default_output_charset) {
        eval {
          $converter = Text::Iconv->new( 'utf8', $default_output_charset);
        };
        if ($@) {
          (my $config_dir = $view->{content_dir}) =~ s!/Content$!/Config!;
          die "The conversion from ('utf8' => $default_output_charset) is not supported by Text::Iconv please check file ${config_dir}/Config.xml";
        }
      }
    }
  }

  # 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 ) {

    if ($converter) {
      $$filtered_html = $converter->convert($$filtered_html)
        || die "Can not convert page from 'something' to $default_output_charset"
        if $$filtered_html;
    }

    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;
    my $record = {
                   exclude_params_set => $exclude_params_set,
                   filename           => $template_file,
                   include_mtimes     => $view->{include_mtimes},
                   component_ids      => $view->{component_ids},
                   has_form           => $has_form,
                   filtered_html      => $tt_parser->parse($$filtered_html)   || die $tt_parser->error(),
    };

    # make directories, if approriate
    ( my $dir = $page_id ) =~ s!(/)?[^/]*?$!!;

    if ($dir) {
      File::Path::mkpath("$view->{cache_dir}/$dir");
    }

    my ( $extra_param, $param_hash ) = ( "", "" );

    # get a list of requested params in the *.xsl file
    if ( my @xml_params = sort keys %{ $Apache::PageKit::Content::PAGE_ID_XSL_PARAMS->{$page_id} } ) {
      my $param_obj = $view->{input_param_object};

      for my $xml_param (@xml_params) {
        my $value = $param_obj->param($xml_param) || '';
        $extra_param .= "&$xml_param=" . $value;
      }
      $param_hash = Digest::MD5::md5_hex($extra_param);
    }

    # Store record
    Storable::lock_store( $record, "$view->{cache_dir}/$page_id.$pkit_view.$lang$param_hash" );
  }

  # include mtimes and component_ids are filled in by _include_components
  # and _fill_in_content
  delete $view->{include_mtimes};
  delete $view->{lang_tmpl};
  delete $view->{component_ids_hash};
}

sub _preparse_model_tags {
  use bytes;
  my ( $view, $html_code_ref ) = @_;

  my $exclude_params_set = {};

  # "compile" PageKit templates into HTML::Templates
  if ( $view->{relaxed_parser} eq 'yes' ) {

    # new parser

    # the new parser is a lot more flexible over the old one. it can parse



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