summaryrefslogtreecommitdiff
path: root/lib/tags.pm
blob: 5e6e0a61b4886b7f1c0054b0fdaffc451de7ea51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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;