<?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);
	
	$category_id = isset($_GET['category_id']) ? (is_numeric($_GET['category_id']) ? $_GET['category_id'] : 0) : 0;
	$actualites_id = isset($_GET['actualites_id']) ? (is_numeric($_GET['actualites_id']) ? $_GET['actualites_id'] : 0) : 0;
	$category = $actualites = $error = array();


	if(isset($_POST['actualites_submit']) and isset($_POST['actualites_del']) and $actualites_id != 0) {
		$sql = 'DELETE FROM ' . ACTUALITES_TABLE . ' WHERE actualites_id = ' . $actualites_id . ' LIMIT 1';
		if(mysql_query($sql)) {
			$error[] = 'L\'actualité a bien été supprimée.';
		}
	} elseif(isset($_POST['actualites_submit']) and $actualites_id != 0) {
		$actualites_title_en = trim($_POST['actualites_title_en']);
		$actualites_text_en = trim($_POST['actualites_text_en']);
		$sql = 'UPDATE ' . ACTUALITES_TABLE . ' SET ' . $db->sql_insert_update('UPDATE', array(
			'category_id'			=>	$_POST['actualites_category_id'],
			'actualites_datetime'	=>	(int) time(),
			'actualites_title_fr'	=>	(string) $_POST['actualites_title_fr'],
			'actualites_title_en'	=>	(string) empty($actualites_title_en) ? NULL : $actualites_title_en,
			'actualites_text_fr'	=>	(string) $_POST['actualites_text_fr'],
			'actualites_text_en'	=>	(string) empty($actualites_text_en) ? NULL : $actualites_text_en,
			//'actualites_flv'		=>	($_POST['actualites_filename'] == 0) ? NULL : $_POST['actualites_filename']
			'actualites_flv'		=>	(string) $_POST['actualites_filename']
		)) . ' WHERE actualites_id = ' . $actualites_id;
		if(mysql_query($sql)) {
			$error[] = 'L\'actualité a bien été mise à jour.';
		}
	} elseif(isset($_POST['actualites_submit']) and $actualites_id == 0) {
		$actualites_title_en = trim($_POST['actualites_title_en']);
		$actualites_text_en = trim($_POST['actualites_text_en']);
		$sql = 'INSERT INTO ' . ACTUALITES_TABLE . $db->sql_insert_update('INSERT', array(
			'category_id'			=>	$_POST['actualites_category_id'],
			'actualites_datetime'	=>	(int) time(),
			'actualites_title_fr'	=>	(string) $_POST['actualites_title_fr'],
			'actualites_title_en'	=>	(string) empty($actualites_title_en) ? NULL : $actualites_title_en,
			'actualites_text_fr'	=>	(string) $_POST['actualites_text_fr'],
			'actualites_text_en'	=>	(string) empty($actualites_text_en) ? NULL : $actualites_text_en,
			//'actualites_flv'		=>	($_POST['actualites_filename'] == 0) ? NULL : $_POST['actualites_filename']
			'actualites_flv'		=>	(string) $_POST['actualites_filename']
		));
		if(mysql_query($sql)) {
			$error[] = 'L\'actualité a bien été ajoutée.';
		}
	}


	if(isset($_POST['category_submit']) and isset($_POST['category_del']) and $category_id != 0) {
		$sql_1 = 'DELETE FROM ' . CATEGORY_TABLE . ' WHERE category_parent_id = 5 AND category_id = ' . $category_id . ' LIMIT 1';
		$sql_2 = 'DELETE FROM ' . ACTUALITES_TABLE . ' WHERE category_id = ' . $category_id;
		if(mysql_query($sql_1) and mysql_query($sql_2)) {
			$error[] = 'La rubrique et les actualités associées ont été supprimé.';
		}
	} elseif(isset($_POST['category_submit']) and $category_id != 0) {
		$category_name_en = trim($_POST['category_name_en']);
		$sql = 'UPDATE ' . CATEGORY_TABLE . ' SET ' . $db->sql_insert_update('UPDATE', array(
			'category_name_fr'		=>	(string) $_POST['category_name_fr'],
			'category_name_en'		=>	(string) empty($category_name_en) ? NULL : $category_name_en,
			'category_sort'			=>	(int) $_POST['category_sort']
		)) . ' WHERE category_parent_id = 5 AND category_id = ' . $category_id;
		if(mysql_query($sql)) {
			$error[] = 'La rubrique a bien été mise à jour.';
		}
	} elseif(isset($_POST['category_submit']) and $category_id == 0) {
		$category_name_en = trim($_POST['category_name_en']);
		$sql = 'INSERT INTO ' . CATEGORY_TABLE . $db->sql_insert_update('INSERT', array(
			'category_parent_id'	=>	5,
			'category_name_fr'		=>	(string) $_POST['category_name_fr'],
			'category_name_en'		=>	(string) empty($category_name_en) ? NULL : $category_name_en,
			'category_page'			=>	(string) 'actualites',
			'category_url'			=>	NULL,
			'category_is'			=>	0,
			'category_var'			=>	1,
			'category_display'		=>	1,
			'category_bg_img'		=>	'bg_c_1',
			'category_bg_color'		=>	'ffffff',
			'category_sort'			=>	(int) $_POST['category_sort']
		));
		if(mysql_query($sql)) {
			$error[] = 'La rubrique a bien été ajouté.';
		}
	}
	
	$sql = 'SELECT * FROM ' . CATEGORY_TABLE . ' WHERE category_parent_id = 5 ORDER BY category_sort ASC';
	$result = mysql_query($sql);
	while($row = mysql_fetch_assoc($result)) {
		$category[$row['category_id']] = array(
			'category_name_fr'	=>	$row['category_name_fr'],
			'category_sort'		=>	$row['category_sort']
		);
	}
	mysql_free_result($result);

	$sql = 'SELECT * FROM ' . ACTUALITES_TABLE . ' ORDER BY actualites_id DESC';
	$result = mysql_query($sql);
	while($row = mysql_fetch_assoc($result)) {
		$actualites[$row['actualites_id']] = array(
			'actualites_title_fr'	=>	$row['actualites_title_fr'],
			'actualites_title_en'	=>	$row['actualites_title_en'],
			'actualites_text_fr'	=>	$row['actualites_text_fr'],
			'actualites_text_en'	=>	$row['actualites_text_en'],
			'actualites_flv'		=>	$row['actualites_flv']
		);
	}
	mysql_free_result($result);

	if($actualites_id) {
		$sql = 'SELECT * FROM ' . ACTUALITES_TABLE . ' WHERE actualites_id = ' . $actualites_id;
		$result = mysql_query($sql);
		$actualite = mysql_fetch_assoc($result);
		mysql_free_result($result);

		$actualites_category_id = $actualite['category_id'];
		$actualites_title_fr = $actualite['actualites_title_fr'];
		$actualites_title_en = $actualite['actualites_title_en'];
		$actualites_text_fr = $actualite['actualites_text_fr'];
		$actualites_text_en = $actualite['actualites_text_en'];
		$actualites_filename = $actualite['actualites_flv'];
	} else {
		$actualites_category_id = 0;
		$actualites_title_fr = '';
		$actualites_title_en = '';
		$actualites_text_fr = '';
		$actualites_text_en = '';
		$actualites_filename = '';
	}

	if($category_id) {
		$sql = 'SELECT * FROM ' . CATEGORY_TABLE . ' WHERE category_parent_id = 5 AND category_id = ' . $category_id;
		$result = mysql_query($sql);
		$categorys = mysql_fetch_assoc($result);
		mysql_free_result($result);

		$category_name_fr = $categorys['category_name_fr'];
		$category_name_en = $categorys['category_name_en'];
		$category_sort = $categorys['category_sort'];
	} else {
		$category_name_fr = '';
		$category_name_en = '';
		$category_sort = '';
	}
	
	
