<?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);
	
	$commits_id = isset($_GET['commits_id']) ? (is_numeric($_GET['commits_id']) ? $_GET['commits_id'] : 0) : 0;
	
	$commits = $error = array();
	
	if(isset($_POST['commits_submit']) and isset($_POST['commits_del']) and $commits_id != 0) {
		$sql = 'DELETE FROM ' . COMMITS_TABLE . ' WHERE commits_id = ' . $commits_id . ' LIMIT 1';
		if(mysql_query($sql)) {
			$error[] = 'L\'engnagement a bien été supprimé.';
		}
	} elseif(isset($_POST['commits_submit']) and $commits_id != 0) {
		$commits_name_en = trim($_POST['commits_name_en']);
		$commits_text_en = trim($_POST['commits_text_en']);
		$sql = 'UPDATE ' . COMMITS_TABLE . ' SET ' . $db->sql_insert_update('UPDATE', array(
			'commits_name_fr'	=>	(string) $_POST['commits_name_fr'],
			'commits_name_en'	=>	(string) empty($commits_name_en) ? NULL : $commits_name_en,
			'commits_text_fr'	=>	(string) $_POST['commits_text_fr'],
			'commits_text_en'	=>	(string) empty($commits_text_en) ? NULL : $commits_text_en,
			'commits_sort'		=>	(int) $_POST['commits_sort']
		)) . ' WHERE commits_id = ' . $commits_id;
		if(mysql_query($sql)) {
			$error[] = 'L\'engagement a bien été mise à jour.';
		}
	} elseif(isset($_POST['commits_submit']) and $commits_id == 0) {
		$commits_name_en = trim($_POST['commits_name_en']);
		$commits_text_en = trim($_POST['commits_text_en']);
		$sql = 'INSERT INTO ' . COMMITS_TABLE . $db->sql_insert_update('INSERT', array(
			'commits_name_fr'	=>	(string) $_POST['commits_name_fr'],
			'commits_name_en'	=>	(string) empty($commits_name_en) ? NULL : $commits_name_en,
			'commits_text_fr'	=>	(string) $_POST['commits_text_fr'],
			'commits_text_en'	=>	(string) empty($commits_text_en) ? NULL : $commits_text_en,
			'commits_sort'		=>	(int) $_POST['commits_sort']
		));
		if(mysql_query($sql)) {
			$error[] = 'L\'engagement a bien été rentré.';
		}
	}
	
	if(isset($_POST['content_submit'])) {
		$content_en = trim($_POST['content_en']);
		$sql = 'UPDATE ' . CONTENT_TABLE . ' SET ' . $db->sql_insert_update('UPDATE', array(
			'content_text_fr'	=>	(string) trim($_POST['content_fr']),
			'content_text_en'	=>	(string) empty($content_en) ? NULL : $content_en
		)) . ' WHERE content_id = 2';
		if(mysql_query($sql)) {
			$error[] = 'Mise à jour effectuée.';
		}
	}
	
	if($commits_id) {
		$sql = 'SELECT * FROM ' . COMMITS_TABLE . ' WHERE commits_id = ' . $commits_id;
		$result = mysql_query($sql);
		$commit = mysql_fetch_assoc($result);
		
		$commits_name_fr = $commit['commits_name_fr'];
		$commits_name_en = $commit['commits_name_en'];
		$commits_text_fr = $commit['commits_text_fr'];
		$commits_text_en = $commit['commits_text_en'];
		$commits_sort = $commit['commits_sort'];
	} else {
		$commits_name_fr = '';
		$commits_name_en = '';
		$commits_text_fr = '';
		$commits_text_en = '';
		$commits_sort = '';
	}
	
	$sql = 'SELECT * FROM ' . CONTENT_TABLE . ' WHERE content_id = 2';
	$result = mysql_query($sql);
	$content = mysql_fetch_assoc($result);

	$sql = 'SELECT * FROM ' . COMMITS_TABLE . ' ORDER BY commits_sort ASC';
	$result = mysql_query($sql);
	while($row = mysql_fetch_assoc($result)) {
		$commits[$row['commits_id']] = array(
			'commits_name_fr'	=>	$row['commits_name_fr'],
			'commits_name_en'	=>	$row['commits_name_en'],
			'commits_text_fr'	=>	$row['commits_text_fr'],
			'commits_text_en'	=>	$row['commits_text_en'],
			'commits_sort'		=>	$row['commits_sort']
		);
	}
	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_fr','content_en','commits_text_fr','commits_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 éthique</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_fr" rows="10" id="content_fr"><?php echo $content['content_text_fr']; ?></textarea></td>
					<td><textarea name="content_en" rows="10" id="content_en"><?php echo $content['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 engagements</h1>
<form name="commits_edit_form" action="" method="get">
<table>
	<tr>
		<td width="30%" class="bggrey">Sélectionner une l'action</td>
		<td width="70%">
			<select name="commits_id" id="commits_id" onchange="this.form.submit();">
				<option value="0">Sélectionner</option>
				<option value="0">Ajouter un engagement</option>
<?php

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

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

	}

?>
			</select>
		</td>
	</tr>
</table>
</form>
<form name="commits_form" action="" method="post">
	<table>
		<tr>
			<td width="30%" class="bggrey">Titre de l'engagement</td>
			<td width="70%"><table>
				<tr class="bggrey">
					<td>Français</td>
					<td>Anglais</td>
				</tr>
				<tr>
					<td width="50%"><input name="commits_name_fr" type="text" id="commits_name_fr" value="<?php echo $commits_name_fr; ?>" /></td>
					<td width="50%"><input name="commits_name_en" type="text" id="commits_name_en" value="<?php echo $commits_name_en; ?>" /></td>
				</tr>
			</table></td>
		</tr>
		<tr>
			<td class="bggrey">Texte</td>
			<td><table>
				<tr>
					<td width="50%"><textarea name="commits_text_fr" rows="10" id="commits_text_fr"><?php echo $commits_text_fr; ?></textarea></td>
					<td width="50%"><textarea name="commits_text_en" rows="10" id="commits_text_en"><?php echo $commits_text_en; ?></textarea></td>
				</tr>
			</table></td>
		</tr>
		<tr>
			<td class="bggrey">Ordre</td>
			<td><input name="commits_sort" type="text" id="commits_sort" value="<?php echo $commits_sort; ?>" /></td>
		</tr>
<?php

	if($commits_id) {

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

	}

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

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