<?php

	define('IN_HUGLO', true);
	require_once('./../includes/constants.php');
	require_once(ROOT_PATH . 'includes/db.php');
	require_once(ROOT_PATH . 'language/fr.php');
	require_once(ROOT_PATH . 'cp/includes/sessions.php');
	
	$db	= new db();
	$db->sql_connect(SQL_HOST, SQL_USER, SQL_PASSWD, SQL_BASE);
	
	$years = isset($_POST['inter_edit_year']) ? (is_numeric($_POST['inter_edit_year']) ? $_POST['inter_edit_year'] : 0) : 0;
	$months = isset($_POST['inter_edit_month']) ? (is_numeric($_POST['inter_edit_month']) ? $_POST['inter_edit_month'] : 0) : 0;
	
	$year = $inter = $error = array();
	
	if(isset($_POST['inter_add_submit'])) {
		$inter_text_en = trim($_POST['inter_text_en']);
		$sql = 'INSERT INTO ' . INTER_TABLE . $db->sql_insert_update('INSERT', array(
			'inter_year'	=>	(int) $_POST['inter_year'],
			'inter_month'	=>	(int) $_POST['inter_month'],
			'inter_day'		=>	(string) $_POST['inter_day'],
			'inter_text_fr'	=>	(string) $_POST['inter_text_fr'],
			'inter_text_en'	=>	(string) empty($inter_text_en) ? NULL : $inter_text_en
		));
		if(mysql_query($sql)) {
			$error[] = 'Intervention ajouté';
		}
	}
	
	if(isset($_POST['inter_submit'])) {
		
		foreach($_POST['inter_month'] as $key => $value) {
			if(isset($_POST['inter_del'][$key])) {
				$sql = 'DELETE FROM ' . INTER_TABLE . ' WHERE inter_id = ' . $key . ' LIMIT 1';
				mysql_query($sql);
			} else {
				$inter_text_en = trim($_POST['inter_text_en'][$key]);
				$sql = 'UPDATE ' . INTER_TABLE . ' SET ' . $db->sql_insert_update('UPDATE', array(
					'inter_year'	=>	(int) $_POST['inter_year'][$key],
					'inter_month'	=>	(int) $_POST['inter_month'][$key],
					'inter_day'		=>	(string) $_POST['inter_day'][$key],
					'inter_text_fr'	=>	(string) $_POST['inter_text_fr'][$key],
					'inter_text_en'	=>	(string) empty($inter_text_en) ? NULL : $inter_text_en
				)) . ' WHERE inter_id = ' . $key;
				if(mysql_query($sql)) {
					
				}
			}

		}
	}
	
	$sql = 'SELECT DISTINCT inter_year FROM ' . INTER_TABLE . ' ORDER BY inter_year DESC';
	$result = mysql_query($sql);
	while($row = mysql_fetch_assoc($result)) {
		$year[] = $row['inter_year'];
	}
	mysql_free_result($result);
	
	if(isset($_POST['inter_edit_submit'])) {
		if(isset($_POST['inter_edit_submit_del'])) {
			$sql = 'DELETE FROM ' . INTER_TABLE . ' WHERE inter_year = ' . $years;
			mysql_query($sql);
		} else {
			$sql = 'SELECT * FROM ' . INTER_TABLE . ' WHERE inter_year = ' . $years .' AND inter_month = ' . $months .' ORDER BY inter_id ASC';
			$result = mysql_query($sql);
			while($row = mysql_fetch_assoc($result)) {
				$inter[$row['inter_id']] = array(
					'inter_year'	=>	$row['inter_year'],
					'inter_month'	=>	$row['inter_month'],
					'inter_day'		=>	$row['inter_day'],
					'inter_text_fr'	=>	$row['inter_text_fr'],
					'inter_text_en'	=>	$row['inter_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 = ['inter_text_fr','inter_text_en',
<?php

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

?>
		'inter_text_fr[<?php echo $key; ?>]',
		'inter_text_en[<?php echo $key; ?>]',
<?php

	}

?>
		'inter_text_fr','inter_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>Interventions et colloque</h1>
<h2>Ajouter une intervention</h2>
<form name="inter_add_form" action="" method="post">
<table>
	<tr>
		<td width="30%" class="bggrey">Année de l'intervention</td>
		<td width="70%"><input type="text" name="inter_year" id="inter_year" /></td>
	</tr>
	<tr>
		<td class="bggrey">Mois de l'intervention</td>
		<td>
			<select name="inter_month" id="inter_month">
<?php

	foreach($lang['MONTH'] as $key => $value) {

?>
				<option value="<?php echo $key; ?>"><?php echo $value; ?></option>
<?php

	}

?>
			</select>
		</td>
	</tr>
	<tr>
		<td class="bggrey">Jour(s) de l'intervention</td>
		<td><input type="text" name="inter_day" id="inter_day" /> Séparé les jours par une virgule : exemple 24, 25, 26</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="inter_text_fr" id="inter_text_fr" cols="45" rows="5"></textarea></td>
				<td width="50%"><textarea name="inter_text_en" id="inter_text_en" cols="45" rows="5"></textarea></td>
			</tr>
		</table></td>
	</tr>
	<tr>
		<td><input type="submit" name="inter_add_submit" id="inter_add_submit" value="Ajouter" class="bouton" /></td>
		<td>&nbsp;</td>
	</tr>
</table>
</form>
<h2>Editer les interventions</h2>
<form name="inter_edit_form" action="" method="post">
<table>
	<tr>
		<td width="30%" class="bggrey">Choisir une année</td>
		<td width="70%">
			<select name="inter_edit_year" id="inter_edit_year">
<?php

	foreach($year as $value) {

?>
				<option value="<?php echo $value; ?>"<?php echo ($value == $years) ? ' selected="selected"' : ''; ?>><?php echo $value; ?></option>
<?php

	}

?>
			</select>
		</td>
	</tr>
	<tr>
		<td class="bggrey">Choisir un mois</td>
		<td>
			<select name="inter_edit_month" id="inter_edit_month">
<?php

	foreach($lang['MONTH'] as $key => $value) {

?>
				<option value="<?php echo $key; ?>"<?php echo ($key == $months) ? ' selected="selected"' : ''; ?>><?php echo $value; ?></option>
<?php

	}

?>
			</select>
		</td>
	</tr>
	<tr>
		<td class="bggrey">Supprimer une année</td>
		<td><input name="inter_edit_submit_del" type="checkbox" id="inter_edit_submit_del" value="1" /> 
			<span style="color:#f00;"><strong>Sélectionnez une année puis valider avec &quot;Lancer la recherche&quot; pour supprimer l'année.<br />
			ATTENTION : Si vous cochez cette case, l'opération est irréversible et définitive après avoir cliquez sur &quot;Lancer la rechercher&quot;</strong></span></td>
	</tr>
	<tr>
		<td><input type="submit" name="inter_edit_submit" id="inter_edit_submit" value="Lancer la recherche" class="bouton" /></td>
		<td>&nbsp;</td>
	</tr>
</table>
</form>
<br />
<form action="" method="post" name="inter_form">
	<table>
<?php

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

?>
		<tr>
			<td class="bggrey">Année de l'intervention</td>
			<td><input name="inter_year[<?php echo $key; ?>]" type="text" id="inter_year[]" value="<?php echo $value['inter_year']; ?>" /></td>
		</tr>
		<tr>
			<td class="bggrey">Mois de l'intervention</td>
			<td>
				<select name="inter_month[<?php echo $key; ?>]" id="inter_month[]">
<?php

	foreach($lang['MONTH'] as $dkey => $dvalue) {

?>
					<option value="<?php echo $dkey; ?>"<?php echo ($dkey == $value['inter_month']) ? ' selected="selected"' : ''; ?>><?php echo $dvalue; ?></option>
<?php

	}

?>
				</select>

			</td>
		</tr>
		<tr>
			<td class="bggrey">Jour(s) de l'intervention</td>
			<td><input name="inter_day[<?php echo $key; ?>]" type="text" id="inter_day[]" value="<?php echo $value['inter_day']; ?>" />Séparé les jours par une virgule : exemple 24, 25, 26</td>
		</tr>
		<tr>
			<td width="30%" class="bggrey">Texte</td>
			<td width="70%"><table>
				<tr class="bggrey">
					<td>Français</td>
					<td>Anglais</td>
				</tr>
				<tr>
					<td width="50%"><textarea name="inter_text_fr[<?php echo $key; ?>]" id="inter_text_fr[<?php echo $key; ?>]" cols="45" rows="5"><?php echo $value['inter_text_fr']; ?></textarea></td>
					<td width="50%"><textarea name="inter_text_en[<?php echo $key; ?>]" id="inter_text_en[<?php echo $key; ?>]" cols="45" rows="5"><?php echo $value['inter_text_en']; ?></textarea></td>
				</tr>
			</table></td>
		</tr>
		<tr>
			<td class="bggrey">Supprimer cette intervention</td>
			<td><input name="inter_del[<?php echo $key; ?>]" type="checkbox" id="inter_del[<?php echo $key; ?>]" value="1" /> Cochez, puis valider</td>
		</tr>
<?php

		if($years and $months) {

?>
		<tr>
			<td><input type="submit" name="inter_submit" id="inter_submit" value="Valider les modifications" class="bouton" /></td>
			<td>&nbsp;</td>
		</tr>
<?php

		}

	}

?>
	</table>
</form>
</body>
</html>
<?php

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