Show traditions based on the current logged-in user
[scpubgit/stemmaweb.git] / lib / stemmaweb / Controller / Root.pm
CommitLineData
b8a92065 1package stemmaweb::Controller::Root;
2use Moose;
3use namespace::autoclean;
4use Text::Tradition::Analysis qw/ run_analysis /;
5
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
17stemmaweb::Controller::Root - Root Controller for stemmaweb
18
19=head1 DESCRIPTION
20
21Serves up the main container pages.
22
23=head1 URLs
24
25=head2 index
26
27The root page (/). Serves the main container page, from which the various
28components will be loaded.
29
30=cut
31
32sub 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
70ccaf75 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.
b8a92065 45
46=cut
70ccaf75 47
b8a92065 48sub directory :Local :Args(0) {
49 my( $self, $c ) = @_;
50 my $m = $c->model('Directory');
51 # TODO not used yet, will load user texts later
70ccaf75 52 my $user = $c->user_exists ? $c->user->id : 'public';
53# my $user = $c->request->param( 'user' ) || 'ALL';
54 my @textlist = $m->traditionlist($user);
b8a92065 55 $c->stash->{texts} = \@textlist;
56 $c->stash->{template} = 'directory.tt';
57}
58
fb6e49b3 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');
b9a04e24 70 my $tradition = $m->tradition( $textid );
71 my $collation = $tradition->collation;
72 my $needsave = !$collation->has_cached_svg;
fb6e49b3 73 $c->stash->{'result'} = $collation->as_svg;
adffd13c 74 $m->save( $tradition ); # to save generate SVG in the cache
fb6e49b3 75 $c->forward('View::SVG');
76}
77
b8a92065 78=head2 alignment
79
80 GET /alignment/$textid
81
82Returns an alignment table for the text specified at $textid.
83
84=cut
85
86sub alignment :Local :Args(1) {
87 my( $self, $c, $textid ) = @_;
88 my $m = $c->model('Directory');
89 my $collation = $m->tradition( $textid )->collation;
ce956045 90 my $alignment = $collation->alignment_table;
b8a92065 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 }
b8a92065 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
117sub stemma :Local :Args(1) {
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
a86eba5d 129 $c->stash->{'result'} = $tradition->stemma_count
130 ? $tradition->stemma(0)->as_svg
96e44262 131 : '';
b8a92065 132 $c->forward('View::SVG');
133}
134
135=head2 stemmadot
136
137 GET /stemmadot/$textid
138
139Returns the 'dot' format representation of the current stemma hypothesis.
140
141=cut
142
143sub stemmadot :Local :Args(1) {
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}
151
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;