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

    LJ::set_active_crumb('acctstatus');

    my $remote = LJ::get_remote();
    return LJ::bad_input($ML{'error.noremote'})
        unless $remote;

    my $authas = $GET{'authas'} || $remote->{'user'};
    my $u = LJ::get_authas_user($authas);
    return LJ::bad_input($ML{'error.invalidauth'})
        unless $u;

    # visibility status name mappings
    my %statusvis = ( "V" => "$ML{'.journalstatus.select.activated'}",
                      "D" => "$ML{'.journalstatus.select.deleted'}" );
    if ($u->{'statusvis'} eq "S") {
        %statusvis = ("S" => "$ML{'.journalstatus.select.suspended'}");
    }

    # process form submission
    if (LJ::did_post() && LJ::check_referer('/accountstatus.bml')) {

        my $ok = sub {
            return "<?h1 $ML{'.header.success'} h1?>\n<?p " . $_[0] . " p?>";
        };

        my $dberr = sub {
            return "<?h1 $ML{'Error'} h1?>\n<?p " . $_[0] . " p?>";
        };

        # are they suspended?
        return LJ::bad_input($ML{'.error.nochange.suspend'})
            if $u->{'statusvis'} eq 'S';

        # are they expunged?
        return LJ::bad_input($ML{'.error.nochange.expunged'})
            if $u->{'statusvis'} eq 'X';

        # invalid statusvis
        return LJ::bad_input($ML{'.error.invalid'})
            unless $POST{'statusvis'} eq 'D' || $POST{'statusvis'} eq 'V';

        # no need to change?
        return $ok->(BML::ml('.message.nochange', {'statusvis' => $statusvis{$POST{'statusvis'}} }))
            if $u->{'statusvis'} eq $POST{'statusvis'};

        # do update
        my $res = LJ::update_user($u, { 'statusvis' => $POST{'statusvis'},
                                        'raw' => 'statusvisdate=NOW()' });

        # error updating?
        return $dberr->($ML{'.error.db'})
            unless $res;

        # success
        return $ok->(BML::ml('.message.success', {'statusvis' => $statusvis{$POST{'statusvis'}} }));

    }

    # update form

    my $ret;

    # authas switcher form
    $ret .= "<form method='get' action='accountstatus.bml'>\n";
    $ret .= LJ::make_authas_select($remote, { 'authas' => $GET{'authas'} }) . "\n";
    $ret .= "</form>\n\n";

    my $getextra = "?authas=$authas" unless $authas eq $remote->{'user'};
    $ret .= "<form method='post' action='accountstatus.bml$getextra'>\n";

    $ret .= "<?h1 $ML{'.journalstatus.head'} h1?><?p $ML{'.journalstatus.about'} p?>\n";
    $ret .= "<div style='margin-left: 30px'>$ML{'.journalstatus.select.head'}\n";
    $ret .= LJ::html_select({ 'name' => 'statusvis', 'selected' => $u->{'statusvis'} },
                              map { $_, $statusvis{$_} }
                              reverse sort keys %statusvis) . "\n";

    $ret .= LJ::html_submit(undef, $ML{'.btn.status'}, 
                            { 'disabled' => $u->{'statusvis'} eq 'S' }) . "\n";
    $ret .= "</div>\n</form>\n";

    return $ret;
}
_code?>
<=body
page?>

