-#!/usr/bin/perl\r
-#\r
-# Copyright (C) 2008 Luke Reeves\r
-#\r
-# This program is free software; you can redistribute it and/or modify\r
-# it under the terms of the GNU General Public License as published by\r
-# the Free Software Foundation; either version 2 of the License, or\r
-# (at your option) any later version.\r
-#\r
-# This program is distributed in the hope that it will be useful,\r
-# but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
-# GNU General Public License for more details.\r
-#\r
-# You should have received a copy of the GNU General Public License\r
-# along with this program; if not, write to the Free Software\r
-# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307\r
-# USA\r
-#\r
-\r
-package Insipid::Tags;\r
-\r
-use strict;\r
-use warnings;\r
-\r
-use vars qw(@ISA @EXPORT @EXPORT_OK);\r
-use CGI qw/:standard/;\r
-use CGI::Carp qw(fatalsToBrowser);\r
-use Insipid::Config;\r
-use Insipid::Database;\r
-use Insipid::Sessions;\r
-require Exporter;\r
-\r
-@ISA = qw(Exporter);\r
-\r
-@EXPORT = qw(\r
-show_tags\r
-get_tags\r
-get_tags_list\r
-set_tags\r
-tag_operations\r
-);\r
-\r
-sub tag_operations {\r
- print '<h2>Rename Tag</h2>';\r
- print '<select name="rename">';\r
- show_tags(1);\r
- print '</select>';\r
- print '<h2>Delete Tag</h2>';\r
- print '<select name="delete">';\r
- show_tags(1);\r
- print '</select>';\r
-}\r
-\r
-# Display the tag list. Takes one parameter for the mode - 0 is for the\r
-# sidebar, 1 is for a SELECT box. TODO: Cache the actual result set so\r
-# that when there's more than one tag list on a page we only hit the database\r
-# once.\r
-sub show_tags {\r
- my ($mode) = shift;\r
- if(!defined($mode)) { $mode = 0; }\r
- \r
- my ($sql, $sth);\r
- if($mode eq 0) { print "<div id=\"leftside\">"; }\r
-\r
- # If the user has already chosen a tag, get the intersection list\r
- if(defined(url_param('tag')) && (logged_in() eq 1)) {\r
- $sql = "select $tbl_tags.name,count(*) from $tbl_bookmarks \r
- inner join $tbl_bookmark_tags as bt1 on \r
- ($tbl_bookmarks.id = bt1.bookmark_id) \r
- inner join $tbl_tags on \r
- ($tbl_tags.id = bt1.tag_id)\r
- inner join $tbl_bookmark_tags as bt2 on \r
- ($tbl_bookmarks.id = bt2.bookmark_id) \r
- inner join $tbl_tags as t2 on \r
- (t2.id = bt2.tag_id and t2.name = ?)\r
- where ($tbl_tags.name != ?)\r
- group by $tbl_tags.name";\r
- $sth = $dbh->prepare($sql);\r
- $sth->execute(url_param('tag'), url_param('tag'));\r
-\r
- if($sth->rows ne 0) {\r
- print "<div id=\"taglist\" style=\"\">";\r
- print "<table cellpadding =\"0\" cellspacing=\"0\" ";\r
- print 'class="tagsummarytable"><tbody>';\r
- print "<tr><td align=\"center\">";\r
- print "<div class=\"inline_title\">Add Tag</div></td>";\r
- \r
- while(my @rs = $sth->fetchrow_array()) {\r
- my $tt = url_param('tag');\r
- my $link = "$tag_url$tt+$rs[0]";\r
- $tt =~ s/ /\+/g;\r
-\r
- print "<tr><td> <span class=\"tagtabletext\">($rs[1]) </span><a href=\"$link\">$rs[0]</a> ";\r
- print "</tr></td>\n";\r
- }\r
-\r
- print "</tbody></table></div></div>";\r
-\r
- return;\r
- }\r
- }\r
-\r
- # Access_spec contains a where clause to count only public bookmarks \r
- # if the user is not logged in\r
- my $access_where = "";\r
- if(logged_in() eq 0) {\r
- $access_where = " where ($tbl_bookmarks.access_level = 1) ";\r
- }\r
-\r
- my $order_clause;\r
- if($dbtype eq "Pg") {\r
- $order_clause = "order by upper($tbl_tags.name)";\r
- } else {\r
- $order_clause = "order by $tbl_tags.name";\r
- }\r
-\r
- $sql = "select $tbl_tags.name, count(*) \r
- from $tbl_bookmarks \r
- inner join $tbl_bookmark_tags on\r
- ($tbl_bookmarks.id = $tbl_bookmark_tags.bookmark_id)\r
- inner join $tbl_tags on\r
- ($tbl_tags.id = $tbl_bookmark_tags.tag_id)\r
- $access_where\r
- group by $tbl_tags.name\r
- $order_clause"; \r
-\r
- $sth = $dbh->prepare($sql);\r
- $sth->execute;\r
-\r
- if($mode eq 0) {\r
- print '<div id="taglist" style="">';\r
- print '<table cellpadding="0" cellspacing="0" ';\r
- print 'class="tagsummarytable"><tbody>';\r
- print '<tr><td align="center"><div class="inline_title">';\r
- print 'Tag List</div></td>';\r
- }\r
- \r
- while(my @r = $sth->fetchrow_array) {\r
- if($mode eq 0) {\r
- print "<tr><td> <span class=\"tagtabletext\">($r[1])";\r
- print " </span><a href=\"$tag_url$r[0]\">$r[0]</a> ";\r
- print "</td></tr>\n";\r
- } else {\r
- print "<option name=\"$r[0]\">$r[0]</option>";\r
- }\r
- }\r
-\r
- if($mode eq 0) {\r
- print "</tbody></table></div>";\r
- print "</div>";\r
- }\r
-}\r
-\r
-# Get a string representing a URLs tags\r
-sub get_tags {\r
- my ($url) = (@_);\r
- my @tags = get_tags_list($url);\r
-\r
- my $rv = "";\r
- foreach (@tags) {\r
- $rv = "$rv $_";\r
- }\r
-\r
- # Trim leading whitespace\r
- $rv =~ s/^\s+//;\r
- return $rv;\r
-}\r
-\r
-# Get a list of the tags for a given URL id\r
-sub get_tags_list {\r
- my ($url) = (@_);\r
- my $sql = "select $tbl_tags.name from $tbl_tags \r
- inner join $tbl_bookmark_tags on \r
- ($tbl_tags.id = $tbl_bookmark_tags.tag_id) \r
- inner join $tbl_bookmarks on\r
- ($tbl_bookmark_tags.bookmark_id = $tbl_bookmarks.id)\r
- where ($tbl_bookmarks.url = ?)";\r
- \r
- my $sth = $dbh->prepare($sql);\r
- $sth->execute($url);\r
-\r
- my @tags;\r
- while(my @r = $sth->fetchrow_array) {\r
- push(@tags, $r[0]);\r
- }\r
-\r
- return @tags;\r
-}\r
-\r
-# Sets tags for a bookmark. Takes a bookmark ID and a string\r
-# representing the tags as parameters.\r
-sub set_tags {\r
- my ($bookmark_id, $tag_string) = (@_);\r
-\r
- if(logged_in() ne 1) {\r
- push(@errors, "You have to be logged in to perform that operation.");\r
- return;\r
- }\r
- \r
- my @tags = split(" ", $tag_string);\r
- \r
- # Clear old tags first.\r
- my $sql = "delete from $tbl_bookmark_tags where (bookmark_id = ?)";\r
- my $sth = $dbh->prepare($sql);\r
- $sth->execute($bookmark_id);\r
- \r
- foreach my $cur (@tags) {\r
- # check if this tag exists in tags table\r
- my $sql = "select count(id) from $tbl_tags where (name = ?)";\r
- my $sth = $dbh->prepare($sql);\r
- $sth->execute($cur);\r
- my @rv = $sth->fetchrow_array;\r
- my $tagcount = $rv[0];\r
-\r
- # or create a new tag\r
- if ($tagcount < 1) {\r
- my $sql = "insert into $tbl_tags (name) values(?)";\r
- my $sth = $dbh->prepare($sql);\r
- $sth->execute($cur);\r
- }\r
-\r
- # and fetch the tag ID\r
- $sql = "select id from $tbl_tags where (name = ?)";\r
- $sth = $dbh->prepare($sql);\r
- $sth->execute($cur);\r
- my $tid = $sth->fetchrow_array;\r
-\r
- $sql = "insert into $tbl_bookmark_tags(bookmark_id, tag_id) \r
- values( ? , ? )";\r
- $sth = $dbh->prepare($sql);\r
- $sth->execute($bookmark_id, $tid);\r
- }\r
-}\r
-\r
-\r
-1;\r
-__END__\r
+#!/usr/bin/perl
+#
+# Copyright (C) 2008 Luke Reeves
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+# USA
+#
+
+package Insipid::Tags;
+
+use strict;
+use warnings;
+
+use vars qw(@ISA @EXPORT @EXPORT_OK);
+use CGI qw/:standard/;
+use CGI::Carp qw(fatalsToBrowser);
+use Insipid::Config;
+use Insipid::Database;
+use Insipid::Sessions;
+require Exporter;
+
+use Data::Dumper;
+
+@ISA = qw(Exporter);
+
+@EXPORT = qw(
+show_tags
+get_tags
+get_tags_list
+set_tags
+tag_operations
+);
+
+sub tag_operations {
+
+ if(logged_in() ne 1) {
+ push(@errors, "You have to be logged in to perform that operation.");
+ return;
+ }
+
+ if(param('save') && param('newName')) {
+ print '<p>Reanming...</p>';
+
+ my $newTag = param('newName');
+ my $oldTagName = param('rename');
+
+ # is the new name already a tag ?
+ my $check = already_a_tag($newTag);
+ if($check) {
+ # delete the old one and rewrite the id
+ }
+ else {
+ # just rename the tag
+ my $sql = "UPDATE $tbl_tags SET name = ? WHERE name = ?";
+ my $sth = $dbh->prepare($sql);
+ $sth->execute($newTag, $oldTagName);
+ }
+
+ print '<span style="color: green;">Done !</span>';
+
+ }
+
+ print '<h2>Rename Tag</h2>';
+ print '<form method="post" action="">';
+ print '<select name="rename">';
+ show_tags(1);
+ print '</select>';
+ print '<input type="text" name="newName" value="" /><br />';
+ print '<input type="submit" value="Rename" />';
+ print '<input type=hidden name="op" value="tags">';
+ print '<input type=hidden name="save" value="yes">';
+ print '</form>';
+
+ print '<h2>Delete Tag</h2>';
+ print '<select name="delete">';
+ show_tags(1);
+ print '</select>';
+}
+
+# Display the tag list. Takes one parameter for the mode - 0 is for the
+# sidebar, 1 is for a SELECT box. TODO: Cache the actual result set so
+# that when there's more than one tag list on a page we only hit the database
+# once.
+sub show_tags {
+ my ($mode) = shift;
+ if(!defined($mode)) { $mode = 0; }
+
+ my ($sql, $sth);
+ if($mode eq 0) { print "<div id=\"leftside\">"; }
+
+ # If the user has already chosen a tag, get the intersection list
+ if(defined(url_param('tag')) && (logged_in() eq 1)) {
+ $sql = "select $tbl_tags.name,count(*) from $tbl_bookmarks
+ inner join $tbl_bookmark_tags as bt1 on
+ ($tbl_bookmarks.id = bt1.bookmark_id)
+ inner join $tbl_tags on
+ ($tbl_tags.id = bt1.tag_id)
+ inner join $tbl_bookmark_tags as bt2 on
+ ($tbl_bookmarks.id = bt2.bookmark_id)
+ inner join $tbl_tags as t2 on
+ (t2.id = bt2.tag_id and t2.name = ?)
+ where ($tbl_tags.name != ?)
+ group by $tbl_tags.name";
+ $sth = $dbh->prepare($sql);
+ $sth->execute(url_param('tag'), url_param('tag'));
+
+ if($sth->rows ne 0) {
+ print "<div id=\"taglist\" style=\"\">";
+ print "<table cellpadding =\"0\" cellspacing=\"0\" ";
+ print 'class="tagsummarytable"><tbody>';
+ print "<tr><td align=\"center\">";
+ print "<div class=\"inline_title\">Add Tag</div></td>";
+
+ while(my @rs = $sth->fetchrow_array()) {
+ my $tt = url_param('tag');
+ my $link = "$tag_url$tt+$rs[0]";
+ $tt =~ s/ /\+/g;
+
+ print "<tr><td> <span class=\"tagtabletext\">($rs[1]) </span><a href=\"$link\">$rs[0]</a> ";
+ print "</tr></td>\n";
+ }
+
+ print "</tbody></table></div></div>";
+
+ return;
+ }
+ }
+
+ # Access_spec contains a where clause to count only public bookmarks
+ # if the user is not logged in
+ my $access_where = "";
+ if(logged_in() eq 0) {
+ $access_where = " where ($tbl_bookmarks.access_level = 1) ";
+ }
+
+ my $order_clause;
+ if($dbtype eq "Pg") {
+ $order_clause = "order by upper($tbl_tags.name)";
+ } else {
+ $order_clause = "order by $tbl_tags.name";
+ }
+
+ $sql = "select $tbl_tags.name, count(*)
+ from $tbl_bookmarks
+ inner join $tbl_bookmark_tags on
+ ($tbl_bookmarks.id = $tbl_bookmark_tags.bookmark_id)
+ inner join $tbl_tags on
+ ($tbl_tags.id = $tbl_bookmark_tags.tag_id)
+ $access_where
+ group by $tbl_tags.name
+ $order_clause";
+
+ $sth = $dbh->prepare($sql);
+ $sth->execute;
+
+ if($mode eq 0) {
+ print '<div id="taglist" style="">';
+ print '<table cellpadding="0" cellspacing="0" ';
+ print 'class="tagsummarytable"><tbody>';
+ print '<tr><td align="center"><div class="inline_title">';
+ print 'Tag List</div></td>';
+ }
+
+ while(my @r = $sth->fetchrow_array) {
+ if($mode eq 0) {
+ print "<tr><td> <span class=\"tagtabletext\">($r[1])";
+ print " </span><a href=\"$tag_url$r[0]\">$r[0]</a> ";
+ print "</td></tr>\n";
+ } else {
+ print "<option name=\"$r[0]\">$r[0]</option>";
+ }
+ }
+
+ if($mode eq 0) {
+ print "</tbody></table></div>";
+ print "</div>";
+ }
+}
+
+# Get a string representing a URLs tags
+sub get_tags {
+ my ($url) = (@_);
+ my @tags = get_tags_list($url);
+
+ my $rv = "";
+ foreach (@tags) {
+ $rv = "$rv $_";
+ }
+
+ # Trim leading whitespace
+ $rv =~ s/^\s+//;
+ return $rv;
+}
+
+# Get a list of the tags for a given URL id
+sub get_tags_list {
+ my ($url) = (@_);
+ my $sql = "select $tbl_tags.name from $tbl_tags
+ inner join $tbl_bookmark_tags on
+ ($tbl_tags.id = $tbl_bookmark_tags.tag_id)
+ inner join $tbl_bookmarks on
+ ($tbl_bookmark_tags.bookmark_id = $tbl_bookmarks.id)
+ where ($tbl_bookmarks.url = ?)";
+
+ my $sth = $dbh->prepare($sql);
+ $sth->execute($url);
+
+ my @tags;
+ while(my @r = $sth->fetchrow_array) {
+ push(@tags, $r[0]);
+ }
+
+ return @tags;
+}
+
+# Sets tags for a bookmark. Takes a bookmark ID and a string
+# representing the tags as parameters.
+sub set_tags {
+ my ($bookmark_id, $tag_string) = (@_);
+
+ if(logged_in() ne 1) {
+ push(@errors, "You have to be logged in to perform that operation.");
+ return;
+ }
+
+ my @tags = split(" ", $tag_string);
+
+ # Clear old tags first.
+ my $sql = "delete from $tbl_bookmark_tags where (bookmark_id = ?)";
+ my $sth = $dbh->prepare($sql);
+ $sth->execute($bookmark_id);
+
+ foreach my $cur (@tags) {
+ # check if this tag exists in tags table
+ my $sql = "select count(id) from $tbl_tags where (name = ?)";
+ my $sth = $dbh->prepare($sql);
+ $sth->execute($cur);
+ my @rv = $sth->fetchrow_array;
+ my $tagcount = $rv[0];
+
+ # or create a new tag
+ if ($tagcount < 1) {
+ my $sql = "insert into $tbl_tags (name) values(?)";
+ my $sth = $dbh->prepare($sql);
+ $sth->execute($cur);
+ }
+
+ # and fetch the tag ID
+ $sql = "select id from $tbl_tags where (name = ?)";
+ $sth = $dbh->prepare($sql);
+ $sth->execute($cur);
+ my $tid = $sth->fetchrow_array;
+
+ $sql = "insert into $tbl_bookmark_tags(bookmark_id, tag_id)
+ values( ? , ? )";
+ $sth = $dbh->prepare($sql);
+ $sth->execute($bookmark_id, $tid);
+ }
+}
+
+# check if given word is an existing tag
+# if so return the tag id
+sub already_a_tag {
+ my ($tag) = (@_);
+
+ my($tagId, $sql, $sth, @result);
+
+ if($tag) {
+ $sql = "select id from `$tbl_tags` where (name = ?)";
+ $sth = $dbh->prepare($sql);
+ $sth->execute($tag);
+ @result = $sth->fetchrow_array();
+ #print "..".$result[0];
+ $tagId = $result[0];
+ }
+}
+
+1;
+__END__