summaryrefslogtreecommitdiff
path: root/lib/tags.pm
diff options
context:
space:
mode:
authorJules Laplace <carbon@melanarchy.org>2013-08-02 17:14:41 -0500
committerJules Laplace <carbon@melanarchy.org>2013-08-02 17:14:41 -0500
commite9192b3d42660a5781101df4357d276318151e8a (patch)
tree059eb6ace6147cf9559af74ed1ab5e221c80e280 /lib/tags.pm
parent79670053c7247d3a49b607960efd284e93f057e5 (diff)
cgi-bin & lib
Diffstat (limited to 'lib/tags.pm')
-rw-r--r--lib/tags.pm147
1 files changed, 147 insertions, 0 deletions
diff --git a/lib/tags.pm b/lib/tags.pm
new file mode 100644
index 0000000..5e6e0a6
--- /dev/null
+++ b/lib/tags.pm
@@ -0,0 +1,147 @@
+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<br>\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<br>\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<br>\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, "<a href=\"$BUCKY/$BUCKY_LEXICON_TAG/$tag\" class=\"quietlink\">$tag</a>" );
+ }
+ my $return_string = join(', ', @$tags_links) if ref($tags);
+ if (defined($limit) && ($limit < 0) )
+ { $return_string .= " <a href=\"$BUCKY/details/$thread_id\" class=\"quietlink\">...</a>"; }
+ 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<br>\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<br>\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} . "<br>\n";
+ return grep ( /^$tag_name$/, @{$thread->{tags}} )
+ }
+1;
+