From: Chris W. <la...@us...> - 2004-10-24 16:40:16
|
Update of /cvsroot/openinteract/OpenInteract2/extra_packages/delicious_tags/OpenInteract2 In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18386/delicious_tags/OpenInteract2 Added Files: DeliciousTag.pm DeliciousTaggableObject.pm Log Message: intial import of delicious_tags package --- NEW FILE: DeliciousTag.pm --- package OpenInteract2::DeliciousTag; # $Id: DeliciousTag.pm,v 1.1 2004/10/24 16:40:08 lachoy Exp $ use strict; @OpenInteract2::DeliciousTag::ISA = qw( OpenInteract2::DeliciousTagPersist ); sub fetch_all_tags { my ( $class ) = @_; my $tags = $class->db_select({ select_modifier => 'DISTINCT', select => [ 'tag' ], from => [ $class->table_name ], order => 'tag', return => 'single-list', }); return $tags; } sub fetch_all_tags_with_count { my ( $class, $option ) = @_; my $counts = $class->db_select({ select => [ 'tag', 'count(*)' ], from => [ $class->table_name ], order => 'tag', group => 'tag', }); return $counts; } sub fetch_tags_for_object { my ( $class, $object_type, $object_id ) = @_; my $tags = $class->db_select({ select => [ 'tag' ], from => [ $class->table_name ], where => 'object_type = ? AND object_id = ?', value => [ $object_type, $object_id ], order => 'tag', return => 'single-list', }); return $tags; } # Return all objects that have a given tag in \@tags; we uniq-ify them # first so there's only one instance of each in the returned arrayref sub fetch_tag_objects { my ( $class, $tags, $skip_type, $skip_id ) = @_; my $where = $class->_create_or_clause_for_tags( @{ $tags } ); my @values = @{ $tags }; if ( $skip_type and $skip_id ) { $where .= ' AND ( object_type != ? AND object_id != ? )'; push @values, $skip_type, $skip_id; } my $tag_objects = $class->fetch_group({ where => $where, value => \@values, order => 'object_type', }); my @uniq_tag_objects = (); my %seen = (); foreach my $tag_object ( @{ $tag_objects } ) { my ( $type, $id ) = ( $tag_object->{object_type}, $tag_object->{object_id} ); next if ( $seen{ $type }->{ $id } ); $seen{ $type }->{ $id }++; push @uniq_tag_objects, $tag_object; } return \@uniq_tag_objects; } sub fetch_tags_with_count_for_object { my ( $class, $object_type, $object_id ) = @_; my $tags = $class->fetch_tags_for_object( $object_type, $object_id ); return $class->_fetch_counts_for_tags( $tags ); } sub fetch_count { my ( $class, $tag ) = @_; my $count = $class->db_select({ select => [ 'count(*)' ], from => [ $class->table_name ], where => 'tag = ?', value => [ $tag ], return => 'single-list', }); return $count->[0]; } sub fetch_related_tags { my ( $class, @base_tags ) = @_; my $table = $class->table_name; my $base_where = $class->_create_or_clause_for_tags( @base_tags ); my $sql = qq{ SELECT d2.tag FROM $table d1, $table d2 WHERE ( $base_where ) AND d2.object_type = d1.object_type AND d2.object_id = d1.object_id AND d2.tag != d1.tag }; my $tags = $class->db_select({ sql => $sql, value => \@base_tags, return => 'single-list', }); return $tags; } sub fetch_related_tags_with_count { my ( $class, $base_tag ) = @_; my $tags = $class->fetch_related_tags( $base_tag ); return $class->_fetch_counts_for_tags( $tags ); } sub _fetch_counts_for_tags { my ( $class, $tags ) = @_; my $where = $class->_create_or_clause_for_tags( @{ $tags } ); my $counts = $class->db_select({ select => [ 'tag', 'count(*)' ], from => [ $class->table_name ], where => $where, value => $tags, group => 'tag', }); return $counts; } sub _create_or_clause_for_tags { my ( $class, @tags ) = @_; return '(' . join( ' OR ', map { 'tag = ?' } @tags ) . ')'; } 1; --- NEW FILE: DeliciousTaggableObject.pm --- package OpenInteract2::DeliciousTaggableObject; # $Id: DeliciousTaggableObject.pm,v 1.1 2004/10/24 16:40:08 lachoy Exp $ use strict; use OpenInteract2::DeliciousTag; sub c_fetch_my_tags { my ( $class, $object_type, $id ) = @_; return OpenInteract2::DeliciousTag ->fetch_tags_for_object( $object_type, $id ); } sub c_fetch_my_tags_with_count { my ( $class, $object_type, $id ) = @_; return OpenInteract2::DeliciousTag ->fetch_tags_with_count_for_object( $object_type, $id ); } sub c_fetch_my_related_object_info { my ( $class, $object_type, $id, @tags ) = @_; my $related_tags = OpenInteract2::DeliciousTag ->fetch_related_tags( $object_type, $id, @tags ); return OpenInteract2::DeliciousTag ->fetch_tag_objects( $related_tags, $object_type, $id ); } sub fetch_my_tags { my ( $object ) = @_; my $type = $object->CONFIG->{object_name}; return __PACKAGE__->c_fetch_my_tags( $type, $object->id ); } sub fetch_my_tags_with_count { my ( $object ) = @_; my $type = $object->CONFIG->{object_name}; return __PACKAGE__->c_fetch_my_tags_with_count( $type, $object->id ); } sub fetch_my_related_object_info { my ( $object, @optional_tags ) = @_; my $type = $object->CONFIG->{object_name}; return __PACKAGE__->c_fetch_my_related_object_info( $type, $object->id ); } 1; |