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