|
From: <ix...@us...> - 2001-12-02 12:42:44
|
ixjonez 01/12/02 04:42:43
Modified: lib/LiveFrame Tag: 0`¶@h¶@h¶@È È È àt P P
°¦ ÈÀ ¶@¶@¶@¶@ ¶@ ¶@
°¶@°¶@¸¶@¸¶@`® `®
ȶ@ȶ@ж@ж@ض@ض@à¶@à¶@è¶@è¶@ð¶@ð¶@ø¶@ø¶@
/tmp/cvs-serv1406/lib/LiveFrame/Application.pm No
tag Application.pm
Log:
various fixs related to forms and the run() method. configure a PLUGIN_BASE
so templates can find plugins. make error page handling a bit more robust.
Revision Changes Path
No revision
No revision
No revision
No revision
No revision
No revision
No revision
No revision
No revision
No revision
No revision
No revision
No revision
No revision
1.4 +86 -35 lab/lib/LiveFrame/Application.pm
Index: /tmp/cvs-serv1406/lib/LiveFrame/Application.pm
===================================================================
RCS file: /cvsroot/liveframe/lab/lib/LiveFrame/Application.pm,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- /tmp/cvs-serv1406/lib/LiveFrame/Application.pm 2001/11/30 02:25:16 1.3
+++ /tmp/cvs-serv1406/lib/LiveFrame/Application.pm 2001/12/02 12:42:42 1.4
@@ -21,6 +21,7 @@
actions => {},
attrs => {},
cgi => undef,
+ committed => undef,
config => undef,
config_processed => undef,
config_file => undef,
@@ -172,11 +173,7 @@
my $self = shift;
if (@_) {
- my $name = shift;
- unless ($self->action($name)) {
- die "current action $name is not defined\n";
- }
- $self->{current_action_name} = $name;
+ $self->{current_action_name} = shift;
} elsif (! defined $self->{current_action_name}) {
my $path_info = $self->cgi()->path_info();
if ($path_info) {
@@ -299,6 +296,7 @@
my $tproc_opts =
{
INCLUDE_PATH => $self->template_dir(),
+ PLUGIN_BASE => 'LiveFrame::Template::Plugin',
};
$self->{tproc} = Template->new($tproc_opts);
@@ -341,23 +339,44 @@
for my $name ($self->attrs()) {
$tparams->{$name} = $self->attr($name);
}
+
+ $tparams->{application} = $self;
+ $tparams->{config} = $self->config();
+ $tparams->{cgi} = $self->cgi();
+ $tparams->{page} = $page;
+
+ my $current_form_name = $self->current_form_name() || '';
+ if ($current_form_name eq $page) {
+ my $form = $self->current_form();
+ if ($form) {
+ $tparams->{form} = $form;
+ }
+ }
- $tparams->{self_url} = $self->url($cgi->script_name());
- $tparams->{img_url} = $self->url($config->img_url());
- $tparams->{css_url} = $self->url($config->css_url());
+ $tparams->{self_url} = $self->url($cgi->script_name());
+ $tparams->{img_url} = $self->url($config->img_url());
+ $tparams->{css_url} = $self->url($config->css_url());
$tproc->process($self->template($page), $tparams) or do {
- $self->send_tmpl_error($tproc->error());
+ $self->send_tmpl_error($tproc->error(), $page);
};
return 1;
}
+sub send_header {
+ my ($self) = @_;
+
+ print $self->cgi()->header() unless $self->{committed};
+ $self->{committed} = 1;
+
+ return 1;
+}
+
sub send_page {
my ($self, $page) = @_;
-
- print $self->cgi()->header();
+ $self->send_header();
return $self->process_template($page);
}
@@ -371,13 +390,23 @@
}
sub send_error {
- my ($self, $msg) = @_;
+ my ($self, $msg, $tmpl) = @_;
- print $self->cgi()->header();
+ $self->send_header();
my $error_page = $self->default_error_page();
if (defined $error_page) {
- return $self->process_template($error_page, {error_msg => $msg});
+ my $tparams = { error_msg => $msg };
+
+ if (defined $tmpl) {
+ $tparams->{error_type} = 'template';
+ $tparams->{error_tmpl} = $tmpl;
+ }
+ else {
+ $tparams->{error_type} = 'application';
+ }
+
+ return $self->process_template($error_page, $tparams);
}
$msg = HTML::Entities::encode_entities($msg);
@@ -386,6 +415,9 @@
<p>
<b>Application Error</b>
</p>
+<p>
+An internal application error was encountered:
+</p>
<pre>
$msg
</pre>
@@ -395,20 +427,34 @@
}
sub send_tmpl_error {
- my ($self, $error) = @_;
+ my ($self, $error, $page) = @_;
my $msg = $error->info() || '';
$msg =~ s|\\n|\n|g;
- $msg = HTML::Entities::encode_entities($msg);
+ my $tmpl = $self->template($page);
- print <<EOT;
+ eval { $self->send_error($msg, $tmpl) };
+ if ($@) {
+ $msg = HTML::Entities::encode_entities($msg);
+
+ print <<EOT;
<p>
<b>Template Error</b>
</p>
+<p>
+An error was encountered processing <i>$tmpl</i>:
+</p>
<pre>
$msg
</pre>
+<p>
+Additionally, an error was encountered processing the default error page:
+</p>
+<pre>
+$@
+</pre>
EOT
+ }
return 1;
}
@@ -544,7 +590,7 @@
$self->fill_in_form($form);
# XXX: requires the previous action to have the same name
- # as the form in the page that it sends
+ # as the form in the page htat it sends
$self->current_action_name($form->name());
$action = $self->current_action();
}
@@ -554,27 +600,32 @@
# XXX: how to signal that an action requires a form submission?
}
- # execute the action method, which will send a page when it
- # completes
- eval { $action->perform($self) };
- if ($@) {
- $self->send_error($@);
+ # if the action wasn't unset (usually by an invalid form
+ # submission), execute the action method, which will send a
+ # page when it completes
+ if ($action) {
+ eval { $action->perform($self) };
+ if ($@) {
+ $self->send_error($@);
+ }
}
}
- else {
+
+ unless ($action) {
# no action exists for the specified action name so assume
# it's a page name
- my $msg = sprintf("requested action [%s] not defined",
- $self->current_action_name());
- $self->send_error($msg);
- return 0;
- # XXX: set a config switch to enable page sending?
-# eval {
-# $self->send_page($self->current_action_name());
-# };
-# if ($@) {
-# $self->send_error($@);
-# }
+ # XXX: set a config switch to disable page sending?
+# my $msg = sprintf("requested action [%s] not defined",
+# $self->current_action_name());
+# $self->send_error($msg);
+# return 0;
+ eval {
+ $self->send_page($self->current_action_name() ||
+ $self->default_action_name());
+ };
+ if ($@) {
+ $self->send_error($@);
+ }
}
return 0;
No revision
No revision
1.4 +86 -35 lab/lib/LiveFrame/Application.pm
Index: Application.pm
===================================================================
RCS file: /cvsroot/liveframe/lab/lib/LiveFrame/Application.pm,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- Application.pm 2001/11/30 02:25:16 1.3
+++ Application.pm 2001/12/02 12:42:42 1.4
@@ -21,6 +21,7 @@
actions => {},
attrs => {},
cgi => undef,
+ committed => undef,
config => undef,
config_processed => undef,
config_file => undef,
@@ -172,11 +173,7 @@
my $self = shift;
if (@_) {
- my $name = shift;
- unless ($self->action($name)) {
- die "current action $name is not defined\n";
- }
- $self->{current_action_name} = $name;
+ $self->{current_action_name} = shift;
} elsif (! defined $self->{current_action_name}) {
my $path_info = $self->cgi()->path_info();
if ($path_info) {
@@ -299,6 +296,7 @@
my $tproc_opts =
{
INCLUDE_PATH => $self->template_dir(),
+ PLUGIN_BASE => 'LiveFrame::Template::Plugin',
};
$self->{tproc} = Template->new($tproc_opts);
@@ -341,23 +339,44 @@
for my $name ($self->attrs()) {
$tparams->{$name} = $self->attr($name);
}
+
+ $tparams->{application} = $self;
+ $tparams->{config} = $self->config();
+ $tparams->{cgi} = $self->cgi();
+ $tparams->{page} = $page;
+
+ my $current_form_name = $self->current_form_name() || '';
+ if ($current_form_name eq $page) {
+ my $form = $self->current_form();
+ if ($form) {
+ $tparams->{form} = $form;
+ }
+ }
- $tparams->{self_url} = $self->url($cgi->script_name());
- $tparams->{img_url} = $self->url($config->img_url());
- $tparams->{css_url} = $self->url($config->css_url());
+ $tparams->{self_url} = $self->url($cgi->script_name());
+ $tparams->{img_url} = $self->url($config->img_url());
+ $tparams->{css_url} = $self->url($config->css_url());
$tproc->process($self->template($page), $tparams) or do {
- $self->send_tmpl_error($tproc->error());
+ $self->send_tmpl_error($tproc->error(), $page);
};
return 1;
}
+sub send_header {
+ my ($self) = @_;
+
+ print $self->cgi()->header() unless $self->{committed};
+ $self->{committed} = 1;
+
+ return 1;
+}
+
sub send_page {
my ($self, $page) = @_;
-
- print $self->cgi()->header();
+ $self->send_header();
return $self->process_template($page);
}
@@ -371,13 +390,23 @@
}
sub send_error {
- my ($self, $msg) = @_;
+ my ($self, $msg, $tmpl) = @_;
- print $self->cgi()->header();
+ $self->send_header();
my $error_page = $self->default_error_page();
if (defined $error_page) {
- return $self->process_template($error_page, {error_msg => $msg});
+ my $tparams = { error_msg => $msg };
+
+ if (defined $tmpl) {
+ $tparams->{error_type} = 'template';
+ $tparams->{error_tmpl} = $tmpl;
+ }
+ else {
+ $tparams->{error_type} = 'application';
+ }
+
+ return $self->process_template($error_page, $tparams);
}
$msg = HTML::Entities::encode_entities($msg);
@@ -386,6 +415,9 @@
<p>
<b>Application Error</b>
</p>
+<p>
+An internal application error was encountered:
+</p>
<pre>
$msg
</pre>
@@ -395,20 +427,34 @@
}
sub send_tmpl_error {
- my ($self, $error) = @_;
+ my ($self, $error, $page) = @_;
my $msg = $error->info() || '';
$msg =~ s|\\n|\n|g;
- $msg = HTML::Entities::encode_entities($msg);
+ my $tmpl = $self->template($page);
- print <<EOT;
+ eval { $self->send_error($msg, $tmpl) };
+ if ($@) {
+ $msg = HTML::Entities::encode_entities($msg);
+
+ print <<EOT;
<p>
<b>Template Error</b>
</p>
+<p>
+An error was encountered processing <i>$tmpl</i>:
+</p>
<pre>
$msg
</pre>
+<p>
+Additionally, an error was encountered processing the default error page:
+</p>
+<pre>
+$@
+</pre>
EOT
+ }
return 1;
}
@@ -544,7 +590,7 @@
$self->fill_in_form($form);
# XXX: requires the previous action to have the same name
- # as the form in the page that it sends
+ # as the form in the page htat it sends
$self->current_action_name($form->name());
$action = $self->current_action();
}
@@ -554,27 +600,32 @@
# XXX: how to signal that an action requires a form submission?
}
- # execute the action method, which will send a page when it
- # completes
- eval { $action->perform($self) };
- if ($@) {
- $self->send_error($@);
+ # if the action wasn't unset (usually by an invalid form
+ # submission), execute the action method, which will send a
+ # page when it completes
+ if ($action) {
+ eval { $action->perform($self) };
+ if ($@) {
+ $self->send_error($@);
+ }
}
}
- else {
+
+ unless ($action) {
# no action exists for the specified action name so assume
# it's a page name
- my $msg = sprintf("requested action [%s] not defined",
- $self->current_action_name());
- $self->send_error($msg);
- return 0;
- # XXX: set a config switch to enable page sending?
-# eval {
-# $self->send_page($self->current_action_name());
-# };
-# if ($@) {
-# $self->send_error($@);
-# }
+ # XXX: set a config switch to disable page sending?
+# my $msg = sprintf("requested action [%s] not defined",
+# $self->current_action_name());
+# $self->send_error($msg);
+# return 0;
+ eval {
+ $self->send_page($self->current_action_name() ||
+ $self->default_action_name());
+ };
+ if ($@) {
+ $self->send_error($@);
+ }
}
return 0;
|