refactor the hardcoded attribute stuff in as_dot
[scpubgit/stemmatology.git] / TreeOfTexts / lib / TreeOfTexts / Controller / Root.pm
1 package TreeOfTexts::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 TreeOfTexts::Controller::Root - Root Controller for TreeOfTexts
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;
53     foreach my $id ( $m->tradition_ids ) {
54         my $data = { 
55                 'id' => $id,
56                 'name' => $m->name( $id ),
57         };
58         push( @textlist, $data );
59     }
60     
61     $c->stash->{texts} = \@textlist;
62         $c->stash->{template} = 'directory.tt';
63 }
64
65 =head2 alignment
66
67  GET /alignment/$textid
68
69 Returns an alignment table for the text specified at $textid.
70
71 =cut
72
73 sub alignment :Local :Args(1) {
74         my( $self, $c, $textid ) = @_;
75         my $m = $c->model('Directory');
76         my $collation = $m->tradition( $textid )->collation;
77         my $alignment = $collation->make_alignment_table;
78         
79         # Turn the table, so that witnesses are by column and the rows
80         # are by rank.
81         my $wits = [ map { $_->{'witness'} } @{$alignment->{'alignment'}} ];
82         my $rows;
83         foreach my $i ( 0 .. $alignment->{'length'} - 1 ) {
84                 my @rankrdgs = map { $_->{'tokens'}->[$i]->{'t'} } 
85                         @{$alignment->{'alignment'}};
86                 push( @$rows, { 'rank' => $i+1, 'readings' => \@rankrdgs } );
87         }
88         $c->log->debug( Dumper( $rows ) );
89         $c->stash->{'witnesses'} = $wits;
90         $c->stash->{'table'} = $rows;
91         $c->stash->{'template'} = 'alignment.tt';
92 }
93
94 =head2 stemma
95
96  GET /stemma/$textid
97  POST /stemma/$textid, { 'dot' => $dot_string }
98
99 Returns an SVG representation of the stemma hypothesis for the text.  If 
100 the URL is called with POST and a new dot string, updates the stemma and
101 returns the SVG as with GET.
102
103 =cut
104
105 sub stemma :Local :Args(1) {
106         my( $self, $c, $textid ) = @_;
107         my $m = $c->model('Directory');
108         my $tradition = $m->tradition( $textid );
109         
110         if( $c->req->method eq 'POST' ) {
111                 # Update the stemma
112                 my $dot = $c->request->body_params->{'dot'};
113                 $tradition->add_stemma( $dot );
114                 $m->store( $tradition );
115         }
116         
117         $c->stash->{'result'} = $tradition->stemma->as_svg;
118         $c->forward('View::SVG');
119 }
120
121 =head2 stemmadot
122
123  GET /stemmadot/$textid
124  
125 Returns the 'dot' format representation of the current stemma hypothesis.
126
127 =cut
128
129 sub stemmadot :Local :Args(1) {
130         my( $self, $c, $textid ) = @_;
131         my $m = $c->model('Directory');
132         my $tradition = $m->tradition( $textid );
133         
134         $c->response->body( $tradition->stemma->editable );
135         $c->forward('View::Plain');
136 }
137
138 =head2 default
139
140 Standard 404 error page
141
142 =cut
143
144 sub default :Path {
145     my ( $self, $c ) = @_;
146     $c->response->body( 'Page not found' );
147     $c->response->status(404);
148 }
149
150 =head2 end
151
152 Attempt to render a view, if needed.
153
154 =cut
155
156 sub end : ActionClass('RenderView') {}
157
158 =head1 AUTHOR
159
160 Tara L Andrews
161
162 =head1 LICENSE
163
164 This library is free software. You can redistribute it and/or modify
165 it under the same terms as Perl itself.
166
167 =cut
168
169 __PACKAGE__->meta->make_immutable;
170
171 1;