]> git.decadent.org.uk Git - maypole.git/blob - lib/Maypole/View/Base.pm
removed Maypole::Model->description,
[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 use Carp;
7
8 sub new { bless {}, shift }    # By default, do nothing.
9
10 sub paths {
11     my ( $self, $r ) = @_;
12     my $root = $r->config->template_root || $r->get_template_root;
13     return (
14         $root,
15         (
16             $r->model_class
17               && File::Spec->catdir( $root, $r->model_class->moniker )
18         ),
19         File::Spec->catdir( $root, "custom" ),
20         File::Spec->catdir( $root, "factory" )
21     );
22 }
23
24 sub vars {
25     my ( $self, $r ) = @_;
26     my $class = $r->model_class;
27     my $base  = $r->config->uri_base;
28     $base =~ s/\/+$//;
29     my %args = (
30         request => $r,
31         objects => $r->objects,
32         base    => $base,
33         config  => $r->config
34
35           # ...
36     );
37     if ($class) {
38         my $classmeta = $r->template_args->{classmetadata} ||= {};
39         $classmeta->{name}              ||= $class;
40         $classmeta->{table}             ||= $class->table;
41         $classmeta->{columns}           ||= [ $class->display_columns ];
42         $classmeta->{list_columns}      ||= [ $class->list_columns ];
43         $classmeta->{colnames}          ||= { $class->column_names };
44         $classmeta->{related_accessors} ||= [ $class->related($r) ];
45         $classmeta->{moniker}           ||= $class->moniker;
46         $classmeta->{plural}            ||= $class->plural_moniker;
47         $classmeta->{cgi}               ||= { $class->to_cgi };
48
49         # User-friendliness facility for custom template writers.
50         if ( @{ $r->objects || [] } > 1 ) {
51             $args{ $r->model_class->plural_moniker } = $r->objects;
52         }
53         else {
54             ( $args{ $r->model_class->moniker } ) = @{ $r->objects || [] };
55         }
56     }
57
58     # Overrides
59     %args = ( %args, %{ $r->template_args || {} } );
60     %args;
61 }
62
63 sub process {
64     my ( $self, $r ) = @_;
65     $r->{content_type}      ||= "text/html";
66     $r->{document_encoding} ||= "utf-8";
67     my $status = $self->template($r);
68     return $self->error($r) if $status != OK;
69     return OK;
70 }
71
72 sub error {
73     my ( $self, $r, $desc ) = @_;
74     $desc = $desc ? "$desc: " : "";
75     carp $desc . $r->{error};
76     if ( $r->{error} =~ /not found$/ ) {
77
78         # This is a rough test to see whether or not we're a template or
79         # a static page
80         return -1 unless @{ $r->{objects} || [] };
81
82         $r->{error} = <<EOF;
83
84 <H1> Template not found </H1>
85
86 This template was not found while processing the following request:
87
88 <B>@{[$r->{action}]}</B> on table <B>@{[ $r->{table} ]}</B> with objects:
89
90 <PRE>
91 @{[join "\n", @{$r->{objects}}]}
92 </PRE>
93
94 Looking for template <B>@{[$r->{template}]}</B> in paths:
95
96 <PRE>
97 @{[ join "\n", $self->paths($r) ]}
98 </PRE>
99 EOF
100         $r->{content_type} = "text/html";
101         $r->{output}       = $r->{error};
102         return OK;
103     }
104     $r->{content_type} = "text/plain";
105     $r->{output}       = $r->{error};
106     $r->send_output;
107     return ERROR;
108 }
109
110 sub template { die shift() . " didn't define a decent template method!" }
111
112 1;
113
114
115 =head1 NAME
116
117 Maypole::View::Base - Base class for view classes
118
119 =head1 DESCRIPTION
120
121 This is the base class for Maypole view classes. This is an abstract class
122 that defines the interface, and can't be used directly.
123
124 =head2 process
125
126 This is the entry point for the view. It templates the request and returns a
127 C<Maypole::Constant> indicate success or failure for the view phase.
128
129 Anyone subclassing this for a different rendering mechanism needs to provide
130 the following methods:
131
132 =head2 template
133
134 In this method you do the actual processing of your template. it should use
135 L<paths> to search for components, and provide the templates with easy access
136 to the contents of L<vars>. It should put the result in C<$r-E<gt>output> and
137 return C<OK> if processing was sucessfull, or populate C<$r-E<gt>error> and
138 return C<ERROR> if it fails.
139
140 =head1 Other overrides
141
142 Additionally, individual derived model classes may want to override the
143
144 =head2 new
145
146 The default constructor does nothing. You can override this to perform actions
147 during view initialization.
148
149 =head2 paths
150
151 Returns search paths for templates. the default method returns folders for the
152 model class's C<moniker>, factory, custom under the configured template root.
153
154 =head2 vars
155
156 returns a hash of data the template should have access to. The default one
157 populates classmetadata if there is a table class, as well as setting the data
158 objects by name if there is one or more objects available.
159
160 =head2 error
161
162
163 =cut