868b3f265ed89b2ec424185e3cc6143f16ab619a
[scpubgit/stemmatology.git] / TreeOfTexts / lib / TreeOfTexts / Controller / Stemmagraph.pm
1 package TreeOfTexts::Controller::Stemmagraph;
2 use Moose;
3 use namespace::autoclean;
4 use File::Temp;
5 use Text::Tradition::Collation;
6 use Text::Tradition::Stemma;
7
8 BEGIN { extends 'Catalyst::Controller' }
9
10 #
11 # Sets the actions in this controller to be registered with no prefix
12 # so they function identically to actions created in MyApp.pm
13 #
14 __PACKAGE__->config(namespace => '');
15
16 =head1 NAME
17
18 TreeOfTexts::Controller::Root - Root Controller for TreeOfTexts
19
20 =head1 DESCRIPTION
21
22 [enter your description here]
23
24 =head1 METHODS
25
26 =cut
27
28 sub get_graph :Local {
29         my( $self, $c ) = @_;
30         # If called interactively, we have params 'display', 'output', 'witnesses'
31         # If called non-interactively, we look at headers and content.
32         # The body is actually a File::Temp object; this is undocumented but 
33         # so it seems to be.
34         my $dotfile;
35         $DB::single = 1;
36         my $must_unlink = 0;
37         if( $c->request->params->{'dot'} ) {
38             # Make a File::Temp object.
39             my $tmpfile = File::Temp->new( UNLINK => 0 );
40             print $tmpfile $c->request->params->{'dot'};
41             $dotfile = $tmpfile->filename;
42             $must_unlink = 1;
43         } else {
44             $dotfile = $c->request->body;
45         }
46         my $format = 'svg';
47
48     # Render the dot in the given format.
49     my $collation = Text::Tradition::Collation->new();
50     my $stemma = Text::Tradition::Stemma->new( 'collation' => $collation, 'dot' => $dotfile );
51     unlink( $dotfile ) if $must_unlink;
52     $c->stash->{result} = $stemma->as_svg;
53     $c->forward( "View::SVG" );
54 }
55
56 =head2 default
57
58 Standard 404 error page
59
60 =cut
61
62 sub default :Path {
63     my ( $self, $c ) = @_;
64     $c->response->body( 'Page not found' );
65     $c->response->status(404);
66 }
67
68 =head2 end
69
70 Attempt to render a view, if needed.
71
72 =cut
73
74 sub end : ActionClass('RenderView') {}
75
76 =head1 AUTHOR
77
78 Tara L Andrews
79
80 =head1 LICENSE
81
82 This library is free software. You can redistribute it and/or modify
83 it under the same terms as Perl itself.
84
85 =cut
86
87 __PACKAGE__->meta->make_immutable;
88
89 1;