1 package Blog
::Helper
::Tag
;
5 use base
'Blog::Helper';
10 $self = $self->next::method
( @_ );
12 $self->{ empty_tags
} = [];
13 $self->{ levels
} = 24;
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
',
26 with_objects => 'post_tag_map
'
32 foreach my $tag ( @$tags ) {
34 foreach my $method ( qw/ id name / ) {
35 $hash->{ $method } = $tag->$method;
37 $hash->{ count } = $tag->map_count;
38 if ( $hash->{ count } > 0 ) {
41 push @{ $self->{ empty_tags } }, $hash;
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 } );
54 # special case all tags having the same count
55 if ( $max - $min == 0 ) {
56 $min = $min - $self->{ levels };
59 $factor = $self->{ levels } / ( $max - $min );
64 if ( scalar @$ar < $self->{ levels } ) {
65 $factor *= ( scalar @$ar / $self->{ levels } );
71 foreach my $tag ( @$ar ) {
72 $tag->{ level } = int( ( log( $tag->{ count } ) - $min ) * $factor );
84 This library is free software, you can redistribute it and/or modify
85 it under the same terms as Perl itself.