clean
[sgn.git] / lib / SGN / Controller / Root.pm
blob71dd98e45c35b2bd2a68204b10d46cc0dd0ee4bd
1 package SGN::Controller::Root;
2 use Moose;
3 use namespace::autoclean;
5 use Scalar::Util 'weaken';
6 use CatalystX::GlobalContext ();
8 use CXGN::Login;
9 use CXGN::People::Person;
11 BEGIN { extends 'Catalyst::Controller' }
14 # Sets the actions in this controller to be registered with no prefix
15 # so they function identically to actions created in MyApp.pm
17 __PACKAGE__->config(namespace => '');
19 =head1 NAME
21 SGN::Controller::Root - Root Controller for SGN
23 =head1 DESCRIPTION
25 Web application to run the SGN web site.
27 =head1 PUBLIC ACTIONS
29 =head2 index
31 The root page (/)
33 =cut
35 sub index :Path :Args(0) {
36 my ( $self, $c ) = @_;
38 # Hello World
39 $c->stash->{template} = '/index.mas';
40 $c->stash->{schema} = $c->dbic_schema('SGN::Schema');
41 $c->stash->{static_content_path} = $c->config->{static_content_path};
44 =head2 default
46 Attempt to find index.pl pages, and prints standard 404 error page if
47 nothing could be found.
49 =cut
51 sub default :Path {
52 my ( $self, $c ) = @_;
54 return 1 if $c->forward('/redirects/find_redirect');
56 $c->throw_404;
59 =head2 bare_mason
61 Render a bare mason component, with no autohandler wrapping.
62 Currently used for GBrowse integration (GBrowse makes a subrequest for
63 the mason header and footer).
65 =cut
67 sub bare_mason :Path('bare_mason') {
68 my ( $self, $c, @args ) = @_;
70 # figure out our template path
71 my $t = File::Spec->catfile( @args );
72 $t .= '.mas' unless $t =~ m|\.[^/\\\.]+$|;
73 $c->stash->{template} = $t;
75 # TODO: check that it exists
77 $c->forward('View::BareMason');
80 =head1 PRIVATE ACTIONS
82 =head2 end
84 Attempt to render a view, if needed.
86 =cut
88 sub render : ActionClass('RenderView') { }
89 sub end : Private {
90 my ( $self, $c ) = @_;
92 return if @{$c->error};
94 # don't try to render a default view if this was handled by a CGI
95 $c->forward('render') unless $c->req->path =~ /\.pl$/;
97 # enforce a default text/html content type regardless of whether
98 # we tried to render a default view
99 $c->res->content_type('text/html') unless $c->res->content_type;
101 if( $c->res->content_type eq 'text/html' ) {
102 # insert any additional header html collected during rendering
103 $c->forward('insert_collected_html');
105 # tell caches our responses vary depending on the Cookie header
106 $c->res->headers->push_header('Vary', 'Cookie');
107 } else {
108 $c->log->debug("skipping JS pack insertion for page with content type ".$c->res->content_type)
109 if $c->debug;
114 sub insert_collected_html :Private {
115 my ( $self, $c ) = @_;
117 $c->forward('/js/resolve_javascript_classes');
119 my $b = $c->res->body;
120 my $inserted_head_pre = $b && $b =~ s{<!-- \s* INSERT_HEAD_PRE_HTML \s* --> }{ $self->_make_head_pre_html( $c ) }ex;
121 my $inserted_head_post = $b && $b =~ s{<!-- \s* INSERT_HEAD_POST_HTML \s* -->}{ $self->_make_head_post_html( $c ) }ex;
122 if( $inserted_head_pre || $inserted_head_post ) {
123 $c->res->body( $b );
125 # we have changed the size of the body. remove the
126 # content-length and let catalyst recalculate the content-length
127 # if it can
128 $c->res->headers->remove_header('content-length');
130 delete $c->stash->{$_} for qw( add_head_html add_css_files add_js_classes );
134 sub _make_head_pre_html {
135 my ( $self, $c ) = @_;
136 return join "\n", @{ $c->stash->{head_pre_html} || [] };
139 sub _make_head_post_html {
140 my ( $self, $c ) = @_;
142 my $head_post_html = join "\n", (
143 @{ $c->stash->{add_head_html} || [] },
144 ( map {
145 qq{<link rel="stylesheet" type="text/css" href="$_" />}
146 } @{ $c->stash->{css_uris} || [] }
148 ( map {
149 qq{<script src="$_" type="text/javascript"></script>}
150 } @{ $c->stash->{js_uris} || [] }
154 return $head_post_html;
157 =head2 auto
159 Run for every request to the site.
161 =cut
163 sub auto : Private {
164 my ($self, $c) = @_;
165 CatalystX::GlobalContext->set_context( $c );
166 $c->stash->{c} = $c;
167 weaken $c->stash->{c};
169 # gluecode for logins
171 unless( $c->config->{'disable_login'} ) {
172 my $dbh = $c->dbc->dbh;
173 if ( my $sp_person_id = CXGN::Login->new( $dbh )->has_session ) {
175 my $sp_person = CXGN::People::Person->new( $dbh, $sp_person_id);
177 $c->authenticate({
178 username => $sp_person->get_username(),
179 password => $sp_person->get_password(),
184 return 1;
189 ############# helper methods ##########
191 sub _find_cgi_action {
192 my ($self,$c,$path) = @_;
194 $path =~ s!/+!/!g;
195 my $cgi = $c->controller('CGI')
196 or return;
198 my $index_action = $cgi->cgi_action_for( $path )
199 or return;
201 $c->log->debug("found CGI index action '$index_action'") if $c->debug;
203 return $index_action;
206 =head1 AUTHOR
208 Robert Buels, Jonathan "Duke" Leto
210 =head1 LICENSE
212 This library is free software. You can redistribute it and/or modify
213 it under the same terms as Perl itself.
215 =cut
217 __PACKAGE__->meta->make_immutable;