Revision 485 (by ahitrov, 2015/03/05 14:45:04) |
Data encoding for extra_fields
|
<%once>
use JSON::XS;
my $json = JSON::XS->new;
</%once>
<%args>
$object => undef
$name => undef
$SETS => undef
</%args>
<%init>
return undef unless ref $SETS;
return undef unless $name;
return undef if $SETS->{$name} eq '';
warn "\nTAG Store:\n" if $DEBUG;
warn Dumper $SETS->{$name} if $DEBUG;
my ($prop) = grep { $_->{attr} eq $name } $object->structure;
return undef if exists $prop->{virtual} && $prop->{virtual};
my $class = $object->class;
my $is_extra = scalar( grep { ref $_ && $_->{attr} eq $name } $class->class_table->required_properties ) ? 0 : 1;
my $result;
my @tags;
if ( $object->id ) {
@tags = $keeper->get_documents(
class => 'tag::Tag',
lclass => 'tag::Cloud',
ldest => $object->id,
ldestclass => $object->class,
);
if ( @tags ) {
$result = [];
foreach my $tag ( @tags ) {
push @$result, { id => $tag->id, name => $is_extra ? $tag->name : Encode::decode('utf-8', $tag->name) };
}
unless ( $is_extra ) {
$result = Encode::encode('utf-8', $json->encode( $result ));
}
}
warn Dumper $result if $DEBUG;
return $result;
} else {
return undef;
}
</%init>