[vhffs-dev] [1184] Groups can now be tagged.

[ Thread Index | Date Index | More vhffs.org/vhffs-dev Archives ]


Revision: 1184
Author:   beuss
Date:     2008-04-05 18:25:18 +0200 (Sat, 05 Apr 2008)

Log Message:
-----------
Groups can now be tagged. This fix #237 for current VHFFS generation.

Modified Paths:
--------------
    trunk/vhffs-api/src/Vhffs/Tag.pm
    trunk/vhffs-backend/src/pgsql/initdb.sql.in
    trunk/vhffs-panel/group/prefs.pl
    trunk/vhffs-panel/templates/group/tags.tmpl


Modified: trunk/vhffs-api/src/Vhffs/Tag.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Tag.pm	2008-04-02 22:00:59 UTC (rev 1183)
+++ trunk/vhffs-api/src/Vhffs/Tag.pm	2008-04-05 16:25:18 UTC (rev 1184)
@@ -114,15 +114,34 @@
 sub get_all {
 	my ($main) = @_;
 	
+	return _fetch_tags($main, q{SELECT tag_id, label, description, updated, updater_id, category_id
+		FROM vhffs_tag ORDER BY label});
+}
+
+sub get_by_category_id {
+	my ($main, $category_id) = @_;
+	
+	return _fetch_tags($main, q{SELECT tag_id, label, description, updated, updater_id, category_id
+		FROM vhffs_tag WHERE category_id = ? ORDER BY label}, $category_id);
+}
+
+=head2
+	Vhffs::Tag::_fetch_tags($main, $sql, @params)
+
+Fetches a tag list using C<$sql> and C<@params>.
+C<$sql> is a query having the following format
+   SELECT tag_id, label, description, updated, updater_id, category_id FROM vhffs_tag...
+=cut
+
+sub _fetch_tags {
+	my ($main, $sql, @params) = @_;
+	
 	my $tags = [];
 	
-	my $sql = q{SELECT tag_id, label, description, updated, updater_id, category_id
-		FROM vhffs_tag ORDER BY label};
-	
 	my $dbh = $main->get_db_object();
 	
 	my $sth = $dbh->prepare($sql);
-	$sth->execute() or return undef;
+	$sth->execute(@params) or return undef;
 	
 	while(my $t = $sth->fetchrow_arrayref()) {
 		push @$tags, _new Vhffs::Tag($main, @$t);
@@ -147,4 +166,69 @@
 	return $dbh->do($sql, undef, $self->{category_id}, $self->{label}, $self->{description}, $self->{updated}, $self->{updater_id}, $self->{tag_id});
 }
 
+# Since perl allows us to do such things, let's have
+# an unobstrusive approach
+
+package Vhffs::Object;
+
+=head2 get_tags
+
+    my $tags = $o->get_tags();
+Returns an array of all tag categories for an object. Each
+element contains {label, tags}, tags is an array containing
+all tags of the given category for this object ({tag_id, label}).
+
+=cut
+
+sub get_tags {
+	my ($o) = @_;
+	
+	my $dbh = $o->get_db_object();
+	my $tags = [];
+	
+	my $sql = q{SELECT t.tag_id, t.label as tag_label, c.label as cat_label FROM vhffs_tag t INNER JOIN vhffs_tag_category c ON c.tag_category_id = t.category_id
+			INNER JOIN vhffs_object_tag ot ON ot.tag_id = t.tag_id WHERE ot.object_id = ? ORDER BY c.label, t.label};
+	
+	my $sth = $dbh->prepare($sql);
+	$sth->execute($o->get_oid()) or return undef;
+
+	my $cat = undef;
+		
+	while(my $t = $sth->fetchrow_hashref()) {
+		if( (!defined $cat) || ($cat->{label} ne $t->{cat_label}) ) {
+			$cat = {
+				label => $t->{cat_label},
+				tags => []
+			};
+			push @$tags, $cat;
+		}
+		my $tag = {
+			tag_id => $t->{tag_id},
+			label => $t->{tag_label}
+		};
+		push @{$cat->{tags}}, $tag;
+	}
+	
+	return $tags;
+}
+
+sub add_tag {
+	my ($o, $tag, $updater, $updated) = @_;
+	
+	my $dbh = $o->get_db_object();
+	$updated = time() unless($updated);
+	
+	return $dbh->do(q{INSERT INTO vhffs_object_tag(object_id, tag_id, updated, updater_id) VALUES(?, ?, ?, ?)},
+		undef, $o->get_oid(), $tag->{tag_id}, $updater->get_uid(), $updated);
+}
+
+sub delete_tag {
+	my ($o, $tag) = @_;
+	
+	my $dbh = $o->get_db_object();
+	
+	return $dbh->do(q{DELETE FROM vhffs_object_tag WHERE object_id = ? AND tag_id = ?}, undef,
+		$o->get_oid(), $tag->{tag_id});
+}
+
 1;

Modified: trunk/vhffs-backend/src/pgsql/initdb.sql.in
===================================================================
--- trunk/vhffs-backend/src/pgsql/initdb.sql.in	2008-04-02 22:00:59 UTC (rev 1183)
+++ trunk/vhffs-backend/src/pgsql/initdb.sql.in	2008-04-05 16:25:18 UTC (rev 1184)
@@ -355,8 +355,8 @@
 CREATE TABLE vhffs_object_tag (
     object_id int4 NOT NULL,
     tag_id int4 NOT NULL,
-    created int8 NOT NULL,
-    creator_id int4 NOT NULL,
+    updated int8 NOT NULL,
+    updater_id int4 NOT NULL,
     CONSTRAINT vhffs_object_tag_pkey PRIMARY KEY ( object_id, tag_id )
 ) WITH OIDS;
 

Modified: trunk/vhffs-panel/group/prefs.pl
===================================================================
--- trunk/vhffs-panel/group/prefs.pl	2008-04-02 22:00:59 UTC (rev 1183)
+++ trunk/vhffs-panel/group/prefs.pl	2008-04-05 16:25:18 UTC (rev 1184)
@@ -40,15 +40,17 @@
 use strict;
 
 use lib '%VHFFS_LIB_DIR%';
-use Vhffs::User;
+use Vhffs::Functions;
 use Vhffs::Group;
 use Vhffs::Main;
 use Vhffs::Panel::Main;
 use Vhffs::Panel::Group;
 use Vhffs::Panel::Object;
 use Vhffs::Panel::Template;
-use Vhffs::Functions;
 use Vhffs::Services::MailGroup;
+use Vhffs::Tag;
+use Vhffs::Tag::Category;
+use Vhffs::User;
 
 my $panel = new Vhffs::Panel::Main();
 exit 0 unless $panel;
@@ -70,7 +72,8 @@
 	$template->param( MESSAGE => gettext( 'You\'re not allowed to do this, object is not in active state or you don\'t have enough ACL rights' ) );
 } else {
 
-	$template = new Vhffs::Panel::Template( filename => $templatedir.'/panel/group/prefs.tmpl', global_vars => 1 );
+	$template = new Vhffs::Panel::Template( filename => $templatedir.'/panel/group/prefs.tmpl', global_vars => 1, 
+		die_on_bad_params => 0 );
 
 	if( defined( $cgi->param( 'update_desc_submit' ) ) ) {
 		# Description modification
@@ -195,6 +198,10 @@
 		}
 	} elsif( defined( $cgi->param('update_quota_submit')) ) {
         update_quota();
+    } elsif( defined( $cgi->param('add_tag_submit') ) ) {
+    	add_tag();
+    } elsif( defined( $cgi->param('delete_tag_submit') ) ) {
+    	delete_tag();
     }
 
 
