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 meaning
56 lexical transposition / ];
57 my $valid_scopes = [ qw/ local global / ];
58 $c->stash->{'result'} = { 'types' => $valid_relationships, 'scopes' => $valid_scopes };
59 $c->forward('View::JSON');
66 Runs the relationship mapper for the specified text ID.
70 sub text :Chained('/') :PathPart('relation') :CaptureArgs(1) {
71 my( $self, $c, $textid ) = @_;
72 # If the tradition has more than 500 ranks or so, split it up.
73 my $tradition = $c->model('Directory')->tradition( $textid );
74 my $length = $tradition->collation->end->rank;
76 # Segment the tradition in order not to overload the browser.
77 # Split it up into units of 500 ranks, but have each segment show
78 # 550 ranks so that overlap works.
81 while( $r + 50 < $length ) {
85 $c->stash->{'textsegments'} = [];
86 foreach my $i ( 0..$#divs ) {
87 my $seg = { 'start' => $divs[$i] };
88 $seg->{'display'} = "Segment " . ($i+1);
89 push( @{$c->stash->{'textsegments'}}, $seg );
93 $c->stash->{'textid'} = $textid;
94 $c->stash->{'tradition'} = $tradition;
97 sub main :Chained('text') :PathPart('') :Args(0) {
99 my $startseg = $c->req->param('start');
100 my $tradition = delete $c->stash->{'tradition'};
101 my $collation = $tradition->collation;
104 # Only render the subgraph from startseg to +550 or end,
106 $svgopts = { 'from' => $startseg };
107 $svgopts->{'to'} = $startseg + 550
108 if $startseg + 550 < $collation->end->rank;
109 } elsif( exists $c->stash->{'textsegments'} ) {
110 # This is the unqualified load of a long tradition. We implicitly start
111 # at zero, but go only as far as 550.
113 $svgopts = { 'to' => 550 };
115 my $svg_str = $collation->as_svg( $svgopts );
116 $svg_str =~ s/\n//gs;
117 $c->stash->{'startseg'} = $startseg if defined $startseg;
118 $c->stash->{'svg_string'} = $svg_str;
119 $c->stash->{'text_title'} = $tradition->name;
120 $c->stash->{'template'} = 'relate.tt';
125 GET relation/$textid/relationships
127 Returns the list of relationships defined for this text.
129 POST relation/$textid/relationships { request }
131 Attempts to define the requested relationship within the text. Returns 200 on
132 success or 403 on error.
134 DELETE relation/$textid/relationships { request }
139 sub relationships :Chained('text') :PathPart :Args(0) {
140 my( $self, $c ) = @_;
141 my $tradition = delete $c->stash->{'tradition'};
142 my $collation = $tradition->collation;
143 my $m = $c->model('Directory');
144 if( $c->request->method eq 'GET' ) {
145 my @pairs = $collation->relationships; # returns the edges
147 foreach my $p ( @pairs ) {
148 my $relobj = $collation->relations->get_relationship( @$p );
149 next if $relobj->type eq 'collated'; # Don't show these
150 my $relhash = { source => $p->[0], target => $p->[1],
151 type => $relobj->type, scope => $relobj->scope };
152 $relhash->{'note'} = $relobj->annotation if $relobj->has_annotation;
153 push( @all_relations, $relhash );
155 $c->stash->{'result'} = \@all_relations;
156 } elsif( $c->request->method eq 'POST' ) {
157 my $node = $c->request->param('source_id');
158 my $target = $c->request->param('target_id');
159 my $relation = $c->request->param('rel_type');
160 my $note = $c->request->param('note');
161 my $scope = $c->request->param('scope');
163 my $opts = { 'type' => $relation,
165 $opts->{'annotation'} = $note if $note;
168 my @vectors = $collation->add_relationship( $node, $target, $opts );
169 $c->stash->{'result'} = \@vectors;
170 $m->save( $tradition );
171 } catch( Text::Tradition::Error $e ) {
172 $c->response->status( '403' );
173 $c->stash->{'result'} = { 'error' => $e->message };
175 } elsif( $c->request->method eq 'DELETE' ) {
176 my $node = $c->request->param('source_id');
177 my $target = $c->request->param('target_id');
180 my @vectors = $collation->del_relationship( $node, $target );
181 $m->save( $tradition );
182 $c->stash->{'result'} = \@vectors;
183 } catch( Text::Tradition::Error $e ) {
184 $c->response->status( '403' );
185 $c->stash->{'result'} = { 'error' => $e->message };
188 $c->forward('View::JSON');
194 Attempt to render a view, if needed.
198 sub end : ActionClass('RenderView') {}
206 This library is free software. You can redistribute it and/or modify
207 it under the same terms as Perl itself.
211 __PACKAGE__->meta->make_immutable;