Line # Revision Author
1 480 ahitrov package tag::Cloud;
2
3 use base 'Contenido::Link';
4 use Contenido::Globals;
5 use Data::Recursive::Encode;
6 use Data::Dumper;
7 use JSON::XS;
8
9 sub class_name
10 {
11 return 'Тег к объекту';
12 }
13
14 sub class_description
15 {
16 return 'Организация облака тегов';
17 }
18
19 sub extra_properties
20 {
21 return (
22 );
23 }
24
25 sub available_sources
26 {
27 return [ qw(tag::Tag) ];
28 }
29
30 sub available_destinations
31 {
32 return $state->{tag}->tag_destinations;
33 }
34
35 sub class_table
36 {
37 return 'tag::SQL::TagsCloudTable';
38 }
39
40 sub pre_store
41 {
42 my $self = shift;
43
44 return 1;
45 }
46
47 sub post_store
48 {
49 my $self = shift;
50 warn "Store tag cloud element\n" if $DEBUG;
51 my $object = $keeper->get_document_by_id($self->dest_id, class => $self->dest_class ) if $self->dest_id && $self->dest_class;
52 my $tag = $self->keeper->get_document_by_id($self->source_id, class => $self->source_class ) if $self->source_id && $self->source_class;
53 if ( ref $object && ref $tag ) {
54 my ($prop) = grep { $_->{type} eq 'tagset' } $object->structure;
55 my $class = $object->class;
56 my $is_extra = grep { ref $_ && $_->{attr} eq $name } $class->extra_properties ? 1 : 0;
57 if ( ref $prop && !(exists $prop->{virtual} && $prop->{virtual}) ) {
58 my $name = $prop->{attr};
59 my $struct;
60 if ( ref $object->$name ) {
61 $struct = $object->$name;
62 } elsif ( $object->$name ) {
63 $struct = JSON::XS->new->utf8->decode( $object->$name );
64 }
65 if ( ref $struct eq 'ARRAY' && @$struct && !(grep { $_->{id} == $tag->id } @$struct) ) {
66 push @$struct, { id => $tag->id, name => Encode::decode('utf-8', $tag->name) };
67 unless ( $is_extra ) {
68 $struct = Encode::encode('utf-8', JSON::XS->new->encode( $struct ));
69 }
70 $object->$name( $struct );
71 $object->store;
72 }
73 }
74 } else {
75 warn "Tag Cloud update error: cloud_element_id=".$self->id.", no source or destination available\n";
76 }
77 }
78
79 sub post_delete
80 {
81 my $self = shift;
82 my $object = $keeper->get_document_by_id($self->dest_id, class => $self->dest_class ) if $self->dest_id && $self->dest_class;
83 my $tag = $self->keeper->get_document_by_id($self->source_id, class => $self->source_class ) if $self->source_id && $self->source_class;
84 if ( ref $object && ref $tag ) {
85 my ($prop) = grep { $_->{type} eq 'tagset' } $object->structure;
86 my $class = $object->class;
87 my $is_extra = grep { ref $_ && $_->{attr} eq $name } $class->extra_properties ? 1 : 0;
88 if ( ref $prop && !(exists $prop->{virtual} && $prop->{virtual}) ) {
89 my $name = $prop->{attr};
90 my $struct;
91 if ( ref $object->$name ) {
92 $struct = $object->$name;
93 } elsif ( $object->$name ) {
94 $struct = JSON::XS->new->utf8->decode( $object->$name );
95 }
96 if ( ref $struct eq 'ARRAY' && @$struct && (grep { $_->{id} == $tag->id } @$struct) ) {
97 @$struct = grep { $_->{id} != $tag->id } @$struct;
98 unless ( $is_extra ) {
99 $struct = Encode::encode('utf-8', JSON::XS->new->encode( $struct ));
100 }
101 $object->$name( $struct );
102 $object->store;
103 }
104 }
105 } else {
106 warn "Tag Cloud delete error: cloud_element_id=".$self->id.", no source or destination available\n";
107 }
108 }
109
110 1;