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 my $length = $tradition->collation->end->rank;
75 # Segment the tradition in order not to overload the browser.
76 # Split it up into units of 500 ranks, but have each segment show
77 # 550 ranks so that overlap works.
80 while( $r + 50 < $length ) {
84 $c->stash->{'textsegments'} = [];
85 foreach my $i ( 0..$#divs ) {
86 my $seg = { 'start' => $divs[$i] };
87 $seg->{'display'} = "Segment " . ($i+1);
88 push( @{$c->stash->{'textsegments'}}, $seg );
92 $c->stash->{'textid'} = $textid;
93 $c->stash->{'tradition'} = $tradition;
96 sub main :Chained('text') :PathPart('') :Args(0) {
98 my $startseg = $c->req->param('start');
99 my $tradition = delete $c->stash->{'tradition'};
100 my $collation = $tradition->collation;
103 # Only render the subgraph from startseg to +550 or end,
105 $svgopts = { 'from' => $startseg };
106 $svgopts->{'to'} = $startseg + 550
107 if $startseg + 550 < $collation->end->rank;
108 } elsif( exists $c->stash->{'textsegments'} ) {
109 # This is the unqualified load of a long tradition. We implicitly start
110 # at zero, but go only as far as 550.
112 $svgopts = { 'to' => 550 };
114 my $svg_str = $collation->as_svg( $svgopts );
115 $svg_str =~ s/\n//gs;
116 $c->stash->{'startseg'} = $startseg if defined $startseg;
117 $c->stash->{'svg_string'} = $svg_str;
118 $c->stash->{'text_title'} = $tradition->name;
119 $c->stash->{'template'} = 'relate.tt';
124 GET relation/$textid/relationships
126 Returns the list of relationships defined for this text.
128 POST relation/$textid/relationships { request }
130 Attempts to define the requested relationship within the text. Returns 200 on
131 success or 403 on error.
133 DELETE relation/$textid/relationships { request }
138 sub relationships :Chained('text') :PathPart :Args(0) {
139 my( $self, $c ) = @_;
140 my $tradition = delete $c->stash->{'tradition'};
141 my $collation = $tradition->collation;
142 my $m = $c->model('Directory');
143 if( $c->request->method eq 'GET' ) {
144 my @pairs = $collation->relationships; # returns the edges
146 foreach my $p ( @pairs ) {
147 my $relobj = $collation->relations->get_relationship( @$p );
148 next if $relobj->type eq 'collated'; # Don't show these
149 my $relhash = { source => $p->[0], target => $p->[1],
150 type => $relobj->type, scope => $relobj->scope };
151 $relhash->{'note'} = $relobj->annotation if $relobj->has_annotation;
152 push( @all_relations, $relhash );
154 $c->stash->{'result'} = \@all_relations;
155 } elsif( $c->request->method eq 'POST' ) {
156 my $node = $c->request->param('source_id');
157 my $target = $c->request->param('target_id');
158 my $relation = $c->request->param('rel_type');
159 my $note = $c->request->param('note');
160 my $scope = $c->request->param('scope');
162 my $opts = { 'type' => $relation,
164 $opts->{'annotation'} = $note if $note;
167 my @vectors = $collation->add_relationship( $node, $target, $opts );
168 $c->stash->{'result'} = \@vectors;
169 $m->save( $tradition );
170 } catch( Text::Tradition::Error $e ) {
171 $c->response->status( '403' );
172 $c->stash->{'result'} = { 'error' => $e->message };
174 } elsif( $c->request->method eq 'DELETE' ) {
175 my $node = $c->request->param('source_id');
176 my $target = $c->request->param('target_id');
179 my @vectors = $collation->del_relationship( $node, $target );
180 $m->save( $tradition );
181 $c->stash->{'result'} = \@vectors;
182 } catch( Text::Tradition::Error $e ) {
183 $c->response->status( '403' );
184 $c->stash->{'result'} = { 'error' => $e->message };
187 $c->forward('View::JSON');
193 Attempt to render a view, if needed.
197 sub end : ActionClass('RenderView') {}
205 This library is free software. You can redistribute it and/or modify
206 it under the same terms as Perl itself.
210 __PACKAGE__->meta->make_immutable;