package WebGUI::Template; #------------------------------------------------------------------- # WebGUI is Copyright 2001-2002 Plain Black LLC. #------------------------------------------------------------------- # Please read the legal notices (docs/legal.txt) and the license # (docs/license.txt) that came with this distribution before using # this software. #------------------------------------------------------------------- # http://www.plainblack.com info@plainblack.com #------------------------------------------------------------------- use HTML::Template; use strict; use Tie::IxHash; use WebGUI::ErrorHandler; use WebGUI::HTMLForm; use WebGUI::Session; use WebGUI::SQL; #------------------------------------------------------------------- sub _newPositionFormat { return ""; } #------------------------------------------------------------------- sub countPositions { my ($template, $i); $template = get($_[0]); $i = 1; while ($template =~ m/template\.position$i/) { $i++; } return $i-1; } #------------------------------------------------------------------- sub draw { my $template = get($_[0]); $template =~ s/\n//g; $template =~ s/\r//g; $template =~ s/\'/\\\'/g; $template =~ s/\/\/ig; $template =~ s/\/$1/ig; return $template; } #------------------------------------------------------------------- sub get { my $templateId = $_[0] || 1; my $namespace = $_[1] || "Page"; my ($template) = WebGUI::SQL->quickArray("select template from template where templateId=".$templateId." and namespace=".quote($namespace)); $template =~ s/\^(\d+)\;/_newPositionFormat($1)/eg; #compatibility with old-style templates return $template; } #------------------------------------------------------------------- sub getList { my (%list); tie %list, 'Tie::IxHash'; %list = WebGUI::SQL->buildHash("select templateId,name from template where namespace='Page' order by name"); return \%list; } #------------------------------------------------------------------- sub getPositions { my (%hash, $template, $i); tie %hash, "Tie::IxHash"; for ($i=1; $i<=countPositions($_[0]); $i++) { $hash{$i} = $i; } return \%hash; } #------------------------------------------------------------------- sub process { my ($t, $html); $html = $_[0]; $t = HTML::Template->new( scalarref=>\$html, loop_context_vars=>1, die_on_bad_params=>0, strict=>0 ); while (my ($section, $hash) = each %session) { while (my ($key, $value) = each %$hash) { if (ref $value eq 'ARRAY') { next; #$value = '['.join(', ',@$value).']'; } elsif (ref $value eq 'HASH') { next; #$value = '{'.join(', ',map {"$_ => $value->{$_}"} keys %$value).'}'; } unless (lc($key) eq "password" || lc($key) eq "identifier") { $t->param($section.".".$key=>$value); } } } $t->param(%{$_[1]}); $t->param("webgui.version"=>$WebGUI::VERSION); return $t->output; } #------------------------------------------------------------------- sub select { my ($templates, $output, $f, $key); $f = WebGUI::HTMLForm->new(1); $templates = getList(); $f->select("templateId",$templates,'',[$_[0]],'','','onChange="changeTemplatePreview(this.form.templateId.value)"'); $output = ' '; $output .= $f->printRowsOnly; $output .= '
'; return $output; } 1;