<?php
//v1.0.4 www.bcoos.net 2006/10 GPL
// www.bcoos.net 2006-09 1.0.2 GPL
// exoops.info Licence: GPL
require_once("./admin_header.php");
$eh     = new ErrorHandler(0);
xoops_cp_header();
function partner_admin() {
	global $db;
	include_once(XOOPS_ROOT_PATH."/class/upload.class.php");
	echo "<h2><b>"._AM_PARTNER_TITLE."</b></h2>";
	echo "<b>"._AM_PARTNER_LIST."</b>";
	OpenTable();
	echo "<table width='100%'>";
	echo"<tr><center><td width='120'>"._AM_PARTNER_ADD_IMAGE."</td><td>"._TITLE."</td><td>"._DESCRIPTION."</td><td>"._AM_PARTNER_STATUS."</td><td>"._AM_PARTNER_FUNCTIONS."</td></center></tr>";
	$sql = "SELECT id, url, image, title, description, status FROM ".$db->prefix("partner")."";
	$result = $db->query($sql);
	while ($row = $db->fetchRow($result)) {
		list ($id, $url, $image, $title, $description, $status) = $row;
		echo "<tr><td><a href='".$url."'><img src='".XOOPS_URL."/modules/partner/cache/images/".$image."' /></a></td><td>".$title."</td><td>".$description."</td><td>";
		if ($status == 1) {echo _AM_PARTNER_ACTIVE; } else { echo _AM_PARTNER_INACTIVE;}
		echo "</td><td><a href='index.php?op=partner_edit&amp;id=".$id."'>"._AM_PARTNER_EDIT."</a> | <a href='index.php?op=partner_delete&amp;id=".$id."'>"._DELETE."</a></td></tr>";
	} 
	echo "</table>";
	CloseTable();
	echo "<br />";
	echo "<b>"._AM_PARTNER_ADD."</b>";
	OpenTable();
	
	echo "
	<form action='index.php' method='post' enctype='multipart/form-data'>
	<table>
	<tr><td width='150'>"._TITLE."</td><td><input type='text' class='text' name='title' size='25' maxlength='50' /></td></tr>
	<tr><td>"._URL."</td><td><input type='text' class='text' name='url' size='50' maxlength='100' /></td></tr>
	<tr><td>"._AM_PARTNER_ADD_IMAGE."</td><td>";
	$upload = new Upload();
	$upload->printFormField("image");
	echo "</td></tr>
	<tr><td>"._DESCRIPTION."</td><td><input type='text' class='text' name='description'' size='50' maxlength='50' /></td></tr>
	<tr><td>"._AM_PARTNER_STATUS."</td><td><input type='checkbox' class='text' name='status' value='1' /></td></tr>
	<tr><td></td><td><input type='hidden' name='op' value='partner_add' /><input type='submit' class='button' value='"._AM_PARTNER_ADD_SUBMIT."' /></td></tr>
	</table>
	</form>
	";
	CloseTable();
	
}
function partner_add() { 
	global $_FILES, $_POST, $db, $myts, $eh;
	include_once(XOOPS_ROOT_PATH."/class/upload.class.php");
	$title = $myts->makeTboxData4Save($_POST["title"]);
	// Check if Title is empty
	if ( empty($title) ) {
		redirect_header("index.php", 3, _AM_PARTNER_ERR_TITLE);
		exit();
	}
	
	$description = $myts->makeTboxData4Save($_POST["description"]);
	// Check if Description is empty
	if ( empty($description) ) {
		redirect_header("index.php", 3, _AM_PARTNER_ERR_DESC);
		exit();
	}
	$url = formatURL($myts->makeTboxData4Save($_POST["url"]));
	if ( empty($url) ) {
		redirect_header("index.php", 3, _AM_PARTNER_ERR_URL);
		exit();
	}
	
	$id = $myts->makeTboxData4Save($_POST["id"]);
	if (!$id) { 
		$sql = "SELECT id FROM `".$db->prefix("partner")."` ORDER BY id DESC LIMIT 1";
		$queryid = $db->query($sql) or $eh->show("0013");
		if ($db->getRowsNum($queryid) == 0 ) {
			$newid = 1;
		} else {
			$newid =  $db->result($queryid,0);
			$newid++;
		}
	}
		
	if ( !empty($_FILES['image']['name']) )  {
		$upload = new Upload();
		$upload->setAllowedMimeTypes(".gif|.jpeg|.jpg|.png");
		$upload->setUploadPath(XOOPS_ROOT_PATH."/modules/partner/cache/images");
		$upload->setDestinationFileName('image');
		$upload->setFileName($newid);
		$result = $upload->doUpload();
		
		if ($result) {
			$logourl = $upload->FileName.".".$upload->getFileExtension();
		} else {
			$errors = $upload->getUploadErrors();
			$errorstring = "";
			$errorstring .= "<strong>::Errors occured::</strong><br />\n";
			while(list($filename,$values) = each($errors)) {
				$errorstring .= "File: " . $filename . "<br />";
				$count = count($values);
				for($i=0; $i<$count; $i++) { $errorstring .= "==>" . $values[$i] . "<br />"; }
			}
			redirect_header("index.php", 3, $errorstring);
			exit();
		}
	}
	$status = $myts->makeTboxData4Save($_POST["status"]);
	if ($id) {
		$id = intval($id);
		$sql   = "
		UPDATE ".$db->prefix("partner")." SET
		url='$url',";
		if ( !empty($_FILES['image']['name']) ) { $sql .= "image='$logourl',"; }
		$sql .= "
		title='$title',
		description='$description',
		status='$status' 
		WHERE id='$id'";
		
		$db->query($sql) or $eh->show("0013");
		redirect_header("index.php", 1, _AM_PARTNER_ADD_MODSUBMIT);
		
	} else {
		$sql   = "
		INSERT INTO ".$db->prefix("partner")." SET
		id='$newid',
		url='$url',
		image='$logourl',
		title='$title',
		description='$description',
		status='$status'";
		
		$db->query($sql) or $eh->show("0013");
		redirect_header("index.php", 1, _AM_PARTNER_ADD_ENDSUBMIT);
	}
	
exit();
} 
function partner_edit($id) { 
	global $db, $myts;
	include_once(XOOPS_ROOT_PATH."/class/upload.class.php");
	
	$sql = "SELECT id, url, image, title, description, status FROM ".$db->prefix("partner")." WHERE id=".$id;
	$result = $db->query($sql);
	list($id, $url, $image, $title, $description, $status) = $db->fetchRow($result);
	$id   = $myts->makeTboxData4Edit($id);
	$url   = $myts->makeTboxData4Edit($url);
	$image   = $myts->makeTboxData4Edit($image);
	$title    = $myts->makeTboxData4Edit($title);
	$description    = $myts->makeTboxData4Edit($description);
	$status    = $myts->makeTboxData4Edit($status);
	
	echo "<b><a href='".XOOPS_URL."/modules/partner/admin/' >"._AM_PARTNER_TITLE."</a> > "._AM_PARTNER_TITLE_EDIT."</b>";
	OpenTable();
	echo "
	<form action='index.php' method='post' enctype='multipart/form-data'>
	<table>
	<tr><td width='150'>"._TITLE."</td><td><input type='text' class='text' name='title' size='25' maxlength='50' value ='".$title."' /></td></tr>
	<tr><td>"._URL."</td><td><input type='text' class='text' name='url' size='50' maxlength='100' value ='".$url."' /></td></tr>
	<tr><td>"._AM_PARTNER_ADD_IMAGE."</td><td>";
	$upload = new Upload();
	$upload->printFormField("image");
	echo "</td></tr>
	<tr><td>"._DESCRIPTION."</td><td><input type='text' class='text' name='description' size='50' maxlength='50' value ='".$description."' /></td></tr>
	<tr><td>"._AM_PARTNER_STATUS."</td><td><input type='checkbox' class='text' name='status'"; 
	if ($status == 1) { echo " checked value='0' "; } else { echo " value='1' ";}
	echo " /></td></tr>
	<tr><td></td><td><input type='hidden' name='op' value='partner_add' /><input type='hidden' name='id' value='".$id."' /><input type='submit' class='button' value='"._AM_PARTNER_ADD_SUBMIT."' /></td></tr>
	</table>
	</form>
	";
	CloseTable();
	
} 
function partner_delete($id, $ok=0) { 
	global $db, $myts;
	$id = intval($id);
	$result = $db->query("SELECT image, title FROM ".$db->prefix("partner")." WHERE id=$id");
	list($oldimage,$title) = $db->fetchRow($result);
	
	if ($ok == 1) {
		@unlink(XOOPS_ROOT_PATH . "/modules/partner/cache/images/".basename($oldimage));
		$db->query("DELETE FROM ".$db->prefix("partner")." WHERE id=$id");
		redirect_header("index.php", 1, _AM_PARTNER_ADD_DELSUBMIT);
		exit();
	} else {
		echo _AM_PARTNER_ADD_SUREDELE." ".$title."<br /><br />";
		echo "<table><tr><td>";
		echo myTextForm("index.php", _NO);
		echo "</td><td>";
		echo myTextForm("index.php?op=partner_delete&amp;id=$id&amp;ok=1", _YES);
		echo "</td></tr></table><br /><br />";
	}
	
	
	
} 
OpenTable();
//------------------------------------------------------------------------------------------//
$op = !empty($_POST['op']) ? $_POST['op'] : $_GET['op'];
switch($op) {
	case "partner_admin":
		partner_admin();
		break;
	case "partner_add":
		partner_add($_POST['cid'], $_POST['imageurl'], $_POST['imagealt'], $_POST['clickurl'], $_POST['imptotal'], $_POST['display'], $_POST['ccode'], $_POST['custom']);
		break;
	case "partner_delete":
		partner_delete($_GET['id'], $_GET['ok']);
		break;
	case "partner_edit":
		partner_edit($_GET['id']);
		break;
		
	default:
		partner_admin();
		break;
}
//------------------------------------------------------------------------------------------//
CloseTable();
xoops_cp_footer();
?>
