<?php
// $Id: index.php,v 1.1 2004/07/11 10:28:44 Farsus Exp $
// -------------------------------------------------------------------------//
//			                 RUNCMS                                         //
//                                                                          //
//	 reliable - Unique - Nocost &  Simplicity & ease off use                //
//                       < http://www.runcms.org >                          //
// -------------------------------------------------------------------------//
// Original Author: Half-Dead
// Author Website : http://www.e-xoops.com
// License Type   : Proprietary: See /manual/LICENSES/E-Xoops.txt
// ------------------------------------------------------------------------- //

include_once("./admin_header.php");


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

xoops_cp_header();
OpenTable();
echo "<h4>"._AM_PARTNERADMIN."</h4>";

echo "
<table width='100%' border='0' align='center'><tr class='bg2'>
<form action='./index.php' method='post'>
<td width='1%' align='left' nowrap><b>" ._AM_TITLE."</b></td>
<td><b>" ._AM_DESCRIPTION."</b></td>
<td width='5' align='left' nowrap><b>" ._AM_IMAGE."</b></td>
<td width='5' align='left' nowrap><b>" ._AM_HITS."</b></td>
<td width='5' align='left' nowrap><b>" ._AM_WEIGHT."</b></td>
<td>&nbsp;</td></tr>";

$result = $db->query("SELECT id, hits, url, weight, image, title, description, status FROM ".$db->prefix("partners")." ORDER BY status DESC, weight ASC, title DESC");
while ( list($id, $hits, $url, $weight, $image, $title, $description, $status) = $db->fetch_row($result) ) {
$title        = $myts->makeTboxData4Show($title);
$description  = $myts->makeTboxData4Show($description);

echo "
<tr class='bg1'>
<td width='1%' align='left' valign='top' nowrap><a href='$url' target='_blank'>$title</a></td>
<td align='left' valign='top'>$description</td>
<td width='5' align='center' valign='top' nowrap>";

if ( !empty($image) ) {
	echo "<a href='$url' target='_blank'><img src='".formatURL(XOOPS_URL . "/modules/partners/cache/images/", $image)."' alt='$title' border='0'></a>";
}

echo "
</td>
<td width='5' align='left' valign='top' nowrap>$hits</td>
<td width='5' align='left' valign='top' nowrap>";
if ( !empty($status) ) {
	echo "<input type='text' class='text' name='weight[$id]' value='$weight' size='3' maxlength='3'>";
	}
echo "
</td><td width='1%' align='right' valign='top' nowrap>
<a href='index.php?op=edit_partner&amp;id=".$id."'>"._EDIT."</a>|<a href='index.php?op=delete_partner&amp;id=".$id."&amp;del=0'>"._DELETE."</a>
</td></tr>";
}

echo "
<tr><td colspan='6' align='right'>
<input type='hidden' name='op' value='reorder_partners' />
<input type='submit' class='button' name='submit' value='"._AM_REORDER."'>
</td></tr></form></table>";

CloseTable();
OpenTable();

echo "
<form action='index.php' method='post' enctype='multipart/form-data'>
<h4>"._ADD."</h4>

<table><tr>

<td>"._AM_WEIGHT."</td>
<td><input type='text' class='text' name='weight' size='3' maxlength='3' /></td>

</tr><tr>

<td>"._AM_IMAGE."</td>
<td><input type='text' class='text' name='image' size='35' maxlength='255' /> :: <input type='file' class='file' name='image1' /></td>

</tr><tr>

<td>"._AM_URL."</td>
<td><input type='text' class='text' name='url' size='35' maxlength='255' /></td>

</tr><tr>

<td valign='top'>"._AM_TITLE."</td>
<td><input type='text' class='text' name='title' size='35' maxlength='60' /></td>

</tr><tr>

<td valign='top'>"._AM_DESCRIPTION."</td>
<td><textarea class='textarea' name='description' cols='60' rows='10'></textarea></td>

</tr><tr>

<td valign='top'>"._STATUS."</td>
<td>
<select class='select' name='status'>
<option value='1'>"._ACTIVE."</option>
<option value='0'>"._INACTIVE."</option>
</select>
</td>

</tr><tr>

<td colspan='2' align='right'>
<input type='hidden' name='op' value='add_partner' />
<input type='submit' class='button' value='"._ADD."' />
<td>

</tr></table>
</form>";

CloseTable();
}

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

xoops_cp_header();
OpenTable();

$result = $db->query("SELECT weight, hits, url, image, title, description, status FROM ".$db->prefix("partners")." WHERE id=$id");
list($weight, $hits, $url, $image, $title, $description, $status) = $db->fetch_row($result);
$title        = $myts->makeTboxData4Edit($title);
$description  = $myts->makeTboxData4Edit($description);

if ( empty($status) ) {
	$chk = " selected='selected'";
	}

echo "<h4><a href='index.php'>"._MAIN."</a> :: "._EDIT."</h4>";

if ( !empty($image) ) {
	echo "<img src='".formatURL(XOOPS_URL . "/modules/partners/cache/images/", $image)."' border='0' />";
	}

