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