sub get_tags_from_string
{
my ($tags_string) = @_;
my @tags;
my @raw_tags;
my @new_tags;
# Determine delimiters: commas or spaces
# count commmas
my $countComma = $tags_string =~ s/(\,)/$1/gi;
# comma delimiter?
if ( $countComma > 0 )
{
@raw_tags = split ( '\,', $tags_string );
}
# no comma delimiter, try for next delimiter
else
{
# count chunks of whitespace
my $countWhitespace = $tags_string =~ s/(\s+)/$1/gi;
# whitespace delimiter?
if ( $countWhitespace > 0 )
{
@raw_tags = split ( /\s+/, $tags_string );
}
# no delimiter, treat entire thing as tag
else
{
push( @raw_tags, $tags_string );
}
}
# clean up each raw tag
foreach my $raw_tag (@raw_tags)
{
# Clean whitespace, bad chars
$raw_tag = scrub($raw_tag);
# print "raw tag: $raw_tag
\n";
next unless (length($raw_tag) > 0) && (length($raw_tag) <= 16);
# If already a tag, just store name
if ( tag_already( $raw_tag ) )
{
# print "Tag Already: $raw_tag
\n";
push ( @tags, $raw_tag );
}
# If not a tag, add to new tags array so we can create new tag
else
{
# print "Tag New: $raw_tag
\n";
new_tag( $raw_tag );
push ( @tags, $raw_tag );
}
}
return \@tags;
# retrieve already existing tags
# create new tags
}
sub tags_stringify_links
{
my ($t, $limit) = @_;
my $tags = $t->{tags} || return '';
my $thread_id = $t->{id};
my $tags_links;
foreach my $tag (@$tags)
{
next if (defined($limit) && $limit-- <= 0);
push( @$tags_links, "$tag" );
}
my $return_string = join(', ', @$tags_links) if ref($tags);
if (defined($limit) && ($limit < 0) )
{ $return_string .= " ..."; }
return $return_string || '';
# return join(', ', @$tags_links ) if ref($tags);
# return '';
}
sub tags_stringify
{
my ($tags) = @_;
return join(', ', @$tags ) if ref($tags);
return '';
}
sub tag_assign_mechanism
{
my ($tag_name, $t) = @_;
# Verify inputs
error("no tag specified!") if (!defined($tag_name));
error("no thread specified!") if (!defined($t));
# Retrieve tag object
my $tag = get_tag( $tag_name ) || error("no tag $tag_name");
# Check to see if tag is already associated with thread
# Add association for thread_id
if ( ! tag_thread_already( $tag, $t) )
{
update_tag_for_thread( $tag, $t );
return "Assigning tag $tag_name
\n";
}
}
sub tag_remove_mechanism
{
my ($tag_name, $t) = @_;
# Verify inputs
error("no tag specified!") if (!defined($tag_name));
error("no thread specified!") if (!defined($t));
# Retrieve tag object
my $tag = get_tag( $tag_name ) || error("no tag $tag_name");
if ( tag_thread_already( $tag, $t) )
{
delete_tag_for_thread( $tag, $t );
return "Removing tag $tag_name
\n";
}
}
sub tag_already
{
my ($tag_name) = @_;
my $tag = get_tag_count( $tag_name );
return ($tag > 0);
}
sub tag_thread_already
{
my ( $tag_name, $thread ) = @_;
if (ref($tag_name))
{ $tag_name = $tag_name->{tag}; }
if (! ref($thread))
{ $thread = get_thread( $thread ); }
# print "those tags: " . $thread->{tags} . "
\n";
return grep ( /^$tag_name$/, @{$thread->{tags}} )
}
1;