Committer: afedorov
LJSUP-7955: Top 6 commenters for ONTDU trunk/cgi-bin/LJ/Widget/TopUsers.pm
Modified: trunk/cgi-bin/LJ/Widget/TopUsers.pm =================================================================== --- trunk/cgi-bin/LJ/Widget/TopUsers.pm 2011-03-01 11:31:45 UTC (rev 18424) +++ trunk/cgi-bin/LJ/Widget/TopUsers.pm 2011-03-01 11:33:41 UTC (rev 18425) @@ -5,6 +5,7 @@ use Carp qw(croak); use LJ::ExtBlock; +use LJ::JSON; # Keys to get data from ext_block my %keys = (); @@ -16,54 +17,48 @@ my %debug_data = ( 'ontd_authors' => '[{"count":"2","userid":"3"},{"count":"1","userid":"4"}]', - 'ontd_commenters' => '[{"count":"276","userid":"2"},{"count":"170","userid":"3"},{"count":"139","userid":"4"},{"count":"124","userid":"5"},{"count":"123","userid":"6"}]', + 'ontd_commenters' => '[{"count":"276","userid":"2"},{"count":"170","userid":"3"},{"count":"139","userid":"4"},{"count":"124","userid":"5"},{"count":"123","userid":"6"},{"count":"120","userid":"6"}]', ); sub _fetch_data { %keys = ( - #'ontd_authors' => { title => "widget.topusers.top5posters.title", order => 2 }, - 'ontd_commenters' => { title => "widget.topusers.top5commenters.title", order => 2 }, + #'ontd_authors' => { title => "widget.topusers.top5posters.title", order => 1, data => [] }, + 'ontd_commenters' => { title => "widget.topusers.top5commenters.title", order => 2, data => [] }, ); foreach my $key (keys %keys) { - my $data; + my $json_data; if ($use_debug_data) { LJ::ExtBlock->create_or_replace($key, $debug_data{$key}) if $use_debug_data > 1; - $data = $debug_data{$key}; + $json_data = $debug_data{$key}; } else { my $block = LJ::ExtBlock->load_by_id($key); - $data = $block->blocktext() if $block; + $json_data = $block->blocktext() if $block; } + next unless $json_data; - next unless $data; + my $data = LJ::JSON->from_json($json_data); + next unless ref($data) eq 'ARRAY'; + + foreach (@$data) { + my $count = $_->{count}; + my $user = LJ::load_userid($_->{userid}); + my $userpic = $user->userpic() if $user; + warn "Cannot load user with id=$_->{userid}\n" unless $user; + warn "Cannot load userpic with id=$_->{userid}\n" unless $userpic; - $data =~ s/\[|\]//g; - while ($data) { - $data =~ s/\{([^\}]*)\},?//; - if ($1) { - my ($count, $user) = 2 x undef; - foreach my $pair (split(/,/, $1)) { - $pair =~ s/\{|\}|\"//g; - my ($name, $value) = split(/:/, $pair); - if ('count' eq $name) { - $count = $value; - } elsif ('userid' eq $name) { - $user = LJ::load_userid($value); - warn "Cannot load user with id=$value\n" unless $user; - } - } - if ($count && $user) { - my $userpic = $user->userpic(); - $userpic = $userpic->url if $userpic; - push @{$keys{$key}->{'data'}}, - { - count => $count, - userpic => $userpic, - display => $user->ljuser_display(), - }; - } + if ($count && $user && $userpic) { + push @{$keys{$key}->{'data'}}, + { + count => $count, + userpic => $userpic ? $userpic->url : '', + display => $user->ljuser_display, + user => $user->user, + url => $user->journal_base, + }; } + } @{$keys{$key}->{'data'}} = sort { $b->{'count'} <=> $a->{'count'} } @{$keys{$key}->{'data'}}; $keys{$key}->{'title'} = BML::ml($keys{$key}->{'title'}); @@ -83,39 +78,34 @@ my @keys = sort { $keys{$a}->{'order'} <=> $keys{$b}->{'order'} } keys %keys; # Head of whole widget - $ret .= "<div class='w-topusers w-ontd'><div class='w-head'><h2><span class='w-head-in'>". $class->ml('widget.topusers.spotlight.title') ."</span></h2> - <i class='w-head-corner'></i></div><div class='w-content'>"; + $ret .= "<img src='http://localhostr.com/files/yRmFCoW/topcommenters.gif' alt='Top commenters' /><div id='topcommentersbox'>"; foreach my $key (@keys) { - - # Header of widget column - $ret .= "<h3>".$keys{$key}->{'title'}."</h3>"; my $index = 1; - $ret .= "<ol>"; + $ret .= "<ol>"; foreach my $data (@{$keys{$key}->{'data'}}) { # Element begin - $ret .= "<li>"; + $ret .= "<li style='background-image: url($data->{userpic});'>"; # User info - $ret .= $data->{'display'}; - $ret .= "<span class='num'>" . $data->{'count'} . "</span>"; + $ret .= "<a href='$data->{url}'>$data->{user} <span>$data->{'count'}</span></a>"; # Element end $ret .= "</li>"; $index++; - last if $index > 5; + last if $index > 6; } # Footer of coumn - $ret .= "</li></ol>"; + $ret .= "</ol><br style='clear: both;' />"; } # Footer of whole widget - $ret .= "</div></div>"; + $ret .= "</div>"; return $ret; }