<?php
/*===================================================+
|| # HoloCMS - Website and Content Management System
|+===================================================+
|| # Copyright © 2008 Meth0d. All rights reserved.
|| # http://www.meth0d.org
|+===================================================+
|| # HoloCMS is provided "as is" and comes without
|| # warrenty of any kind. HoloCMS is free software!
|+===================================================+
|| # Staff App System by HandyAndy160 of RaGEZONE forum
|| # http://nipperhotel.tk
|+===================================================*/

require_once('../core.php');
if($hkzone !== true){ header("Location: index.php?throwBack=true"); exit; }
if(!session_is_registered(acp)){ header("Location: index.php?p=login"); exit; }

$pagename = "Gestisci le richieste per lo Staff";

if($do == "clear") {
	$column = $_GET['column'];
	if($column == "1")
	{
		mysql_query("UPDATE applications SET `appstatus` = '2'");
		header("location:index.php?p=application_manage");
	}
	if($column == "2")
	{
		mysql_query("UPDATE applications SET `appstatus` = '1'");
		header("location:index.php?p=application_manage");
	}
	else
	{
		header("location:index.php?p=application_manage");
	}
}

if($do == "delete" && is_numeric($key)){

	$check = mysql_query("SELECT id FROM applications WHERE id = '".$key."' LIMIT 1") or die(mysql_error());
	$exists = mysql_num_rows($check);

	if($exists > 0){
		mysql_query("DELETE FROM applications WHERE id = '".$key."' LIMIT 1") or die(mysql_error());
		$msg = "La richiesta è stata eliminata.";
	} else {
		$msg = "Non è stato possibile eliminare la richiesta. Richiesta non trovata";
	}

} elseif($do == "edit" && is_numeric($key)){

	$check = mysql_query("SELECT * FROM applications WHERE id = '".$key."' LIMIT 1") or die(mysql_error());
	$exists = mysql_num_rows($check);

	if($exists > 0){
		$rare = mysql_fetch_assoc($check);
		$editor_mode = true;
	} else {
		$msg = "Impossibile visualizzare la richiesta. Richiesta non trovata.";
	}

} elseif($do == "save" && is_numeric($key)){

		$check = mysql_query("SELECT id FROM applications WHERE id = '".$key."' LIMIT 1") or die(mysql_error());

	$exists = mysql_num_rows($check);

	if($exists > 0){

		$id = $key;

		$appstat = addslashes($_POST['appstatus']);

		$appuser = addslashes($_POST['1username']);
		$appuserid = addslashes($_POST['2username']);

		if($appstat != 0 && $appstat != 1 && $appstat != 2) { $appstat = 0; }
		if(!is_numeric($appstat)) { $appstat = 0; }
		if($appstat == 1) { 

		mysql_query("INSERT INTO cms_alerts (userid,alert,type) VALUES ('".$appuserid."','Salve.<br>La sua richiesta è stata rifiutata. Ti ringraziamo per aver richiesto, e speriamo in una tua buona posizione in hotel in futuro.<br>Grazie e divertiti in $sitename,<br>- $sitename Management','2')"); 
		mysql_query("UPDATE applications SET appstatus = '".$appstat."' WHERE id = '".$id."' LIMIT 1") or die(mysql_error());
		$msg = "RIFIUTATA. Lo status della richiesta è stato editato.
. Inviato un alert a $appuser !";
}
		elseif($appstat == 2) { 
		mysql_query("INSERT INTO cms_alerts (userid,alert,type) VALUES ('".$appuserid."','Salve.<br>La sua richiesta è stata accettata. Ora è un membro dello staff, complimenti! <br>Controlla la tua email, deve essere arrivata una email dall'admin dell'hotel.<br>Ancora una volta, Congratulazioni!<br>- $sitename Management','2')"); 
		mysql_query("UPDATE applications SET appstatus = '".$appstat."' WHERE id = '".$id."' LIMIT 1") or die(mysql_error());
		$msg = "ACCETTATA. Lo status della richiesta è stato editato. Inviato un alert a $appuser !";
}
		


	} else {

		$msg = "Error editing Application Status; Can't find selected Application.";

	
}

} 

