| | 381 | sub mt_blog_stats_tag_cloud_tab { |
| | 382 | my ($app, $tmpl, $param) = @_; |
| | 383 | |
| | 384 | my $blog = $app->blog; |
| | 385 | my $blog_id = $blog->id if $blog; |
| | 386 | |
| | 387 | my $terms = {}; |
| | 388 | $terms->{blog_id} = $blog_id if $blog_id; |
| | 389 | $terms->{object_datasource} = 'entry'; |
| | 390 | my $args = {}; |
| | 391 | $args->{group} = [ 'tag_id' ]; |
| | 392 | $args->{sort} = 'count(*)'; |
| | 393 | $args->{direction} = 'descend'; |
| | 394 | $args->{limit} = 100; |
| | 395 | |
| | 396 | my $iter = MT::ObjectTag->count_group_by($terms, $args); |
| | 397 | my @tag_loop; |
| | 398 | my $ntags = 0; |
| | 399 | my $min = undef; |
| | 400 | my $max = undef; |
| | 401 | while (my ($count, $tag_id) = $iter->()) { |
| | 402 | my $tag = MT::Tag->load($tag_id) or next; |
| | 403 | next if $tag->is_private; # weed these from the dashboard |
| | 404 | $ntags += $count; |
| | 405 | $min = defined $min ? ($count < $min ? $count : $min) : $count; |
| | 406 | $max = defined $max ? ($count > $max ? $count : $max) : $count; |
| | 407 | push @tag_loop, { name => $tag->name, count => $count }; |
| | 408 | } |
| | 409 | |
| | 410 | my $factor; |
| | 411 | if ($max - $min == 0) { |
| | 412 | $min -= 6; |
| | 413 | $factor = 1; |
| | 414 | } else { |
| | 415 | $factor = 5 / log($max - $min + 1); |
| | 416 | } |
| | 417 | $factor *= ($ntags / 6) if $ntags < 6; |
| | 418 | |
| | 419 | foreach my $tag (@tag_loop) { |
| | 420 | # now calc rank |
| | 421 | my $rank; |
| | 422 | my $count = $tag->{count}; |
| | 423 | if ($count - $min + 1 == 0) { |
| | 424 | $rank = 0; |
| | 425 | } else { |
| | 426 | $rank = 6 - int(log($count - $min + 1) * $factor); |
| | 427 | } |
| | 428 | $tag->{rank} = $rank; |
| | 429 | } |
| | 430 | |
| | 431 | @tag_loop = sort { $a->{name} cmp $b->{name} } @tag_loop; |
| | 432 | $param->{tag_loop} = \@tag_loop; |
| | 433 | } |
| | 434 | |