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