<?php
/*******************************************************************************
 *                    _ _ _ _
 *                   / _ _  /
 *            _ _   / /__/ /_ _   ____ _ _   _ _ _    __
 *           / _ ` /   _ _// _ ` / __// _ ` / _ \\ \ / /
 *          / _ _// /\ \  / _ _/ \_ \/ _ _// / \/ \ ` /
 *          \_ , /_/  \_\ \_ ,  /__ /\_ , /_/      \_/
 *
 *******************************************************************************
 *    Application:  eReserv
 *    Version:      6
 *    Author:       David Di Certo
 *    Website:      http://www.cogites.com
 *    Email:        contact@cogites.com
 *    Copyright:    2013 - David Di Certo
 *******************************************************************************
 *    == FR ==      La revente et la redistribution de l'application sont
 *                  strictement interdites.
 *
 *                  Vous êtes tenu d'avoir pris connaissance des
 *                  Conditions Générales de Vente disponibles sur Cogites.com
 *                  (y compris pour la version Free).
 *
 *    == EN ==      Resale and redistribution of the application are
 *                  Strictly prohibited.
 *
 *                  You are required to have read the Terms and Conditions
 *                  available on Cogites.com (including the Free version).
 *
 *******************************************************************************
*/

	require "../_container/container_top.php";
	include_once ('business/UserMgr.php');
	include_once ('business/MailMgr.php');

	$userMgr = new UserMgr();
	$mailMgr = new MailMgr();

	$query = getPost('query');
	$action = getPost('action');
	$userid = getPost('userid');

	if (!empty($action) && ($action == "add" || $action == "update" || $action == "DELETE_USER" || $action == "disable" || $action == "CHANGE_PASSWORD")) {
		Logger::info('BEGIN: [' . $action . ']');
		if ($action == "add") {
			$user = array();
			$user['username'] = getPost('inputName');
			$user['email'] = getPost('inputEmail');
			$user['comment'] = getPost('inputComment');
			$user['password'] = getPost('inputPassword1');
			$res = $userMgr->insert($user);
			if ($res > 0) {
				$msgType = "success";
				$msg = $tr->translate("msgSuccess");
			}else{
				$msgType = "error";
				$msg = $tr->translate("msgError");
			}
		}
		if ($action == "update") {
			$user = array();
			$user['username'] = getPost('inputName');
			$user['email'] = getPost('inputEmail');
			$user['comment'] = getPost('inputComment');
			$user['user_id'] = $userid;
			$res = $userMgr->update($user);
			if ($res == 1) {
				$msgType = "success";
				$msg = $tr->translate("msgSuccess");
				$session->userEmail = getPost('inputEmail');
			}else{
				$msgType = "error";
				$msg = $tr->translate("msgError");
			}
		}
		if ($action == "CHANGE_PASSWORD") {
			$res = $userMgr->changePassword($userid, getPost('inputPassword1'));
			if ($res == 1) {
				$msgType = "success";
				$msg = $tr->translate("msgSuccess");
				$mail[] = new MailVo();
				$mail[0]->from = $config['renter_email'];
				$mail[0]->to = getPost('inputEmail');
				$mail[0]->subject = "eReserv6: nouveau mot de passe";
				$mail[0]->content = "Votre mot de passe pour accéder à eReserv a été changé.<br><br>";
				$mail[0]->content .= "Votre Login: " . getPost('inputName') . "<br>";
				include_once ('config/Mailer.php');
				$m = new Mailer($config);
				$r = $m->sendMail($mail);
				if (!$r) {
					$msgType = "error";
					$msg = $tr->translate("msgMailError");
				}
			}else{
				$msgType = "error";
				$msg = $tr->translate("msgError");
			}
		}
		if ($action == "DELETE_USER") {
			if ($userid !== $session->userId){
				$res = $userMgr->delete($userid);
				if ($res == 1) {
					$msgType = "success";
					$msg = $tr->translate("msgSuccess");
				}else{
					$msgType = "error";
					$msg = $tr->translate("msgError");
				}
			}else{
				$msgType = "error";
				$msg = $tr->translate("msgDeleteOwnUser");
			}
		}
		if ($action == "disable") {
			$user = array();
			$user['user_id'] = $userid;
			$user['enable'] = false;
			$userMgr->update($user);
		}
		Logger::info('END: [' . $action . ']');
		echo '<div class="alert alert-' . $msgType . '">
			<strong>' . $msg . '</strong>
			</div>';
	}
	
	$arrUser = $userMgr->getUsers();