echo "
<form action='./index.php' method='post' enctype='multipart/form-data'>
<table><tr>

<td>"._AM_HITS."</td>
<td><input type='text' class='text' name='hits' size='3' maxlength='8' value='$hits' /></td>

</tr><tr>

<td>"._AM_WEIGHT."</td>
<td><input type='text' class='text' name='weight' size='3' maxlength='3' value='$weight' /></td>

</tr><tr>

<td>"._AM_URL."</td>
<td><input type='text' class='text' name='url' size='35' maxlength='255' value='$url' /> <a href='$url' target='_blank'>"._VISIT."</a></td>

</tr><tr>

<td>"._AM_IMAGE."</td>
<td><input type='text' class='text' name='image' size='35' maxlength='255' value='$image' /> :: <input type='file' class='file' name='image1' /></td>

</tr><tr>

<td>"._AM_TITLE."</td>
<td><input type='text' class='text' name='title' size='35' maxlength='60' value='$title' /></td>

</tr><tr>

<td>"._AM_DESCRIPTION."</td>
<td><textarea class='textarea' name='description' cols='50' rows='10' />$description</textarea></td>

</tr><tr>

<td>"._STATUS.":</td>
<td>
<select class='select' name='status'>
<option value='1'>"._ACTIVE."</option>
<option value='0'$chk>"._INACTIVE."</option>
</select>
</td>

</tr><tr>

<td colspan='2' align='right'>
<input type='hidden' name='id' value='$id' />
<input type='hidden' name='op' value='change_partner' />
<input type='submit' class='button' value='"._EDIT."' />
</td>

</tr></table>
</form>";

