a0f4fcba5976e7948c606a737df01e3217f7c796
[scpubgit/stemmatology.git] / stemmaweb / lib / stemmaweb / Controller / Stexaminer.pm
1 package stemmaweb::Controller::Stexaminer;
2 use Moose;
3 use namespace::autoclean;
4 use File::Temp;
5 use JSON;
6 use Text::Tradition::Analysis qw/ run_analysis wit_stringify /;
7
8 BEGIN { extends 'Catalyst::Controller' }
9
10
11 =head1 NAME
12
13 stemmaweb::Controller::Stexaminer - Simple controller for stemma display
14
15 =head1 DESCRIPTION
16
17 The stemma analysis tool with the pretty colored table.
18
19 =head1 METHODS
20
21  GET stexaminer/$textid
22  
23 Renders the application for the text identified by $textid.
24
25 =head2 index
26
27 =cut
28
29 sub index :Path :Args(1) {
30     my( $self, $c, $textid ) = @_;
31     my $m = $c->model('Directory');
32         my $tradition = $m->tradition( $textid );
33         if( $tradition->stemma_count ) {
34                 my $stemma = $tradition->stemma(0);
35                 # TODO Think about caching the stemma in a session 
36                 $c->stash->{svg} = $stemma->as_svg;
37                 $c->stash->{text_title} = $tradition->name;
38                 $c->stash->{template} = 'stexaminer.tt'; 
39                 # TODO Run the analysis as AJAX from the loaded page.
40                 my $t = run_analysis( $tradition, 'exclude_type1' => 1 );
41                 # Stringify the reading groups
42                 foreach my $loc ( @{$t->{'variants'}} ) {
43                         my $mst = wit_stringify( $loc->{'missing'} );
44                         $loc->{'missing'} = $mst;
45                         foreach my $rhash ( @{$loc->{'readings'}} ) {
46                                 my $gst = wit_stringify( $rhash->{'group'} );
47                                 $rhash->{'group'} = $gst;
48                         }
49                 }
50                 # Values for TT rendering
51                 $c->stash->{variants} = $t->{'variants'};
52                 $c->stash->{total} = $t->{'variant_count'};
53                 $c->stash->{genealogical} = $t->{'genealogical_count'};
54                 $c->stash->{conflict} = $t->{'conflict_count'};         
55                 # Also make a JSON stash of the data for the statistics tables
56                 $c->stash->{reading_statistics} = to_json( $t->{'variants'} );
57         } else {
58                 $c->stash->{error} = 'Tradition ' . $tradition->name 
59                         . 'has no stemma for analysis.';
60         }
61 }
62
63 =head2 end
64
65 Attempt to render a view, if needed.
66
67 =cut
68
69 sub end : ActionClass('RenderView') {}
70
71 =head1 AUTHOR
72
73 Tara L Andrews
74
75 =head1 LICENSE
76
77 This library is free software. You can redistribute it and/or modify
78 it under the same terms as Perl itself.
79
80 =cut
81
82 __PACKAGE__->meta->make_immutable;
83
84 1;