?>
<!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 =['actualites_text_fr','actualites_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 echo $_POST['actualites_filename']; ?> -->
<?php include('menu.php'); ?>

<?php

	if(count($error) > 0) {

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

	}

?>
<h1>Actualités</h1>
<h2>Ajouter une rubrique</h2>
<form name="category_edit_form" action="" method="get">
	<table>
		<tr>
			<td width="30%" class="bggrey">Sélectionner une action</td>
			<td width="70%">
				<select name="category_id" id="category_id" onchange="this.form.submit();">
					<option value="0">Sélectionner</option>
					<option value="0">Ajouter une rubrique</option>
<?php

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

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

	}

?>
				</select>
			</td>
		</tr>
	</table>
</form>
<form name="category_form" action="" method="post">
	<table>
		<tr>
			<td width="30%" class="bggrey">Nom de la rubrique</td>
			<td width="70%"><table>
				<tr class="bggrey">
					<td width="50%">Français</td>
					<td width="50%">Anglais</td>
				</tr>
				<tr>
					<td><input name="category_name_fr" type="text" id="category_name_fr" value="<?php echo $category_name_fr; ?>" /></td>
					<td><input type="text" name="category_name_en" id="category_name_en" value="<?php echo $category_name_en; ?>" /></td>
				</tr>
			</table></td>
		</tr>
		<tr>
			<td class="bggrey">Position</td>
			<td><input name="category_sort" type="text" id="category_sort" value="<?php echo $category_sort; ?>" /></td>
		</tr>
