Revision 480 (by ahitrov, 2015/03/03 12:14:40) Tags plugin: initial import

<%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 = grep { ref $_ && $_->{attr} eq $name } $class->extra_properties ? 1 : 0;

    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 => 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>