@@ -263,6 +270,8 @@
         $template->param( QUOTA => $group->get_quota );
         $template->param( USED_QUOTA => $group->get_quota_used );
     }
+    
+    fill_tags();
 }
 
 $panel->build( $template );
@@ -289,3 +298,67 @@
     }
 }
 
+sub fill_tags {
+	my $categories = Vhffs::Tag::Category::get_all($vhffs);
+	foreach my $c (@{$categories}) {
+		$c->{tags} = Vhffs::Tag::get_by_category_id($vhffs, $c->{category_id});
+	}
+	$template->param( CATEGORIES => $categories );
+	
+	$template->param( CURRENT_TAGS => $group->get_tags );
+}
+
+sub add_tag {
+	unless( $user->can_modify( $group ) ) {
+		$panel->add_error( gettext( 'You\'re not allowed to do this (ACL rights)' ) );
+		return 0;
+	}
+	
+	my $tag_id = $cgi->param('tag_id');
+	
+	unless(defined $tag_id) {
+		$panel->add_error( gettext('CGI error') );
+		return 0;
+	}
+	
+	my $tag = Vhffs::Tag::get_by_tag_id($vhffs, $tag_id);
+	
+	unless(defined $tag) {
+		$panel->add_error( gettext('Tag not found') );
+		return 0;
+	}
+	
+	if($group->add_tag($tag, $user)) {
+		$panel->add_info( gettext('Tag added') );
+	} else {
+		$panel->add_error( gettext('Unable to delete tag') );
+	}
+}
+
+sub delete_tag {
+	unless( $user->can_modify( $group ) ) {
+		$panel->add_error( gettext( 'You\'re not allowed to do this (ACL rights)' ) );
+		return 0;
+	}
+	
+	my $tag_id = $cgi->param('tag_id');
+	
+	unless(defined $tag_id) {
+		$panel->add_error( gettext('CGI error') );
+		return 0;
+	}
+	
+	my $tag = Vhffs::Tag::get_by_tag_id($vhffs, $tag_id);
+	
+	unless(defined $tag) {
+		$panel->add_error( gettext('Tag not found') );
+		return 0;
+	}
+	
+	if($group->delete_tag($tag, $user)) {
+		$panel->add_info( gettext('Tag deleted') );
+	} else {
+		$panel->add_error( gettext('Unable to delete tag') );
+	}
+}
+

