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