Commit | Line | Data |
b8a92065 |
1 | package stemmaweb::Controller::Relation; |
2 | use Moose; |
3 | use namespace::autoclean; |
b28e606e |
4 | use TryCatch; |
b8a92065 |
5 | |
6 | BEGIN { extends 'Catalyst::Controller' } |
7 | |
8 | |
9 | =head1 NAME |
10 | |
11 | stemmaweb::Controller::Relation - Controller for the relationship mapper |
12 | |
13 | =head1 DESCRIPTION |
14 | |
b28e606e |
15 | The reading relationship mapper with draggable nodes. |
b8a92065 |
16 | |
17 | =head1 METHODS |
18 | |
b28e606e |
19 | =head2 index |
20 | |
b8a92065 |
21 | GET relation/$textid |
22 | |
23 | Renders the application for the text identified by $textid. |
24 | |
b8a92065 |
25 | =cut |
26 | |
9529f69c |
27 | sub 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 | |
36 | Returns the help window HTML. |
37 | |
38 | =cut |
39 | |
40 | sub 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 | |
49 | Returns a data structure giving the valid types and scopes for a relationship. |
50 | |
51 | =cut |
52 | |
9c2e7b80 |
53 | sub 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 |
70 | sub text :Chained('/') :PathPart('relation') :CaptureArgs(1) { |
71 | my( $self, $c, $textid ) = @_; |
72 | $c->stash->{'tradition'} = $c->model('Directory')->tradition( $textid ); |
73 | } |
74 | |
75 | sub main :Chained('text') :PathPart('') :Args(0) { |
b28e606e |
76 | my( $self, $c ) = @_; |
9c2e7b80 |
77 | my $tradition = delete $c->stash->{'tradition'}; |
78 | my $collation = $tradition->collation; |
9529f69c |
79 | my $svg_str = $collation->as_svg; |
80 | $svg_str =~ s/\n//gs; |
81 | $c->stash->{'svg_string'} = $svg_str; |
82 | $c->stash->{'text_title'} = $tradition->name; |
83 | $c->stash->{'template'} = 'relate.tt'; |
84 | |
b28e606e |
85 | } |
86 | |
87 | =head2 relationships |
88 | |
9529f69c |
89 | GET $textid/relationships |
90 | |
91 | Returns the list of relationships defined for this text. |
b28e606e |
92 | |
9529f69c |
93 | POST $textid/relationships { request } |
94 | |
95 | Attempts to define the requested relationship within the text. Returns 200 on |
96 | success or 403 on error. |
b28e606e |
97 | |
9529f69c |
98 | DELETE $textid/relationships { request } |
99 | |
b28e606e |
100 | |
101 | =cut |
102 | |
9529f69c |
103 | sub relationships :Chained('text') :PathPart :Args(0) { |
b28e606e |
104 | my( $self, $c ) = @_; |
6d124a83 |
105 | my $tradition = delete $c->stash->{'tradition'}; |
106 | my $collation = $tradition->collation; |
cdd592f3 |
107 | my $m = $c->model('Directory'); |
9529f69c |
108 | if( $c->request->method eq 'GET' ) { |
109 | my @pairs = $collation->relationships; # returns the edges |
110 | my @all_relations; |
111 | foreach my $p ( @pairs ) { |
112 | my $relobj = $collation->relations->get_relationship( @$p ); |
545163a2 |
113 | next if $relobj->type eq 'collated'; # Don't show these |
69a19c91 |
114 | my $relhash = { source => $p->[0], target => $p->[1], |
115 | type => $relobj->type, scope => $relobj->scope }; |
116 | $relhash->{'note'} = $relobj->annotation if $relobj->has_annotation; |
117 | push( @all_relations, $relhash ); |
9529f69c |
118 | } |
119 | $c->stash->{'result'} = \@all_relations; |
120 | } elsif( $c->request->method eq 'POST' ) { |
121 | my $node = $c->request->param('source_id'); |
122 | my $target = $c->request->param('target_id'); |
123 | my $relation = $c->request->param('rel_type'); |
124 | my $note = $c->request->param('note'); |
125 | my $scope = $c->request->param('scope'); |
126 | |
127 | my $opts = { 'type' => $relation, |
69a19c91 |
128 | 'scope' => $scope }; |
129 | $opts->{'annotation'} = $note if $note; |
9529f69c |
130 | |
131 | try { |
132 | my @vectors = $collation->add_relationship( $node, $target, $opts ); |
133 | $c->stash->{'result'} = \@vectors; |
cdd592f3 |
134 | $m->save( $tradition ); |
9529f69c |
135 | } catch( Text::Tradition::Error $e ) { |
136 | $c->response->status( '403' ); |
137 | $c->stash->{'result'} = { 'error' => $e->message }; |
138 | } |
139 | } elsif( $c->request->method eq 'DELETE' ) { |
140 | my $node = $c->request->param('source_id'); |
141 | my $target = $c->request->param('target_id'); |
142 | |
143 | try { |
144 | my @vectors = $collation->del_relationship( $node, $target ); |
cdd592f3 |
145 | $m->save( $tradition ); |
9529f69c |
146 | $c->stash->{'result'} = \@vectors; |
147 | } catch( Text::Tradition::Error $e ) { |
148 | $c->response->status( '403' ); |
149 | $c->stash->{'result'} = { 'error' => $e->message }; |
150 | } |
b28e606e |
151 | } |
b28e606e |
152 | $c->forward('View::JSON'); |
153 | } |
9529f69c |
154 | |
b28e606e |
155 | |
b8a92065 |
156 | =head2 end |
157 | |
158 | Attempt to render a view, if needed. |
159 | |
160 | =cut |
161 | |
162 | sub end : ActionClass('RenderView') {} |
163 | |
164 | =head1 AUTHOR |
165 | |
166 | Tara L Andrews |
167 | |
168 | =head1 LICENSE |
169 | |
170 | This library is free software. You can redistribute it and/or modify |
171 | it under the same terms as Perl itself. |
172 | |
173 | =cut |
174 | |
175 | __PACKAGE__->meta->make_immutable; |
176 | |
177 | 1; |