1 package stemmaweb::Controller::Relation;
3 use namespace::autoclean;
6 BEGIN { extends 'Catalyst::Controller' }
11 stemmaweb::Controller::Relation - Controller for the relationship mapper
15 The reading relationship mapper with draggable nodes.
23 Renders the application for the text identified by $textid.
27 sub index :Path :Args(0) {
29 $c->stash->{'template'} = 'relate.tt';
36 Returns the help window HTML.
40 sub help :Local :Args(0) {
42 $c->stash->{'template'} = 'relatehelp.tt';
47 GET relation/definitions
49 Returns a data structure giving the valid types and scopes for a relationship.
53 sub definitions :Local :Args(0) {
55 my $valid_relationships = [ qw/ spelling orthographic grammatical lexical transposition / ];
56 my $valid_scopes = [ qw/ local global / ];
57 $c->stash->{'result'} = { 'types' => $valid_relationships, 'scopes' => $valid_scopes };
58 $c->forward('View::JSON');
65 Runs the relationship mapper for the specified text ID.
69 sub text :Chained('/') :PathPart('relation') :CaptureArgs(1) {
70 my( $self, $c, $textid ) = @_;
71 # If the tradition has more than 500 ranks or so, split it up.
72 my $tradition = $c->model('Directory')->tradition( $textid );
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;
77 my $length = $tradition->collation->end->rank;
78 # We should display no more than roughly 500 nodes, or roughly 700
80 my $segments = $numnodes / 500;
81 if( $numedges / 700 > $segments ) {
82 $segments = $numedges / 700;
84 my $segsize = sprintf( "%.0f", $length / $segments );
85 my $margin = sprintf( "%.0f", $segsize / 10 );
87 # Segment the tradition in order not to overload the browser.
90 while( $r + $margin < $length ) {
94 $c->stash->{'textsegments'} = [];
95 $c->stash->{'segsize'} = $segsize;
96 $c->stash->{'margin'} = $margin;
97 foreach my $i ( 0..$#divs ) {
98 my $seg = { 'start' => $divs[$i] };
99 $seg->{'display'} = "Segment " . ($i+1);
100 push( @{$c->stash->{'textsegments'}}, $seg );
104 $c->stash->{'textid'} = $textid;
105 $c->stash->{'tradition'} = $tradition;
108 sub main :Chained('text') :PathPart('') :Args(0) {
109 my( $self, $c ) = @_;
110 my $startseg = $c->req->param('start');
111 my $tradition = delete $c->stash->{'tradition'};
112 my $collation = $tradition->collation;
115 # Only render the subgraph from startseg to endseg or to END,
117 my $endseg = $startseg + $c->stash->{'segsize'} + $c->stash->{'margin'};
118 $svgopts = { 'from' => $startseg };
119 $svgopts->{'to'} = $endseg if $endseg < $collation->end->rank;
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.
123 my $endseg = $c->stash->{'segsize'} + $c->stash->{'margin'};
125 $svgopts = { 'to' => $endseg };
127 my $svg_str = $collation->as_svg( $svgopts );
128 $svg_str =~ s/\n//gs;
129 $c->stash->{'startseg'} = $startseg if defined $startseg;
130 $c->stash->{'svg_string'} = $svg_str;
131 $c->stash->{'text_title'} = $tradition->name;
132 $c->stash->{'template'} = 'relate.tt';
137 GET relation/$textid/relationships
139 Returns the list of relationships defined for this text.
141 POST relation/$textid/relationships { request }
143 Attempts to define the requested relationship within the text. Returns 200 on
144 success or 403 on error.
146 DELETE relation/$textid/relationships { request }
151 sub relationships :Chained('text') :PathPart :Args(0) {
152 my( $self, $c ) = @_;
153 my $tradition = delete $c->stash->{'tradition'};
154 my $collation = $tradition->collation;
155 my $m = $c->model('Directory');
156 if( $c->request->method eq 'GET' ) {
157 my @pairs = $collation->relationships; # returns the edges
159 foreach my $p ( @pairs ) {
160 my $relobj = $collation->relations->get_relationship( @$p );
161 next if $relobj->type eq 'collated'; # Don't show these
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 );
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');
175 my $opts = { 'type' => $relation,
177 $opts->{'annotation'} = $note if $note;
180 my @vectors = $collation->add_relationship( $node, $target, $opts );
181 $c->stash->{'result'} = \@vectors;
182 $m->save( $tradition );
183 } catch( Text::Tradition::Error $e ) {
184 $c->response->status( '403' );
185 $c->stash->{'result'} = { 'error' => $e->message };
187 } elsif( $c->request->method eq 'DELETE' ) {
188 my $node = $c->request->param('source_id');
189 my $target = $c->request->param('target_id');
192 my @vectors = $collation->del_relationship( $node, $target );
193 $m->save( $tradition );
194 $c->stash->{'result'} = \@vectors;
195 } catch( Text::Tradition::Error $e ) {
196 $c->response->status( '403' );
197 $c->stash->{'result'} = { 'error' => $e->message };
200 $c->forward('View::JSON');
206 Attempt to render a view, if needed.
210 sub end : ActionClass('RenderView') {}
218 This library is free software. You can redistribute it and/or modify
219 it under the same terms as Perl itself.
223 __PACKAGE__->meta->make_immutable;