1 |
480 |
ahitrov |
<% $json %> |
2 |
|
|
<%once> |
3 |
|
|
|
4 |
|
|
use JSON::XS; |
5 |
|
|
|
6 |
|
|
</%once> |
7 |
|
|
<%args> |
8 |
|
|
|
9 |
|
|
$action => undef |
10 |
|
|
$id => undef |
11 |
|
|
$class => undef |
12 |
|
|
$tag => undef |
13 |
|
|
|
14 |
|
|
</%args> |
15 |
|
|
<%init> |
16 |
|
|
|
17 |
|
|
my %result; |
18 |
|
|
warn Dumper \%ARGS; |
19 |
|
|
my $doc = $keeper->get_document_by_id( $id, class => $class ); |
20 |
|
|
|
21 |
|
|
if ( ref $doc && $ARGS{tag} ) { |
22 |
|
|
my @links = $keeper->get_links( |
23 |
|
|
class => 'tag::Cloud', |
24 |
|
|
dest_id => $id, |
25 |
|
|
dest_class => $class, |
26 |
|
|
); |
27 |
|
|
my @tags; |
28 |
|
|
if ( @links ) { |
29 |
|
|
my %ids = map { $_->source_id => 1 } @links; |
30 |
|
|
my @ids = keys %ids; |
31 |
|
|
@tags = $keeper->get_documents( |
32 |
|
|
id => \@ids, |
33 |
|
|
class => 'tag::Tag', |
34 |
|
|
) if @ids; |
35 |
|
|
} |
36 |
|
|
my ($tobj) = $keeper->get_documents( |
37 |
|
|
class => 'tag::Tag', |
38 |
|
|
name => $ARGS{tag}, |
39 |
|
|
ilike => 1, |
40 |
|
|
limit => 1, |
41 |
|
|
); |
42 |
|
|
if ( $action eq 'add' ) { |
43 |
|
|
unless ( ref $tobj ) { |
44 |
|
|
$tobj = tag::Tag->new( $keeper ); |
45 |
|
|
$tobj->name( $ARGS{tag} ); |
46 |
|
|
$tobj->status( 1 ); |
47 |
|
|
$tobj->store; |
48 |
|
|
} |
49 |
|
|
my $exists = (grep { $_->id == $tobj->id } @tags) ? 1 : 0; |
50 |
|
|
if ( ref $tobj && !$exists ) { |
51 |
|
|
my $link = tag::Cloud->new( $keeper ); |
52 |
|
|
$link->source_id( $tobj->id ); |
53 |
|
|
$link->source_class( $tobj->class ); |
54 |
|
|
$link->dest_id( $id ); |
55 |
|
|
$link->dest_class( $class ); |
56 |
|
|
$link->status( 1 ); |
57 |
|
|
$link->store; |
58 |
|
|
} else { |
59 |
|
|
$result{fallback} = 1; |
60 |
|
|
} |
61 |
|
|
} elsif ( $action eq 'remove' ) { |
62 |
|
|
if ( ref $tobj ) { |
63 |
|
|
my @delete = grep { $_->source_id == $tobj->id } @links; |
64 |
|
|
foreach my $obj ( @delete ) { |
65 |
|
|
$obj->delete; |
66 |
|
|
} |
67 |
|
|
my $count = $keeper->get_links( |
68 |
|
|
class => 'tag::Cloud', |
69 |
|
|
count => 1, |
70 |
|
|
source_id => $tobj->id, |
71 |
|
|
); |
72 |
|
|
$tobj->delete unless $count; |
73 |
|
|
} else { |
74 |
|
|
$result{fallback} = 1; |
75 |
|
|
} |
76 |
|
|
} |
77 |
|
|
} |
78 |
|
|
$result{ok} = 1 unless exists $result{fallback}; |
79 |
|
|
|
80 |
|
|
my $json = encode_json \%result; |
81 |
|
|
|
82 |
|
|
</%init> |