###############################################################################
# ManageCats.pl                                                               #
###############################################################################
# YaBB: Yet another Bulletin Board                                            #
# Open-Source Community Software for Webmasters                               #
# Version:        YaBB 2 RC 1                                                 #
# Released:       December 27, 2004                                           #
# Distributed by: http://www.yabbforum.com                                    #
# =========================================================================== #
# Copyright (c) 2000-2005 YaBB (www.yabbforum.com) - All Rights Reserved.     #
# Software by: The YaBB Development Team                                      #
#              with assistance from the YaBB community.                       #
# Sponsored by: Xnull Internet Media, Inc. - http://www.ximinc.com            #
#               Your source for web hosting, web design, and domains.         #
###############################################################################

$managecatsplver = 'YaBB 2 RC 1 $Revision: 1.15 $';
if($action eq 'detailedversion') { return 1; }

sub DoCats {
	&is_admin_or_gmod;
	$i = 1;
	while( $_ = each(%FORM) ) {
		if( $_ ne 'baction' and $FORM{$_} == 1) {$editcats[$i] = $_;}
		$i++;
	}
	
	if ($FORM{'baction'} eq "edit"){&AddCats(@editcats);} 
	elsif ($FORM{'baction'} eq "delme"){
		foreach $catid (@editcats){
			if ($catid eq ""){next;}
			unless ($mloaded == 1){require "$boardsdir/forum.master";}
			##Check if category has any boards, and if it does remove them.
			$boardlist = $cat{$catid};
			if ($boardlist ne ""){require "$admindir/ManageBoards.pl"; &DeleteBoards($boardlist);}
			
			delete $cat{"$catid"};
			delete $catinfo{"$catid"};
			
			$x = 0;
			foreach $categoryid (@categoryorder) {
				if ($catid eq $categoryid){splice (@categoryorder,$x,1); last;}
			$x++;
			}
			&Write_ForumMaster;
			
			$yymain .= qq~$admin_txt{'830'} <i>$catid</i> $admin_txt{'831'}<br />~;
		}
		$action_area = "managecats";
		&AdminTemplate;
		exit;
	}
}

