minor tweaks to display logic
[scpubgit/stemmatology.git] / stemmaweb / lib / stemmaweb / Controller / Stexaminer.pm
CommitLineData
5c9ecf66 1package stemmaweb::Controller::Stexaminer;
2376359f 2use Moose;
3use namespace::autoclean;
f8d13166 4use Encode qw/ decode_utf8 /;
2376359f 5use File::Temp;
6use JSON;
a44aaf2a 7use Text::Tradition::Analysis qw/ run_analysis wit_stringify /;
f8d13166 8use Text::Tradition::Collation;
9use Text::Tradition::Stemma;
2376359f 10
11BEGIN { extends 'Catalyst::Controller' }
12
13
14=head1 NAME
15
5c9ecf66 16stemmaweb::Controller::Stexaminer - Simple controller for stemma display
2376359f 17
18=head1 DESCRIPTION
19
20The stemma analysis tool with the pretty colored table.
21
22=head1 METHODS
23
f8d13166 24=head2 index
25
2376359f 26 GET stexaminer/$textid
27
28Renders the application for the text identified by $textid.
29
2376359f 30=cut
31
32sub index :Path :Args(1) {
33 my( $self, $c, $textid ) = @_;
34 my $m = $c->model('Directory');
35 my $tradition = $m->tradition( $textid );
1c0900ef 36 if( $tradition->stemma_count ) {
37 my $stemma = $tradition->stemma(0);
f8d13166 38 $c->stash->{svg} = $stemma->as_svg( { size => [ 600, 350 ] } );
39 $c->stash->{graphdot} = $stemma->editable({ linesep => ' ' });
1c0900ef 40 $c->stash->{text_title} = $tradition->name;
41 $c->stash->{template} = 'stexaminer.tt';
42 # TODO Run the analysis as AJAX from the loaded page.
ffa22d6f 43 my $t = run_analysis( $tradition, 'exclude_type1' => 1 );
a44aaf2a 44 # Stringify the reading groups
45 foreach my $loc ( @{$t->{'variants'}} ) {
46 my $mst = wit_stringify( $loc->{'missing'} );
47 $loc->{'missing'} = $mst;
48 foreach my $rhash ( @{$loc->{'readings'}} ) {
49 my $gst = wit_stringify( $rhash->{'group'} );
50 $rhash->{'group'} = $gst;
e41080b6 51 my $roots = join( ', ', @{$rhash->{'independent_occurrence'}} );
52 $rhash->{'independent_occurrence'} = $roots;
53 unless( $rhash->{'text'} ) {
54 $rhash->{'text'} = $rhash->{'readingid'};
55 }
a44aaf2a 56 }
57 }
23306161 58 # Values for TT rendering
1c0900ef 59 $c->stash->{variants} = $t->{'variants'};
60 $c->stash->{total} = $t->{'variant_count'};
61 $c->stash->{genealogical} = $t->{'genealogical_count'};
23306161 62 $c->stash->{conflict} = $t->{'conflict_count'};
63 # Also make a JSON stash of the data for the statistics tables
64 $c->stash->{reading_statistics} = to_json( $t->{'variants'} );
1c0900ef 65 } else {
66 $c->stash->{error} = 'Tradition ' . $tradition->name
67 . 'has no stemma for analysis.';
68 }
2376359f 69}
70
f8d13166 71=head2 graphsvg
72
73 POST stexaminer/graphsvg
74 dot: <stemmagraph dot string>
75 layerwits: [ <a.c. witnesses ]
76
77Returns an SVG string of the given graph, extended to include the given
78layered witnesses.
79
80=cut
81
82sub graphsvg :Local {
83 my( $self, $c ) = @_;
84 my $dot = $c->request->param('dot');
85 my @layerwits = $c->request->param('layerwits[]');
86 open my $stemma_fh, '<', \$dot;
87 binmode( $stemma_fh, ':encoding(UTF-8)' );
88 my $emptycoll = Text::Tradition::Collation->new();
89 my $tempstemma = Text::Tradition::Stemma->new(
90 collation => $emptycoll, 'dot' => $stemma_fh );
91 my $svgopts = { size => [ 600, 350 ] };
92 if( @layerwits ) {
93 $svgopts->{'layerwits'} = \@layerwits;
94 }
95 $c->stash->{'result'} = $tempstemma->as_svg( $svgopts );
96 $c->forward('View::SVG');
97}
98
2376359f 99=head2 end
100
101Attempt to render a view, if needed.
102
103=cut
104
105sub end : ActionClass('RenderView') {}
106
107=head1 AUTHOR
108
109Tara L Andrews
110
111=head1 LICENSE
112
113This library is free software. You can redistribute it and/or modify
114it under the same terms as Perl itself.
115
116=cut
117
118__PACKAGE__->meta->make_immutable;
119
1201;