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