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';
34 GET relation/definitions
36 Returns a data structure giving the valid types and scopes for a relationship.
40 sub definitions :Local :Args(0) {
42 my $valid_relationships = [ qw/ spelling orthographic grammatical meaning
43 lexical transposition / ];
44 my $valid_scopes = [ qw/ local global / ];
45 $c->stash->{'result'} = { 'types' => $valid_relationships, 'scopes' => $valid_scopes };
46 $c->forward('View::JSON');
53 Runs the relationship mapper for the specified text ID.
57 sub text :Chained('/') :PathPart('relation') :CaptureArgs(1) {
58 my( $self, $c, $textid ) = @_;
59 $c->stash->{'tradition'} = $c->model('Directory')->tradition( $textid );
62 sub main :Chained('text') :PathPart('') :Args(0) {
64 my $tradition = delete $c->stash->{'tradition'};
65 my $collation = $tradition->collation;
66 my $svg_str = $collation->as_svg;
68 $c->stash->{'svg_string'} = $svg_str;
69 $c->stash->{'text_title'} = $tradition->name;
70 $c->stash->{'template'} = 'relate.tt';
76 GET $textid/relationships
78 Returns the list of relationships defined for this text.
80 POST $textid/relationships { request }
82 Attempts to define the requested relationship within the text. Returns 200 on
83 success or 403 on error.
85 DELETE $textid/relationships { request }
90 sub relationships :Chained('text') :PathPart :Args(0) {
92 my $tradition = delete $c->stash->{'tradition'};
93 my $collation = $tradition->collation;
94 my $m = $c->model('Directory');
95 if( $c->request->method eq 'GET' ) {
96 my @pairs = $collation->relationships; # returns the edges
98 foreach my $p ( @pairs ) {
99 my $relobj = $collation->relations->get_relationship( @$p );
100 my $relhash = { source => $p->[0], target => $p->[1],
101 type => $relobj->type, scope => $relobj->scope };
102 $relhash->{'note'} = $relobj->annotation if $relobj->has_annotation;
103 push( @all_relations, $relhash );
105 $c->stash->{'result'} = \@all_relations;
106 } elsif( $c->request->method eq 'POST' ) {
107 my $node = $c->request->param('source_id');
108 my $target = $c->request->param('target_id');
109 my $relation = $c->request->param('rel_type');
110 my $note = $c->request->param('note');
111 my $scope = $c->request->param('scope');
113 my $opts = { 'type' => $relation,
115 $opts->{'annotation'} = $note if $note;
118 my @vectors = $collation->add_relationship( $node, $target, $opts );
119 $c->stash->{'result'} = \@vectors;
120 $m->save( $tradition );
121 } catch( Text::Tradition::Error $e ) {
122 $c->response->status( '403' );
123 $c->stash->{'result'} = { 'error' => $e->message };
125 } elsif( $c->request->method eq 'DELETE' ) {
126 my $node = $c->request->param('source_id');
127 my $target = $c->request->param('target_id');
130 my @vectors = $collation->del_relationship( $node, $target );
131 $m->save( $tradition );
132 $c->stash->{'result'} = \@vectors;
133 } catch( Text::Tradition::Error $e ) {
134 $c->response->status( '403' );
135 $c->stash->{'result'} = { 'error' => $e->message };
138 $c->forward('View::JSON');
144 Attempt to render a view, if needed.
148 sub end : ActionClass('RenderView') {}
156 This library is free software. You can redistribute it and/or modify
157 it under the same terms as Perl itself.
161 __PACKAGE__->meta->make_immutable;