<?php
// $Id: smilies.php,v 1.1 2004/07/11 10:28:56 Farsus Exp $
// -------------------------------------------------------------------------//
//			       RUNCMS                                                   //
//                                                                          //
//	 reliable - Unique - Nocost &  Simplicity & ease off use                //
//                       < http://www.runcms.org >                          //
// -------------------------------------------------------------------------//
// Original Author: Kazumi Ono
// Author Website : http://www.mywebaddons.com/ , http://www.myweb.ne.jp
// License Type   : GPL: See /manual/LICENSES/GPL.txt
// ------------------------------------------------------------------------- //

if ( $xoopsUser->isAdmin($xoopsModule->mid()) ) {

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function SmilesAdmin() {
global $db, $myts, $xoopsModule;

$url_smiles = XOOPS_URL."/images/smilies";

xoops_cp_header();
OpenTable();
echo "<h4 style='text-align:left;'>"._AM_CURRENTSMILE."</h4>";

if ($getsmiles = $db->query("SELECT * FROM ".$db->prefix("smiles"))) {
	if ($db->num_rows($getsmiles) > 0) {
		?>
		<table border="0" cellpadding="0" cellspacing="0" align="center" valign="top" width="100%"><tr>
		<td class="bg2">
		<table width="100%" border="0" cellpadding="4" cellspacing="1"><tr class="bg3">
		<td><b><?php echo _AM_CODE;?></b></td>
		<td align="center"><b><?php echo _AM_SMILIE;?></b></td>
		<td align="center"><b><?php echo _AM_EMOTION;?></b></td>
		<td align="right"><b><?php echo _ACTION;?></b></td>
		</tr>
		<?php
		while ($smiles = $db->fetch_array($getsmiles)) {
			?>
			<tr class="bg1">
			<td><?php echo $myts->makeTboxData4Show($smiles['code']);?></td>
			<td align="center"><img src="<?php echo formatURL($url_smiles, $smiles['smile_url']);?>"></td>
			<td align="center"><?php echo $myts->makeTboxData4Show($smiles['emotion']);?></td>
			<td align="right"><a href="admin.php?fct=smilies&op=SmilesEdit&id=<?php echo $smiles['id'];?>"><?php echo _EDIT;?></a> | <a href="admin.php?fct=smilies&op=SmilesDel&id=<?php echo $smiles['id'];?>"><?php echo _DELETE;?></a></td>
			</tr>
			<?php
		}
		echo "</table></td></tr></table>";
	}
	} else {
		echo _AM_CNRFTSD;
	}

?>
<br /><br />
<h4 style="text-align:left;"><?php echo _AM_ADDSMILE;?></h4>
<form action="admin.php" method="post" enctype="multipart/form-data">
<table border="0" cellpadding="0" cellspacing="0" align="center" valign="top" width="100%"><tr>
<td class="bg2">
<table width="100%" border="0" cellpadding="4" cellspacing="1"><tr>
<td class="bg3"><b><?php echo _AM_SMILECODE;?></b></td>
<td class="bg1"><input type="text" class="text" name="code" /></td>
</tr><tr>
<td class="bg3" valign="top"><b><?php echo _AM_SMILEURL;?></b></td>
<td class="bg1">
<input type="text" class="text" name="smile_url" /> :: <input type="file" class="file" name="image" />
<br /><?php printf(_AM_VALIDUNDER, "images/smilies/");?>
</td>
</tr><tr>
<td class="bg3"><b><?php echo _AM_SMILEEMOTION;?></b></td>
<td class="bg1"><input type="text" class="text" name="emotion" /></td>
</tr><tr>
<td class="bg3">&nbsp;</td>
<td class="bg1">
<input type="hidden" name="fct" value="smilies" />
<input type="hidden" name="op" value="SmilesAdd" />
<input type="submit" class="button" value="<?php echo _ADD;?>" />
</td></tr></table>
</td></tr></table>
</form>
<?php

CloseTable();
xoops_cp_footer();
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function SmilesEdit($id) {
global $db, $myts, $xoopsModule;

xoops_cp_header();
OpenTable();
echo "<h4 style='text-align:left;'>"._AM_EDITSMILE."</h4>";

if ($getsmiles = $db->query("SELECT * FROM ".$db->prefix("smiles")." WHERE id = $id")) {
	if ($smiles = $db->fetch_array($getsmiles)) {
		?>
		<form action="admin.php" method="post" enctype="multipart/form-data">
		<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>
		<td class="bg3"><b><?php echo _AM_SMILECODE;?></b></td>
		<td class="bg1"><input type="text" class="text" name="code" value="<?php echo $myts->makeTboxData4Edit($smiles['code']);?>" /></td>
		</tr><tr>
		<td class="bg3"><b><?php echo _AM_SMILEURL;?></b></td>
		<td class="bg1">
		<input type="text" class="text" name="smile_url" value="<?php echo $myts->makeTboxData4Edit($smiles['smile_url']);?>" /> :: <input type="file" class="file" name="image" />
		<br /><?php printf(_AM_VALIDUNDER, "images/smilies/");?>
		</td>
		</tr><tr>
		<td class="bg3"><b><?php echo _AM_SMILEEMOTION;?></b></td>
		<td class="bg1"><input type="text" class="text" name="emotion" value="<?php echo $myts->makeTboxData4Edit($smiles['emotion']);?>" /></td>
		</tr><tr>
		<td class="bg3">
		<input type="hidden" name="id" value="<?php echo $smiles['id'];?>" />
		<input type="hidden" name="op" value="SmilesSave" />
		<input type="hidden" name="fct" value="smilies" />
		</td>
		<td class="bg1"><input type="submit" class="button" value="<?php echo _SAVE;?>" /></td>
		</tr></table>
		</td></tr></table>
		</form>
		<?php
	}
	} else {
		echo _AM_CNRFTSD;
	}

CloseTable();
xoops_cp_footer();
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function SmilesAdd($code, $smile_url, $emotion) {
global $db, $myts, $HTTP_POST_FILES;

$code      = $myts->makeTboxData4Save($code);
$smile_url = $myts->makeTboxData4Save($smile_url);
$emotion   = $myts->makeTboxData4Save($emotion);
$newid     = $db->genId($db->prefix("smilies")."_id_seq");

if ( !empty($HTTP_POST_FILES['image']['name']) ) {
	include_once(XOOPS_ROOT_PATH."/class/fileupload.php");
	$upload = new fileupload();
	$upload->set_upload_dir(XOOPS_ROOT_PATH."/images/smilies/", 'image');
	$upload->set_accepted("gif|jpg|png", 'image');
	$upload->set_overwrite(1, 'image');
	$result = $upload->upload();
	if ($result['image']['filename']) {
		$smile_url = $result['image']['filename'];
	}
}

if ( !empty($code) && !empty($smile_url) ) {
	$db->query("INSERT INTO ".$db->prefix("smiles")." SET id=$newid, code='$code', smile_url='$smile_url', emotion='$emotion'");
	redirect_header("admin.php?fct=smilies&op=SmilesAdmin", 2, _UPDATED);
	} else {
		redirect_header("admin.php?fct=smilies&op=SmilesAdmin", 2, _NOTUPDATED);
	}

exit();
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function SmilesSave($id, $code, $smile_url, $emotion) {
global $db, $myts, $HTTP_POST_FILES;

$code      = $myts->makeTboxData4Save($code);
$smile_url = $myts->makeTboxData4Save($smile_url);
$emotion   = $myts->makeTboxData4Save($emotion);

if ( !empty($HTTP_POST_FILES['image']['name']) ) {
	include_once(XOOPS_ROOT_PATH."/class/fileupload.php");
	$upload = new fileupload();
	$upload->set_upload_dir(XOOPS_ROOT_PATH."/images/smilies/", 'image');
	$upload->set_accepted("gif|jpg|png", 'image');
	$upload->set_overwrite(1, 'image');
	$result = $upload->upload();
	if ($result['image']['filename']) {
		$smile_url = $result['image']['filename'];
	}
}

if ( !empty($code) && !empty($smile_url) ) {
	$db->query("UPDATE ".$db->prefix("smiles")." SET code='$code', smile_url='$smile_url', emotion = '$emotion' WHERE id = $id");
	redirect_header("admin.php?fct=smilies&op=SmilesAdmin", 2, _UPDATED);
	} else {
		redirect_header("admin.php?fct=smilies&op=SmilesAdmin", 2, _NOTUPDATED);
	}

exit();
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function SmilesDel($id, $ok=0) {
global $db, $xoopsModule;

if ($ok==1) {
	if ($result = $db->query("SELECT smile_url FROM ".$db->prefix("smiles")." where id=$id")) {
		list($smile) = $db->fetch_row($result);
		@unlink(XOOPS_ROOT_PATH."/images/smilies/".basename($smile));
	}
	$db->query("DELETE FROM ".$db->prefix("smiles")." where id=$id");
	redirect_header("admin.php?fct=smilies&op=SmilesAdmin", 2, _UPDATED);
	exit();
	} else {
		xoops_cp_header();
		OpenTable();
		echo "
		<br /><font color='#ff0000'><b>"._AM_WAYSYWTDTS."</b>
		<br /><br /></font><table><tr><td>";
		echo myTextForm("admin.php?fct=smilies&op=SmilesDel&id=$id&ok=1", _YES);
		echo "</td><td>";
		echo myTextForm("admin.php?fct=smilies&op=SmileAdmin", _NO);
		echo "</td></tr></table><br /><br />";
		CloseTable();
		xoops_cp_footer();
	}
}

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