<?php
if ( !eregi("admin.php", $PHP_SELF) ) { 
	die ("Access Denied"); 
}
if ( $xoopsUser->isAdmin($xoopsModule->mid()) ) {

function SmilesAdmin() {
	global $xoopsDB, $xoopsModule;
	$url_smiles = XOOPS_URL."/images/smilies";
	$myts =& MyTextSanitizer::getInstance();
	xoops_cp_header();
	//$xoopsModule->printAdminMenu();
	//echo "<br />";
	OpenTable();
	echo "<h4 style='text-align:left;'>"._AM_CURRENTSMILE."</h4>";

	if ($getsmiles = $xoopsDB->query("SELECT * FROM ".$xoopsDB->prefix("smiles"))) {
		if (($numsmiles = $xoopsDB->getRowsNum($getsmiles)) == "0") {
			
		} else {
			echo "<table border='0' cellpadding='0' cellspacing='0' align='center' valign='top' width='100%'><tr><td class='bg2'>\n";
    			echo "<table width='100%' border='0' cellpadding='4' cellspacing='1'>\n";
			echo "<tr class='bg3'><td><b>" ._AM_CODE."</b></td>";
			echo "<td align='center'><b>" ._AM_SMILIE."</b></td>";
			echo "<td align='right'><b>"._AM_ACTION."</b></td>";
			echo "</tr>\n";
			while ($smiles = $xoopsDB->fetchArray($getsmiles)) {
				$smiles['code'] = $myts->makeTboxData4Show($smiles['code']);
				$smiles['smile_url'] = $myts->makeTboxData4Show($smiles['smile_url']);
				echo "<tr class='bg1'>";
				echo "<td>".$smiles['code']."</td>";
				echo "<td align='center'><img src='".$url_smiles."/".$smiles['smile_url']."'></td>";
				echo "<td align='right'><a href='admin.php?fct=smilies&op=SmilesEdit&id=".$smiles['id']."'>" ._AM_EDIT."</a>&nbsp;|&nbsp;";
				echo "<a href='admin.php?fct=smilies&op=SmilesDel&id=".$smiles['id']."'>" ._AM_DEL."</a></td>";
				echo "</tr>\n";

			}
			echo "</table></td></tr></table>\n";
		}
	} else {
		echo _AM_CNRFTSD;
	}
	echo "<br /><br /><h4 style='text-align:left;'>"._AM_ADDSMILE."</h4>\n";
	echo "<form action='admin.php' method='post'>\n";
	echo "<table border='0' cellpadding='0' cellspacing='0' align='center' valign='top' width='100%'><tr><td class='bg2'>\n";
    	echo "<table width='100%' border='0' cellpadding='4' cellspacing='1'>\n";
	echo "<tr><td class='bg3'><b>" ._AM_SMILECODE."</b></td><td class='bg1'><input type='text' name='code' /></td></tr>\n";
	echo "<tr><td class='bg3'><b>" ._AM_SMILEURL."</b></td><td class='bg1'><input type='text' name='smile_url' /></td></tr>\n";
	echo "<tr><td class='bg3'><b>" ._AM_SMILEEMOTION."</b></td><td class='bg1'><input type='text' name='emotion' /></td></tr>\n";
	echo "<tr><td class='bg3'>&nbsp;</td><td class='bg1'><input type='hidden' name='fct' value='smilies' />";
	echo "<input type='hidden' name='op' value='SmilesAdd' />";
	echo "<input type='submit' value='" ._AM_ADD. "' /></td></tr>\n";
	echo "</table></td></tr></table></form>\n";
	CloseTable();

	xoops_cp_footer();
}

function SmilesEdit($id) {
	global $xoopsDB, $xoopsModule;
	$myts =& MyTextSanitizer::getInstance();
	xoops_cp_header();
	OpenTable();
	echo "<h4 style='text-align:left;'>"._AM_EDITSMILE."</h4>";
	if ($getsmiles = $xoopsDB->query("SELECT * FROM ".$xoopsDB->prefix("smiles")." WHERE id = $id")) {
		$numsmiles = $xoopsDB->getRowsNum($getsmiles);
		if ( $numsmiles == 0 ) {
		
		} else {

			if ($smiles = $xoopsDB->fetchArray($getsmiles)) {
				$smiles['code'] = $myts->makeTboxData4Edit($smiles['code']);
				$smiles['smile_url'] = $myts->makeTboxData4Edit($smiles['smile_url']);
				$smiles['emotion'] = $myts->makeTboxData4Edit($smiles['emotion']);
				echo "<form action='admin.php' method='post'>";
				echo "<table border='0' cellpadding='0' cellspacing='0' valign='top' width='100%'><tr><td class='bg2'>\n";
    				echo "<table width='100%' border='0' cellpadding='4' cellspacing='1'>\n";
				echo "<tr><td class='bg3'><b>" ._AM_SMILECODE."</b></td><td class='bg1'><input type='text' name='code' value='".$smiles['code']."' /></td></tr>\n";
				echo "<tr><td class='bg3'><b>" ._AM_SMILEURL."</b></td><td class='bg1'><input type='text' name='smile_url' value='".$smiles['smile_url']."' /></td></tr>\n";
				echo "<tr><td class='bg3'><b>" ._AM_SMILEEMOTION."</b></td><td class='bg1'><input type='text' name='emotion' value='".$smiles['emotion']."' /></td></tr>";

				echo "<tr><td class='bg3'><input type='hidden' name='id' value='".$smiles['id']."' />";
				echo "<input type='hidden' name='op' value='SmilesSave' />";
				echo "<input type='hidden' name='fct' value='smilies' />";
				echo "</td><td class='bg1'><input type='submit' value='" ._AM_SAVE. "' /></td></tr>";
				echo "</table></td></tr></table></form>";
			}
		}
	} else {
		echo _AM_CNRFTSD;
	}
	CloseTable();
	xoops_cp_footer();
}

function SmilesAdd($code,$smile_url,$emotion) {
	global $xoopsDB;
	$myts =& MyTextSanitizer::getInstance();
	$code = $myts->makeTboxData4Save($code);
	$smile_url = $myts->makeTboxData4Save($smile_url);
	$emotion = $myts->makeTboxData4Save($emotion);
	$newid = $xoopsDB->genId($xoopsDB->prefix("smilies")."_id_seq");
    	$xoopsDB->query("INSERT INTO ".$xoopsDB->prefix("smiles")." (id, code, smile_url, emotion) VALUES ($newid, '$code','$smile_url','$emotion')");

    	redirect_header("admin.php?fct=smilies&op=SmilesAdmin",2,_AM_DBUPDATED);
	exit();
}

function SmilesSave($id,$code,$smile_url,$emotion) {
	global $xoopsDB;
	$myts =& MyTextSanitizer::getInstance();
	$code = $myts->makeTboxData4Save($code);
	$smile_url = $myts->makeTboxData4Save($smile_url);
	$emotion = $myts->makeTboxData4Save($emotion);

    	$xoopsDB->query("UPDATE ".$xoopsDB->prefix("smiles")." SET code = '$code', smile_url = '$smile_url', emotion = '$emotion' WHERE id = $id");
	redirect_header("admin.php?fct=smilies&op=SmilesAdmin",2,_AM_DBUPDATED);
	exit();
}

function SmilesDel($id, $ok=0) {
	global $xoopsDB, $xoopsModule;
    	if($ok==1) {
		$xoopsDB->query("DELETE FROM ".$xoopsDB->prefix("smiles")." where id=$id");
		redirect_header("admin.php?fct=smilies&op=SmilesAdmin",2,_AM_DBUPDATED);
		exit();
    	} else {
		xoops_cp_header();
		OpenTable();
		echo "<div><br />";
		echo "<font color='#ff0000'>";
		echo "<b>"._AM_WAYSYWTDTS."</b><br /><br /></font>";
		echo "<table><tr><td>\n";
		echo myTextForm("admin.php?fct=smilies&op=SmilesDel&id=$id&ok=1", _AM_YES);
		echo "</td><td>\n";
		echo myTextForm("admin.php?fct=smilies&op=SmileAdmin", _AM_NO);
		echo "</td></tr></table>\n";
		echo "<br /><br /></div>";
		CloseTable();
		xoops_cp_footer();
	}
}

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