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

include ( 'sessioninc.php' );

define( 'PAGE_ID', 'affiliate_mgt' );

if ( !checkAdminPermission( PAGE_ID ) ) {

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

$psize = getPageSize();

//Default Sorting
if ($_REQUEST['sortby'] == 'email') {
	$sortby = ' email ';
} elseif ($_REQUEST['sortby'] == 'register') {
	$sortby = ' regdate ';
} elseif ($_REQUEST['sortby'] == 'status') {
	$sortby = ' status ';
} else {
	$sortby = ' name ';
}

if (!isset($sortby) or $sortby == '') $sortby = ' name ';

if (!isset($_REQUEST['sortorder']) && $_SESSION['sortorder'] != '') {

	$sortorder = $_SESSION['sortorder'];

} else {

	$sortorder = checkSortType($_REQUEST['sortorder']);
}

//For updating affiliates
if ( $_POST['groupaction'] && count($_POST['txtchk']) > 0 ) {

	foreach ($lang['status_act'] as $key => $val ) {
		if ($val == $_POST['groupaction']) {
			$action = $key;
		}
	}

	$sqlwhere = "";

	foreach ( $_POST['txtchk'] as $affid) {
		if ($sqlwhere != "") $sqlwhere .= ' or ';
		$sqlwhere .= " id = '".$affid."' ";
	}

	if ($action != '') {
		$osDB->query( ' UPDATE ! SET status = ? where '.$sqlwhere, array( AFFILIATE_TABLE, $action ) );
	}

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

	header('location: affiliatesview.php');
	exit;
}

//Paging View style

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

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

$page = (int)$_GET['offset'];

if( $page == 0 ) {
	$page = 1;
}

$upr = ($page) * $psize - $psize;
$lwr = ($page) * $psize ;

$t->assign( 'data',$osDB->getAll( 'SELECT * FROM ! ORDER BY ' . $sortby .' '. $sortorder . " LIMIT $upr,$lwr", array( AFFILIATE_TABLE ) ) );

$t->assign( "sortorder",trim($sortorder) );
$t->assign( 'upr', $upr );

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

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

?>