<?php
@session_start();
if(isset($_SESSION['ekdhlos_userid'])) {include_once('php/php_headers.php');session_null('insert_messages');html_header('Όλοι οι χρήστες');}
else {include_once('php/functions.php');html_header('Όλοι οι χρήστες','public_message');}


require_once "pagination/Paginated.php";
require_once "pagination/DoubleBarLayout.php";
echo '<h1 class="center">Ενεργοί χρήστες</h1>';
$users = get("simple","users","","","all");
if(isset($_GET['page']) && is_numeric($_GET['page'])) $page = $_GET['page'];
else $page = 1;
$pagedResults = new Paginated($users, 8, $page);
echo '
<table id="gradient-style">
	<thead>
		<tr>
			<th scope="col" class="width_th_message">Ονοματεπώνυμο</th>
			<th scope="col" class="width_th_date">Email</th>
			<th scope="col" class="width_th_date">Επίπεδο</th>
			<th scope="col" class="width_status">Σύνολο μηνυμάτων</th>
			<th scope="col" class="width_status">Ενεργές ομάδες</th>
			<th scope="col" class="width_status">Ενεργές συσκευές</th>
		</tr>
	</thead>
	<tbody>';
	while($row = $pagedResults->fetchPagedRow()){
		$name = $row['f_name'].' '.$row['l_name'];
		$email = $row['email'];
		$groups = $devises = null;
		data_on();
			/* μηνύματα */
			$sql_msg = "SELECT * FROM message WHERE user_id = '{$row['id']}'";
			$result = mysql_query($sql_msg) or die('error');
			$messages = mysql_num_rows($result);
			
			/* ομάδες */
			$sql_groups = "SELECT * FROM groups WHERE status='yes' AND user_id = '{$row['id']}'";
			$result = mysql_query($sql_groups) or die('error');
			while($row3 = mysql_fetch_array($result)){
				if($row3['visibility']=='public') $groups .= "<span class=\"h8\" title=\"Η ομάδα είναι δημόσια\">".$row3['g_name']."</span>, ";
				else $groups .= "<span class=\"h9\" title=\"Η ομάδα είναι ιδιωτική\">".$row3['g_name']."</span>, ";
			}
			if($groups == null) $groups="<span class=\"h10\">Ο χρήστης δεν έχει εισάγει ομάδες!</span>";
			else $groups = substr($groups,0,-2);
			
			/* συσκευές */
			$sql_device = "SELECT * FROM device WHERE status='yes' AND user_id = '{$row['id']}'";
			$result2 = mysql_query($sql_device) or die('error');
			while($row2 = mysql_fetch_array($result2)){
				if($row2['visibility']=='public') $devises .= "<span class=\"h8\" title=\"Η συσκευή είναι δημόσια\">".$row2['id']."</span>, ";
				else $devises .= "<span class=\"h9\" title=\"Η συσκευή είναι ιδιωτική\">".$row2['id']."</span>, ";
			}
			if($devises == null) $devises="<span class=\"h10\">Ο χρήστης δεν έχει εισάγει συσκευές!</span>";
			else $devises = substr($devises,0,-2);
			
			
		data_off();
		
		if($row['level']==0) $level = "<span class=\"h9\">Διαχειριστής</span>";
		else $level = "<span class=\"h8\">Απλός χρήστης</span>";
		echo '
		<tr>
			<td>'.$name.'</td>
			<td>'.$email.'</td>
			<td>'.$level.'</td>
			<td>'.$messages.'</td>
			<td>'.$groups.'</td>
			<td>'.$devises.'</td>
		</tr>';
	}
	echo '
	</tbody>';
$pagedResults->setLayout(new DoubleBarLayout());
echo '
	<tfoot>
		<tr>
			<td colspan="6">';
			if(sizeof($users)>8) echo $pagedResults->fetchPagedNavigation();
			echo '</td>
		</tr>
	</tfoot>
</table>
';
html_footer();
?>