Modified: trunk/vhffs-panel/templates/group/tags.tmpl
===================================================================
--- trunk/vhffs-panel/templates/group/tags.tmpl	2008-04-02 22:00:59 UTC (rev 1183)
+++ trunk/vhffs-panel/templates/group/tags.tmpl	2008-04-05 16:25:18 UTC (rev 1184)
@@ -1,4 +1,27 @@
-<h2>Tags</h2>
+<a name="tags"></a><h2>Tags</h2>
+<TMPL_IF NAME="CURRENT_TAGS">
 <h3><TMPL_I18N KEY="Current tags for this project"></h3>
-
+<ul>
+<TMPL_LOOP NAME="CURRENT_TAGS">
+<li><TMPL_VAR NAME="LABEL">
+<ul>
+<TMPL_LOOP NAME="TAGS">
+<li><a href="?delete_tag_submit=true&amp;tag_id=<TMPL_VAR NAME="TAG_ID">#tags"><TMPL_VAR NAME="LABEL"></a></li>
+</TMPL_LOOP>
+</ul>
+</li>
+</TMPL_LOOP>
+</ul>
+</TMPL_IF>
 <h3><TMPL_I18N KEY="Add a new tag"></h3>
+<ul>
+<TMPL_LOOP NAME="CATEGORIES">
+<li><TMPL_VAR NAME="LABEL">:
+<ul>
+<TMPL_LOOP NAME="TAGS">
+<li><a href="?add_tag_submit=true&amp;tag_id=<TMPL_VAR NAME="TAG_ID">#tags"><TMPL_VAR NAME="LABEL"></a></li>
+</TMPL_LOOP>
+</ul>
+</li>
+</TMPL_LOOP>
+</ul>
\ No newline at end of file


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/