137d28acf08a5f9e0d97ba0ca4efe5560ef8010d
[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 [enter your description here]
22
23 =head1 METHODS
24
25 =head2 index
26
27 The root page (/).  Lists the traditions available in the DB to work on,
28 and should also eventually have an 'Upload new' interface.
29
30 =cut
31
32 sub index :Path :Args(0) {
33     my ( $self, $c ) = @_;
34
35     my $m = $c->model('Directory');
36     my @all_texts;
37     foreach my $id ( $m->tradition_ids ) {
38         my $data = { 
39                 'id' => $id,
40                 'name' => $m->name( $id ),
41         };
42         push( @all_texts, $data );
43     }
44     
45     $c->stash->{texts} = \@all_texts;
46     $c->stash->{template} = 'frontpage.tt';
47 }
48
49 =head2 tradition (TODO)
50
51 The main page for a tradition, with information about it and links to the
52 available tools.
53
54 =head2 relationships
55
56 The relationship editor tool.
57
58 =cut
59
60 sub relationships :Local {
61         my( $self, $c ) = @_;
62         my $m = $c->model('Directory');
63         my $tradition = $m->tradition( $c->request->params->{'textid'} );
64         my $table = $tradition->collation->make_alignment_table();
65         my $witlist = shift @$table;
66         $c->stash->{witnesses} = $wits;
67         $c->stash->{alignment} = $table;
68         $c->stash->{template} = 'relate.tt';    
69 }
70
71 =head2 stexaminer
72
73 The stemma analysis tool with the pretty colored table.
74
75 =cut
76
77 sub stexaminer :Local {
78     my( $self, $c ) = @_;
79     my $m = $c->model('Directory');
80         my $tradition = $m->tradition( $c->request->params->{'textid'} );
81         my $stemma = $tradition->stemma;
82         # TODO Think about caching the stemma in a session 
83         $c->stash->{svg} = $stemma->as_svg;
84         $c->stash->{text_title} = $tradition->name;
85         $c->stash->{template} = 'index.tt'; 
86         # TODO Run the analysis as AJAX from the loaded page.
87         my $t = run_analysis( $tradition );
88         $c->stash->{variants} = $t->{'variants'};
89         $c->stash->{total} = $t->{'variant_count'};
90         $c->stash->{genealogical} = $t->{'genealogical_count'};
91         $c->stash->{conflict} = $t->{'conflict_count'};
92 }
93
94 =head2 alignment_table 
95
96 Return a JSON alignment table of a given text.
97
98 =cut
99
100 sub alignment_table :Local {
101         my( $self, $c ) = @_;
102         my $m = $c->model( 'Directory' );
103         my $tradition = $m->tradition( $c->request->params->{'textid'} );
104         my $table = $tradition->collation->make_alignment_table();
105         $c->stash->{'result'} => $table;
106         $c->forward-( 'View::JSON' );
107 }
108
109 =head1 OPENSOCIAL URLs
110
111 =head2 view_table
112
113 Simple gadget to return the analysis table for the stexaminer
114
115 =cut
116
117 sub view_table :Local {
118     my( $self, $c ) = @_;
119     my $m = $c->model('Directory');
120         my $id = $c->request->params->{'textid'};
121         my $t = run_analysis( $m->tradition( $id ), $m->stemma( $id ) );
122         $c->stash->{variants} = $t->{'variants'};
123     $c->stash->{template} = 'table_gadget.tt';
124 }
125
126 =head2 view_svg
127
128 Simple gadget to return the SVG for a given stemma
129
130 =cut
131
132 sub view_svg :Local {
133     my( $self, $c ) = @_;
134     my $m = $c->model('Directory');
135     my $stemma = $m->tradition( $c->request->params->{'textid'} )->stemma;
136         if( $stemma ) {
137                 $c->stash->{svg} = $stemma->as_svg;
138         }
139     $c->stash->{template} = 'stemma_gadget.tt';
140 }
141
142 =head2 default
143
144 Standard 404 error page
145
146 =cut
147
148 sub default :Path {
149     my ( $self, $c ) = @_;
150     $c->response->body( 'Page not found' );
151     $c->response->status(404);
152 }
153
154 =head2 end
155
156 Attempt to render a view, if needed.
157
158 =cut
159
160 sub end : ActionClass('RenderView') {}
161
162 =head1 AUTHOR
163
164 Tara L Andrews
165
166 =head1 LICENSE
167
168 This library is free software. You can redistribute it and/or modify
169 it under the same terms as Perl itself.
170
171 =cut
172
173 __PACKAGE__->meta->make_immutable;
174
175 1;