<?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;
	$public_id = isset($_GET['public_id']) ? (is_numeric($_GET['public_id']) ? $_GET['public_id'] : 0) : 0;
	
	$category = $public = $error = array();
	
	if(isset($_POST['public_submit']) and isset($_POST['public_del']) and $public_id != 0) {
		$sql = 'DELETE FROM ' . PUBLIC_TABLE . ' WHERE public_id = ' . $public_id . ' LIMIT 1';
		if(unlink(ROOT_PATH . 'imgs/publications/' . $_POST['public_filename_temp'] . '.jpg')) {
			if(mysql_query($sql)) {
				$error[] = 'La publication a été supprimé.';
			}
		}
	} elseif(isset($_POST['public_submit']) and $public_id != 0) {
		if($_FILES['public_filename']['name'] != '') {
			if($_FILES['public_filename']['type'] == 'image/jpg' or $_FILES['public_filename']['type'] == 'image/jpeg') {
				$size = getimagesize($_FILES['public_filename']['tmp_name']);
				if($size[0] <= 300 and $_FILES['public_filename']['size'] <= 1000000) {
					move_uploaded_file($_FILES['public_filename']['tmp_name'], ROOT_PATH . 'imgs/publications/' . $_POST['public_filename_temp'] . '.jpg');
				} else {
					$error[] = 'Erreur, les images ne respectent pas les conditions demandées.';
				}
			} else {
				$error[] = 'Les images doivent être au format JPG.';
			}
		}
			
		if(count($error) <= 0) {
			$public_title_en = trim($_POST['public_title_en']);
			$public_text_en = trim($_POST['public_text_en']);
			$sql = 'UPDATE ' . PUBLIC_TABLE . ' SET ' . $db->sql_insert_update('UPDATE', array(
				'category_id'		=>	(string) $_POST['public_category_id'],
				'public_title_fr'	=>	(string) $_POST['public_title_fr'],
				'public_title_en'	=>	(string) empty($public_title_en) ? NULL : $public_title_en,
				'public_text_fr'	=>	(string) $_POST['public_text_fr'],
				'public_text_en'	=>	(string) empty($public_text_en) ? NULL : $public_text_en,
				'public_sort'		=>	(int) $_POST['public_sort']
			)) . ' WHERE public_id = ' . $public_id;
			if(mysql_query($sql)) {
				$error[] = 'La publication a bien été mise à jour.';
			}
		}
	} elseif(isset($_POST['public_submit']) and $public_id == 0) {
		if($_FILES['public_filename']['name'] != '') {
			if($_FILES['public_filename']['type'] == 'image/jpg' or $_FILES['public_filename']['type'] == 'image/jpeg') {
				$size = getimagesize($_FILES['public_filename']['tmp_name']);
				if($size[0] <= 300 and $_FILES['public_filename']['size'] <= 1000000) {
					$filename = substr(md5(microtime()), 8, mt_rand(8, 32));
					move_uploaded_file($_FILES['public_filename']['tmp_name'], ROOT_PATH . 'imgs/publications/' . $filename . '.jpg');
					
					$public_title_en = trim($_POST['public_title_en']);
					$public_text_en = trim($_POST['public_text_en']);
					$sql = 'INSERT INTO ' . PUBLIC_TABLE . $db->sql_insert_update('INSERT', array(
						'category_id'		=>	(string) $_POST['public_category_id'],
						'public_title_fr'	=>	(string) $_POST['public_title_fr'],
						'public_title_en'	=>	(string) empty($public_title_en) ? NULL : $public_title_en,
						'public_text_fr'	=>	(string) $_POST['public_text_fr'],
						'public_text_en'	=>	(string) empty($public_text_en) ? NULL : $public_text_en,
						'public_img'		=>	(string) $filename,
						'public_sort'		=>	(int) $_POST['public_sort']
					));
					if(mysql_query($sql)) {
						$error[] = 'La publication a bien été ajouté.';
					}
				} else {
					$error[] = 'Erreur, les images ne respectent pas les conditions demandées.';
				}
			} else {
				$error[] = 'Les images doivent être au format JPG.';
			}
		} else {
			$error[] = 'Vous n\'avez pas sélectionner d\'image.';
		}
	}
	
	if(isset($_POST['category_submit']) and isset($_POST['category_del']) and $category_id != 0) {
		$sql = 'DELETE FROM ' . CATEGORY_TABLE . ' WHERE category_parent_id = 13 AND category_id = ' . $category_id . ' LIMIT 1';
		if(mysql_query($sql)) {
			$error[] = 'La rubrique a é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_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'	=>	13,
			'category_name_fr'		=>	(string) $_POST['category_name_fr'],
			'category_name_en'		=>	(string) empty($category_name_en) ? NULL : $category_name_en,
			'category_page'			=>	(string) 'publications',
			'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 = 13 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 public_id, public_title_fr FROM ' . PUBLIC_TABLE . ' ORDER BY public_sort ASC';
	$result = mysql_query($sql);
	while($row = mysql_fetch_assoc($result)) {
		$public[$row['public_id']] = array(
			'public_title_fr'	=>	$row['public_title_fr']
		);
	}
	mysql_free_result($result);

	if($category_id) {
		$sql = 'SELECT * FROM ' . CATEGORY_TABLE . ' WHERE category_parent_id = 13 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 = '';
	}
	
	if($public_id) {
		$sql = 'SELECT * FROM ' . PUBLIC_TABLE . ' WHERE public_id = ' . $public_id;
		$result = mysql_query($sql);
		$publics = mysql_fetch_assoc($result);
		mysql_free_result($result);

		$public_category_id = $publics['category_id'];
		$public_title_fr = $publics['public_title_fr'];
		$public_title_en = $publics['public_title_en'];
		$public_text_fr = $publics['public_text_fr'];
		$public_text_en = $publics['public_text_en'];
		$public_filename_temp = $publics['public_img'];
		$public_sort = $publics['public_sort'];
	} else {
		$public_category_id = 0;
		$public_title_fr = '';
		$public_title_en = '';
		$public_text_fr = '';
		$public_text_en = '';
		$public_filename_temp = '';
		$public_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 =['public_text_fr','public_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>
<br />
<?php

	}

?>
<h1>Publications</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" /></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 publications</h2>
<form name="public_edit_form" action="" method="get">
<table>
	<tr>
		<td width="30%" class="bggrey">Sélectionner une action</td>
		<td width="70%">
			<select name="public_id" id="public_id" onchange="this.form.submit();">
				<option value="0">Sélectionner</option>
				<option value="0">Ajouter une publication</option>
<?php

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

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

	}

?>
			</select>
		</td>
	</tr>
</table>
</form>
<form action="" method="post" enctype="multipart/form-data" name="public_form">
	<table>
		<tr>
			<td class="bggrey">Dans quelle rubrique ?</td>
			<td>
				<select name="public_category_id" id="public_category_id">
<?php

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

?>
					<option value="<?php echo $key; ?>"><?php echo $value['category_name_fr']; ?></option>
<?php

	}

?>
				</select>
			</td>
		</tr>
		<tr>
			<td width="30%" class="bggrey">Titre de l'ouvrage</td>
			<td width="70%"><table>
				<tr class="bggrey">
					<td>Français</td>
					<td>Anglais</td>
				</tr>
				<tr>
					<td width="50%"><input name="public_title_fr" type="text" id="public_title_fr" value="<?php echo $public_title_fr; ?>" /></td>
					<td width="50%"><input name="public_title_en" type="text" id="public_title_en" value="<?php echo $public_title_en; ?>" /></td>
				</tr>
			</table></td>
		</tr>
		<tr>
			<td class="bggrey">Texte descriptif</td>
			<td><table>
				<tr>
					<td width="50%"><textarea name="public_text_fr" rows="10" id="public_text_fr"><?php echo $public_text_fr; ?></textarea></td>
					<td width="50%"><textarea name="public_text_en" rows="10" id="public_text_en"><?php echo $public_text_en; ?></textarea></td>
				</tr>
			</table></td>
		</tr>
		<tr>
			<td class="bggrey">Position</td>
			<td><input name="public_sort" type="text" id="public_sort" value="<?php echo $public_sort; ?>" /></td>
		</tr>
		<tr>
			<td class="bggrey">Couverture</td>
			<td>
				<input type="file" name="public_filename" id="public_filename" />
				<input name="public_filename_temp" type="hidden" id="public_filename_temp" value="<?php echo $public_filename_temp; ?>" />
			</td>
		</tr>
<?php

	if($public_id) {

?>
		<tr>
			<td class="bggrey">&nbsp;</td>
			<td><img src="<?php echo BASE_PATH; ?>/imgs/publications//<?php echo $public_filename_temp; ?>.jpg" width="189"  /></td>
		</tr>

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

	}

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

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