<?php

if ( !eregi("admin.php", $PHP_SELF) ) { 
	die ("Access Denied"); 
}
// check if the user is authorised
if ( $xoopsUser->isAdmin($xoopsModule->mid()) ) {
	include_once(XOOPS_ROOT_PATH."/class/xoopsblock.php");

	function list_blocks(){
		global $xoopsUser;
		OpenTable();
		echo "
		<h4 style='text-align:left;'>"._AM_BADMIN."</h4>	
		<form action='admin.php' name='blockadmin' id='blockadmin' method='post' style='position:relative;z-index:3'><b>"._AM_SIDEBADMIN."</b><br />
		<table border='0' cellpadding='0' cellspacing='0' valign='top' width='100%'><tr><td class='bg2'>
		<table width='100%' border='0' cellpadding='4' cellspacing='1'>
		<tr valign='middle' class='bg3'><td width='20%'><b>"._AM_BLKDESC."</b></td><td width='20%'><b>"._AM_TITLE."</b></td><td><b>"._AM_SIDE."</b></td><td align='center'><b>"._AM_WEIGHT."</b></td><td align='center'><b>"._AM_VISIBLE."</b></td><td align='right'><b>"._AM_ACTION."</b></td></tr>
		";
		$arr = XoopsBlock::getAllBlocks("object", XOOPS_SIDEBLOCK_BOTH);
		foreach ( $arr as $ele ) {
			show_block($ele);
		}
		echo "
		</table></td></tr></table>
		<br />
		<b>"._AM_CENTERBADMIN."</b><br />
		<table border='0' cellpadding='0' cellspacing='0' valign='top' width='100%'><tr><td class='bg2'>
		<table width='100%' border='0' cellpadding='4' cellspacing='1'>
		<tr valign='middle' class='bg3'><td width='20%'><b>"._AM_BLKDESC."</b></td><td width='20%'><b>"._AM_TITLE."</b></td><td><b>"._AM_SIDE."</b></td><td align='center'><b>"._AM_WEIGHT."</b></td><td align='center'><b>"._AM_VISIBLE."</b></td><td align='right'><b>"._AM_ACTION."</b></td></tr>
		";
		$arr = XoopsBlock::getAllBlocks("object", XOOPS_CENTERBLOCK_ALL);
		foreach($arr as $ele){
			show_block($ele);
		}
		echo "
		</table></td></tr></table><br /><div>
		<input type='hidden' name='fct' value='blocksadmin' />
		<input type='hidden' name='op' value='order' />
		<input type='submit' name='submit' value='"._SUBMIT."' />
		</div>
		</form>
		";
		echo "<br /><br />";
		echo "
                <h4 style='text-align:left;'>" ._AM_ADDBLOCK."</h4>
                <form action='admin.php?fct=blocksadmin' method='post'>
		<table border='0' cellpadding='0' cellspacing='0' valign='top' width='100%'><tr><td class='bg2'>
                <table width='100%' border='0' cellpadding='4' cellspacing='1'>
		<tr align='left' valign='top'><td class='bg3' width='30%'>
		<b>"._AM_BLKTYPE."</b>
		</td><td class='bg1'>
                <select name='bside'>
                <option value='0'>" ._AM_SBLEFT."</option>
                <option value='1'>" ._AM_SBRIGHT."</option>
		<option value='3'>" ._AM_CBLEFT."</option>
		<option value='5'>" ._AM_CBCENTER."</option>
                <option value='4'>" ._AM_CBRIGHT."</option>
                </select>
                </td></tr><tr align='left' valign='top'><td class='bg3'>
		<b>"._AM_WEIGHT."</b>
                </td><td class='bg1'>
                <input type='text' name='bweight' size='2' maxlength='2' />
                </td></tr><tr align='left' valign='top'><td class='bg3'>
                <b>" ._AM_VISIBLE."</b>
		</td><td class='bg1'>
                <select name='bvisible'>
                <option value='0'>" ._AM_NO."</option>
                <option value='1' selected='selected'>" ._AM_YES."</option>
                </select>
                </td></tr><tr align='left' valign='top'><td class='bg3'>
                <b>" ._AM_TITLE."</b>
		</td><td class='bg1'>
                <input type='text' name='btitle' size='60' maxlength='60' />
                </td></tr><tr align='left' valign='top'><td class='bg3'>
                <b>" ._AM_CONTENT."</b><br /><br /><span style='font-size:x-small;font-weight:bold;'>"._AM_USEFULTAGS."</span><br /><span style='font-size:x-small;font-weight:normal;'>".sprintf(_AM_BLOCKTAG1, "{X_SITEURL}", XOOPS_URL."/")."</span>
		</td><td class='bg1'>
                <textarea name='bcontent' cols='60' rows='10'></textarea>
                </td></tr><tr align='left' valign='top'><td class='bg3'>
                <b>" ._AM_CTYPE."</b>
		</td><td class='bg1'>
                <select name='bctype'>
                <option value='H'>" ._AM_HTML."</option>
                <option value='P'>" ._AM_PHP."</option>
		<option value='S'>" ._AM_AFWSMILE."</option>
		<option value='T' selected='selected'>" ._AM_AFNOSMILE."</option>
                </select>
                </td></tr>
                <tr align='left' valign='top'><td class='bg3'>&nbsp;</td>
		<td class='bg1'>
                <input type='hidden' name='op' value='save' />
                <input type='submit' value='" ._AM_SUBMIT."' />
		</td></tr></table></td></tr></table>
                </form>
		";
                CloseTable();
	}

	function show_block($ele){
		$sel0 = $sel1 = $ssel0 = $ssel1 = $ssel2 = $ssel3 = $ssel4 = "";
		if ( $ele->getVar("visible") == 1 ) {
			$sel1 = " selected='selected'";
		} else {
			$sel0 = " selected='selected'";
		}
		if ( $ele->getVar("side") == XOOPS_SIDEBLOCK_LEFT){
			$ssel0 = " selected='selected'";
		} elseif ( $ele->getVar("side") == XOOPS_SIDEBLOCK_RIGHT ){
			$ssel1 = " selected='selected'";
		} elseif ( $ele->getVar("side") == XOOPS_CENTERBLOCK_LEFT ){
			$ssel2 = " selected='selected'";
		} elseif ( $ele->getVar("side") == XOOPS_CENTERBLOCK_RIGHT ){
			$ssel4 = " selected='selected'";
		} elseif ( $ele->getVar("side") == XOOPS_CENTERBLOCK_CENTER ){
			$ssel3 = " selected='selected'";
		}
		if ( $ele->getVar("title") == "" ) {
			$title = "&nbsp;";
		} else {
			$title = $ele->getVar("title");
		}
		echo "<tr valign='top' class='bg1'><td>".$ele->getVar("name")."</td><td>".$title."</td><td><select name='side[]'><option value='".XOOPS_SIDEBLOCK_LEFT."'$ssel0>"._AM_SBLEFT."</option><option value='".XOOPS_SIDEBLOCK_RIGHT."'$ssel1>"._AM_SBRIGHT."</option><option value='".XOOPS_CENTERBLOCK_LEFT."'$ssel2>"._AM_CBLEFT."</option><option value='".XOOPS_CENTERBLOCK_CENTER."'$ssel3>"._AM_CBCENTER."</option><option value='".XOOPS_CENTERBLOCK_RIGHT."'$ssel4>"._AM_CBRIGHT."</option></select></td><td align='center'><input type='text' name='weight[]' value='".$ele->getVar("weight")."' size='5' maxlength='5' /></td><td align='center'><select name='visible[]'><option value='0'$sel0>"._NO."</option><option value='1'$sel1>"._YES."</option></select></td><td align='right'><a href='admin.php?fct=blocksadmin&amp;op=edit&amp;bid=".$ele->getVar("bid")."'>"._AM_EDIT."</a>";
		if ( $ele->isCustom() ) {
			echo "&nbsp;<a href='admin.php?fct=blocksadmin&amp;op=delete&amp;bid=".$ele->getVar("bid")."'>"._AM_DELETE."</a>";
		}
		echo "
		<input type='hidden' name='oldside[]' value='".$ele->getVar("side")."' />
		<input type='hidden' name='oldweight[]' value='".$ele->getVar("weight")."' />
		<input type='hidden' name='oldvisible[]' value='".$ele->getVar("visible")."' />
		<input type='hidden' name='bid[]' value='".$ele->getVar("bid")."' />
		</td></tr>
		";
	}

	function save_block($bside, $bweight, $bvisible, $btitle, $bcontent, $bctype){
		$myblock = new XoopsBlock();
		$myblock->setVar("side", $bside);
		$myblock->setVar("weight", $bweight);
		//$myblock->setVar("position", $bposition);
		$myblock->setVar("visible", $bvisible);
		$myblock->setVar("weight", $bweight);
		$myblock->setVar("title", $btitle);
		$myblock->setVar("content", $bcontent);
		$myblock->setVar("c_type", $bctype);
		$myblock->setVar("type", "C");
		switch ($bctype) {
			case "H":
			$name = _AM_CUSTOMHTML;
			break;
			case "P":
			$name = _AM_CUSTOMPHP;
			break;
			case "S":
			$name = _AM_CUSTOMSMILE;
			break;
			default:
			$name = _AM_CUSTOMNOSMILE;
			break;
		}
		$myblock->setVar("name", $name);
		if ( !$myblock->store() ) {
			xoops_cp_header();
			$myblock->getErrors();
			xoops_cp_footer();
			exit();
		}
		redirect_header("admin.php?fct=blocksadmin",1,_AM_DBUPDATED);
		exit();
	}

	function edit_block($bid){
		$myblock = new XoopsBlock($bid);
		OpenTable();
                echo "<h4 style='text-align:left;'>" ._AM_EDITBLOCK."</h4>
                <form action='admin.php?fct=blocksadmin' method='post'>
		<table border='0' cellpadding='0' cellspacing='0' valign='top' width='100%'><tr><td class='bg2'>
                <table width='100%' border='0' cellpadding='4' cellspacing='1'>
		<tr align='left' valign='top'><td class='bg3' width='30%'>
		<b>"._AM_NAME."</b></td><td class='bg1'>".$myblock->getVar("name")."</td></tr>
		<tr align='left' valign='top'><td class='bg3'>
                <b>"._AM_BLKTYPE."</b></td>
		<td class='bg1'>
                <select name='bside'>
                <option value='0'";
		if ( $myblock->getVar("side") == 0 ) echo " selected='selected'";
		echo ">" ._AM_SBLEFT."</option>
                <option value='1'";
		if ( $myblock->getVar("side") == 1 ) echo " selected='selected'";
		echo ">" ._AM_SBRIGHT."</option>
		<option value='3'";
		if ( $myblock->getVar("side") == 3 ) echo " selected='selected'";
		echo ">" ._AM_CBLEFT."</option>
		<option value='5'";
		if ( $myblock->getVar("side") == 5 ) echo " selected='selected'";
		echo ">" ._AM_CBCENTER."</option>
                <option value='4'";
		if ( $myblock->getVar("side") == 4 ) echo " selected='selected'";
		echo ">" ._AM_CBRIGHT."</option>
                </select>
                </td></tr><tr align='left' valign='top'><td class='bg3'>
                <b>"._AM_WEIGHT."</b>
                </td><td class='bg1'>
                <input type='text' name='bweight' size='2' maxlength='2' value='";
		echo $myblock->getVar("weight");
		echo "' />
                </td></tr><tr align='left' valign='top'><td class='bg3'>
                <b>"._AM_VISIBLE."</b></td><td class='bg1'>
                <select name='bvisible'>
                <option value='0'";
		if ( $myblock->getVar("visible") == 0 ) echo " selected='selected'";
		echo ">" ._AM_NO."</option>
                <option value='1'";
		if ( $myblock->getVar("visible") == 1 ) echo " selected='selected'";
		echo ">" ._AM_YES."</option>";
                echo "</select>
                </td></tr><tr align='left' valign='top'><td class='bg3'>
                <b>" ._AM_TITLE."</b></td><td class='bg1'>
                <input type='text' name='btitle' size='60' maxlength='60' value='";
		echo $myblock->getVar("title", "E");
		echo "' />
                </td></tr><tr align='left' valign='top'><td class='bg3'>
                <b>"._AM_CONTENT."</b><br /><br /><span style='font-size:x-small;font-weight:bold;'>"._AM_USEFULTAGS."</span><br /><span style='font-size:x-small;font-weight:normal;'>".sprintf(_AM_BLOCKTAG1, "{X_SITEURL}", XOOPS_URL."/")."</span></td><td class='bg1'>
                <textarea name='bcontent' cols='60' rows='10'>";
		echo $myblock->getContent("E");
		echo "</textarea>
                </td></tr>";
		if ( !$myblock->isCustom() ) {
			echo "<tr align='left' valign='top'><td class='bg3'>
                	<b>" ._AM_POSCONTT."</b></td><td class='bg1'>
                	<select name='bposition'>
                	<option value='0'";
			if ( $myblock->getVar("position") == 0) echo " selected='selected'";
			echo ">" ._AM_ABOVEORG."</option>
                	<option value='1'";
			if ( $myblock->getVar("position") == 1) echo " selected='selected'";
			echo ">" ._AM_AFTERORG."</option>
                	</select>
                	</td></tr>";
		}
		echo "<tr align='left' valign='top'><td class='bg3'>
                <b>" ._AM_CTYPE."</b></td><td class='bg1'>
                <select name='bctype'>
                <option value='H'";
		if ( $myblock->getVar("c_type") == "H") echo " selected='selected'";
		echo ">" ._AM_HTML."</option>
                <option value='P'";
		if ( $myblock->getVar("c_type") == "P") echo " selected='selected'";
		echo ">" ._AM_PHP."</option>
		<option value='S'";
		if ( $myblock->getVar("c_type") == "S") echo " selected='selected'";
		echo ">" ._AM_AFWSMILE."</option>
		<option value='T'";
		if ( $myblock->getVar("c_type") == "T") echo " selected='selected'";
		echo ">" ._AM_AFNOSMILE."</option>
                </select>
                </td></tr>";

		$edit_form = $myblock->getOptions();
		if ( $edit_form != false ) {
			echo "<tr align='left' valign='top'><td class='bg3'><b>"._AM_OPTIONS."</b></td><td class='bg1'>";
			echo $edit_form;
			echo "</td></tr>";
		}
                echo "<tr align='left' valign='top'><td class='bg3'>&nbsp;</td>
		<td class='bg1'>
                <input type='hidden' name='op' value='update' />
                <input type='hidden' name='bid' value='".$myblock->getVar("bid")."' />
                <input type='submit' value='" ._AM_SAVECHANGES."' />&nbsp;
		<input type='button' onclick='location=\"admin.php?fct=blocksadmin\"' value='" ._CANCEL."' />
		</td></tr></table></td></tr></table>
                </form>
		";
                CloseTable();
	}

	function update_block($bid, $bside, $bweight, $bposition, $bvisible, $btitle, $bcontent, $bctype, $options=""){
		$myblock = new XoopsBlock($bid);
		$myblock->setVar("side", $bside);
		$myblock->setVar("weight", $bweight);
		$myblock->setVar("position", $bposition);
		$myblock->setVar("visible", $bvisible);
		$myblock->setVar("weight", $bweight);
		$myblock->setVar("title", $btitle);
		$myblock->setVar("content", $bcontent);
		if ( isset($options) && (count($options) > 0) ) {
			$options = implode("|", $options);
			$myblock->setVar("options", $options);
		}
		$myblock->setVar("c_type", $bctype);
		if ( $myblock->getVar("type") == "C" ) {
			switch ($bctype) {
				case "H":
				$name = _AM_CUSTOMHTML;
				break;
				case "P":
				$name = _AM_CUSTOMPHP;
				break;
				case "S":
				$name = _AM_CUSTOMSMILE;
				break;
				default:
				$name = _AM_CUSTOMNOSMILE;
				break;
			}
			$myblock->setVar("name", $name);
		}
		$myblock->store();
		redirect_header("admin.php?fct=blocksadmin",1,_AM_DBUPDATED);
		exit();
	}

	function delete_block($bid){
		$myblock = new XoopsBlock($bid);
		if ( $myblock->getVar("type") == "S" ) {
			$message = _AM_SYSTEMCANT;
			redirect_header("admin.php?fct=blocksadmin",4,$message);
			exit();
		} elseif ($myblock->getVar("type") == "M") {
			$message = _AM_MODULECANT;
			redirect_header("admin.php?fct=blocksadmin",4,$message);
			exit();
		} else {
			echo "<h4 style='text-align:left;'>".sprintf(_AM_RUSUREDEL,$myblock->getVar("title"))."</h4>";
			echo "<table><tr><td>\n";
			echo myTextForm("admin.php?fct=blocksadmin&op=delete_ok&bid=".$myblock->getVar("bid") , _AM_YES);
			echo "</td><td>\n";
			echo myTextForm("admin.php?fct=blocksadmin" , _AM_NO);
			echo "</td></tr></table>\n";
		}
	}

	function delete_block_ok($bid){
		$myblock = new XoopsBlock($bid);
		$myblock->delete();
		redirect_header("admin.php?fct=blocksadmin",1,_AM_DBUPDATED);
		exit();
	}

	function order_block($bid, $weight, $visible, $side){
		$myblock = new XoopsBlock($bid);
		$myblock->setVar("weight", $weight);
		$myblock->setVar("visible", $visible);
		$myblock->setVar("side", $side);
		$myblock->store();
	}

/*	function preview_cblock(){
	}*/

} else {
	echo "Access Denied";
}
?>