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

$psize = getPageSize();

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

$with_photo = $_REQUEST['with_photo'];

$cpage = $_REQUEST['page'];

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

$lookgender_search="";

/* Bypass cross matching in search if set in global settings */
if ($config['bypass_search_lookgender'] == 'N' or $config['bypass_search_lookgender'] == '0' ) {
	$lookgender_search = " AND usr.lookgender in ('A' ";
	if ($_REQUEST['txtgender'] == 'M' || $_REQUEST['txtgender'] == 'F') {
		$lookgender_search .= ",'B'";
	}
	$lookgender_search .= ",'".$_REQUEST['txtgender']."') ";
}

$gender_search = " AND usr.gender in ( ";

if ($_REQUEST['txtlookgender'] == 'A') {
	$gender_search .= "'M','F','C'";
} elseif ( $_REQUEST['txtlookgender'] == 'B') {
	$gender_search .= "'M','F'";
} else {
	$gender_search .= "'".$_REQUEST['txtlookgender']."'";
}
$gender_search .= ") ";

if ($_REQUEST['sort_by'] == '') {
	$sort_by=$config['search_sort_by'];
} else {
	$sort_by=$_REQUEST['sort_by'];
}

if ($_REQUEST['sort_order'] == '') {
	$sort_order='asc';
} else {
	$sort_order=$_REQUEST['sort_order'];
}

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

$sortme = " order by ";

if ($sort_by == 'username') {

	$sortme .= 'usr.username ';

} elseif ( $sort_by == 'age' ) {

	$sortme .= ' age ';

} elseif ( $sort_by == 'level' ) {

	$sortme .= ' usr.level ';

} elseif ( $sort_by == 'logintime' ) {

	$sortme .= 'usr.lastvisit ';
	if ($_REQUEST['sort_order'] == '') {
		$sort_order=' desc ';
	} else {
		$sort_order=$_REQUEST['sort_order'];
	}

} elseif ( $sort_by == 'online' ) {

	$sortme .= ' onl.is_online desc, usr.username ';
}

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

$sortme .= $sort_order." ";

$bannedlist = '';
if ($_SESSION['UserId'] != '') {
	$bannedusers =& $osDB->getAll('select usr.id as ref_userid from ! as bdy, ! as usr where bdy.act=? and((usr.username = bdy.username and bdy.ref_username = ?) or (usr.username = bdy.ref_username and bdy.username = ? ))', array(BUDDY_BAN_TABLE, USER_TABLE, 'B', $_SESSION['UserName'], $_SESSION['UserName']) );
	if (count($bannedusers) > 0) {
		$bannedlist=' and usr.id not in (';
		$bdylst = '';
		foreach ($bannedusers as $busr) {
			if ($bdylst != '') $bdylst .= ',';
			$bdylst .= "'".$busr['ref_userid']."'";
		}
		$bannedlist .=$bdylst.') ';
	}
	unset($bannedusers);
}

$yearstart  = date("Y-m-d", mktime(0, 0, 0, date("m"),   date("d"),   date("Y") - $_REQUEST['txtlookageend'])-1);
$yearend  = date("Y-m-d", mktime(0, 0, 0, date("m"),   date("d")+1,   date("Y") - $_REQUEST['txtlookagestart']));

if ( $with_photo == '1' ) {

	$rs =& $osDB->getAll( 'SELECT distinct usr.*, floor((to_days(curdate())-to_days(birth_date))/365.25)  as age, onl.is_online  FROM  ! as pics, ! as mem, ! usr left join ! as onl on onl.userid=usr.id WHERE pics.userid = usr.id and mem.roleid=usr.level and mem.includeinsearch=1 AND usr.id > 0 and lower(usr.status) in (?, ?) AND usr.active=1 AND usr.birth_date BETWEEN ? AND ? ! ! ! ! ', array( USER_SNAP_TABLE, MEMBERSHIP_TABLE, USER_TABLE, ONLINE_USERS_TABLE, 'active', get_lang('status_enum','active'), $yearstart, $yearend, $bannedlist, $gender_search, $lookgender_search, $sortme ) );

} else {
	$rs =& $osDB->getAll( 'SELECT usr.*, floor((to_days(curdate())-to_days(birth_date))/365.25)  as age, onl.is_online  FROM ! as mem, ! usr left join ! as onl on onl.userid=usr.id WHERE lower(usr.status) in(?, ?) AND mem.roleid = usr.level and mem.includeinsearch=1 AND usr.id > 0 AND usr.active=1 AND usr.birth_date BETWEEN ? AND ? ! ! ! ! ', array(MEMBERSHIP_TABLE, USER_TABLE, ONLINE_USERS_TABLE,  'active', get_lang('status_enum','active'), $yearstart, $yearend, $bannedlist, $gender_search, $lookgender_search, $sortme ) );
}

$rcount = count($rs);

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 );
		}

		$rs = array_slice($rs, $start, $psize);

	}

}

setcookie($config['cookie_prefix']."osdate_info[search_ages]", $_REQUEST['txtlookagestart'].':'.$_REQUEST['txtlookageend'], strtotime("+30day"), "/" );

$querystring = array(
			'txtgender'			=> $_REQUEST['txtgender'],
			'txtlookgender'		=> $_REQUEST['txtlookgender'],
			'txtlookagestart' 	=> $_REQUEST['txtlookagestart'],
			'txtlookageend'		=> $_REQUEST['txtlookageend'],
			'with_photo'		=> $_REQUEST['with_photo']
			) ;

$_SESSION['txtgender'] = $_REQUEST['txtgender'];
$_SESSION['txtlookgender']= $_REQUEST['txtlookgender'];
$_SESSION['lookageend'] = $_REQUEST['txtlookageend'];
$_SESSION['lookagestart'] = $_REQUEST['txtlookagestart'];
$_SESSION['with_photo'] = $_REQUEST['with_photo'];


if ( count($rs) <= 0  ) {

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

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

	$t->assign ( 'backlink', 'searchprofile.php' );

} else {

	if ( $_REQUEST['savesearch'] == 'on' && isset( $_SESSION['UserId'] ) ) {

		$osDB->query( 'INSERT INTO ! ( userid, query) VALUES(? , ?)', array(USER_SEARCH_TABLE, $_SESSION['UserId'], $sql ) );
	}

	$data = array();

	foreach( $rs as $row ) {

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

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

		$data[] = $row;
	}

	unset($rs);

	hasRight('');

	$lang['sort_types'] = get_lang_values('sort_types');

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

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

	unset($data, $querystring);
}
$t->assign ( 'lang', $lang );

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

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

exit;

?>