?>
	<div>
		<ul class="breadcrumb">
			<li><a href="./"><?php echo $tr->translate("menuHome"); ?></a> <span class="divider">/</span></li>
			<li class="active"><?php echo $tr->translate("menuUsers"); ?></li>
		</ul>
	</div>
	<div class="sortable row-fluid">
		<a data-rel="tooltip" title="<?php echo $tr->translate('AddReserv'); ?>" class="well span2 top-block btn-action" href="#" name="newReservation" id="newReservation" _page="reservationDetail.php" _action="Add">
			<i class="icon-red icon-user"></i>
			<div><?php echo $tr->translate("AddReserv"); ?></div>
			<div></div>
		</a>
		<a data-rel="tooltip" title="<?php echo $tr->translate("AddUser"); ?>" class="well span2 top-block btn-action" href="#" name="newUser" id="newUser" _page="userDetail.php" _action="Add">
			<span class="icon32 icon-red icon-user"></span>
			<div><?php echo $tr->translate("AddUser"); ?></div>
		</a>
		<a data-rel="tooltip" title="<?php echo $tr->translate("AddPayment"); ?>" class="well span2 top-block btn-action" href="#" name="newPayment" id="newPayment" _page="paymentDetail.php" _action="Add">
			<i class="icon-cog"></i>
			<div><?php echo $tr->translate("AddPayment"); ?></div>
			<div></div>
		</a>
	</div>
			
	<div class="row-fluid sortable">
		<div class="box span12">
			<div class="box-header well" data-original-title>
				<h2><i class="icon-user"></i> <?php echo $tr->translate("Users"); ?></h2>
			</div>
			<div class="box-content">
			<form class="form-horizontal" method="post" name="userForm" id="userForm" action="">
				<input type="hidden" name="action" id="action" value="" />
				<input type="hidden" name="userid" id="userid" value="" />

				<div class="table-responsive">
				<table id="tableUsers" name="tableUsers" class="table table-condensed table-striped table-bordered bootstrap-datatable datatable">
				  <thead>
					  <tr>
						  <th><?php echo $tr->translate("thUserName"); ?></th>
						  <th><?php echo $tr->translate("thUserLastConnect"); ?></th>
						  <th><?php echo $tr->translate("thUserEmail"); ?></th>
						  <th><?php echo $tr->translate("thUserComment"); ?></th>
						  <th><?php echo $tr->translate("thUserEnabled"); ?></th>
						  <th><?php echo $tr->translate("thUserAction"); ?></th>
					  </tr>
				  </thead>   
				  <tbody>
				  <?php
					foreach ($arrUser as $user) {
				  ?>
					<tr>
						<td><?php echo $user->username; ?></td>
						<td><?php echo $user->lastconnectdate; ?></td>
						<td>
						<?php if ($user->email) { ?>
							<a class="icon-envelope" href="mailto:<?php echo $user->email; ?>"></a>
						<?php } ?>
						<?php echo $user->email; ?></td>
						<td><?php echo $user->comment; ?></td>
						<td>
							<span class="label label-<?php if ($user->enable) { echo "success";}else{ echo "important"; }?>"><?php echo $user->enable; ?></span>
						</td>
						<td>
							<a class="btn btn-info btn-mini btn-action" title="<?php echo $tr->translate("tipUserEdit"); ?>" data-rel="tooltip" href="#" _id="<?php echo $user->user_id; ?>" _page="userDetail.php" _action="<?php echo $tr->translate("Edit"); ?>">
								<i class="icon-edit icon-white"></i>
							</a>
							<a class="btn btn-danger btn-mini" title="<?php echo $tr->translate("tipUserDelete"); ?>" data-rel="tooltip" href="#" onclick="goAction('DELETE_USER', <?php echo $user->user_id; ?>);">
								<i class="icon-trash icon-white"></i>
							</a>
						</td>
					</tr>
					<?php } ?>
				  </tbody>
			  </table>
			  </div>
			</div>
		</div><!--/span-->
	
	</div><!--/row-->

	<div class="row-fluid sortable" id="editUser">
	</div><!--/row-->
<?php require "../_container/container_bottom.php"; ?>
<script>
	function deleteAction() {
		blockUI();
		userForm.submit();
	}

	function goAction(action, uid){
		if (action == "DELETE_USER"){
			$("#action").val(action);
			$("#userid").val(uid);
			$("#dialog-confirm").dialog("open");
		}
	}
</script>
