<?php
if ( !defined( 'SMARTY_DIR' ) ) {
	include_once( '../init.php' );
}

include ( 'sessioninc.php' );

define( 'PAGE_ID', 'banner_mgt' );

if ( !checkAdminPermission( PAGE_ID ) ) {

	header( 'location: not_authorize.php' );
	exit;

}

$sortorder = ($_REQUEST['sort']=='asc')?'desc':'asc';

$orderby = 'id';

if (isset($_REQUEST['sortby'])) {
	switch ($_REQUEST['sortby']) {
		case 'size':
			$orderby = 'size '.$sortorder.', id';
			break;
		case 'clicks':
			$orderby = 'clicks '.$sortorder.', id';
			break;
		case 'enabled':
			$orderby = 'enabled '.$sortorder.', id';
			break;
		case 'srno':
		default:
			$orderby = 'id';
			break;
	}
}

$t->assign('sortorder', $sortorder);

//Delete Banner
if ( $_POST['txtid'] && $_POST['txtid'] != '' ) {

	$osDB->query( 'DELETE FROM ! WHERE id = ?', array( BANNER_TABLE, $_POST['txtid'] ) );

} elseif ( $_POST['enable'] ) {

	foreach( $_POST['txtcheck'] as $val ) {

		$osDB->query( 'UPDATE ! SET enabled = ? WHERE id = ?', array( BANNER_TABLE, 'Y', $val ) );
	}
} elseif ( $_POST['disable'] ) {

	foreach( $_POST['txtcheck'] as $val ) {

		$osDB->query( 'UPDATE ! SET enabled = ? WHERE id = ?', array( BANNER_TABLE, 'N', $val ) );
	}
} elseif( $_GET['edit'] ) {

	$row =& $osDB->getRow( 'SELECT * FROM ! WHERE id = ?', array( BANNER_TABLE, $_GET['edit'] ) );

	$t->assign( 'data', checkBannerRow($row) );

	unset($row);

	$t->assign('rendered_page', $t->fetch('admin/banneredit.tpl'));

	$t->display( 'admin/index.tpl' );

	exit;
}

$rs =& $osDB->getAll( 'SELECT * FROM ! where language is null order by ! !', array( BANNER_TABLE , $orderby, $sortorder) );

$data = array();

foreach( $rs as $row ) {

	$data[] = checkBannerRow($row);
}

$t->assign( 'data', $data );

unset($data, $rs);

$t->assign('rendered_page', $t->fetch('admin/managebanner.tpl'));

$t->display( 'admin/index.tpl' );

function checkBannerRow($row){
	$row['bannerurl'] = stripslashes( $row['bannerurl'] );
	$row['tooltip'] = stripslashes( $row['tooltip'] );
	if ($row['size'] != 'text') {

		$dim = split( 'x', $row['size'] );

		$row['width'] = trim( $dim[0] );

		$row['height'] = trim( $dim[1] );

		$row['type'] = substr( $row['name'], -3, 3 );
	} else {

		$row['type'] = 'text';

		$row['width'] = $row['height'] = 'N.A.';
	}

	return $row;
}
exit;
?>