<?php

	define('IN_HUGLO', true);
	require_once('./../includes/constants.php');
	require_once(ROOT_PATH . 'includes/db.php');
	require_once(ROOT_PATH . 'cp/includes/sessions.php');
	
	
	function get($_s){		return isset($_GET[$_s] )  && $_GET[$_s] ? $_GET[$_s] : '' ;	}
	function post($_s){	return isset($_POST[$_s] )  && $_POST[$_s] ? trim($_POST[$_s]) : '' ;	} // IMPORTANT TRIM
	function c($_s){ echo $_s;}
	function v2txt($str){ return  "'".sqlite_escape_string(str_replace("\\'","'",$str))."'" ;}
	function p($_s){ echo "<pre>"  ;var_dump($_s) ; echo '</pre>'; }
	
	$db	= new db();
	$db->sql_connect(SQL_HOST, SQL_USER, SQL_PASSWD, SQL_BASE);
	
	
	$error = array();
	
	//defaults
	$current = array('clients_id'=>0,"clients_login"=>'','clients_passwd'=>'','clients_compagny'=>'','clients_url'=>'http://') ;
	
	
	$id 		= (int)get('clients_id') ;
	$action = get('action') ;
	
	if ($action=="delete" && $id > 0){
		$sql = 'DELETE FROM site_clients WHERE clients_id = ' . $id ;
		if(mysql_query($sql)) {
			$error[] = 'L\'utilisateur a bien été supprimé.';
		}
		$id = 0 ;
	}
	
	// actions handler
	if(post('content_submit')) {	
		if ($id>0){	//update
			$sql = 'UPDATE site_clients SET ' . $db->sql_insert_update('UPDATE', array(
				'clients_login'		=>	post('clients_login'),
				'clients_passwd'		=>	post('clients_passwd'),
				'clients_compagny'			=>	post('clients_compagny') ,
				'clients_url'			=>	post('clients_url')
			)) . ' WHERE clients_id =  ' . $id;
			if(mysql_query($sql)) {
				$error[] = 'L\'utilisateur a bien été modifié.';
			}			
		}else{//insert	
			$sql = 'INSERT INTO site_clients  ' . $db->sql_insert_update('INSERT', array(
				'clients_login'		=>	post('clients_login'),
				'clients_passwd'		=>	post('clients_passwd'),
				'clients_compagny'			=>	post('clients_compagny') ,
				'clients_url'			=>	post('clients_url')
			)) ;		
			if(mysql_query($sql)) {
				$error[] = 'L\'utilisateur a bien été ajouté.';
			}
		}
	}
	
	//init current row
	if ($id>0){
		$sql = 'SELECT * FROM site_clients WHERE clients_id = ' . $id ;
		$result = mysql_query($sql);
		$_current = mysql_fetch_assoc($result);
		if (! $_current) {
			$id = 0 ;	
		}else{
			$current = $_current ;
		}
	}
	
	
	// get list
	$result = mysql_query("SELECT * FROM site_clients ORDER BY clients_login") ;	
	$out = array();
	while ($row = mysql_fetch_assoc($result)) {
		$out[] = $row;
	}
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Huglo Lepage &bull; Administration</title>
<link href="global.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php include('menu.php'); ?>
<?php

	if(count($error) > 0) {

?>
<br />
<table class="green">
	<tr>
		<td><?php echo implode($error, '<br />'); ?></td>
	</tr>
</table>
<?php

	}

?>


<? if ($id == 0 && $action == "" ){ ?>
<h1>Utilisateurs</h1>
<div class="bggrey">
<a href="?action=add&clients_id=0">+ Ajouter un utilisateur</a>
</div>
<table>
<? foreach($out as $r){ ?>
<tr>
	<td><? c($r["clients_login"]) ;?></td>
	<td><? c($r["clients_passwd"]) ;?></td>
	<td><? c($r["clients_compagny"]) ;?></td>
	<td><? c($r["clients_url"]) ;?></td>
    <td><a href="?action=edit&clients_id=<? c($r["clients_id"]) ;?> ">Modifier</a></td>
    <td><a href="?action=delete&clients_id=<? c($r["clients_id"]) ;?>" 
    onclick=" return confirm('Êtes-vous sûr de vouloir supprimer définitivement ?')">Supprimer</a></td>
    </tr>
<? } ?>
</table>
<? }else{ ?>
	<h1><a href="?">RETOUR A LA LISTE</a></h1>
<? } ?>
<?php if ($id > 0 || $id == 0 && $action != ""){ ?>

<form name="content_form" action="?clients_id=<? c($id) ; ?>" method="post">
	<table>
		<tr>
			<td width="30%" class="bggrey">Accès</td>
			<td width="70%"><input name="clients_login"  id="clients_login" 
            value="<?php echo $current['clients_login']; ?>"  style="width:80%" /></td>
				
		</tr>
        <tr>
			<td width="30%" class="bggrey">Mot de passe</td>
			<td width="70%"><input name="clients_passwd"  id="clients_passwd"
             value="<?php echo $current['clients_passwd']; ?>"  style="width:80%" /></td>
				
		</tr>
        <tr>
			<td width="30%" class="bggrey">Société</td>
			<td width="70%"><input name="clients_compagny"  id="clients_compagny" 
            value="<?php echo $current['clients_compagny']; ?>"  style="width:80%" /></td>
				
		</tr>
        <tr>
			<td width="30%" class="bggrey">Url</td>
			<td width="70%"><input name="clients_url"  id="clients_url" 
            value="<?php echo $current['clients_url']; ?>" style="width:80%" /></td>
				
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td><input type="submit" name="content_submit" id="content_submit" 
            value="Valider" class="bouton" /></td>
		</tr>
	</table>
</form>
<?php  }  ?>
</body>
</html>
<?php

	unset($dates, $error);
	$db->sql_close();
	
?>