<?php
defined('_VALID') or die('Restricted Access!');

$page           = ( isset($_GET['page']) && is_numeric($_GET['page']) ) ? intval($_GET['page']) : NULL;
$orders         = array('on', 'mr', 'mv', 'ma');
$order          = ( isset($_GET['o']) && in_array($_GET['o'], $orders) ) ? $_GET['o'] : 'mr';
$genders        = array('Male', 'Female');
$gender         = ( isset($_GET['g']) && in_array($_GET['g'], $genders) ) ? $_GET['g'] : NULL;
$interests      = array('Guys','Girls', 'Guys+Girls');
$interest       = ( isset($_GET['i']) && in_array($_GET['i'], $interests) ) ? str_replace('+', ' + ', $_GET['i']) : NULL;
$avatar         = ( isset($_GET['a']) && ( $_GET['a'] == 'yes' or $_GET['a'] == 'no' ) ) ? $_GET['a'] : NULL;
$online         = ( isset($_GET['u']) && ( $_GET['u'] == 'yes' or $_GET['u'] == 'no' ) ) ? $_GET['u'] : NULL;
$sql_add        = NULL;
$sql_add_count  = NULL;
$title_g        = NULL;
$title_o        = NULL;

if ( $search_query ) {
    $sql_add       .= " AND s.username LIKE '%" .mysql_real_escape_string($search_query). "%'";
    $sql_add_count .= " AND s.username LIKE '%" .mysql_real_escape_string($search_query). "%'";
}

if ( $gender ) {
    $sql_add       .= " AND s.gender = '" .mysql_real_escape_string($gender). "'";
    $sql_add_count .= " AND s.gender = '" .mysql_real_escape_string($gender). "'";
}

if ( $interest ) {
    $sql_add       .= " AND s.interested = '" .mysql_real_escape_string($interest). "'";
    $sql_add_count .= " AND s.interested = '" .mysql_real_escape_string($interest). "'";
}

if ( $avatar ) {
    $condition      = ( $avatar == 'yes' ) ? ' != \'\'' : ' = \'\'';
    $sql_add       .= " AND s.photo " .$condition;
    $sql_add_count .= " AND s.photo " .$condition;
}

if ( $online ) {
    $condition      = ( $online == 'yes' ) ? 'o.online > ' .(time()-300) : 'o.online < ' .(time()-300);
    $sql_add       .= " AND " .$condition;
    $sql_add_count .= " AND " .$condition;
}

switch ( $order ) {
    case 'mr':
        $sql_add   .= " ORDER BY s.addtime DESC";
        break;
    case 'mv':
        $sql_add   .= " ORDER BY s.profile_viewed DESC";
        break;
    case 'on':
        $sql_add   .= " ORDER BY s.logintime DESC";
        break;
    case 'ma':
        $sql_add   .= " ORDER BY s.video_viewed+s.profile_viewed+s.watched_video DESC";
        break;
}

$sql            = "SELECT COUNT(s.UID) AS total_users FROM signup AS s, users_online AS o
                   WHERE s.account_status = 'Active' AND s.UID = o.UID" .$sql_add_count;
$rsc            = $conn->execute($sql);
$total_users    = $rsc->fields['total_users'];
$pagination     = new Pagination($config['users_per_page']);
$limit          = $pagination->getLimit($total_users);
$sql            = "SELECT s.UID, s.username, s.photo, s.gender FROM signup AS s, users_online AS o
                   WHERE s.account_status = 'Active' AND s.UID = o.UID" .$sql_add. " LIMIT " .$limit;
$rs             = $conn->execute($sql);
$users          = $rs->getrows();
$page_link      = $pagination->getPagination('search');
$start_num      = $pagination->getStartItem();
$end_num        = $pagination->getEndItem();

$smarty->assign('users', $users);
$smarty->assign('users_total', $total_users);
$smarty->assign('page_link', $page_link);
$smarty->assign('start_num', $start_num);
$smarty->assign('end_num', $end_num);
$smarty->assign('order', $order);
$smarty->assign('gender', $gender);
$smarty->assign('interest', $interest);
$smarty->assign('avatar', $avatar);
?>