CloseTable();
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function change_partner($id, $weight, $hits, $url, $image, $title, $description, $status) {
global $db, $myts, $HTTP_POST_FILES;

if ( empty($title) || empty($url) ) {
	redirect_header("index.php", 1, _AM_BESURE);
	exit();
}

$url          = formatURL($url);
$title        = $myts->makeTboxData4Save($title);
$description  = $myts->makeTboxData4Save($description);

if ( !is_numeric($weight) ) {
	$weight = 0;
	}

if ( !is_numeric($hits) ) {
	$hits = 0;
	}

if ( !empty($HTTP_POST_FILES['image1']['name']) ) {
	include_once(XOOPS_ROOT_PATH."/class/fileupload.php");
	$upload = new fileupload();
	$upload->set_upload_dir(XOOPS_ROOT_PATH . "/modules/partners/cache/images/", 'image1');
	$upload->set_accepted("gif|jpg|png", 'image1');
	$upload->set_overwrite(1, 'image1');
	$result = $upload->upload();
	if ($result['image1']['filename']) {
		$old = $db->query("SELECT image FROM ".$db->prefix("partners")." WHERE id=$id");
		list($oldimage) = $db->fetch_row($old);
		@unlink(XOOPS_ROOT_PATH."/modules/partners/cache/images/".basename($oldimage));
		$image = $result['image1']['filename'];
		} else {
			redirect_header("index.php", 3, $upload->errors());
			exit();
		}
}

$sql = "
	UPDATE ".$db->prefix("partners")." SET
	hits=$hits,
	weight=$weight,
	url='$url',
	image='$image',
	title='$title',
	description='$description',
	status=$status
	WHERE id=$id";

if ($db->query($sql)) {
	redirect_header("index.php", 1, _UPDATED);
	} else {
		redirect_header("index.php", 1, _NOTUPDATED);
	}
	exit();
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function add_partner($weight, $url, $image, $title, $description, $status) {
global $db, $myts, $HTTP_POST_FILES;

if ( empty($title) || empty($url) ) {
	redirect_header("index.php", 1, _AM_BESURE);
	exit();
}

$url          = formatURL($url);
$title        = $myts->makeTboxData4Save($title);
$description  = $myts->makeTboxData4Save($description);

if ( !is_numeric($weight) ) {
	$weight = 0;
}

if ( !empty($HTTP_POST_FILES['image1']['name']) ) {
	include_once(XOOPS_ROOT_PATH."/class/fileupload.php");
	$upload = new fileupload();
	$upload->set_upload_dir(XOOPS_ROOT_PATH."/modules/partners/cache/images/", 'image1');
	$upload->set_accepted("gif|jpg|png", 'image1');
	$upload->set_overwrite(1, 'image1');
	$result = $upload->upload();
	if ($result['image1']['filename']) {
		$image = $result['image1']['filename'];
		} else {
			redirect_header("index.php", 3, $upload->errors());
			exit();
		}
}

$newid = $db->genId($db->prefix("partners")."_num_seq");

$sql = "
	INSERT INTO ".$db->prefix("partners")." SET
	id=$newid,
	hits=0,
	weight=$weight,
	url='$url',
	image='$image',
	title='$title',
	description='$description',
	status=$status";

if ($db->query($sql)) {
	redirect_header("index.php", 1, _UPDATED);
	} else {
		redirect_header("index.php", 1, _NOTUPDATED);
	}
	exit();
}

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

if ($del == 1) {
	$old = $db->query("SELECT image FROM ".$db->prefix("partners")." WHERE id=$id");
	list($oldimage) = $db->fetch_row($old);
	@unlink(XOOPS_ROOT_PATH."/modules/partners/cache/images/".basename($oldimage));

	$sql = "DELETE FROM ".$db->prefix("partners")." WHERE id=$id";
	if ($db->query($sql)) {
		redirect_header("index.php", 1, _UPDATED);
		} else {
			redirect_header("index.php", 1, _NOTUPDATED);
		}
		exit();

} else {
	xoops_cp_header();
	OpenTable();
	echo "<b>"._AM_SURETODELETE."</b>";
	echo "<table><tr><td>";
	echo myTextForm("index.php?op=delete_partner&amp;id=".$id."&amp;del=1", _YES);
	echo "</td><td>";
	echo myTextForm("index.php" , _NO);
	echo "</td></tr></table>";
	CloseTable();
	}
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function reorder_partners($weight) {
global $db;

foreach ($weight as $id=>$order) {
	if ( isset($id) && is_numeric($id) && isset($order) ) {
		if ( !is_numeric($order) ) { $order = 0; }
		$db->query("UPDATE ".$db->prefix("partners")." SET weight='$order' WHERE id=$id");
	}
}

redirect_header("./index.php", 1, _UPDATED);
exit();
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function configure_partners() {

xoops_cp_header();
OpenTable();

echo "<h4>"._AM_CONFIG."</h4>";
include_once('../cache/config.php');
include_once('./settings.inc.php');

CloseTable();
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function save_partners() {
global $HTTP_POST_VARS;

if ( !empty($HTTP_POST_VARS) ) {

if ( is_numeric($HTTP_POST_VARS["partners_cookietime"]) ) {
	$partners_cookietime = $HTTP_POST_VARS["partners_cookietime"];
	} else {
		$partners_cookietime = 86400;
	}

if ( is_numeric($HTTP_POST_VARS["partners_limit"]) ) {
	$partners_limit = $HTTP_POST_VARS["partners_limit"];
	} else {
		$partners_limit = 0;
	}

if ( is_numeric($HTTP_POST_VARS["partners_blimit"]) ) {
	$partners_blimit = $HTTP_POST_VARS["partners_blimit"];
	} else {
		$partners_blimit = 0;
	}

$content = '<?php
// '._AM_CCOMMON.'
$partners_cookietime = '.$partners_cookietime.';

// '._AM_CMAIN.'
$partners_randomize = '.$HTTP_POST_VARS["partners_randomize"].';
$partners_limit     = '.$partners_limit.';
$partners_show      = '.$HTTP_POST_VARS["partners_show"].';
$partners_order     = "'.$HTTP_POST_VARS["partners_order"].'";
$partners_orderd    = "'.$HTTP_POST_VARS["partners_orderd"].'";

// '._AM_CBLOCK.'
$partners_brandomize = '.$HTTP_POST_VARS["partners_brandomize"].';
$partners_blimit     = '.$partners_blimit.';
$partners_bshow      = '.$HTTP_POST_VARS["partners_bshow"].';
$partners_border     = "'.$HTTP_POST_VARS["partners_border"].'";
$partners_borderd    = "'.$HTTP_POST_VARS["partners_borderd"].'";
?>';

if ( $fp = fopen('../cache/config.php', "w") ) {
	fwrite($fp, $content);
	fclose($fp);
	redirect_header("./index.php", 1, _UPDATED);
	} else {
		redirect_header("./index.php", 1, _NOTUPDATED);
	}

	} else {
		redirect_header("./index.php", 1, _AM_NOTHING);
	}

exit();
}

//---------------------------------------------------------------------------------------//
$op = !empty($HTTP_POST_VARS['op']) ? $HTTP_POST_VARS['op'] : $HTTP_GET_VARS['op'];

switch ($op) {

	case "change_partner":
		change_partner($HTTP_POST_VARS["id"], $HTTP_POST_VARS["weight"], $HTTP_POST_VARS["hits"], $HTTP_POST_VARS["url"], $HTTP_POST_VARS["image"], $HTTP_POST_VARS["title"], $HTTP_POST_VARS["description"], $HTTP_POST_VARS["status"]);
		break;

	case "add_partner":
		add_partner($HTTP_POST_VARS["weight"], $HTTP_POST_VARS["url"], $HTTP_POST_VARS["image"], $HTTP_POST_VARS["title"], $HTTP_POST_VARS["description"], $HTTP_POST_VARS["status"]);
		break;

	case "delete_partner":
		delete_partner($HTTP_GET_VARS['id'], $HTTP_GET_VARS['del']);
		break;

	case "edit_partner":
		edit_partner($HTTP_GET_VARS['id']);
		break;

	case "reorder_partners":
		reorder_partners($HTTP_POST_VARS["weight"]);
		break;

	case "configure_partners":
		configure_partners();
		break;

	case "save_partners":
		save_partners();
		break;

	default:
		partners_manage();
		break;
}

xoops_cp_footer();
?>
