2 use base qw(Class::Accessor Class::Data::Inheritable);
4 use Class::DBI::Loader;
5 use UNIVERSAL::require;
6 use Apache::Constants ":common";
10 __PACKAGE__->mk_classdata($_) for qw( _config init_done view_object );
11 __PACKAGE__->mk_accessors ( qw( config ar params objects model_class
12 args action template ));
13 __PACKAGE__->config({});
14 __PACKAGE__->init_done(0);
19 if ($real ne "Apache::MVC") {
21 *{$real."::handler"} = sub { Apache::MVC::handler($real, @_) };
25 # This is really dirty.
28 if (ref $self) { return $self->_config_accessor(@_) }
29 return $self->_config(@_);
33 my ($calling_class, $dsn) = @_;
34 $calling_class = ref $calling_class if ref $calling_class;
35 my $config = $calling_class->config;
36 $config->{model} ||= "Apache::MVC::Model::CDBI";
37 $config->{model}->require;
38 $config->{dsn} = $dsn;
39 $config->{loader} = Class::DBI::Loader->new(
40 namespace => $calling_class,
43 $config->{classes} = [ $config->{loader}->classes ];
44 for my $subclass (@{$config->{classes}}) {
46 unshift @{$subclass."::ISA"}, $config->{model};
47 $config->{model}->adopt($subclass)
48 if $config->{model}->can("adopt");
54 my $config = $class->config;
55 $config->{view} ||= "Apache::MVC::View::TT";
56 $config->{view}->require;
57 $config->{display_tables} ||= [ $class->config->{loader}->tables ];
58 $class->view_object($class->config->{view}->new);
64 my ($self, $table) = @_;
65 return $self->config->{loader}->_table2class($table);
69 # See Apache::MVC::Workflow before trying to understand this.
71 $class->init unless $class->init_done;
72 my $r = bless { config => $class->config }, $class;
76 $r->model_class($r->class_of($r->{table}));
77 my $status = $r->is_applicable;
78 return $status unless $status == OK;
79 $status = $r->call_authenticate;
80 return $status unless $status == OK;
81 $r->additional_data();
83 $r->model_class->process($r);
84 $r->view_object->process($r);
85 return $r; # For debugging.
90 require Apache; require Apache::Request;
91 $self->{ar} = Apache::Request->new(Apache->request);
96 my $uri = $self->{ar}->path_info();
97 my @pi = split /\//, $uri;
98 shift @pi while @pi and !$pi[0];
99 $self->{table} = shift @pi;
100 $self->{action} = shift @pi;
101 $self->{args} = \@pi;
103 $self->{params} = { $self->{ar}->content };
108 my $config = $self->config;
109 $config->{ok_tables} = {map {$_ => 1} @{$config->{display_tables}}};
110 warn "We don't have that table ($self->{table})"
111 unless $config->{ok_tables}{$self->{table}};
112 return DECLINED() unless exists $config->{ok_tables}{$self->{table}};
114 # Does the action method exist?
115 # XXX We should set the method class to the class for the table
116 my $cv = $self->model_class->can($self->{action});
117 warn "We don't have that action ($self->{action})" unless $cv;
118 return DECLINED() unless $cv;
121 $self->{method_attribs} = join " ", attributes::get($cv);
122 do { warn "$self->{action} not exported";
124 } unless $self->{method_attribs} =~ /\bExported\b/i;
128 sub call_authenticate {
130 return $self->model_class->authenticate($self) if
131 $self->model_class->can("authenticate");
132 return $self->authenticate();
135 sub additional_data {}
137 sub authenticate { return OK }