<?php

	$actions = array('search');
	
	(key_exists('i', $_GET) && $_GET['i']<>'') ? $i = $_GET['i'] : $i = 1;
	(key_exists('time_order', $_GET) && $_GET['time_order']<>'') ? $time_order = $_GET['time_order'] : $time_order = '';
	(key_exists('name_order', $_GET) && $_GET['name_order']<>'') ? $name_order = $_GET['name_order'] : $name_order = ''	;
	(key_exists('action', $_GET) && $_GET['action']<>'' && in_array($_GET['action'], $actions)) ? $action = $_GET['action'] : $action = '';
	(key_exists('online', $_GET) && $_GET['online']<>'') ? $online = $_GET['online'] : $online = 0;
	(key_exists('icq', $_GET) && $_GET['icq']<>'') ? $icq = $_GET['icq'] : $icq = 0;
	(key_exists('msn', $_GET) && $_GET['msn']<>'') ? $msn = $_GET['msn'] : $msn = '';
	
	$image_gender = array
	(
		0 => 'male',
		1 => 'female'
	);
	
	if(session_is_registered('mobile_logged_in') && session_is_registered('mobile_page_users') && $_SESSION['mobile_page_users']!=0)
	{
		$nastranu = $_SESSION['mobile_page_users'];
	}
	else
	{
		$nastranu = 20;
	}
	
	//zistenie prave prihlasenych uzivatelov
	$query = "SELECT user_id FROM mobile_logged_users";
	$result = $dba->GetResult($query);
	$j = 0;
	while($row = mysql_fetch_array($result))
	{
		$logged_users[$j] = $row['user_id'];
		$j++;
	}
	unset($j);
	//koniec zistenia prihlasenych pouzivatelov
	
	//skladanie final_query pri standardnom vypise uzivatelov
	if($action == '')
	{
		$query = "SELECT ID FROM mobile_users";
		$chunk = $dba->GetLimit($query);
		$lim = $chunk[0];
		$stranok = $chunk[2];
		
		$selection = 'a.ID, a.login, a.prihlaseni, a.mail, a.gender, a.last_login, a.last_login_time, a.moj_mobil, a.mesto, a.icq, a.msn, a.kredits, a.kredits_earned, b.nazov as mobil, b.ID';
		$orders = array('asc', 'desc');
		
		if(in_array($name_order, $orders)) 
		{
//			$final_query = "SELECT $selection FROM mobile_users a, mobile_mobil b WHERE a.moj_mobil=b.ID ORDER BY login $name_order LIMIT $lim, $nastranu";
			$final_query = "SELECT $selection FROM mobile_users a, mobile_mobil b WHERE a.moj_mobil=b.ID LIMIT $lim, $nastranu";
		}
		elseif(in_array($time_order, $orders)) 
		{
//			$final_query = "SELECT $selection FROM mobile_users a, mobile_mobil b WHERE a.moj_mobil=b.ID ORDER BY last_login_time $time_order LIMIT $lim, $nastranu";
			$final_query = "SELECT $selection FROM mobile_users a, mobile_mobil b WHERE a.moj_mobil=b.ID LIMIT $lim, $nastranu";
		}
		else $final_query = "SELECT $selection FROM mobile_users a, mobile_mobil b WHERE a.moj_mobil=b.ID LIMIT $lim, $nastranu";
		$search = '';
		$search_gender = '';
	}
	
	// vyhladavanie pouzivatelov
	if($action == 'search')
	{
		$genders = array(0, 1);
		(key_exists('search', $_GET) && $_GET['search']<>'') ? $search = $_GET['search'] : $search = '';
		(key_exists('gender', $_GET) && $_GET['gender']<>'' && in_array($_GET['gender'], $genders)) ? $search_gender = $_GET['gender'] : $search_gender = 'all';
		(key_exists('online', $_GET) && $_GET['online']<>'') ? $online = $_GET['online'] : $online = 0;
		(key_exists('icq', $_GET) && $_GET['icq']<>'') ? $icq = $_GET['icq'] : $icq = 0;
		(key_exists('msn', $_GET) && $_GET['msn']<>'') ? $msn = $_GET['msn'] : $msn = '';
		
		$and = '';
		$and2 = '';
		$and3 = '';
		$and4 = '';
		$and5 = '';
		$where = '';
		$order = '';
		$hladane_online = '';
		$hladane_icq = '';
		$hladane_msn = '';
		$hladane_mobil = ' mobile_users.moj_mobil = mobile_mobil.ID ';
		$selection = 'mobile_users.ID, mobile_users.login, mobile_users.prihlaseni, mobile_users.mail, mobile_users.gender, mobile_users.last_login, mobile_users.last_login_time, mobile_users.mesto, mobile_users.icq, mobile_users.msn, mobile_users.moj_mobil, mobile_users.kredits, mobile_users.kredits_earned, mobile_mobil.nazov as mobil, mobile_mobil.ID';
		$from = 'mobile_users, mobile_mobil';
		if($online == 1) 
		{
			if($and!='' && $hladane_pohlavie!='') $and2 = 'and';
			$order = 'ORDER BY last_login_time DESC';
			$hladane_online = 'mobile_users.ID = mobile_logged_users.user_id';
			$selection .= ', mobile_logged_users.user_id';
			$from .= ', mobile_logged_users';
		}
		if($icq==1)
		{
			$hladane_icq = " mobile_users.icq<>'' ";
		}
		if($msn==1)
		{
			$hladane_msn = " mobile_users.msn<>'' ";
		}
		
		$hladane_meno = '';
		if($search<>'') $hladane_meno = "login like '$search%'"; 
		
		$hladane_pohlavie = '';
		if($search_gender != 'all') $hladane_pohlavie = "gender = $search_gender";
		
		$where = ' WHERE ';
		if($hladane_meno != '' && $hladane_pohlavie != '') $and = ' AND ';
		if($hladane_online != '' && $hladane_pohlavie != '') $and2 = ' AND ';
		if($hladane_meno != '' && $hladane_online != '') $and = ' AND ';
		if($hladane_icq != '' && ($hladane_meno != '' || $hladane_pohlavie != '' || $hladane_online != '')) $and3 = ' AND ';
		if($hladane_msn != '' && ($hladane_meno != '' || $hladane_pohlavie != '' || $hladane_online != '' || $hladane_icq != '')) $and4 = ' AND ';
		if($hladane_msn != '' || $hladane_meno != '' || $hladane_pohlavie != '' || $hladane_online != '' || $hladane_icq != '') $and5 = ' AND ';
		
		
		
		
		
		$query = "SELECT $selection FROM $from $where $hladane_meno $and $hladane_pohlavie $and2 $hladane_online $and3 $hladane_icq $and4 $hladane_msn $and5 $hladane_mobil $order";
		$chunk = $dba->GetLimit($query);
		$lim = $chunk[0];
		$stranok = $chunk[2];
		
		$final_query = $query." limit $lim, $nastranu";
	}
	
