<?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);
	
	$history_dates_id = isset($_GET['history_dates_id']) ? (is_numeric($_GET['history_dates_id']) ? $_GET['history_dates_id'] : 0) : 0;
	
	$dates = $error = array();
	
	if(isset($_POST['history_submit']) and isset($_POST['history_dates_del']) and $history_dates_id != 0) {
		$sql = 'DELETE FROM ' . HISTORY_DATES_TABLE . ' WHERE history_dates_id = ' . $history_dates_id . ' LIMIT 1';
		if(mysql_query($sql)) {
			$error[] = 'La date-clef a bien été supprimé.';
		}
	} elseif(isset($_POST['history_submit']) and $history_dates_id != 0) {
		$history_dates_text_en = trim($_POST['history_dates_text_en']);
		$sql = 'UPDATE ' . HISTORY_DATES_TABLE . ' SET ' . $db->sql_insert_update('UPDATE', array(
			'history_dates_date'	=>	(int) $_POST['history_dates_date'],
			'history_dates_text_fr'	=>	(string) $_POST['history_dates_text_fr'],
			'history_dates_text_en'	=>	(string) empty($history_dates_text_en) ? NULL : $history_dates_text_en
		)) . ' WHERE history_dates_id = ' . $history_dates_id;
		if(mysql_query($sql)) {
			$error[] = 'La date-clef a bien été mise à jour.';
		}
	} elseif(isset($_POST['history_submit']) and $history_dates_id == 0) {
		$history_dates_text_en = trim($_POST['history_dates_text_en']);
		$sql = 'INSERT INTO ' . HISTORY_DATES_TABLE . $db->sql_insert_update('INSERT', array(
			'history_dates_date'	=>	(int) $_POST['history_dates_date'],
			'history_dates_text_fr'	=>	(string) $_POST['history_dates_text_fr'],
			'history_dates_text_en'	=>	(string) empty($history_dates_text_en) ? NULL : $history_dates_text_en
		));
		if(mysql_query($sql)) {
			$error[] = 'La date-clef a bien été rentré.';
		}
	}
	
	if(isset($_POST['content_submit'])) {
		$content_text_en = trim($_POST['content_text_en']);
		$sql = 'UPDATE ' . CONTENT_TABLE . ' SET ' . $db->sql_insert_update('UPDATE', array(
			'content_text_fr'	=>	(string) trim($_POST['content_text_fr']),
			'content_text_en'	=>	(string) empty($content_text_en) ? NULL : $content_text_en
		)) . ' WHERE content_id = 1';
		if(mysql_query($sql)) {
			$error[] = 'Mise à jour effectuée.';
		}
	}
	
	if($history_dates_id) {
		$sql = 'SELECT * FROM ' . HISTORY_DATES_TABLE . ' WHERE history_dates_id = ' . $history_dates_id;
		$result = mysql_query($sql);
		$date = mysql_fetch_assoc($result);
		
		$history_dates_date = $date['history_dates_date'];
		$history_dates_text_fr = $date['history_dates_text_fr'];
		$history_dates_text_en = $date['history_dates_text_en'];
	} else {
		$history_dates_date = '';
		$history_dates_text_fr = '';
		$history_dates_text_en = '';
	}
	
	$sql = 'SELECT * FROM ' . CONTENT_TABLE . ' WHERE content_id = 1';
	$result = mysql_query($sql);
	$histoire = mysql_fetch_assoc($result);

	$sql = 'SELECT * FROM ' . HISTORY_DATES_TABLE . ' ORDER BY history_dates_date ASC';
	$result = mysql_query($sql);
	while($row = mysql_fetch_assoc($result)) {
		$dates[$row['history_dates_id']] = array(
			'history_dates_date'		=>	$row['history_dates_date'],
			'history_dates_text_fr '	=>	$row['history_dates_text_fr'],
			'history_dates_text_en '	=>	$row['history_dates_text_en'],
		);
	}
	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 = ['content_text_fr','content_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>Notre histoire</h1>
<form name="content_form" action="" method="post">
	<table>
		<tr>
			<td width="30%" class="bggrey">Contenu</td>
			<td width="70%"><table>
				<tr class="bggrey">
					<td width="50%">Français</td>
					<td width="50%">Anglais</td>
				</tr>
				<tr>
					<td><textarea name="content_text_fr" rows="10" id="content_text_fr"><?php echo $histoire['content_text_fr']; ?></textarea></td>
					<td><textarea name="content_text_en" rows="10" id="content_text_en"><?php echo $histoire['content_text_en']; ?></textarea></td>
				</tr>
			</table></td>
		</tr>
		<tr>
			<td><input type="submit" name="content_submit" id="content_submit" value="Valider" class="bouton" /></td>
			<td>&nbsp;</td>
		</tr>
	</table>
</form>
<h1>Les dates-clefs</h1>
<form name="date_edit_form" action="" method="get">
<table>
	<tr>
		<td width="30%" class="bggrey">Sélectionner une l'action</td>
		<td width="70%">
			<select name="history_dates_id" id="history_dates_id" onchange="this.form.submit();">
				<option value="0">Sélectionner</option>
				<option value="0">Ajouter une date clef</option>
<?php

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

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

	}

?>
			</select>
		</td>
	</tr>
</table>
</form>
<form name="date_form" action="" method="post">
	<table>
		<tr>
			<td width="30%" class="bggrey">Date</td>
			<td width="70%"><input name="history_dates_date" type="text" id="history_dates_date" value="<?php echo $history_dates_date; ?>" /></td>
		</tr>
		<tr>
			<td class="bggrey">Texte</td>
			<td><table>
				<tr class="bggrey">
					<td>Français</td>
					<td>Anglais</td>
				</tr>
				<tr>
					<td width="50%"><textarea name="history_dates_text_fr" rows="10" id="history_dates_text_fr"><?php echo $history_dates_text_fr; ?></textarea></td>
					<td width="50%"><textarea name="history_dates_text_en" rows="10" id="history_dates_text_en"><?php echo $history_dates_text_en; ?></textarea></td>
				</tr>
			</table></td>
		</tr>
		<tr>
			<td class="bggrey">Supprimer cette date-clef</td>
			<td><input type="checkbox" name="history_dates_del" id="history_dates_del" /></td>
		</tr>
		<tr>
			<td><input type="submit" name="history_submit" id="history_submit" value="Valider" class="bouton" /></td>
			<td>&nbsp;</td>
		</tr>
	</table>
</form>
</body>
</html>
<?php

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