add help text to relationship mapper
[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
593d5700 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 ) = @_;
72874569 55 my $valid_relationships = [ qw/ spelling orthographic grammatical meaning
56 lexical transposition / ];
581aee24 57 my $valid_scopes = [ qw/ local global / ];
58 $c->stash->{'result'} = { 'types' => $valid_relationships, 'scopes' => $valid_scopes };
59 $c->forward('View::JSON');
2376359f 60}
61
72874569 62=head2 text
581aee24 63
72874569 64 GET relation/$textid/
65
66 Runs the relationship mapper for the specified text ID.
67
581aee24 68=cut
69
72874569 70sub text :Chained('/') :PathPart('relation') :CaptureArgs(1) {
71 my( $self, $c, $textid ) = @_;
72 $c->stash->{'tradition'} = $c->model('Directory')->tradition( $textid );
73}
74
75sub main :Chained('text') :PathPart('') :Args(0) {
581aee24 76 my( $self, $c ) = @_;
1c0900ef 77 my $tradition = delete $c->stash->{'tradition'};
78 my $collation = $tradition->collation;
72874569 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
581aee24 85}
86
87=head2 relationships
88
72874569 89 GET $textid/relationships
90
91Returns the list of relationships defined for this text.
581aee24 92
72874569 93 POST $textid/relationships { request }
94
95Attempts to define the requested relationship within the text. Returns 200 on
96success or 403 on error.
581aee24 97
72874569 98 DELETE $textid/relationships { request }
99
581aee24 100
101=cut
102
72874569 103sub relationships :Chained('text') :PathPart :Args(0) {
581aee24 104 my( $self, $c ) = @_;
bff7bb42 105 my $tradition = delete $c->stash->{'tradition'};
106 my $collation = $tradition->collation;
7c280843 107 my $m = $c->model('Directory');
72874569 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 );
31aaf446 113 my $relhash = { source => $p->[0], target => $p->[1],
114 type => $relobj->type, scope => $relobj->scope };
115 $relhash->{'note'} = $relobj->annotation if $relobj->has_annotation;
116 push( @all_relations, $relhash );
72874569 117 }
118 $c->stash->{'result'} = \@all_relations;
119 } elsif( $c->request->method eq 'POST' ) {
120 my $node = $c->request->param('source_id');
121 my $target = $c->request->param('target_id');
122 my $relation = $c->request->param('rel_type');
123 my $note = $c->request->param('note');
124 my $scope = $c->request->param('scope');
125
126 my $opts = { 'type' => $relation,
31aaf446 127 'scope' => $scope };
128 $opts->{'annotation'} = $note if $note;
72874569 129
130 try {
131 my @vectors = $collation->add_relationship( $node, $target, $opts );
132 $c->stash->{'result'} = \@vectors;
7c280843 133 $m->save( $tradition );
72874569 134 } catch( Text::Tradition::Error $e ) {
135 $c->response->status( '403' );
136 $c->stash->{'result'} = { 'error' => $e->message };
137 }
138 } elsif( $c->request->method eq 'DELETE' ) {
139 my $node = $c->request->param('source_id');
140 my $target = $c->request->param('target_id');
141
142 try {
143 my @vectors = $collation->del_relationship( $node, $target );
7c280843 144 $m->save( $tradition );
72874569 145 $c->stash->{'result'} = \@vectors;
146 } catch( Text::Tradition::Error $e ) {
147 $c->response->status( '403' );
148 $c->stash->{'result'} = { 'error' => $e->message };
149 }
581aee24 150 }
581aee24 151 $c->forward('View::JSON');
152}
72874569 153
581aee24 154
2376359f 155=head2 end
156
157Attempt to render a view, if needed.
158
159=cut
160
161sub end : ActionClass('RenderView') {}
162
163=head1 AUTHOR
164
165Tara L Andrews
166
167=head1 LICENSE
168
169This library is free software. You can redistribute it and/or modify
170it under the same terms as Perl itself.
171
172=cut
173
174__PACKAGE__->meta->make_immutable;
175
1761;