?>

	<div id="register_form01">
	  <div id="menu01_head">:: užívatelia</div>
	  <?php
//		echo $final_query;
	  if(session_is_registered('mobile_logged_in')) fnNavi($i, $stranok, "module=users&amp;action=$action&amp;name_order=$name_order&amp;time_order=$time_order&amp;search=$search&amp;gender=$search_gender&amp;icq=$icq&amp;msn=$msn&amp;online=$online");?>
		<table width="100%" border="0" cellpadding="0" cellspacing="1">
		  <tr>
		    <td id="users_cell" width="20">&nbsp;</td>
		    <td id="users_cell"><b>meno</b></td>
		    <td id="users_cell"><b>mesto</b></td>
		    <td id="users_cell"><b>mobil</b></td>
		    <td id="users_cell" width="22"><b>ICQ</b></td>
		    <td id="users_cell" width="30"><b>MSN</b></td>
		    <td id="users_cell" width="35"><b>mail</b></td>
		    <td id="users_cell" width="40"><b>pošta</b></td>
		    <td id="users_cell" width="20%"><b>posledné prihlásenie</b></td>
		  </tr>
		  <?php
//		  if($_SESSION['my_id'] == 1)  echo $final_query;
		  $result = $dba->GetResult($final_query);
		  $nums = mysql_num_rows($result);
		  while($row = mysql_fetch_array($result))
		  {
		  	  $vypis = true;
//		  	  if($action=='search')
//		  	  {
//		  	  	  if($online == 1)
//		  	  	  {
//		  	  	  	  if(!in_array($row['ID'], $logged_users)) $vypis = false;
//		  	  	  }
//		  	  }
		  	  if($vypis)
		  	  {
			  	  echo "<tr>\n";
			  	  $title = (fnIsAdmin()) ? $row['prihlaseni'].' prihlásení | '.$row['kredits'].' kreditov | '.$row['kredits_earned'].' zarobených' : '';
			      echo "<td id=\"users_cell\" align=\"center\"><img src=\"images/".$image_gender[$row['gender']].".gif\" title=\"$title\"/></td>\n";
			      echo "<td id=\"users_cell\">".$row['login']."</td>\n";
			      echo "<td id=\"users_cell\">".$row['mesto']."</td>\n";
			      echo "<td id=\"users_cell\">".$row['mobil']."</td>\n";
			      echo "<td id=\"users_cell\">"; if($row['icq']!='') echo "<img src=\"http://status.icq.com/online.gif?icq=".$row['icq']."&amp;img=5\" align=\"top\" title=\"".$row['icq']."\" width=\"14\" height=\"14\" alt=\"\"/>"; echo "</td>\n";
			      echo "<td id=\"users_cell\">"; if($row['msn']!='') echo "<span title=\"".$row['msn']."\">msn</span>"; echo "</td>\n";
			      echo "<td id=\"users_cell\">";
			      if($row['mail']!='') echo "<a href=\"mailto:".$row['mail']."\" id=\"users_link\">mail</a>";
			      echo "</td>\n";
			      echo "<td id=\"users_cell\"><a href=\"index.php?module=posta&amp;action=form&amp;komu=".$row['login']."\" id=\"users_link\">pošli</a></td>\n";
			      (in_array($row['ID'], $logged_users)) ? $x = 'online' : $x = $row['last_login'];
			      echo "<td id=\"users_cell\">$x</td>\n";
			      echo "</tr>\n";
		  	  }
		  }
		  ?>
		</table>
	  <?php if(session_is_registered('mobile_logged_in')) fnNavi($i, $stranok, "module=users&amp;action=$action&amp;name_order=$name_order&amp;time_order=$time_order&amp;search=$search&amp;gender=$search_gender&amp;icq=$icq&amp;msn=$msn&amp;online=$online");
	  		if($action == 'search' && $nums == 0) echo "Užívateľ podľa zadaných kritérií sa tu momentálne nenachádza";?>
	</div>