archive page
[blog.pm.git] / lib / Blog / Controller / Tag.pm
blob02ebb40850795fdbfdcedd3f30a5951e49a9ff3d
1 package Blog::Controller::Tag;
3 use strict;
4 use warnings;
5 use base 'Catalyst::Controller';
7 use Blog::Form::Post;
9 use HTML::TagCloud;
11 sub list : Path('') {
12 my ( $self, $c ) = @_;
14 my $tags = $c->model( 'Tag' )->search(
15 select => [ 'id', 'name', \'COUNT(t2.tag_id) AS map_count' ],
16 sort_by => 'name ASC',
17 group_by => 'id',
18 with_objects => 'post_tag_map'
21 if ( @$tags ) {
22 my $cloud = HTML::TagCloud->new;
24 foreach my $tag ( @$tags ) {
25 $cloud->add( $tag->name, $c->uri_for( '/tag', $tag->id ),
26 $tag->map_count )
27 if $tag->map_count;
30 if ( $c->user_in_realm( 'admins' ) ) {
31 $c->stash->{ empty_tags } = [ grep { $_->map_count == 0 } @$tags ];
34 $c->stash->{ cloud } = $cloud->html;
38 sub view : LocalRegex('^(\d+)$') {
39 my ( $self, $c ) = @_;
41 my $tag = $c->model( 'Tag' )->create( id => $c->req->captures->[ 0 ] );
42 $tag->load( speculative => 1 );
44 $self->throw_not_found( $c ) if $tag->not_found;
46 $c->stash->{ tag } = $tag;
48 my $posts = $tag->posts;
50 $c->stash->{ posts } = [ @$posts ];
53 sub delete : LocalRegex('^(\d+)/delete$') {
54 my ( $self, $c ) = @_;
56 my $object = $c->model( 'Tag' )->create( id => $c->req->captures->[ 0 ] );
57 $object->load( speculative => 1, select => [ 'id' ] );
58 $self->throw_not_found( $c ) if $object->not_found;
60 $object->delete;
62 $c->flash->{ msg } = $c->loc( 'Tag was deleted sucessfully' );
64 $c->res->redirect( '/tag' );
67 sub throw_not_found {
68 my ( $self, $c ) = @_;
70 $c->detach( '/default' );
73 =head1 AUTHOR
75 vti
77 =head1 LICENSE
79 This library is free software, you can redistribute it and/or modify
80 it under the same terms as Perl itself.
82 =cut