2 use base qw(Class::Accessor Class::Data::Inheritable);
4 use Class::DBI::Loader;
5 use UNIVERSAL::require;
6 use Apache::Constants ":common";
11 __PACKAGE__->mk_classdata($_) for qw( _config init_done view_object );
12 __PACKAGE__->mk_accessors ( qw( config ar params objects model_class
13 args action template ));
14 __PACKAGE__->config({});
15 __PACKAGE__->init_done(0);
17 # This is really dirty.
20 if (ref $self) { return $self->_config_accessor(@_) }
21 return $self->_config(@_);
25 my ($calling_class, $dsn) = @_;
26 $calling_class = ref $calling_class if ref $calling_class;
27 $calling_class->config->{dsn} = $dsn;
28 $calling_class->config->{loader} = Class::DBI::Loader->new(
29 namespace => $calling_class,
36 my $config = $class->config;
37 $config->{model} ||= "Apache::MVC::Model::CDBI";
38 $config->{view} ||= "Apache::MVC::View::TT";
39 $config->{model}->require;
40 $config->{view}->require;
41 $config->{classes} = [ $class->config->{loader}->classes ];
42 $config->{display_tables} ||= [ $class->config->{loader}->tables ];
43 for my $subclass (@{$config->{classes}}) {
45 push @{$subclass."::ISA"}, $class->config->{model};
47 $class->view_object($class->config->{view}->new);
53 my ($self, $table) = @_;
54 return $self->config->{loader}->_table2class($table);
58 # See Apache::MVC::Workflow before trying to understand this.
59 # XXX This needs to work with Apache without method handlers
61 $class->init unless $class->init_done;
62 my $r = bless { config => $class->config }, $class;
65 $r->model_class($r->class_of($r->{table}));
66 my $status = $r->is_applicable;
67 return $status unless $status == OK;
68 $status = $r->call_authenticate;
69 return $status unless $status == OK;
70 $r->additional_data();
71 $r->model_class->process($r);
72 $r->view_object->process($r);
73 return $r; # For debugging.
78 require Apache; require Apache::Request;
79 $self->{ar} = Apache::Request->new(Apache->request);
84 my @pi = split /\//, $self->{ar}->uri();
85 shift @pi while @pi and !$pi[0];
86 $self->{table} = shift @pi;
87 $self->{action} = shift @pi;
90 $self->{params} = $self->{ar}->content;
95 my $config = $self->config;
96 my %ok = map {$_ => 1} @{$config->{display_tables}};
97 return DECLINED() unless exists $ok{$self->{table}};
99 # Does the action method exist?
100 my $cv = $self->model_class->can($self->{action});
101 return DECLINED() unless $cv;
104 $self->{method_attribs} = join " ", attributes::get($cv);
106 unless $self->{method_attribs} =~ /\bExported\b/i;
110 sub call_authenticate {
112 return $self->model_class->authenticate($self) if
113 $self->model_class->can("authenticate");
114 return $self->authenticate();
117 sub additional_data {}
119 sub authenticate { return OK }