1 package stemmaweb::Controller::Root;
3 use namespace::autoclean;
4 use Text::Tradition::Analysis qw/ run_analysis /;
7 BEGIN { extends 'Catalyst::Controller' }
10 # Sets the actions in this controller to be registered with no prefix
11 # so they function identically to actions created in MyApp.pm
13 __PACKAGE__->config(namespace => '');
17 stemmaweb::Controller::Root - Root Controller for stemmaweb
21 Serves up the main container pages.
27 The root page (/). Serves the main container page, from which the various
28 components will be loaded.
32 sub index :Path :Args(0) {
33 my ( $self, $c ) = @_;
35 $c->stash->{template} = 'index.tt';
38 =head1 Elements of index page
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.
48 sub directory :Local :Args(0) {
50 my $m = $c->model('Directory');
51 # Is someone logged in?
53 if( $c->user_exists ) {
54 my $user = $c->user->get_object;
55 my @list = $m->traditionlist( $user );
56 map { $usertexts{$_->{id}} = 1 } @list;
57 $c->stash->{usertexts} = \@list;
58 $c->stash->{is_admin} = 1 if $user->is_admin;
60 # List public (i.e. readonly) texts separately from any user (i.e.
61 # full access) texts that exist. Admin users therefore have nothing
63 my @plist = grep { !$usertexts{$_->{id}} } $m->traditionlist('public');
64 $c->stash->{publictexts} = \@plist;
65 $c->stash->{template} = 'directory.tt';
72 Returns the page element populated with information about a particular text.
76 sub textinfo :Local :Args(1) {
77 my( $self, $c, $textid ) = @_;
78 my $tradition = $c->model('Directory')->tradition( $textid );
79 # Need text name, witness list, scalar readings, scalar relationships, stemmata
82 traditionname => $tradition->name,
83 witnesses => [ map { $_->sigil } $tradition->witnesses ],
84 readings => scalar $tradition->collation->readings,
85 relationships => scalar $tradition->collation->relationships
87 my @stemmasvg = map { $_->as_svg({ size => [ 500, 375 ] }) } $tradition->stemmata;
88 map { $_ =~ s/\n/ /mg } @stemmasvg;
89 $textinfo->{stemmata} = \@stemmasvg;
90 $c->stash->{'result'} = $textinfo;
91 $c->forward('View::JSON');
96 GET /variantgraph/$textid
98 Returns the variant graph for the text specified at $textid, in SVG form.
102 sub variantgraph :Local :Args(1) {
103 my( $self, $c, $textid ) = @_;
104 my $tradition = $c->model('Directory')->tradition( $textid );
105 my $collation = $tradition->collation;
106 $c->stash->{'result'} = $collation->as_svg;
107 $c->forward('View::SVG');
112 GET /alignment/$textid
114 Returns an alignment table for the text specified at $textid.
118 sub alignment :Local :Args(1) {
119 my( $self, $c, $textid ) = @_;
120 my $tradition = $c->model('Directory')->tradition( $textid );
121 my $collation = $tradition->collation;
122 my $alignment = $collation->alignment_table;
124 # Turn the table, so that witnesses are by column and the rows
126 my $wits = [ map { $_->{'witness'} } @{$alignment->{'alignment'}} ];
128 foreach my $i ( 0 .. $alignment->{'length'} - 1 ) {
129 my @rankrdgs = map { $_->{'tokens'}->[$i]->{'t'} }
130 @{$alignment->{'alignment'}};
131 push( @$rows, { 'rank' => $i+1, 'readings' => \@rankrdgs } );
133 $c->stash->{'witnesses'} = $wits;
134 $c->stash->{'table'} = $rows;
135 $c->stash->{'template'} = 'alignment.tt';
141 POST /stemma/$textid, { 'dot' => $dot_string }
143 Returns an SVG representation of the stemma hypothesis for the text. If
144 the URL is called with POST and a new dot string, updates the stemma and
145 returns the SVG as with GET.
149 sub stemma :Local :Args(1) {
150 my( $self, $c, $textid ) = @_;
151 my $m = $c->model('Directory');
152 my $tradition = $m->tradition( $textid );
154 if( $c->req->method eq 'POST' ) {
156 my $dot = $c->request->body_params->{'dot'};
157 $tradition->add_stemma( $dot );
158 $m->store( $tradition );
161 $c->stash->{'result'} = $tradition->stemma_count
162 ? $tradition->stemma(0)->as_svg( { size => [ 500, 375 ] } )
164 $c->forward('View::SVG');
169 GET /stemmadot/$textid
171 Returns the 'dot' format representation of the current stemma hypothesis.
175 sub stemmadot :Local :Args(1) {
176 my( $self, $c, $textid ) = @_;
177 my $m = $c->model('Directory');
178 my $tradition = $m->tradition( $textid );
180 $c->response->body( $tradition->stemma->editable );
181 $c->forward('View::Plain');
186 Standard 404 error page
191 my ( $self, $c ) = @_;
192 $c->response->body( 'Page not found' );
193 $c->response->status(404);
198 Attempt to render a view, if needed.
202 sub end : ActionClass('RenderView') {}
210 This library is free software. You can redistribute it and/or modify
211 it under the same terms as Perl itself.
215 __PACKAGE__->meta->make_immutable;