]> git.decadent.org.uk Git - maypole.git/blob - lib/Maypole/View/Base.pm
TOO MUCH HASSLE.
[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         ($r->model_class && 
15             File::Spec->catdir($root, $r->model_class->moniker)),
16         File::Spec->catdir($root, "custom"),
17         File::Spec->catdir($root, "factory")
18     );
19 }
20
21 sub vars {
22     my ($self, $r) = @_;
23     my $class = $r->model_class;
24     my %args = (
25         request => $r,
26         objects => $r->objects,
27         base    => $r->config->{uri_base},
28         config  => $r->config
29         # ...
30     ) ;
31     if ($class) { 
32         $args{classmetadata} = {
33             name => $class,
34             table => $class->table,
35             columns => [ $class->display_columns ],
36             colnames => { $class->column_names },
37             related_accessors => [ $class->related($r) ],
38             moniker => $class->moniker,
39             plural  => $class->plural_moniker,
40             cgi => { $class->to_cgi },
41         };
42
43         # User-friendliness facility for custom template writers.
44         if (@{$r->objects || []} > 1) { 
45             $args{$r->model_class->plural_moniker} = $r->objects;
46         } else {
47             ($args{$r->model_class->moniker}) = @{$r->objects ||[]};
48         }
49     }
50
51     # Overrides
52     %args = (%args, %{$r->{template_args}||{}});
53     %args;
54 }
55
56 sub process {
57     my ($self, $r) = @_;
58     my $status = $self->template($r);
59     return $self->error($r) if $status != OK;
60     $r->{content_type} ||= "text/html";
61     return OK;
62 }
63
64 sub error {
65     my ($self, $r) = @_;
66     warn $r->{error};
67     if ($r->{error} =~ /not found$/) { return -1 }
68     $r->{content_type} = "text/plain";
69     $r->{output} = $r->{error};
70     $r->send_output;
71     return ERROR;
72 }
73
74 sub template { die shift()." didn't define a decent template method!" }
75
76 1;