<% $json %> <%once> use JSON::XS; <%args> $action => undef $id => undef $class => undef $tag => undef <%init> my %result; warn Dumper \%ARGS; my $doc = $keeper->get_document_by_id( $id, class => $class ); if ( ref $doc && $ARGS{tag} ) { my @links = $keeper->get_links( class => 'tag::Cloud', dest_id => $id, dest_class => $class, ); my @tags; if ( @links ) { my %ids = map { $_->source_id => 1 } @links; my @ids = keys %ids; @tags = $keeper->get_documents( id => \@ids, class => 'tag::Tag', ) if @ids; } my ($tobj) = $keeper->get_documents( class => 'tag::Tag', name => $ARGS{tag}, ilike => 1, limit => 1, ); if ( $action eq 'add' ) { unless ( ref $tobj ) { $tobj = tag::Tag->new( $keeper ); $tobj->name( $ARGS{tag} ); $tobj->status( 1 ); $tobj->store; } my $exists = (grep { $_->id == $tobj->id } @tags) ? 1 : 0; if ( ref $tobj && !$exists ) { my $link = tag::Cloud->new( $keeper ); $link->source_id( $tobj->id ); $link->source_class( $tobj->class ); $link->dest_id( $id ); $link->dest_class( $class ); $link->status( 1 ); $link->store; } else { $result{fallback} = 1; } } elsif ( $action eq 'remove' ) { if ( ref $tobj ) { my @delete = grep { $_->source_id == $tobj->id } @links; foreach my $obj ( @delete ) { $obj->delete; } my $count = $keeper->get_links( class => 'tag::Cloud', count => 1, source_id => $tobj->id, ); $tobj->delete unless $count; } else { $result{fallback} = 1; } } } $result{ok} = 1 unless exists $result{fallback}; my $json = encode_json \%result;