<?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');
	
	$db	= new db();
	$db->sql_connect(SQL_HOST, SQL_USER, SQL_PASSWD, SQL_BASE);
	
	$meet_id = isset($_GET['meet_id']) ? (is_numeric($_GET['meet_id']) ? $_GET['meet_id'] : 0) : 0;
	
	$meets = $error = array();

	if(isset($_POST['meet_submit']) and isset($_POST['meet_del']) and $meet_id != 0) {
		$sql = 'DELETE FROM ' . MEET_TABLE . ' WHERE meet_id = ' . $meet_id . ' LIMIT 1';
		if(mysql_query($sql)) {
			$error[] = 'L\'adresse a bien été supprimé.';
		}
	} elseif(isset($_POST['meet_submit']) and $meet_id != 0) {
		$meet_title_en = trim($_POST['meet_title_en']);
		$meet_text_en = trim($_POST['meet_text_en']);
		$sql = 'UPDATE ' . MEET_TABLE . ' SET ' . $db->sql_insert_update('UPDATE', array(
			'meet_title_fr'	=>	(string) trim($_POST['meet_title_fr']),
			'meet_title_en'	=>	(string) empty($meet_title_en) ? NULL : $meet_title_en,
			'meet_text_fr'	=>	(string) trim($_POST['meet_text_fr']),
			'meet_text_en'	=>	(string) empty($meet_text_en) ? NULL : $meet_text_en,
			'meet_sort'		=>	(int) $_POST['meet_sort']
		)) . ' WHERE meet_id = ' . $meet_id;
		if(mysql_query($sql)) {
			$error[] = 'L\'adresse a bien été mise à jour.';
		}
	} elseif(isset($_POST['meet_submit']) and $meet_id == 0) {
		$meet_title_en = trim($_POST['meet_title_en']);
		$meet_text_en = trim($_POST['meet_text_en']);
		$sql = 'INSERT INTO ' . MEET_TABLE . $db->sql_insert_update('INSERT', array(
			'meet_title_fr'	=>	(string) trim($_POST['meet_title_fr']),
			'meet_title_en'	=>	(string) empty($meet_title_en) ? NULL : $meet_title_en,
			'meet_text_fr'	=>	(string) trim($_POST['meet_text_fr']),
			'meet_text_en'	=>	(string) empty($meet_text_en) ? NULL : $meet_text_en,
			'meet_sort'		=>	(int) $_POST['meet_sort']
		));
		if(mysql_query($sql)) {
			$error[] = 'L\'adresse a bien été rentré.';
		}
	}

	if($meet_id) {
		$sql = 'SELECT * FROM ' . MEET_TABLE . ' WHERE meet_id = ' . $meet_id;
		$result = mysql_query($sql);
		$meet = mysql_fetch_assoc($result);
		
		$meet_title_fr = $meet['meet_title_fr'];
		$meet_title_en = $meet['meet_title_en'];
		$meet_text_fr = $meet['meet_text_fr'];
		$meet_text_en = $meet['meet_text_en'];
		$meet_sort = $meet['meet_sort'];
	} else {
		$meet_title_fr = '';
		$meet_title_en = '';
		$meet_text_fr = '';
		$meet_text_en = '';
		$meet_sort = '';
	}

	$sql = 'SELECT * FROM ' . MEET_TABLE . ' ORDER BY meet_sort ASC';
	$result = mysql_query($sql);
	while($row = mysql_fetch_assoc($result)) {
		$meets[$row['meet_id']] = array(
			'meet_title_fr'	=>	$row['meet_title_fr'],
		);
	}
	mysql_free_result($result);
	
?>
<!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" />
<script type="text/javascript">
	_editor_url  = "xinha/" 
	_editor_lang = "fr";
	_editor_skin = "blue-look";
</script>
<script type="text/javascript" src="xinha/XinhaCore.js"></script>
<script type="text/javascript">
function xinha_init() {
	var xinha_plugins = [];
	var xinha_editors =['meet_text_fr','meet_text_en'];
	if(!Xinha.loadPlugins(xinha_plugins, xinha_init)) return;
	var xinha_config = new Xinha.Config();
	xinha_config.showLoading   = true;
	xinha_config.height  = 300;
	xinha_config.width  = '99%';
	xinha_config.toolbar = [["htmlmode", "separator", "bold", "italic", "underline", "forecolor", "fontsize", "separator", "createlink", "separator", "killword", "clearfonts", "removeformat", "separator", "undo", "redo"]];
	xinha_config.pageStyle = '<?php include('xinha.css'); ?>';
	xinha_editors = Xinha.makeEditors(xinha_editors, xinha_config, xinha_plugins);
	Xinha.startEditors(xinha_editors);
}
Xinha.addOnloadHandler(xinha_init);
</script>
</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

	}

?>
<h1>Nous contacter</h1>
<h2>Gestion des adresses</h2>
<form name="meet_edit_form" action="" method="get">
<table>
	<tr>
		<td width="30%" class="bggrey">Sélectionner une l'action</td>
		<td width="70%">
			<select name="meet_id" id="meet_id" onchange="this.form.submit();">
				<option value="0">Sélectionner</option>
				<option value="0">Ajouter une adresse</option>
<?php

	foreach($meets as $key => $value) {

?>
				<option value="<?php echo $key; ?>"<?php echo (($key == $meet_id) ? ' selected="seletected"' : ''); ?>><?php echo $value['meet_title_fr']; ?></option>
<?php

	}

?>
			</select>
		</td>
	</tr>
</table>
</form>
<form name="meet_form" action="" method="post">
	<table>
		<tr>
			<td width="30%" class="bggrey">Ville</td>
			<td width="70%"><table>
				<tr class="bggrey">
					<td>Français</td>
					<td>Anglais</td>
				</tr>
				<tr>
					<td width="50%"><input name="meet_title_fr" type="text" id="meet_title_fr" value="<?php echo $meet_title_fr; ?>" /></td>
					<td width="50%"><input name="meet_title_en" type="text" id="meet_title_en" value="<?php echo $meet_title_en; ?>" /></td>
				</tr>
			</table></td>
		</tr>
		<tr>
			<td class="bggrey">Texte</td>
			<td><table>
				<tr>
					<td width="50%"><textarea name="meet_text_fr" rows="10" id="meet_text_fr"><?php echo $meet_text_fr; ?></textarea></td>
					<td width="50%"><textarea name="meet_text_en" rows="10" id="meet_text_en"><?php echo $meet_text_en; ?></textarea></td>
				</tr>
			</table></td>
		</tr>
		<tr>
			<td class="bggrey">Ordre</td>
			<td><input name="meet_sort" type="text" id="meet_sort" value="<?php echo $meet_sort; ?>" /></td>
		</tr>
<?php

	if($meet_id) {

?>
		<tr>
			<td class="bggrey">Supprimer cette adresse</td>
			<td><input type="checkbox" name="meet_del" id="meet_del" /></td>
		</tr>
<?php

	}

?>
		<tr>
			<td><input type="submit" name="meet_submit" id="meet_submit" value="Valider" class="bouton" /></td>
			<td>&nbsp;</td>
		</tr>
	</table>
</form>
</body>
</html>
<?php

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