<?php

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

// rewrite this page later

$psize = getPageSize();

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

// rewrite later

$sql = 'SELECT u.*, floor((to_days(curdate())-to_days(birth_date))/365.25)  as age FROM ! u, ! ou WHERE u.allow_viewonline=? AND u.status in (?,?) AND u.id = ou.userid ';


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

	$sql .= ' and u.id<>' . $_SESSION['UserId'];
}

$cpage = $_GET['page'];

if( $cpage == '' ) {
	$cpage = 1;
}

$data =& $osDB->getAll( $sql, array( USER_TABLE, ONLINE_USERS_TABLE, '1', get_lang('status_enum','active'), 'active') );

$rcount = count($data);

if( $rcount > 0 ) {

	$t->assign( 'totalrecs', $rcount );

	$pages = ceil( $rcount / $psize );

	$start = ( $cpage - 1 ) * $psize;

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

	if( $pages > 1 ) {

		if ( $cpage > 1 ) {

			$prev = $cpage - 1;

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

		}
		$t->assign ( 'cpage', $cpage );

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

		if ( $cpage < $pages ) {

			$next = $cpage + 1;

			$t->assign ( 'next', $next );
		}
		$data = array_slice($data, $start, $psize);
	}

	if ( sizeof( $data ) == 0 ) {

		$t->assign ( 'error', "1" );

	} else {

		$list = array();

		foreach ($data as $row) {

			$row['countryname'] = getCountryName($row['country'] );

			$row['statename'] = getStateName( $row['country'], $row['state_province'] );

			$list[] = $row;
		}

		hasRight('');

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

		unset($list, $data);
	}

}

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

$t->assign('rendered_page', $t->fetch('onlineusers.tpl') );

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

exit;

?>