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 $start ( @divs ) {
87 my $seg = { 'start' => $start };
88 $seg->{'end'} = $start + 550 > $length ? $length : $start + 550;
89 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 +500 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.
111 $svgopts = { 'to' => 550 };
113 my $svg_str = $collation->as_svg( $svgopts );
114 $svg_str =~ s/\n//gs;
115 $c->stash->{'svg_string'} = $svg_str;
116 $c->stash->{'text_title'} = $tradition->name;
117 $c->stash->{'template'} = 'relate.tt';
122 GET relation/$textid/relationships
124 Returns the list of relationships defined for this text.
126 POST relation/$textid/relationships { request }
128 Attempts to define the requested relationship within the text. Returns 200 on
129 success or 403 on error.
131 DELETE relation/$textid/relationships { request }
136 sub relationships :Chained('text') :PathPart :Args(0) {
137 my( $self, $c ) = @_;
138 my $tradition = delete $c->stash->{'tradition'};
139 my $collation = $tradition->collation;
140 my $m = $c->model('Directory');
141 if( $c->request->method eq 'GET' ) {
142 my @pairs = $collation->relationships; # returns the edges
144 foreach my $p ( @pairs ) {
145 my $relobj = $collation->relations->get_relationship( @$p );
146 next if $relobj->type eq 'collated'; # Don't show these
147 my $relhash = { source => $p->[0], target => $p->[1],
148 type => $relobj->type, scope => $relobj->scope };
149 $relhash->{'note'} = $relobj->annotation if $relobj->has_annotation;
150 push( @all_relations, $relhash );
152 $c->stash->{'result'} = \@all_relations;
153 } elsif( $c->request->method eq 'POST' ) {
154 my $node = $c->request->param('source_id');
155 my $target = $c->request->param('target_id');
156 my $relation = $c->request->param('rel_type');
157 my $note = $c->request->param('note');
158 my $scope = $c->request->param('scope');
160 my $opts = { 'type' => $relation,
162 $opts->{'annotation'} = $note if $note;
165 my @vectors = $collation->add_relationship( $node, $target, $opts );
166 $c->stash->{'result'} = \@vectors;
167 $m->save( $tradition );
168 } catch( Text::Tradition::Error $e ) {
169 $c->response->status( '403' );
170 $c->stash->{'result'} = { 'error' => $e->message };
172 } elsif( $c->request->method eq 'DELETE' ) {
173 my $node = $c->request->param('source_id');
174 my $target = $c->request->param('target_id');
177 my @vectors = $collation->del_relationship( $node, $target );
178 $m->save( $tradition );
179 $c->stash->{'result'} = \@vectors;
180 } catch( Text::Tradition::Error $e ) {
181 $c->response->status( '403' );
182 $c->stash->{'result'} = { 'error' => $e->message };
185 $c->forward('View::JSON');
191 Attempt to render a view, if needed.
195 sub end : ActionClass('RenderView') {}
203 This library is free software. You can redistribute it and/or modify
204 it under the same terms as Perl itself.
208 __PACKAGE__->meta->make_immutable;