<?php

	if($category_id) {

?>
		<tr>
			<td class="bggrey">Supprimer la rubrique</td>
			<td><input type="checkbox" name="category_del" id="category_del" /> <span style="color:#f00;"><strong>Attention</strong> toutes les actualités liées à la rubrique seront supprimé.</span></td>
		</tr>
<?php

	}

?>
		<tr>
			<td><input type="submit" name="category_submit" id="category_submit" value="Valider" class="bouton" /></td>
			<td>&nbsp;</td>
		</tr>
	</table>
</form>
<h2>Gestion des actualités</h2>
<form name="actualites_edit_form" action="" method="get">
<table>
	<tr>
		<td width="30%" class="bggrey">Sélectionner une action</td>
		<td width="70%">
			<select name="actualites_id" id="actualites_id" onchange="this.form.submit();">
				<option value="0">Sélectionner</option>
				<option value="0">Ajouter une publication</option>
<?php

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

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

	}

?>
			</select>
		</td>
	</tr>
</table>
</form>
<form action="" method="post" name="actualites_form">
	<table>
		<tr>
			<td class="bggrey">Dans quelle rubrique ?</td>
			<td>
				<select name="actualites_category_id" id="actualites_category_id">
<?php

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

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

	}

?>
				</select>
			</td>
		</tr>
		<tr>
			<td width="30%" class="bggrey">Titre de l'actualité</td>
			<td width="70%"><table>
				<tr class="bggrey">
					<td>Français</td>
					<td>Anglais</td>
				</tr>
				<tr>
					<td width="50%"><input name="actualites_title_fr" type="text" id="actualites_title_fr" value="<?php echo $actualites_title_fr; ?>" /></td>
					<td width="50%"><input name="actualites_title_en" type="text" id="actualites_title_en" value="<?php echo $actualites_title_en; ?>" /></td>
				</tr>
			</table></td>
		</tr>
		<tr>
			<td class="bggrey">Texte</td>
			<td><table>
				<tr>
					<td width="50%"><textarea name="actualites_text_fr" rows="10" id="actualites_text_fr"><?php echo $actualites_text_fr; ?></textarea></td>
					<td width="50%"><textarea name="actualites_text_en" rows="10" id="actualites_text_en"><?php echo $actualites_text_en; ?></textarea></td>
				</tr>
			</table></td>
		</tr>
		<tr>
			<td class="bggrey">Fichier vidéo</td>
			<td>
				<select name="actualites_filename" id="actualites_filename">
					<option value="0">Aucune</option>
<?php

	$path = ROOT_PATH . 'videos/';
	$dir = opendir($path);
	while ($file = readdir($dir)) {
		 if(is_file($path.$file)) {

?>
					<option value="<?php echo substr($file, 0, -4); ?>"<?php echo (($actualites_filename == substr($file, 0, -4)) ? ' selected="seletected"' : ''); ?>><?php echo $file; ?></option>
<?php

		}
	}
	closedir($dir);

?>
				</select>
			</td>
		</tr>
<?php

	if($actualites_id) {

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

	}

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

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