]> git.decadent.org.uk Git - maypole.git/blob - lib/Maypole/View/Base.pm
view exceptions and some code cleaning
[maypole.git] / lib / Maypole / View / Base.pm
1 package Maypole::View::Base;
2 use File::Spec;
3 use UNIVERSAL::moniker;
4 use strict;
5 use Maypole::Constants;
6
7 sub new { bless {}, shift }    # By default, do nothing.
8
9 sub paths {
10     my ( $self, $r ) = @_;
11     my $root = $r->{config}{template_root} || $r->get_template_root;
12     return (
13         $root,
14         (
15             $r->model_class
16               && File::Spec->catdir( $root, $r->model_class->moniker )
17         ),
18         File::Spec->catdir( $root, "custom" ),
19         File::Spec->catdir( $root, "factory" )
20     );
21 }
22
23 sub vars {
24     my ( $self, $r ) = @_;
25     my $class = $r->model_class;
26     my $base  = $r->{config}->{uri_base};
27     $base =~ s/\/+$//;
28     my %args = (
29         request => $r,
30         objects => $r->objects,
31         base    => $base,
32         config  => $r->config
33
34           # ...
35     );
36     if ($class) {
37         $args{classmetadata} = {
38             name              => $class,
39             table             => $class->table,
40             columns           => [ $class->display_columns ],
41             colnames          => { $class->column_names },
42             related_accessors => [ $class->related($r) ],
43             moniker           => $class->moniker,
44             plural            => $class->plural_moniker,
45             cgi               => { $class->to_cgi },
46         };
47
48         # User-friendliness facility for custom template writers.
49         if ( @{ $r->objects || [] } > 1 ) {
50             $args{ $r->model_class->plural_moniker } = $r->objects;
51         }
52         else {
53             ( $args{ $r->model_class->moniker } ) = @{ $r->objects || [] };
54         }
55     }
56
57     # Overrides
58     %args = ( %args, %{ $r->{template_args} || {} } );
59     %args;
60 }
61
62 sub process {
63     my ( $self, $r ) = @_;
64     my $status = $self->template($r);
65     return $self->error($r) if $status != OK;
66     $r->{content_type}      ||= "text/html";
67     $r->{document_encoding} ||= "utf-8";
68     return OK;
69 }
70
71 sub error {
72     my ( $self, $r ) = @_;
73     warn $r->{error};
74     if ( $r->{error} =~ /not found$/ ) {
75
76         # This is a rough test to see whether or not we're a template or
77         # a static page
78         return -1 unless @{ $r->{objects} || [] };
79
80         $r->{error} = <<EOF;
81
82 <H1> Template not found </H1>
83
84 This template was not found while processing the following request:
85
86 <B>@{[$r->{action}]}</B> on table <B>@{[ $r->{table} ]}</B> with objects:
87
88 <PRE>
89 @{[join "\n", @{$r->{objects}}]}
90 </PRE>
91
92 Looking for template <B>@{[$r->{template}]}</B> in paths:
93
94 <PRE>
95 @{[ join "\n", $self->paths($r) ]}
96 </PRE>
97 EOF
98         $r->{content_type} = "text/html";
99         $r->{output}       = $r->{error};
100         return OK;
101     }
102     $r->{content_type} = "text/plain";
103     $r->{output}       = $r->{error};
104     $r->send_output;
105     return ERROR;
106 }
107
108 sub template { die shift() . " didn't define a decent template method!" }
109
110 1;