<?page
title=><?_ml .title _ml?>
body<=
<?_code
{
    use strict;
    use vars qw(%GET);

    LJ::set_active_crumb('birthdays');

    my $remote = LJ::get_remote();
    return "<?needlogin?>" unless $remote;

    my $u;
    if ($GET{user}) {
        $u = LJ::load_user($GET{user});
        return BML::ml('.error.invaliduser', { user => LJ::ehtml($GET{user}) })
            unless $u;
        return BML::ml('.error.badstatus', { user => LJ::ljuser($u->{user}) })
            if $u->{statusvis} ne 'V';
    }

    my $body;
    my $lastmon = 0;

    $body .= "<?p $ML{'.findothers'} p?>";
    $body .= "<form method='get' action='$LJ::SITEROOT/birthdays.bml'>";
    $body .= LJ::html_text({ name => 'user', maxlength => 15, size => 15 });
    $body .= LJ::html_submit(undef, $ML{'.view'});
    $body .= "</form>";

    $body .= '<?p ';
    if ($u) {
        $body .= BML::ml('.description.others', { user => LJ::ljuser($u) });
    } else {
        $body .= $ML{'.description'};
    }
    $body .= ' p?>';

    # TAG:fr:bml_birthdays:get_bdays
    my $dbr = LJ::get_db_reader();
    my $sth = $dbr->prepare(
        qq|SELECT u.user, u.name, MONTH(bdate) AS 'month', DAYOFMONTH(bdate) AS 'day'
           FROM friends f, user u
           WHERE f.userid = ?
             AND f.friendid = u.userid
             AND u.journaltype = 'P'
             AND u.statusvis = 'V'
             AND u.allow_infoshow = 'Y'
             AND MONTH(bdate) != 0
             AND DAYOFMONTH(bdate) != 0
           LIMIT 750|);
    $sth->execute($u ? $u->{userid} : $remote->{userid});

    my @bdays;
    push @bdays, $_ while $_ = $sth->fetchrow_hashref;
    @bdays = sort {
        ($a->{'month'} <=> $b->{'month'}) ||
        ($a->{'day'} <=> $b->{'day'}) ||
        ($a->{'user'} cmp $b->{'user'})
    } @bdays;

    foreach my $bday (@bdays) {
        LJ::text_out(\$bday->{'name'});

        if ($bday->{'month'} != $lastmon) {
            if ($lastmon) { $body .= "</ul>\n"; }
            $lastmon = $bday->{'month'};
            $body .= "<?h1 " . LJ::Lang::month_long($lastmon) . " h1?><ul>\n";
        }

        my $day = sprintf("%2s", $bday->{'day'});
        $day =~ s/ /&nbsp;/;

        my $name = LJ::ehtml($bday->{'name'});
        $body .= "<b><tt>$day</tt></b>: " . LJ::ljuser($bday->{'user'}) . " - $name<br />\n";
    }

    if (@bdays) {
        $body .= "</ul>\n";
    } else {
        $body .= "<?p $ML{'.nobirthdays'} p?>";
    }

    return $body;
}
_code?>
<=body
page?><?_c <LJDEP>
link: htdocs/login.bml
</LJDEP> _c?>

