6610705ddca4ae165f41eb5abc6a8d35c6fe19c7
[scpubgit/stemmatology.git] / stemmaweb / lib / stemmaweb / Controller / Root.pm
1 package stemmaweb::Controller::Root;
2 use Moose;
3 use namespace::autoclean;
4 use Text::Tradition::Analysis qw/ run_analysis /;
5
6
7 BEGIN { extends 'Catalyst::Controller' }
8
9 #
10 # Sets the actions in this controller to be registered with no prefix
11 # so they function identically to actions created in MyApp.pm
12 #
13 __PACKAGE__->config(namespace => '');
14
15 =head1 NAME
16
17 stemmaweb::Controller::Root - Root Controller for stemmaweb
18
19 =head1 DESCRIPTION
20
21 Serves up the main container pages.
22
23 =head1 URLs
24
25 =head2 index
26
27 The root page (/).  Serves the main container page, from which the various
28 components will be loaded.
29
30 =cut
31
32 sub index :Path :Args(0) {
33     my ( $self, $c ) = @_;
34
35     $c->stash->{template} = 'index.tt';
36 }
37
38 =head1 Elements of index page
39
40 =head2 directory
41
42  GET /directory
43
44 Serves a snippet of HTML that lists the available texts.  Eventually this will be available texts by user.
45
46 =cut
47 sub directory :Local :Args(0) {
48         my( $self, $c ) = @_;
49     my $m = $c->model('Directory');
50     # TODO not used yet, will load user texts later
51     my $user = $c->request->param( 'user' ) || 'ALL';
52     my @textlist = $m->traditionlist();
53     $c->stash->{texts} = \@textlist;
54         $c->stash->{template} = 'directory.tt';
55 }
56
57 =head2 variantgraph
58
59  GET /variantgraph/$textid
60  
61 Returns the variant graph for the text specified at $textid, in SVG form.
62
63 =cut
64
65 sub variantgraph :Local :Args(1) {
66         my( $self, $c, $textid ) = @_;
67         my $m = $c->model('Directory');
68         my $tradition = $m->tradition( $textid );
69         my $collation = $tradition->collation;
70         my $needsave = !$collation->has_cached_svg;
71         $c->stash->{'result'} = $collation->as_svg;
72         $m->save( $tradition );  # to save generate SVG in the cache
73         $c->forward('View::SVG');
74 }
75         
76 =head2 alignment
77
78  GET /alignment/$textid
79
80 Returns an alignment table for the text specified at $textid.
81
82 =cut
83
84 sub alignment :Local :Args(1) {
85         my( $self, $c, $textid ) = @_;
86         my $m = $c->model('Directory');
87         my $collation = $m->tradition( $textid )->collation;
88         my $alignment = $collation->alignment_table;
89         
90         # Turn the table, so that witnesses are by column and the rows
91         # are by rank.
92         my $wits = [ map { $_->{'witness'} } @{$alignment->{'alignment'}} ];
93         my $rows;
94         foreach my $i ( 0 .. $alignment->{'length'} - 1 ) {
95                 my @rankrdgs = map { $_->{'tokens'}->[$i]->{'t'} } 
96                         @{$alignment->{'alignment'}};
97                 push( @$rows, { 'rank' => $i+1, 'readings' => \@rankrdgs } );
98         }
99         $c->stash->{'witnesses'} = $wits;
100         $c->stash->{'table'} = $rows;
101         $c->stash->{'template'} = 'alignment.tt';
102 }
103
104 =head2 stemma
105
106  GET /stemma/$textid
107  POST /stemma/$textid, { 'dot' => $dot_string }
108
109 Returns an SVG representation of the stemma hypothesis for the text.  If 
110 the URL is called with POST and a new dot string, updates the stemma and
111 returns the SVG as with GET.
112
113 =cut
114
115 sub stemma :Local :Args(1) {
116         my( $self, $c, $textid ) = @_;
117         my $m = $c->model('Directory');
118         my $tradition = $m->tradition( $textid );
119         
120         if( $c->req->method eq 'POST' ) {
121                 # Update the stemma
122                 my $dot = $c->request->body_params->{'dot'};
123                 $tradition->add_stemma( $dot );
124                 $m->store( $tradition );
125         }
126         
127         $c->stash->{'result'} = $tradition->stemma_count
128                 ? $tradition->stemma(0)->as_svg
129                 : '';
130         $c->forward('View::SVG');
131 }
132
133 =head2 stemmadot
134
135  GET /stemmadot/$textid
136  
137 Returns the 'dot' format representation of the current stemma hypothesis.
138
139 =cut
140
141 sub stemmadot :Local :Args(1) {
142         my( $self, $c, $textid ) = @_;
143         my $m = $c->model('Directory');
144         my $tradition = $m->tradition( $textid );
145         
146         $c->response->body( $tradition->stemma->editable );
147         $c->forward('View::Plain');
148 }
149
150 =head2 default
151
152 Standard 404 error page
153
154 =cut
155
156 sub default :Path {
157     my ( $self, $c ) = @_;
158     $c->response->body( 'Page not found' );
159     $c->response->status(404);
160 }
161
162 =head2 end
163
164 Attempt to render a view, if needed.
165
166 =cut
167
168 sub end : ActionClass('RenderView') {}
169
170 =head1 AUTHOR
171
172 Tara L Andrews
173
174 =head1 LICENSE
175
176 This library is free software. You can redistribute it and/or modify
177 it under the same terms as Perl itself.
178
179 =cut
180
181 __PACKAGE__->meta->make_immutable;
182
183 1;