<?php
@session_start();
if(isset($_SESSION['ekdhlos_userid'])){
include_once('php/functions.php');
require_once "pagination/Paginated.php";
require_once "pagination/DoubleBarLayout.php";

if(isset($_SESSION['user'])) {
	$_SESSION['edit_all_messages'] = get("all","message",$_SESSION['user'],"users","all");
}
if(isset($_SESSION['devs'])) {
	if($_SESSION['devs']=="all"){
		$_SESSION['edit_all_messages'] = get("all","message","all","","all");
		unset($_SESSION['devs']);
	}
	else{
		$_SESSION['edit_all_messages'] = get("all","message",$_SESSION['devs'],"","all");
	}
}
if(!isset($_SESSION['user']) && !isset($_SESSION['devs'])){
	$_SESSION['user'] = "all";
	$_SESSION['edit_all_messages'] = get("all","message",$_SESSION['user'],"users","all");
}
if($_SESSION['edit_all_messages']){
$pagedResults = new Paginated($_SESSION['edit_all_messages'], 5, $_SESSION['page']);
echo '
<h6 class="center">Μηνύματα</h6><br />
<table id="gradient-style">
	<thead>
		<tr>
			<th scope="col" class="width_th_date">Χρήστης</th>
			<th scope="col" class="width_status">Μήνυμα</th>
			<th scope="col" class="width_th_date">Ημερομηνία αποστολής</th>
			<th scope="col" class="width_status">Κατάσταση μηνύματος</th>
			<th scope="col">Revoke</th>
			<th scope="col">Forward</th>
			<th scope="col">Edit</th>
			<th scope="col">Delete</th>
		</tr>
	</thead>';
while($row = $pagedResults->fetchPagedRow()) {
	$word = $total_words = '';
	if(isset($_SESSION['user'])) {
		if ($row['user_id'] == 0){
			$iii = 1;
			while($row['message'][$iii]<>'%'){				
				$iii ++;
			}
			$iii ++;
			$total_words = substr_count(substr($row['message'],$iii), " ") + 1; // metrame to plithos twn leksewn	
			$word = explode(" ", substr($row['message'],$iii)); // tis bazoume se pinaka
		}
		else{
			$total_words = substr_count($row['message'], " ") + 1; // metrame to plithos twn leksewn	
			$word = explode(" ", $row['message']); // tis bazoume se pinaka
		}
	}
	if(isset($_SESSION['devs'])) {
		$mmssgg = get('message','message','id',$row['message_id'],'one');
		$id = get('user_id','message','id',$row['message_id'],'one');
		if ($id == 0){
			$iii = 1;
			while($mmssgg[$iii]<>'%'){				
				$iii ++;
			}
			$iii ++;
			$total_words = substr_count(substr($mmssgg,$iii), " ") + 1; // metrame to plithos twn leksewn	
			$word = explode(" ", substr($mmssgg,$iii)); // tis bazoume se pinaka
		}
		else{
			$total_words = substr_count($mmssgg, " ") + 1; // metrame to plithos twn leksewn	
			$word = explode(" ", $mmssgg); // tis bazoume se pinaka	
		}
	}
	if(isset($_SESSION['user'])) {
		if ($row['user_id'] == 0){
			$fullname = "public user:<br />";
			$iii = 1;
			while($row['message'][$iii]<>'%'){
				$fullname .= $row['message'][$iii];
				$iii ++;
			}
		}
		else{
			if(get('id','users','id',$row['user_id'],'one') == $_SESSION['ekdhlos_userid'])	
				$fullname = get('l_name','users','id',$row['user_id'],'one').' '.get('f_name','users','id',$row['user_id'],'one');
			else	
				$fullname = '<a href="edit_users.php?edit_id='.get('id','users','id',$row['user_id'],'one').'">
				'.get('l_name','users','id',$row['user_id'],'one').' '.get('f_name','users','id',$row['user_id'],'one').'</a>';
		}
	}
	if(isset($_SESSION['devs'])){
		$iidd = get('user_id','message','id',$row['message_id'],'one');
		if ($iidd == 0) {
			$mmssggg = get('message','message','id',$row['message_id'],'one');
			$fullname = "public user:<br />";
			$iii = 1;
			while($mmssggg[$iii]<>'%'){
				$fullname .= $mmssggg[$iii];
				$iii ++;
			}
		}
		else{
			if(get('id','users','id',$iidd,'one') == $_SESSION['ekdhlos_userid'])	
				$fullname = get('l_name','users','id',$iidd,'one').' '.get('f_name','users','id',$iidd,'one');
			else	
				$fullname = '<a href="edit_users.php?edit_id='.get('id','users','id',$row['user_id'],'one').'">
				'.get('l_name','users','id',$iidd,'one').' '.get('f_name','users','id',$iidd,'one').'</a>';
		}
	}
	echo "
	<tbody>
		<tr>
			<td>$fullname</td>
			<td><span class=\"h7\">";
				$j=0;
				if($total_words <= 3){
					for($j; $j < $total_words; $j++){
						echo $word[$j]." ";
					}
				}
				else if($total_words > 3){
					while($j<=3 ){
						if($j == 3)	echo $word[$j]."..";	
						else echo $word[$j]." ";
						$j++;
					}
				}
	if(isset($_SESSION['user'])) {
		echo "</span>
		</td>
			<td>".fix_date($row['date'])."<br />".fix_hour($row['date'])."</td>
			<td>";
			echo katastasi_mnm($row['id']);
			echo"
			</td>";
			if(check_for_revoke_on_not($row['id']) == 0){
				if(get('display','message','id',$row['id'],'one')=="yes"){						
					echo "<td class=\"center\"><a href=\"edit_all_messages.php?revoke_id={$row['id']}\" onclick=\"return checked();\"><img alt=\"Activate Revoke\" src=\"images/revoke.png\" title=\"Αποτρέψτε τις συσκεύες από το να διαβάσουν το μήνυμα!\" /></a></td>";
				}
				else{
					echo "<td class=\"center\"><a href=\"edit_all_messages.php?revoke_id={$row['id']}\" onclick=\"return checked2();\"><img alt=\"Stop Revoke\" src=\"images/tick.png\" title=\"Επιτρέψτε στις συσκεύες να διαβάσουν το μήνυμα!\" /></a></td>";						
				}
			}
			else echo "<td></td>";
			echo "
			<td class=\"center\"><a href=\"edit_all_messages.php?forward_id={$row['id']}&amp;step=1\"><img alt=\"Forward\" src=\"images/fw.png\" title=\"Κάντε κλικ για να προωθήσετε το μήνυμα!\"/></a></td>
			<td class=\"center\"><a href=\"edit_all_messages.php?edit_id={$row['id']}\"><img alt=\"Edit\" src=\"images/icon-edit.png\" title=\"Κάντε κλικ για να επεξεργαστείτε το μήνυμα!\" /></a></td>
			<td class=\"center\"><a href=\"edit_all_messages.php?delete_id={$row['id']}\" onclick=\"return deletechecked();\"><img alt=\"Delete\" src=\"images/icon-delete.png\" title=\"Κάντε κλικ για να διαγράψετε το μήνυμα!\" /></a></td>
		</tr>";
	}
	if(isset($_SESSION['devs'])) {
		echo "</h7>
		</td>
			<td>".fix_date(get('date','message','id',$row['message_id'],'one'))."<br />".fix_hour(get('date','message','id',$row['message_id'],'one'))."</td>
			<td>";
			echo katastasi_mnm($row['message_id']);
			echo"
			</td>";
			if(check_for_revoke_on_not($row['message_id']) == 0){
				if(get('display','message','id',$row['message_id'],'one')=="yes"){						
					echo "<td class=\"center\"><a href=\"edit_all_messages.php?revoke_id={$row['message_id']}\" onclick=\"return checked();\"><img alt=\"Activate Revoke\" src=\"images/revoke.png\" title=\"Αποτρέψτε τις συσκεύες από το να διαβάσουν το μήνυμα!\" /></a></td>";
				}
				else{
					echo "<td class=\"center\"><a href=\"edit_all_messages.php?revoke_id={$row['message_id']}\" onclick=\"return checked2();\"><img alt=\"Stop Revoke\" src=\"images/tick.png\" title=\"Επιτρέψτε στις συσκεύες να διαβάσουν το μήνυμα!\" /></a></td>";						
				}
			}
			else echo "<td></td>";
			echo "
			<td class=\"center\"><a href=\"edit_all_messages.php?forward_id={$row['message_id']}&amp;step=1\"><img alt=\"Forward\" src=\"images/fw.png\" title=\"Κάντε κλικ για να προωθήσετε το μήνυμα!\"/></a></td>
			<td class=\"center\"><a href=\"edit_all_messages.php?edit_id={$row['message_id']}\"><img alt=\"Edit\" src=\"images/icon-edit.png\" title=\"Κάντε κλικ για να επεξεργαστείτε το μήνυμα!\" /></a></td>
			<td class=\"center\"><a href=\"edit_all_messages.php?delete_id={$row['message_id']}\" onclick=\"return deletechecked();\"><img alt=\"Delete\" src=\"images/icon-delete.png\" title=\"Κάντε κλικ για να διαγράψετε το μήνυμα!\" /></a></td>
		</tr>";
	}
}	
echo '
	</tbody>';
$pagedResults->setLayout(new DoubleBarLayout());
echo '
	<tfoot>
		<tr>
			<td colspan="8">';
			if(sizeof($_SESSION['edit_all_messages'])>5) echo $pagedResults->fetchPagedNavigation();
			echo '</td>
		</tr>
	</tfoot>
</table>';
}
else echo '<br /><div id="msg_error" class="more_left_2">Δεν βρέθηκαν αποτελέσματα!</div>';
}
?>