Changeset 25 in bookmarks for trunk/Bookmarks.pm
- Timestamp:
- 05/23/13 00:37:57 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Bookmarks.pm
r24 r25 8 8 has dbh => ( is => 'rw' ); 9 9 has base_uri => ( is => 'ro', isa => 'URI' ); 10 11 has _sth_tags_from_uri => ( 12 is => 'ro', 13 init_arg => undef, 14 lazy => 1, 15 default => sub { $_[0]->dbh->prepare('select tag from tags where uri = ? order by tag'); }, 16 ); 10 17 11 18 sub BUILD { … … 29 36 my $self = shift; 30 37 my $params = shift; 38 39 # look for bookmark by id or uri 31 40 my $sth; 32 41 if ($params->{id}) { … … 40 49 } 41 50 my $bookmark = $sth->fetchrow_hashref; 42 if ($bookmark) { 43 my $sth_tag = $self->dbh->prepare('select tag from tags where uri = ? order by tag'); 44 $sth_tag->execute($bookmark->{uri}); 45 $bookmark->{tags} = [ map { $$_[0] } @{ $sth_tag->fetchall_arrayref } ]; 46 if ($self->base_uri) { 47 $bookmark->{bookmark_uri} = URI->new_abs($bookmark->{id}, $self->base_uri); 48 } 49 } 50 return $bookmark; 51 return unless $bookmark; 52 53 return Bookmark->new({ 54 %$bookmark, 55 tags => [ $self->get_tags({ uri => $bookmark->{uri} }) ], 56 base_uri => $self->base_uri, 57 }); 51 58 } 52 59 … … 85 92 $sth_resource->execute(@bind); 86 93 87 my $sth_tag = $self->dbh->prepare('select tag from tags where uri = ? order by tag');88 94 my @resources; 89 95 while (my $resource = $sth_resource->fetchrow_hashref) { 90 $sth_tag->execute($resource->{uri}); 91 $resource->{tags} = [ map { $$_[0] } @{ $sth_tag->fetchall_arrayref } ]; 92 if ($self->base_uri) { 93 $resource->{bookmark_uri} = URI->new_abs($resource->{id}, $self->base_uri); 94 } 95 push @resources, $resource; 96 push @resources, Bookmark->new({ 97 %$resource, 98 tags => [ $self->get_tags({ uri => $resource->{uri} }) ], 99 base_uri => $self->base_uri, 100 }); 96 101 } 97 102 return @resources; … … 101 106 my $self = shift; 102 107 my $params = shift; 103 my $tag = $params->{selected}; 104 my $sth_all_tags = $self->dbh->prepare('select tag, count(tag) as count, tag = ? as selected from tags group by tag order by tag'); 105 $sth_all_tags->execute($tag); 106 my $all_tags = $sth_all_tags->fetchall_arrayref({}); 107 return @{ $all_tags }; 108 if (my $uri = $params->{uri}) { 109 # get the tags for a particular URI 110 $self->_sth_tags_from_uri->execute($uri); 111 return map { $$_[0] } @{ $self->_sth_tags_from_uri->fetchall_arrayref }; 112 } else { 113 # return all tags 114 my $tag = $params->{selected}; 115 my $sth_all_tags = $self->dbh->prepare('select tag, count(tag) as count, tag = ? as selected from tags group by tag order by tag'); 116 $sth_all_tags->execute($tag); 117 my $all_tags = $sth_all_tags->fetchall_arrayref({}); 118 return @{ $all_tags }; 119 } 108 120 } 109 121
Note: See TracChangeset
for help on using the changeset viewer.