<?page
title<=
<?_code
 if ($GET{'mode'} eq 'create') {
    return $ML{'.title.create'};
 }
 else {
    return $ML{'.title.modify'};
 }
_code?>
<=title
head<=
<style type='text/css'>
    div.opts { margin: 10px 0 10px 30px; }
</style>
<=head
body<=
<?_code

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

 # always have links at top
 my $ret = BML::ml('Backlink', {
     'link' => '/community/manage.bml',
     'text' => $ML{'/community/members.bml.manage2'},
 });
 my %errors;

 my $remote = LJ::get_remote();

 unless ($remote) {
    $ret .= "<?h1 $ML{'Error'} h1?><?p $ML{'error.noremote'} p?>";
    return $ret;
 }

 unless ($remote->{'journaltype'} eq 'P') {
    $ret .= "<?h1 $ML{'Error'} h1?><?p $ML{'.error.maintainertype'} p?>";
    return $ret;
 }

 my $mode = "modify";
 $mode = "create" if $GET{'mode'} eq 'create';

 if (LJ::did_post())
 {
     my $sth;
     my $cuser = LJ::canonical_username($POST{'cuser'});
     my $cu = LJ::load_user($cuser);
     
     unless ($cu) {
         $errors{'username'} = $ML{'.error.notfound'};
     }

     if ($cu && $cu->{'userid'} == $remote->{'userid'}) {
         $errors{'username'} = $ML{'.error.samenames'};
     }

     # if we're changing rather than creating, check that we can
     if ($mode eq 'modify' && !LJ::can_manage_other($remote, $cu)) {
        $errors{'username'} = BML::ml('.error.noaccess', {'comm'=>$cuser});
     }

     # if we're creating, community password must match
     if ($mode eq 'create' && $cu && !LJ::auth_okay($cu, $POST{'cpassword'})) {
         $errors{'password'} = $ML{'.error.badpassword'};
     }

     # disallow changing the journal type if the journal has entries
     if ($mode eq 'create' && !%errors && !LJ::check_priv($remote, "changejournaltype", "")) {
         my $count;
         my $userid=$cu->{'userid'}+0;
         
         my $dbcr = LJ::get_cluster_reader($cu);
         $count = $dbcr->selectrow_array("SELECT COUNT(*) FROM log2 WHERE journalid=$userid AND posterid=journalid");
         
         $errors{'username'} = $ML{'.error.hasentries'} if $count;
     }

     # if it's already a community, don't let them turn it into a community
     if ($mode eq 'create' && !%errors && $cu->{journaltype} eq 'C') {
         $errors{'username'} = $ML{'.error.alreadycomm'};
     }
  
     # if we found errors, we'll redisplay the form below.  otherwise,
     # proceed.
     unless (%errors) {
        my $dbh = LJ::get_db_writer();
        my $cid = $cu->{'userid'};
        my $qmembership = $POST{membership};
        $qmembership = 'closed' unless $qmembership =~ m/(?:open|moderated|closed)/;
        $qmembership = $dbh->quote($qmembership);
        my $qpostlevel = $dbh->quote($POST{'postlevel'} eq "members" ? "members" : "select");

        LJ::update_user($cu, { journaltype => 'C' });
        if ($mode eq 'create') {
           $dbh->do("REPLACE INTO community (userid, membership, postlevel) VALUES ($cid, $qmembership, $qpostlevel)");
           LJ::set_rel($cu, $remote, 'A');

           # delete existing friends
           my $friends = LJ::get_friends($cid, undef, undef, 'force') || {};
           LJ::remove_friend($cid, [ keys %$friends ]);

        } else {
           $dbh->do("UPDATE community SET membership=$qmembership, postlevel=$qpostlevel WHERE userid=$cid");
        }

        my $nonmembers = $POST{'nonmember_posting'} + 0;
        my $moderated = $POST{'moderated'} + 0;

        LJ::set_userprop($cu, 'nonmember_posting', $nonmembers);
        LJ::set_userprop($cu, 'moderated', $moderated);

        if ($moderated && ! LJ::load_rel_user($cu->{'userid'}, 'M')->[0]) {
          LJ::set_rel($cu->{'userid'}, $remote->{'userid'}, 'M');
        }

        $ret .= "<?h1 $ML{'.success'} h1?>";
        if ($mode eq 'create') {
           $ret .= "<?p $ML{'.label.commcreated'} p?>";
        } else {
           $ret .= "<?p $ML{'.label.commchanged'} p?>";
        }

        $ret .= "<?p $ML{'.label.rellinks'} <ul><li><a href='$LJ::SITEROOT/community/$cu->{'user'}/'>$ML{'.label.commsite'}</a></li>";
        $ret .= "<li><a href='/userinfo.bml?user=$cu->{'user'}'>$ML{'.label.comminfo'}</a></li><li>"
            . BML::ml('.label.managepage', { 'aopts' => 'href="/community/manage.bml"' }) . "</li></ul> p?>";

        return $ret;
     }
 }

 # we're either creating a new community or modifying settings of an existing one
 # based on whether $mode is 'create' or 'modify'. Most of the page is the same in
 # either case, and additionally we must preload existing settings when modifying.

 my ($cname, $c);

 $cname = $POST{'cuser'}; # if we're falling through with errors when creating
 
 my %info = (
    'membership'=>$POST{'membership'} || 'open',
    'postlevel'=>$POST{'postlevel'} || 'members',
    'nonmember_posting'=>$POST{'nonmember_posting'} || 0,
    'moderated'=>$POST{'moderated'} || 0,
 );

 if ($mode eq 'modify') {
    $cname = LJ::canonical_username($GET{'comm'});
    $c = LJ::load_user($cname);
    unless ($c) {
       # if no community was specified, redirect to manage.bml
       return BML::redirect("$LJ::SITEROOT/community/manage.bml");
    } 
    unless ($c->{'journaltype'} eq 'C') {
       $ret = "<?h1 $ML{'Error'} h1?><?p $ML{'.error.notcomm'} p?>";
       return $ret;
    } 
    my $dbr = LJ::get_db_reader();
    ($info{'membership'},$info{'postlevel'}) = 
        $dbr->selectrow_array("SELECT membership, postlevel FROM community WHERE userid=$c->{'userid'}");
    LJ::load_user_props($c, "nonmember_posting", "moderated");
    $info{'nonmember_posting'} = $c->{'nonmember_posting'} ? 1 : 0;
    $info{'moderated'} = $c->{'moderated'} ? 1 : 0;
 }

 $ret .= "<form method='post' action='settings.bml?mode=$mode'>";

 if ($mode eq 'modify') {
    $ret .= "<?h1 $ML{'.label.changeheader'} h1?><?p $ML{'.label.changetext'} p?>";
 } else {
    $ret .= "<?h1 $ML{'.label.createheader'} h1?><?p $ML{'.label.createtext'} p?>";
 }

 if ($mode eq 'create') {
    LJ::set_active_crumb('createcommunity');
    $ret .= "<?h2 $ML{'.label.commheader'} h2?>" .
       ($mode eq 'modify' ? "<?p $ML{'.label.commchanged'} p?>" : "<?p $ML{'.label.commcreate'} p?>");
    $ret .= "<?standout <table width='350' cellpadding='7'><tr valign='top'><td><b>$ML{'.label.maintainer'}</b></td>";
    $ret .= "<td><?ljuser $remote->{'user'} ljuser?><br />$ML{'.label.maintainer.login'}</td></tr>";
    $ret .= "<tr valign='top'><td><b>$ML{'.label.community'}</b></td>";
    $ret .= "<td>$ML{'.label.username'}<br /><input name='cuser' maxlength='15' value='$cname' /><br />";
    $ret .= "<?inerr $errors{'username'} inerr?><br />";
    $ret .= "$ML{'.label.password'}<br /><input name='cpassword' type='password' /><br />";
    $ret .= "<?inerr $errors{'password'} inerr?></td></tr></table> standout?>";
 } else {
    LJ::set_active_crumb('commsettings');
    $ret .= LJ::html_hidden('cuser', $cname);
    $ret .= "<?p " . BML::ml('.name',{'name'=>"<?ljcomm $cname ljcomm?>"});
    $ret .= " " . BML::ml('.members',{'link'=>"/community/members.bml?comm=$cname"}) . " p?>";
 }

 $ret .= "<?h1 $ML{'.label.commopts'} h1?><?p $ML{'.label.howoperates'} p?>";
 $ret .= "<?h2 $ML{'.label.membership'} h2?><?p $ML{'.label.whocanjoin'} p?><div class='opts'>";

 # membership levels
 $ret .= "<p>";
 $ret .= LJ::html_check({ type => 'radio', name => 'membership', id => 'memopen', 
                          value => 'open', selected => ($info{membership} eq 'open' ? 1 : 0)});
 $ret .= "<label for='memopen' $ML{'.label.openmemb'}</label></p><p>";
 $ret .= LJ::html_check({ type => 'radio', name => 'membership', id => 'memmoderated', 
                          value => 'moderated', selected => ($info{membership} eq 'moderated' ? 1 : 0)});
 $ret .= "<label for='memmoderated' $ML{'.label.moderatedmemb'}</label></p><p>";
 $ret .= LJ::html_check({ type => 'radio', name => 'membership', id => 'memclosed', 
                          value => 'closed', selected => ($info{membership} eq 'closed' ? 1 : 0)});
 $ret .= "<label for='memclosed' $ML{'.label.closedmemb2'}</label></p>";
 $ret .= "</div>";

 my ($optopen,$optclosed);
 if ($info{'postlevel'} eq 'members') {
    ($optopen,$optclosed)=(" checked='checked'","");
 } else {
    ($optopen,$optclosed)=("", " checked='checked'");
 }

 $ret .= "<?h2 $ML{'.label.postaccess'} h2?><?p $ML{'.label.whocanpost'} p?><div class='opts'>";
 $ret .= "<input type='radio' id='postopen' name='postlevel' value='members'$optopen /><label for='postopen'> $ML{'.label.anybodycan'}</label>";
 $ret .= "<p><input type='radio' id='postclosed' name='postlevel' value='select'$optclosed /><label for='postclosed'> $ML{'.label.selcan'}</label>";
 $ret .= "</div>";

 if ($info{'nonmember_posting'}) {
    ($optopen,$optclosed)=(" checked='checked'","");
 } else {
    ($optopen,$optclosed)=("", " checked='checked'");
 }

 $ret .= "<?h2 $ML{'.label.nmheader'} h2?><?p $ML{'.label.nmtext'} p?><div class='opts'>";
 $ret .= "<input type='radio' id='nonopen' name='nonmember_posting' value='0'$optclosed /><label for='nonopen'> $ML{'.label.nmcant'}</label>";
 $ret .= "<p><input type='radio' id='nonclosed' name='nonmember_posting' value='1'$optopen /><label for='nonclosed'> $ML{'.label.nmcan'}</label>";
 $ret .= "</div>";

 if ($info{'moderated'}) {
    ($optopen,$optclosed)=(" checked='checked'","");
 } else {
    ($optopen,$optclosed)=("", " checked='checked'");
 }

 $ret .= "<?h2 $ML{'.label.modheader'} h2?><?p $ML{'.label.modtext'} p?><div class='opts'>";
 $ret .= "<input type='radio' id='radunmod' name='moderated' value='0'$optclosed /><label for='radunmod'> $ML{'.label.modisnt'}</label>";
 $ret .= "<p><input type='radio' id='radmod' name='moderated' value='1'$optopen /><label for='radmod'> $ML{'.label.modis'}</label>";
 $ret .= "</div>\n";

 $ret .= "<center><input type='submit' value='" .
    ($mode eq 'create' ? "$ML{'.button.createcommunity'}" : "$ML{'.button.changecommunity'}") .
    "' /></center></form>";

 return $ret;

_code?>

<=body
page?>