sub AddCats {
	my @editcats = @_;
	&is_admin_or_gmod;
	if ($INFO{"action"} eq "catscreen"){ $FORM{"amount"} = $#editcats;}

	unless ($mloaded == 1){require "$boardsdir/forum.master";}
	
	$yymain .= qq~
<form action="$adminurl?action=addcat2" method="post">
 <div class="bordercolor" style="padding: 0px; width: 99%; margin-left: 0px; margin-right: auto;">
<table border="0" align="center" cellspacing="1" cellpadding="4" width="100%">
  <tr>
    <td class="titlebg" colspan="5" align="left">
    <img src="$imagesdir/cat.gif" alt="" border="0" />
    <b>$admin_txt{'3'}</b></td>
  </tr><tr>
      <td class="windowbg2" colspan="5" align="left"><br />$admin_txt{'43'}<br /><br /></td>
  </tr>
</table>
</div><br />
 <div class="bordercolor" style="padding: 0px; width: 99%; margin-left: 0px; margin-right: auto;">
<table border="0" align="center" cellspacing="1" cellpadding="4" width="100%">
~;
  
  ### Start Looping through and repeating the board adding wherever needed ####
for ($i = 1; $i != $FORM{'amount'} + 1; $i++){ 
	if ((!$editcats[$i] && $INFO{"action"} eq "catscreen") || ($editcats[$i] eq "" && $INFO{"action"} eq "catscreen")){next;}
	if ($INFO{"action"} eq "catscreen"){	
		$id = $editcats[$i];
			foreach $catid (@categoryorder){$boardlist = $cat{$catid};
			unless ($id eq $catid){next;}
			(@bdlist) = split(/\,/, $boardlist);
			($curcatname, $catperms, $catallowcol) = split(/\|/, $catinfo{"$catid"});
			if($catallowcol eq '1'){$allowChecked = " CHECKED ";}else{$allowChecked = '';}
			if($catallowcol eq ''){$catallowcol = 0; $allowChecked = ' CHECKED ';}
			$cattext = $curcatname; 
		}
	} else {$cattext = "$admin_txt{'44'} $i:";} 	
	require "$admindir/ManageBoards.pl";
	$catperms = &DrawPerms($catperms);
$yymain .= qq~
  <tr>
    <td class="catbg" width="100%" colspan="5" align="left"> <b>$cattext</b></td>
  </tr><tr>
    <td class="windowbg" colspan="2" width="50%">&nbsp;</td>
    <td class="windowbg" colspan="2" width="40%" align="center"><b>$admin_txt{'45'}</b></td>
    <td class="windowbg" width="10%" align="center"><b>$exptxt{'6'}</b></td>
  </tr><tr>
    <td class="windowbg" width="10%" align="right" valign="middle">
    <b>ID:</b></td>
    <td class="windowbg2" width="40%" valign="middle">~;
 if ($INFO{"action"} eq "catscreen"){
    	$yymain .= qq~<br /><input type="hidden" name="theid$i" value="$id" />$id<br /><br />~;
 } else {
 	$yymain .= qq~<br /><input type="text" name="theid$i" value="$id" /><br /><br />~;
 } $yymain .= qq~
    </td>
    <td class="windowbg2" align="center" width="40%" colspan="2" rowspan="2">
    <select multiple="multiple" name="catperms$i" size="5">$catperms</td>
	<td class="windowbg2" align="center" width="10%" rowspan="2"><input type="checkbox" $allowChecked name="allowcol$i" /></td>
  </tr><tr>
    <td class="windowbg" width="10%" align="right" valign="middle"><b>$admin_txt{'68'}:</b>
</td>
    <td class="windowbg2" width="40%"><br />
    <input type="text" name="name$i" value="$curcatname" /><br /><br />
    </td>
</tr>
    ~;
}
  $yymain .= qq~<tr>
      <td class="catbg" width="100%" colspan="5" align="center"> <input type="hidden" name="amount" value=\"$FORM{"amount"}\" /><input type="hidden" name="screenornot" value="$INFO{'action'}" /><input type="submit" value="$admin_txt{'10'}" /></td>
  </tr>
</table>
</div>
</form>~;
$yytitle = "$admin_txt{'3'}";
$action_area = "managecats";
&AdminTemplate;
exit;
}

sub AddCats2{
	&is_admin_or_gmod;
	unless ($mloaded == 1){require "$boardsdir/forum.master";}
	
	for ($i = 1; $i <= $FORM{'amount'}; $i++){
		if ($FORM{"theid$i"} eq ""){next;}
		$id = $FORM{"theid$i"};
		&admin_fatal_error("$admin_txt{'240'} $admin_txt{'44'} $admin_txt{'241'}") if($id !~ /^[0-9A-Za-z#%+-\.@^_]+$/);
		if ($FORM{'screenornot'} ne "catscreen"){
			if ($catinfo{"$id"}){&admin_fatal_error("$admin_txt{'46'}");}
			else {$cat{"$id"} = "";	}
			push (@categoryorder, $id);
		}
		if (!$FORM{"name$i"}){$FORM{"name$i"} = $id;}
		if($FORM{"allowcol$i"} eq 'on'){$FORM{"allowcol$i"} = 1;} else {$FORM{"allowcol$i"} = 0;}
		$catinfo{"$id"} = qq~$FORM{"name$i"}|$FORM{"catperms$i"}|$FORM{"allowcol$i"}~;
		
		&Write_ForumMaster;

		$yymain .= qq~$admin_txt{'830'} <i>$id</i> $admin_txt{'48'}<br />~;
	}
	$action_area = "managecats";
	&AdminTemplate;
	exit;
}

sub ReorderCats {
	&is_admin_or_gmod;
	unless ($mloaded == 1){require "$boardsdir/forum.master";}

	# Prepare the list of current boards to be put in the textarea
	foreach $board (@categoryorder){
		chomp $board;
		$boardslist .= qq~\n$board~;
	}

	$yymain .= qq~
<br /><br />
<form action="$adminurl?action=reordercats2" method="POST">
 <div class="bordercolor" style="padding: 0px; width: 99%; margin-left: 0px; margin-right: auto;">
<table border="0" cellspacing="1" cellpadding="4" align="center">
  <tr>
    <td class="titlebg" colspan="2"><img src="$imagesdir/board.gif" /><b>$admin_txt{'829'}</b><br /><span class="small">$admin_txt{'738'}</span></td>
  </tr><tr>
    <td class="windowbg" valign="top" align="left"><b>$admin_txt{'825'}<b/></td>
    <td class="windowbg" valign="top" align="left"><b>$admin_txt{'826'}<b/></td>
  </tr><tr>
    <td class="windowbg" valign="top" align="center"><br /><textarea name="original" rows="12" cols="20" style="width: 20em; height:20em" readonly>$boardslist</textarea><br /><br />
    <td class="windowbg" valign=top align="center"><br /><textarea name="neworder" cols="20" rows="12" style="width: 20em; height:20em">$boardslist</textarea><br /><br /></td>
  </tr><tr>
    <td class="titlebg" colspan="2" align="center"><input type="submit" value="Submit" /></form></td>
  </tr>
</table>
</div>~;
	
	$yytitle = "$admin_txt{'829'}";
	$action_area = "managecats";
	&AdminTemplate;
	exit;
}

sub ReorderCats2 {
	&is_admin_or_gmod;
	my %verify_hash = ();
	my $tmp_master = "";
	
	@original = split(/[\n\r]/, $FORM{'original'});
	@neworder = split(/[\n\r]/, $FORM{'neworder'});
	@original_test = sort @original;
	@neworder_test = sort @neworder;
	
	foreach (@original_test){
		$verify_hash{"$_"}++;
	}
	foreach (@neworder_test){
		if ( !(exists( $verify_hash{$_} ) ) ){
			# if it does not exist in the original has we have a problem.
			&admin_fatal_error("$admin_txt{'827'}");
		}
	}
	&admin_fatal_error("$admin_txt{'827'}") if("@original_test" ne "@neworder_test");

	unless ($mloaded == 1){require "$boardsdir/forum.master";}
	foreach $item (@neworder) {
		if ($item) {
			$tmp_master .= qq~$item ~;
		}
	}
	$tmp_master .= qq~"~;
	$tmp_master =~ s/ "//;
	@categoryorder = $tmp_master;

	&Write_ForumMaster;

	$yySetLocation = qq~$adminurl?action=managecats~;
	&redirectexit;
}

1;