@include('subheader.php');
@include('header.php');
?>
<table cellpadding='0' cellspacing='8' width='100%' id='tablewrap'>
<tr> <td width='22%' valign='top' id='leftblock'>
 <div>
 <!-- LEFT CONTEXT SENSITIVE MENU -->
<?php @include('sitemenu.php'); ?>
 <!-- / LEFT CONTEXT SENSITIVE MENU -->
 </div>
 </td>
 <td width='78%' valign='top' id='rightblock'>
 <div><!-- RIGHT CONTENT BLOCK -->

<?php if($editor_mode !== true){ ?>
<?php if(isset($msg)){ ?><p><strong><?php echo $msg; ?></strong></p><?php } ?>

<form action='index.php?p=application_manage&do=save' method='post' name='theAdminForm' id='theAdminForm'>
 <div class='tableborder'>
 <div class='tableheaderalt'>Richieste [<a href='index.php?p=application_manage&do=clear&column=1'>Accetta tutte</a>] [<a href='index.php?p=rare_manage&do=clear&column=2'>Rifiuta tutte</a>]</div>
 <table cellpadding='4' cellspacing='0' width='100%'>
 <tr>
  <td class='tablesubheader' width='1%' align='center'>ID</td>
  <td class='tablesubheader' width='28%'>User Name</td>
  <td class='tablesubheader' width='10%' align='center'>Status Richiesta</td>
  <td class='tablesubheader' width='10%' align='center'>Indirizzo IP</td>
  <td class='tablesubheader' width='10%' align='center'>Visualizza Richiesta</td>
  <td class='tablesubheader' width='12%' align='center'>Elimina Richiesta</td>
 </tr>
<?php
$get_rares = mysql_query("SELECT id,username,appstatus,age,country,timezone,realname,modname,time,experience,message1,message2,message3,users,visitoripaddy FROM applications ORDER BY appstatus DESC") or die(mysql_error());
while($row = mysql_fetch_assoc($get_rares)){

if($row['appstatus'] == 1) { $vs="Application Declined."; }
else if($row['appstatus'] == 2) { $vs="Application Accepted."; }
else if($row['appstatus'] == 0) { $vs="New Application!"; }




$info = $vs.$rs.$hc.".";

	printf(" <tr>
  <td class='tablerow1' align='center'>%s</td>
  <td class='tablerow2'><strong>%s</strong><div class='desctext'>%s</div></td>
  <td class='tablerow2' align='center'>%s</td>
  <td class='tablerow2' align='center'>%s</td>
  <td class='tablerow2' align='center'><a href='index.php?p=application_manage&do=edit&key=%s'><img src='./images/edit.gif' alt='View'></a></td>
  <td class='tablerow2' align='center'><a href='index.php?p=application_manage&do=delete&key=%s'><img src='./images/delete.gif' alt='Delete'></a></td>
</tr>", $row['id'], htmlspecialchars(stripslashes($row['username'])), htmlspecialchars($info), $row['appstatus'], "<b>".$row['visitoripaddy']."</b>", $row['id'], $row['id']);
}
?>

 </table>
<?php } else { ?>
<form action='index.php?p=application_manage&do=save&key=<?php echo $rare['id']; ?>' method='post' name='theAdminForm' id='theAdminForm'>
<div class='tableborder'>
<div class='tableheaderalt'>Richieste Visualizzate: <b>(<?php echo $rare['username']; ?>, App ID: <?php echo $rare['id']; ?>)</b></div>

<table width='100%' cellspacing='0' cellpadding='5' align='center' border='0'>
<tr>
<td class='tablerow1'  width='40%'  valign='middle'><b>User Name</b><div class='graytext'>L'username del richiedente.</div></td>
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name='1username' value="<?php echo $rare['username']; ?>"  size='30' maxlength='0' class='textinput'></td>
</tr>

<tr>
<td class='tablerow1'  width='40%'  valign='middle'><b>ID Utente</b><div class='graytext'>ID assegnato all'utente durante la registrazione.</div></td>
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name='2username' value="<?php echo $rare['userid']; ?>"  size='30' maxlength='0' class='textinput'></td>
</tr>


<tr>
<td class='tablerow1'  width='40%'  valign='middle'><b>Nome</b><div class='graytext'>Il nome reale del richiedente.</div></td>
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name='value' value="<?php echo $rare['realname']; ?>" size='30' maxlength='0 class='textinput'></td>
</tr>

<tr>
<td class='tablerow1'  width='40%'  valign='middle'><b>Nome con suffisso MOD-</b><div class='graytext'>The applicants prefered MOD- name.</div></td>
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name='valuestate' value="<?php echo $rare['modname']; ?>" size='30' maxlength='0'class='textinput'></td>
</tr>

<tr>
<td class='tablerow1'  width='40%'  valign='middle'><b>Time Zone</b><div class='graytext'>The applicants timezone.</div></td>
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name='releasestate' value="<?php echo $rare['timezone']; ?>" size='30' maxlength='0'class='textinput'></td>
</tr>

<tr>
<td class='tablerow1'  width='40%'  valign='middle'><b>Tempo che può dedicare all'hotel:</b><div class='graytext'>How much time per week that the applicant can dedicate to the Hotel.</div></td>
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name='category' value="<?php echo $rare['time']; ?>" size='30' maxlength='0'class='textinput'></td>
</tr>

<tr>
<td class='tablerow1'  width='40%'  valign='middle'><b>Età</b><div class='graytext'>The age of the applicant.</div></td>
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name='category' value="<?php echo $rare['age']; ?>" size='1' maxlength='0'class='textinput'></td>
</tr>

<tr>
<td class='tablerow1'  width='40%'  valign='middle'><b>Nazionalità</b><div class='graytext'>The country the applicant is located in.</div></td>
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name='category' value="<?php echo $rare['country']; ?>" size='30' maxlength='0'class='textinput'></td>
</tr>

<tr>
<td class='tablerow1'  width='40%'  valign='middle'><b>Vecchie Esperienze:</b><div class='graytext'>Past Experience that the applicant has had.</div></td>
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name='category' value="<?php echo $rare['experience']; ?>" size='100' maxlength='0'class='textinput'></td>
</tr>

<tr>
<td class='tablerow1'  width='40%'  valign='middle'><b>Cosa sa fare</b><div class='graytext'>Improvements & Suggestions that the applicant has for <?php echo $sitename; ?>. </div></td>
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name='category' value="<?php echo $rare['message1']; ?>" size='100' maxlength='0'class='textinput'></td>
</tr>

<tr>
<td class='tablerow1'  width='40%'  valign='middle'><b>Perchè vuole questo ruolo</b><div class='graytext'>Reasons why the applicant wants to become a staff member for <?php echo $sitename; ?>. </div></td>
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name='category' value="<?php echo $rare['message2']; ?>" size='100' maxlength='0'class='textinput'></td>
</tr>

<tr>
<td class='tablerow1'  width='40%'  valign='middle'><b>Altro (opzione facoltativa)</b><div class='graytext'>Extra stuff added to the applicants form.</div></td>
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name='category' value="<?php echo $rare['message3']; ?>" size='100' maxlength='0'class='textinput'></td>
</tr>

<tr>
<td class='tablerow1'  width='40%'  valign='middle'><b>Indirizzo IP</b><div class='graytext'>The applicants IP Address. </div></td>
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name='category' value="<?php echo $rare['visitoripaddy']; ?>" size='20' maxlength='0'class='textinput'></td>
</tr>

<tr>
<td class='tablerow1'  width='40%'  valign='middle'><b>Status della Richiesta</b><div class='graytext'>0 = Nuova Richiesta, 1 = Accettata, 2 = Declinata.<br>Edit this here, and press Save below.</div></td>
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name='appstatus' value="<?php echo $rare['appstatus']; ?>" size='1' maxlength='1' class='textinput'></td>
</tr>

<tr>
<tr><td align='center' class='tablesubheader' colspan='2' ><input type='submit' value='Save' class='realbutton' accesskey='s'></td></tr>
</form></table></div><br />
<?php } ?>
	 </div><!-- / RIGHT CONTENT BLOCK -->
	 </td></tr>
</table>
</div><!-- / OUTERDIV -->
<div align='center'><br />
<?php
$mtime = explode(' ', microtime());
$totaltime = $mtime[0] + $mtime[1] - $starttime;
printf('Time: %.3f', $totaltime);
?>
</div>