pass $self to a method call, oops
[scpubgit/stemmatology.git] / stemmaweb / lib / stemmaweb / Controller / Relation.pm
CommitLineData
5c9ecf66 1package stemmaweb::Controller::Relation;
2376359f 2use Moose;
3use namespace::autoclean;
581aee24 4use TryCatch;
2376359f 5
6BEGIN { extends 'Catalyst::Controller' }
7
8
9=head1 NAME
10
5c9ecf66 11stemmaweb::Controller::Relation - Controller for the relationship mapper
2376359f 12
13=head1 DESCRIPTION
14
581aee24 15The reading relationship mapper with draggable nodes.
2376359f 16
17=head1 METHODS
18
581aee24 19=head2 index
20
2376359f 21 GET relation/$textid
22
23Renders the application for the text identified by $textid.
24
2376359f 25=cut
26
72874569 27sub index :Path :Args(0) {
28 my( $self, $c ) = @_;
581aee24 29 $c->stash->{'template'} = 'relate.tt';
30}
31
9ca77245 32=head2 help
33
34 GET relation/help
35
36Returns the help window HTML.
37
38=cut
39
40sub help :Local :Args(0) {
41 my( $self, $c ) = @_;
42 $c->stash->{'template'} = 'relatehelp.tt';
43}
44
72874569 45=head2 definitions
581aee24 46
1c0900ef 47 GET relation/definitions
581aee24 48
49Returns a data structure giving the valid types and scopes for a relationship.
50
51=cut
52
1c0900ef 53sub definitions :Local :Args(0) {
581aee24 54 my( $self, $c ) = @_;
d81fdda0 55 my $valid_relationships = [ qw/ spelling orthographic grammatical lexical transposition / ];
581aee24 56 my $valid_scopes = [ qw/ local global / ];
57 $c->stash->{'result'} = { 'types' => $valid_relationships, 'scopes' => $valid_scopes };
58 $c->forward('View::JSON');
2376359f 59}
60
72874569 61=head2 text
581aee24 62
72874569 63 GET relation/$textid/
64
65 Runs the relationship mapper for the specified text ID.
66
581aee24 67=cut
68
72874569 69sub text :Chained('/') :PathPart('relation') :CaptureArgs(1) {
70 my( $self, $c, $textid ) = @_;
93daee83 71 # If the tradition has more than 500 ranks or so, split it up.
72 my $tradition = $c->model('Directory')->tradition( $textid );
22bb5720 73 # See how big the tradition is. Edges are more important than nodes
74 # when it comes to rendering difficulty.
75 my $numnodes = scalar $tradition->collation->readings;
76 my $numedges = scalar $tradition->collation->paths;
93daee83 77 my $length = $tradition->collation->end->rank;
22bb5720 78 # We should display no more than roughly 500 nodes, or roughly 700
79 # edges, at a time.
80 my $segments = $numnodes / 500;
81 if( $numedges / 700 > $segments ) {
82 $segments = $numedges / 700;
83 }
84 my $segsize = sprintf( "%.0f", $length / $segments );
85 my $margin = sprintf( "%.0f", $segsize / 10 );
86 if( $segments > 1 ) {
93daee83 87 # Segment the tradition in order not to overload the browser.
93daee83 88 my @divs;
89 my $r = 0;
22bb5720 90 while( $r + $margin < $length ) {
93daee83 91 push( @divs, $r );
22bb5720 92 $r += $segsize;
93daee83 93 }
94 $c->stash->{'textsegments'} = [];
22bb5720 95 $c->stash->{'segsize'} = $segsize;
96 $c->stash->{'margin'} = $margin;
0d51383b 97 foreach my $i ( 0..$#divs ) {
98 my $seg = { 'start' => $divs[$i] };
99 $seg->{'display'} = "Segment " . ($i+1);
93daee83 100 push( @{$c->stash->{'textsegments'}}, $seg );
101 }
102 }
0d51383b 103 $DB::single = 1;
93daee83 104 $c->stash->{'textid'} = $textid;
105 $c->stash->{'tradition'} = $tradition;
72874569 106}
107
108sub main :Chained('text') :PathPart('') :Args(0) {
581aee24 109 my( $self, $c ) = @_;
93daee83 110 my $startseg = $c->req->param('start');
1c0900ef 111 my $tradition = delete $c->stash->{'tradition'};
112 my $collation = $tradition->collation;
93daee83 113 my $svgopts;
114 if( $startseg ) {
22bb5720 115 # Only render the subgraph from startseg to endseg or to END,
93daee83 116 # whichever is less.
22bb5720 117 my $endseg = $startseg + $c->stash->{'segsize'} + $c->stash->{'margin'};
93daee83 118 $svgopts = { 'from' => $startseg };
22bb5720 119 $svgopts->{'to'} = $endseg if $endseg < $collation->end->rank;
93daee83 120 } elsif( exists $c->stash->{'textsegments'} ) {
121 # This is the unqualified load of a long tradition. We implicitly start
122 # at zero, but go only as far as 550.
22bb5720 123 my $endseg = $c->stash->{'segsize'} + $c->stash->{'margin'};
0d51383b 124 $startseg = 0;
22bb5720 125 $svgopts = { 'to' => $endseg };
93daee83 126 }
127 my $svg_str = $collation->as_svg( $svgopts );
72874569 128 $svg_str =~ s/\n//gs;
0d51383b 129 $c->stash->{'startseg'} = $startseg if defined $startseg;
72874569 130 $c->stash->{'svg_string'} = $svg_str;
131 $c->stash->{'text_title'} = $tradition->name;
132 $c->stash->{'template'} = 'relate.tt';
581aee24 133}
134
135=head2 relationships
136
93daee83 137 GET relation/$textid/relationships
72874569 138
139Returns the list of relationships defined for this text.
581aee24 140
93daee83 141 POST relation/$textid/relationships { request }
72874569 142
143Attempts to define the requested relationship within the text. Returns 200 on
144success or 403 on error.
581aee24 145
93daee83 146 DELETE relation/$textid/relationships { request }
72874569 147
581aee24 148
149=cut
150
72874569 151sub relationships :Chained('text') :PathPart :Args(0) {
581aee24 152 my( $self, $c ) = @_;
bff7bb42 153 my $tradition = delete $c->stash->{'tradition'};
154 my $collation = $tradition->collation;
7c280843 155 my $m = $c->model('Directory');
72874569 156 if( $c->request->method eq 'GET' ) {
157 my @pairs = $collation->relationships; # returns the edges
158 my @all_relations;
159 foreach my $p ( @pairs ) {
160 my $relobj = $collation->relations->get_relationship( @$p );
9251a7be 161 next if $relobj->type eq 'collated'; # Don't show these
31aaf446 162 my $relhash = { source => $p->[0], target => $p->[1],
163 type => $relobj->type, scope => $relobj->scope };
164 $relhash->{'note'} = $relobj->annotation if $relobj->has_annotation;
165 push( @all_relations, $relhash );
72874569 166 }
167 $c->stash->{'result'} = \@all_relations;
168 } elsif( $c->request->method eq 'POST' ) {
169 my $node = $c->request->param('source_id');
170 my $target = $c->request->param('target_id');
171 my $relation = $c->request->param('rel_type');
172 my $note = $c->request->param('note');
173 my $scope = $c->request->param('scope');
174
175 my $opts = { 'type' => $relation,
31aaf446 176 'scope' => $scope };
177 $opts->{'annotation'} = $note if $note;
72874569 178
179 try {
180 my @vectors = $collation->add_relationship( $node, $target, $opts );
181 $c->stash->{'result'} = \@vectors;
7c280843 182 $m->save( $tradition );
72874569 183 } catch( Text::Tradition::Error $e ) {
184 $c->response->status( '403' );
185 $c->stash->{'result'} = { 'error' => $e->message };
186 }
187 } elsif( $c->request->method eq 'DELETE' ) {
188 my $node = $c->request->param('source_id');
189 my $target = $c->request->param('target_id');
190
191 try {
192 my @vectors = $collation->del_relationship( $node, $target );
7c280843 193 $m->save( $tradition );
72874569 194 $c->stash->{'result'} = \@vectors;
195 } catch( Text::Tradition::Error $e ) {
196 $c->response->status( '403' );
197 $c->stash->{'result'} = { 'error' => $e->message };
198 }
581aee24 199 }
581aee24 200 $c->forward('View::JSON');
201}
72874569 202
581aee24 203
2376359f 204=head2 end
205
206Attempt to render a view, if needed.
207
208=cut
209
210sub end : ActionClass('RenderView') {}
211
212=head1 AUTHOR
213
214Tara L Andrews
215
216=head1 LICENSE
217
218This library is free software. You can redistribute it and/or modify
219it under the same terms as Perl itself.
220
221=cut
222
223__PACKAGE__->meta->make_immutable;
224
2251;