remove 'meaning' relationship; apply fix to make ellipse text draggable
[scpubgit/stemmaweb.git] / lib / stemmaweb / Controller / Relation.pm
CommitLineData
b8a92065 1package stemmaweb::Controller::Relation;
2use Moose;
3use namespace::autoclean;
b28e606e 4use TryCatch;
b8a92065 5
6BEGIN { extends 'Catalyst::Controller' }
7
8
9=head1 NAME
10
11stemmaweb::Controller::Relation - Controller for the relationship mapper
12
13=head1 DESCRIPTION
14
b28e606e 15The reading relationship mapper with draggable nodes.
b8a92065 16
17=head1 METHODS
18
b28e606e 19=head2 index
20
b8a92065 21 GET relation/$textid
22
23Renders the application for the text identified by $textid.
24
b8a92065 25=cut
26
9529f69c 27sub index :Path :Args(0) {
28 my( $self, $c ) = @_;
b28e606e 29 $c->stash->{'template'} = 'relate.tt';
30}
31
e847b186 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
9529f69c 45=head2 definitions
b28e606e 46
9c2e7b80 47 GET relation/definitions
b28e606e 48
49Returns a data structure giving the valid types and scopes for a relationship.
50
51=cut
52
9c2e7b80 53sub definitions :Local :Args(0) {
b28e606e 54 my( $self, $c ) = @_;
9529f69c 55 my $valid_relationships = [ qw/ spelling orthographic grammatical meaning
56 lexical transposition / ];
b28e606e 57 my $valid_scopes = [ qw/ local global / ];
58 $c->stash->{'result'} = { 'types' => $valid_relationships, 'scopes' => $valid_scopes };
59 $c->forward('View::JSON');
b8a92065 60}
61
9529f69c 62=head2 text
b28e606e 63
9529f69c 64 GET relation/$textid/
65
66 Runs the relationship mapper for the specified text ID.
67
b28e606e 68=cut
69
9529f69c 70sub text :Chained('/') :PathPart('relation') :CaptureArgs(1) {
71 my( $self, $c, $textid ) = @_;
13aa153c 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;
75 if( $length > 700 ) {
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.
79 my @divs;
80 my $r = 0;
81 while( $r + 50 < $length ) {
82 push( @divs, $r );
83 $r += 500;
84 }
85 $c->stash->{'textsegments'} = [];
ea8e8b3c 86 foreach my $i ( 0..$#divs ) {
87 my $seg = { 'start' => $divs[$i] };
88 $seg->{'display'} = "Segment " . ($i+1);
13aa153c 89 push( @{$c->stash->{'textsegments'}}, $seg );
90 }
91 }
ea8e8b3c 92 $DB::single = 1;
13aa153c 93 $c->stash->{'textid'} = $textid;
94 $c->stash->{'tradition'} = $tradition;
9529f69c 95}
96
97sub main :Chained('text') :PathPart('') :Args(0) {
b28e606e 98 my( $self, $c ) = @_;
13aa153c 99 my $startseg = $c->req->param('start');
9c2e7b80 100 my $tradition = delete $c->stash->{'tradition'};
101 my $collation = $tradition->collation;
13aa153c 102 my $svgopts;
103 if( $startseg ) {
ea8e8b3c 104 # Only render the subgraph from startseg to +550 or end,
13aa153c 105 # whichever is less.
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.
ea8e8b3c 112 $startseg = 0;
13aa153c 113 $svgopts = { 'to' => 550 };
114 }
115 my $svg_str = $collation->as_svg( $svgopts );
9529f69c 116 $svg_str =~ s/\n//gs;
ea8e8b3c 117 $c->stash->{'startseg'} = $startseg if defined $startseg;
9529f69c 118 $c->stash->{'svg_string'} = $svg_str;
119 $c->stash->{'text_title'} = $tradition->name;
120 $c->stash->{'template'} = 'relate.tt';
b28e606e 121}
122
123=head2 relationships
124
13aa153c 125 GET relation/$textid/relationships
9529f69c 126
127Returns the list of relationships defined for this text.
b28e606e 128
13aa153c 129 POST relation/$textid/relationships { request }
9529f69c 130
131Attempts to define the requested relationship within the text. Returns 200 on
132success or 403 on error.
b28e606e 133
13aa153c 134 DELETE relation/$textid/relationships { request }
9529f69c 135
b28e606e 136
137=cut
138
9529f69c 139sub relationships :Chained('text') :PathPart :Args(0) {
b28e606e 140 my( $self, $c ) = @_;
6d124a83 141 my $tradition = delete $c->stash->{'tradition'};
142 my $collation = $tradition->collation;
cdd592f3 143 my $m = $c->model('Directory');
9529f69c 144 if( $c->request->method eq 'GET' ) {
145 my @pairs = $collation->relationships; # returns the edges
146 my @all_relations;
147 foreach my $p ( @pairs ) {
148 my $relobj = $collation->relations->get_relationship( @$p );
545163a2 149 next if $relobj->type eq 'collated'; # Don't show these
69a19c91 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 );
9529f69c 154 }
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');
162
163 my $opts = { 'type' => $relation,
69a19c91 164 'scope' => $scope };
165 $opts->{'annotation'} = $note if $note;
9529f69c 166
167 try {
168 my @vectors = $collation->add_relationship( $node, $target, $opts );
169 $c->stash->{'result'} = \@vectors;
cdd592f3 170 $m->save( $tradition );
9529f69c 171 } catch( Text::Tradition::Error $e ) {
172 $c->response->status( '403' );
173 $c->stash->{'result'} = { 'error' => $e->message };
174 }
175 } elsif( $c->request->method eq 'DELETE' ) {
176 my $node = $c->request->param('source_id');
177 my $target = $c->request->param('target_id');
178
179 try {
180 my @vectors = $collation->del_relationship( $node, $target );
cdd592f3 181 $m->save( $tradition );
9529f69c 182 $c->stash->{'result'} = \@vectors;
183 } catch( Text::Tradition::Error $e ) {
184 $c->response->status( '403' );
185 $c->stash->{'result'} = { 'error' => $e->message };
186 }
b28e606e 187 }
b28e606e 188 $c->forward('View::JSON');
189}
9529f69c 190
b28e606e 191
b8a92065 192=head2 end
193
194Attempt to render a view, if needed.
195
196=cut
197
198sub end : ActionClass('RenderView') {}
199
200=head1 AUTHOR
201
202Tara L Andrews
203
204=head1 LICENSE
205
206This library is free software. You can redistribute it and/or modify
207it under the same terms as Perl itself.
208
209=cut
210
211__PACKAGE__->meta->make_immutable;
212
2131;