<?page
title=><?_ml .title _ml?>
body<=
<?_code

 use strict;
 use vars qw(%GET %POST);

 return LJ::server_down_html() if ($LJ::SERVER_DOWN);

 LJ::set_active_crumb('managecommunity');

 my $dbr = LJ::get_db_reader();

 my $ret;
 my $remote = LJ::get_remote();

 unless ($remote) {
    return "<?needlogin?>";
 }

 return "<?h1 $ML{'Error'} h1?><?p $ML{'.error.badaccounttype'} p?>"
    unless $remote->{journaltype} eq 'P';

 $ret .= "<?h1 $ML{'.commlist.header'} h1?><?p $ML{'.commlist.text'} p?>";
 $ret .= "<div style='margin: 15px 0 15px 40px'>";

 my %show;

 # what communities does remote 
 my $cids = LJ::load_rel_target($remote, 'A');
 my %admin;
 if ($cids) { foreach (@$cids) { $admin{$_} = $show{$_} = 1; } }

 # keep track of what communities remote moderates
 my $mods = LJ::load_rel_target($remote, 'M');
 my %mods; 
 my %modcount;
 if ($mods) { foreach (@$mods) { $mods{$_} = $show{$_} = 1; } }

 my %names = ();
 my %pending = ();
 if (%show) {
    my $in = join(',', keys %show);
    my $us = LJ::load_userids(keys %show);

    my $sth = $dbr->prepare("SELECT userid, membership FROM community ".
                            "WHERE userid IN ($in)");
    $sth->execute;
    my $udbr;
    while (my ($uid, $membership) = $sth->fetchrow_array) {
	my $cu = $us->{$uid};
	next unless $cu && $cu->{statusvis} eq "V";
        $names{$uid} = [ $cu->{user}, $cu->{name}, -1 ];
        if ($mods{$uid}) {
            $udbr = LJ::get_cluster_reader($cu);
            my $sql = "SELECT COUNT(*) FROM modlog WHERE journalid=$uid";
            $modcount{$uid} = $names{$uid}[2] = $udbr->selectrow_array($sql) || 0;
        }
        if ($membership eq 'moderated') {
            my $ids = LJ::get_pending_members($uid) || [];
            $pending{$uid} = scalar @$ids;
        }
    }
 }

 # deleted and expunged communities won't show up in this
 if (%names) {

    # Make up the first table row, linking to the other sort methods
    my @snames;
    $ret .= "<table cellpadding='5'><tr style='text-align: left;'>";

    if (!defined $GET{'sortby'} || $GET{'sortby'} eq 'username') {
        $ret .= "<th>$ML{'.commlist.username'}</th>";
        @snames = sort { $names{$a}->[0] cmp $names{$b}->[0] } keys %names;
    } else {
        $ret .= "<th><a href='".BML::self_link({'sortby' => 'username'}) . "'>$ML{'.commlist.username'}</a></th>";
    }

    if ($GET{'sortby'} eq 'title') {
        $ret .= "<th>$ML{'.commlist.title'}</th>";
        @snames = sort { $names{$a}->[1] cmp $names{$b}->[1] } keys %names;
    } else {
        $ret .= "<th><a href='".BML::self_link({'sortby' => 'title'}) . "'>$ML{'.commlist.title'}</a></th>";
    }

    $ret .= "<th>$ML{'.commlist.actions'}</th>";

    if ($GET{'sortby'} eq 'mod') {
        $ret .= "<th>$ML{'.commlist.moderation'}</th>";
        @snames = sort { $names{$b}->[2] <=> $names{$a}->[2] } keys %names;
    } elsif (@$mods) {
        $ret .= "<th><a href='".BML::self_link({'sortby' => 'mod'}) . "'>$ML{'.commlist.moderation'}</a></th>";
    }

    foreach my $id (@snames) {
        my $user = $names{$id}->[0];
        my $name = $names{$id}->[1];
        $ret .= "<tr><td><?ljcomm $user ljcomm?></td><td>" . LJ::eall($name) . "</td><td nowrap='nowrap'>";
        if ($admin{$id}) {
            $ret .= BML::ml('Actionlink', {
                'link'=>"<a href='/editinfo.bml?authas=$user'>$ML{'.commlist.actinfo'}</a>"}) . "&nbsp;&nbsp;";
            $ret .= BML::ml('Actionlink', {
                'link'=>"<a href='/community/settings.bml?comm=$user'>$ML{'.commlist.actsettings2'}</a>"}) . "&nbsp;&nbsp;";
            $ret .= BML::ml('Actionlink', {
                'link'=>"<a href='/community/sentinvites.bml?comm=$user'>$ML{'.commlist.actinvites'}</a>"}) . "&nbsp;&nbsp;";
            my $pend = $pending{$id} ? BML::ml('.commlist.actpending', { num => $pending{$id},
                                               aopts => "href=\"/community/pending.bml?comm=$user\"" }) : '';
            $ret .= BML::ml('Actionlink', {
                'link'=>"<a href='/community/members.bml?comm=$user'>$ML{'.commlist.actmembers2'}</a>$pend"}) . "&nbsp;&nbsp;";
        }
        $ret .= "</td><td align='center'>";
        if ($mods{$id}) {
            my $num = $modcount{$id} || "0"; # relying on this to be <b>-wrapped
            $ret .= BML::ml('.commlist.moderation.num', { num => $num,
                            link => "/community/moderate.bml?comm=$user" });
        }
        $ret .= "</td></tr>";
    }
    $ret .= "</table>";
 } else {
    $ret .= "$ML{'.commlist.none'}";
 }

 $ret .= "</div>";

 # save any changes
 if (LJ::did_post()) {
     my $op = $POST{email};
     $op = '' if $op && $op !~ /[N]/; # add D back in here when Digest goes live
     LJ::set_userprop($remote, 'opt_communityjoinemail', $op);

     my $mod_emails = $POST{modemail} ? 1 : 0;
     LJ::set_userprop($remote, 'opt_nomodemail', $mod_emails ? undef : 1);
 }

 # load up our userprop
 LJ::load_user_props($remote, 'opt_communityjoinemail', 'opt_nomodemail');
 my $op = $remote->{opt_communityjoinemail};
 $op ||= 'Y';
 my $mod_emails = $remote->{opt_nomodemail} ? 0 : 1;

 # form for setting email option
 $ret .= "<?h1 $ML{'.joinmail.title'} h1?>";
 $ret .= "<?p $ML{'.joinmail.body'} p?>";
 $ret .= "<form method='post' action='manage.bml'>";
 $ret .= "<div style='margin-left: 30px;'>";
 $ret .= LJ::html_check({ type => 'radio', name => 'email', id => 'email_all', 
                          value => '', selected => ($op eq 'Y') });
 $ret .= "<label for='email_all'>$ML{'.joinmail.email.all'}</label><br />";
 if (0) {
     # we don't do this right now, because the maintenance job to send email isn't
     # ready for use on LJ.  this will be removed and the option enabled when that
     # task is rewritten.  NOTE: add D up above in did_post() block.
     $ret .= LJ::html_check({ type => 'radio', name => 'email', id => 'email_digest', 
                              value => 'D', selected => ($op eq 'D') });
     $ret .= "<label for='email_digest'>$ML{'.joinmail.email.digest'}</label><br />";
 }
 $ret .= LJ::html_check({ type => 'radio', name => 'email', id => 'email_none', 
                          value => 'N', selected => ($op eq 'N') });
 $ret .= "<label for='email_none'>$ML{'.joinmail.email.none'}</label><br />";
 $ret .= "</div>";

 # options for getting moderation emails
 $ret .= "<?p $ML{'.modemail.body'} p?>";
 $ret .= "<div style='margin-left: 30px;'>";

 $ret .= LJ::html_check({ type => 'radio', name => 'modemail', id => 'modemail_yes', 
                          value => '1', selected => $mod_emails });
 $ret .= "<label for='modemail_yes'>$ML{'.modemail.yes'}</label><br />";

 $ret .= LJ::html_check({ type => 'radio', name => 'modemail', id => 'modemail_no', 
                          value => '0', selected => !$mod_emails });
 $ret .= "<label for='modemail_no'>$ML{'.modemail.no'}</label><br />";
 $ret .= "</div><?p ";
 $ret .= LJ::html_submit($ML{'.joinmail.save'});
 $ret .= " p?></form>";

 $ret .= "<?h1 $ML{'.create.header'} h1?>";
 $ret .= "<?p " . BML::ml('.create.text', {'link'=>'/community/create.bml'}) . " p?>";

 return $ret;

_code?>

<=body
page?>

