Messing with common i18n modules.
[blog.pm.git] / lib / Blog / Helper / Tag.pm
blobf37aa1a6a9b5d26e4253c549d06446c8154b0784
1 package Blog::Helper::Tag;
3 use strict;
5 use base 'Blog::Helper';
7 sub new {
8 my $self = shift;
10 $self = $self->next::method( @_ );
12 $self->{ empty_tags } = [];
13 $self->{ levels } = 24;
15 return $self;
18 sub cloud_list {
19 my ( $self ) = @_;
21 my $c = Blog->context;
22 my $tags = $c->model( 'Tag' )->search(
23 select => [ 'id', 'name', \'COUNT(t2.tag_id) AS map_count' ],
24 sort_by => 'name ASC',
25 group_by => 'id',
26 with_objects => 'post_tag_map'
29 return unless @$tags;
31 my $ar = [];
32 foreach my $tag ( @$tags ) {
33 my $hash = {};
34 foreach my $method ( qw/ id name / ) {
35 $hash->{ $method } = $tag->$method;
37 $hash->{ count } = $tag->map_count;
38 if ( $hash->{ count } > 0 ) {
39 push @$ar, $hash;
40 } else {
41 push @{ $self->{ empty_tags } }, $hash;
45 return unless @$ar;
47 # from HTML::TagCloud
48 my @sorted_by_count = sort { $b->{ count } <=> $a->{ count } } @$ar;
50 my $min = log( $sorted_by_count[ -1 ]->{ count } );
51 my $max = log( $sorted_by_count[ 0 ]->{ count } );
52 my $factor;
54 # special case all tags having the same count
55 if ( $max - $min == 0 ) {
56 $min = $min - $self->{ levels };
57 $factor = 1;
58 } else {
59 $factor = $self->{ levels } / ( $max - $min );
62 #warn $factor;
64 if ( scalar @$ar < $self->{ levels } ) {
65 $factor *= ( scalar @$ar / $self->{ levels } );
68 #warn $min;
69 #warn $max;
71 foreach my $tag ( @$ar ) {
72 $tag->{ level } = int( ( log( $tag->{ count } ) - $min ) * $factor );
75 return $ar;
78 =head1 AUTHOR
80 vti
82 =head1 LICENSE
84 This library is free software, you can redistribute it and/or modify
85 it under the same terms as Perl itself.
87 =cut