Igor Gariev (gariev) wrote in changelog,
Igor Gariev
gariev
changelog

[livejournal] r19235: LJSUP-8957: Add months and years calenda...

Committer: gariev
LJSUP-8957: Add months and years calendars to all s2 style pages

U   trunk/cgi-bin/LJ/S2.pm
U   trunk/cgi-bin/LJ/User.pm
A   trunk/htdocs/tools/endpoints/calendar.bml
Modified: trunk/cgi-bin/LJ/S2.pm
===================================================================
--- trunk/cgi-bin/LJ/S2.pm	2011-06-06 07:22:58 UTC (rev 19234)
+++ trunk/cgi-bin/LJ/S2.pm	2011-06-06 08:22:22 UTC (rev 19235)
@@ -154,6 +154,10 @@
 
     $page->{head_content} .= LJ::res_includes() . $extra_js;
     LJ::run_hooks('head_content', \$page->{head_content});
+    my $calendar_json = LJ::JSON->to_json( LJ::get_calendar_data_for_month($u) );
+    $page->{head_content} .= "<script type='text/javascript'>\n" . 
+        "Site = window.Site || {};\nSite.journal_calendar = $calendar_json;\n".
+        "</script>\n";
 
     s2_run($r, $ctx, $opts, $entry, $page);
     

Modified: trunk/cgi-bin/LJ/User.pm
===================================================================
--- trunk/cgi-bin/LJ/User.pm	2011-06-06 07:22:58 UTC (rev 19234)
+++ trunk/cgi-bin/LJ/User.pm	2011-06-06 08:22:22 UTC (rev 19235)
@@ -7713,6 +7713,44 @@
     return \@days;
 }
 
+## input: $u, $remote, $year, $month
+## output: hashref with data for rendering calendar for given month,
+##      days:       hashref { day: count of entries for this day }
+##      prev_month: arrayref [year, month] - previous month that has entries
+##      next_month, prev_year, next_year - arrayref of the same format
+##
+sub get_calendar_data_for_month {
+    my ($u, $remote, $year, $month) = @_;
+
+    $remote ||= LJ::get_remote();
+    unless ($year || $month) {
+        ($month, $year) = (localtime)[4, 5];
+        $year += 1900;
+        $month++;
+    }
+
+    my %ret = (journal => $u->user, year => $year, month => $month);
+    my $days = LJ::get_daycounts($u, $remote);
+    foreach my $d (@$days) {
+        ## @$d = ($y, $m, $d, $count)
+        if ($d->[0]==$year && $d->[1]==$month) {
+            $ret{days}->{ $d->[2] } = $d->[3];
+        }
+    }
+    ## $prev_month  = max(  grep { $day < Date($year, $month) }  @$days  );
+    ## max @list    = List::Util::reduce { $a > $b ? $a : $b } @list
+    my $current_month   = [$year, $month];
+    my $less_year       = sub { my ($a, $b) = @_; return $a->[0]<$b->[0];  };
+    my $less            = sub { my ($a, $b) = @_; return $a->[0]<$b->[0] || $a->[0]==$b->[0] && $a->[1]<$b->[1] };
+    $ret{'prev_month'}  = List::Util::reduce { $less->($a, $b) ? $a : $b }  grep { $less->($_, $current_month) }        @$days;
+    $ret{'next_month'}  = List::Util::reduce { $less->($a, $b) ? $b : $a }  grep { $less->($current_month, $_) }        @$days;
+    $ret{'prev_year'}   = List::Util::reduce { $less->($a, $b) ? $a : $b }  grep { $less_year->($_, $current_month) }   @$days;
+    $ret{'next_year'}   = List::Util::reduce { $less->($a, $b) ? $b : $a }  grep { $less_year->($current_month, $_) }   @$days;
+    
+    return \%ret;
+}
+
+
 # <LJFUNC>
 # name: LJ::set_interests
 # des: Change a user's interests.

Added: trunk/htdocs/tools/endpoints/calendar.bml
===================================================================
--- trunk/htdocs/tools/endpoints/calendar.bml	                        (rev 0)
+++ trunk/htdocs/tools/endpoints/calendar.bml	2011-06-06 08:22:22 UTC (rev 19235)
@@ -0,0 +1,14 @@
+<?_code 
+{
+    use strict;
+    use vars '%GET';
+    BML::set_content_type('text/javascript; charset=utf-8');
+    BML::finish();
+    
+    my $journalu = LJ::get_active_journal() 
+        or die "This endpoint must be called from journal pages";
+    my $remote = LJ::get_remote();
+    return LJ::JSON->to_json( LJ::get_calendar_data_for_month($journalu, $remote, $GET{'year'}, $GET{'month'}) );
+}
+_code?>
+

Tags: bml, gariev, livejournal, pm
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments