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


	if(isset($_POST['newsletter_submit']) and isset($_POST['newsletter_del']) and $newsletter_archives_id != 0) {
		if(unlink(ROOT_PATH . 'files/archives/' . $_POST['newsletter_filename_temp'])) {
			$sql = 'DELETE FROM ' . NEWSLETTER_ARCHIVES_TABLE . ' WHERE newsletter_archives_id = ' . $newsletter_archives_id . ' LIMIT 1';
			if(mysql_query($sql)) {
				$error[] = 'La newsletter a été supprimé.';
			}
		} else {
			$error[] = 'Erreur pendant la suppression du fichier newsletter.';
		}
	} elseif(isset($_POST['newsletter_submit']) and $newsletter_archives_id != 0) {
		if($_FILES['newsletter_filename']['name'] != '') {
			if($_FILES['newsletter_filename']['size'] <= 2000000) {
				if(!move_uploaded_file($_FILES['newsletter_filename']['tmp_name'], ROOT_PATH . 'files/archives/' . $_POST['newsletter_filename_temp'])) {
					$error[] = 'Erreur pendant le transfert du fichier.';
				}
			} else {
				$error[] = 'Le fichier n\'est pas inférieur à 2 Mo';
			}
		}
		
		if(count($error) <= 0) {
			$newsletter_archives_name_en = trim($_POST['newsletter_archives_name_en']);
			$sql = 'UPDATE ' . NEWSLETTER_ARCHIVES_TABLE . ' SET ' . $db->sql_insert_update('UPDATE', array(
				'category_id'					=>	$_POST['newsletter_category_id'],
				'newsletter_archives_datetime'	=>	(int) time(),
				'newsletter_archives_name_fr'	=>	(string) $_POST['newsletter_archives_name_fr'],
				'newsletter_archives_name_en'	=>	(string) empty($newsletter_archives_name_en) ? NULL : $newsletter_archives_name_en,
				'newsletter_archives_filename'	=>	(string) $_POST['newsletter_filename_temp']
			)) . ' WHERE newsletter_archives_id = ' . $newsletter_archives_id;
			if(mysql_query($sql)) {
				$error[] = 'La newsletter a été mise à jour.';
			}
		}

	} elseif(isset($_POST['newsletter_submit']) and $newsletter_archives_id == 0) {
		if($_FILES['newsletter_filename']['name'] != '') {
			if($_FILES['newsletter_filename']['size'] <= 2000000) {
				if(!move_uploaded_file($_FILES['newsletter_filename']['tmp_name'], ROOT_PATH . 'files/archives/' . $_FILES['newsletter_filename']['name'])) {
					$error[] = 'Erreur pendant le transfert du fichier.';
				}
			} else {
				$error[] = 'Le fichier n\'est pas inférieur à 2 Mo';
			}
		} else {
			$error[] = 'Vous devez selectionner un fichier.';
		}
		
		if(count($error) <= 0) {
			$filename = substr(md5(microtime()), 8, mt_rand(8, 32));
			$newsletter_archives_name_en = trim($_POST['newsletter_archives_name_en']);
			$sql = 'INSERT INTO ' . NEWSLETTER_ARCHIVES_TABLE . $db->sql_insert_update('INSERT', array(
				'category_id'					=>	$_POST['newsletter_category_id'],
				'newsletter_archives_datetime'	=>	(int) time(),
				'newsletter_archives_name_fr'	=>	(string) $_POST['newsletter_archives_name_fr'],
				'newsletter_archives_name_en'	=>	(string) empty($newsletter_archives_name_en) ? NULL : $newsletter_archives_name_en,
				'newsletter_archives_filename'	=>	(string) $_FILES['newsletter_filename']['name'],
			));
			if(mysql_query($sql)) {
				$error[] = 'La newsletter a bien été ajoutée.';
			}
		}
	}


	if(isset($_POST['category_submit']) and isset($_POST['category_del']) and $category_id != 0) {
		$sql = 'DELETE FROM ' . CATEGORY_TABLE . ' WHERE category_parent_id = 22 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_parent_id = 22 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'	=>	22,
			'category_name_fr'		=>	(string) $_POST['category_name_fr'],
			'category_name_en'		=>	(string) empty($category_name_en) ? NULL : $category_name_en,
			'category_page'			=>	(string) 'newsletter',
			'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 = 22 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 ' . NEWSLETTER_ARCHIVES_TABLE . ' ORDER BY newsletter_archives_id DESC';
	$result = mysql_query($sql);
	while($row = mysql_fetch_assoc($result)) {
		$newsletter[$row['newsletter_archives_id']] = array(
			'category_id'					=>	$row['category_id'],
			'newsletter_archives_name_fr'	=>	$row['newsletter_archives_name_fr'],
			'newsletter_archives_name_en'	=>	$row['newsletter_archives_name_en'],
			'newsletter_archives_filename'	=>	$row['newsletter_archives_filename']
		);
	}
	mysql_free_result($result);

	if($newsletter_archives_id) {
		$sql = 'SELECT * FROM ' . NEWSLETTER_ARCHIVES_TABLE . ' WHERE newsletter_archives_id = ' . $newsletter_archives_id;
		$result = mysql_query($sql);
		$newsletter_archives = mysql_fetch_assoc($result);
		mysql_free_result($result);

		$newsletter_archives_id = $newsletter_archives['newsletter_archives_id'];
		$newsletter_category_id = $newsletter_archives['category_id'];
		$newsletter_archives_name_fr = $newsletter_archives['newsletter_archives_name_fr'];
		$newsletter_archives_name_en = $newsletter_archives['newsletter_archives_name_en'];
		$newsletter_archives_filename = $newsletter_archives['newsletter_archives_filename'];
	} else {
		$newsletter_archives_id = 0;
		$newsletter_category_id = '';
		$newsletter_archives_name_fr = '';
		$newsletter_archives_name_en = '';
		$newsletter_archives_filename = '';
	}

	if($category_id) {
		$sql = 'SELECT * FROM ' . CATEGORY_TABLE . ' WHERE category_parent_id = 22 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" />
</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>Newsletter</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 newsletters</h2>
<form name="newsletter_edit_form" action="" method="get">
<table>
	<tr>
		<td width="30%" class="bggrey">Sélectionner une action</td>
		<td width="70%">
			<select name="newsletter_archives_id" id="newsletter_archives_id" onchange="this.form.submit();">
				<option value="0">Sélectionner</option>
				<option value="0">Ajouter une publication</option>
<?php

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

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

	}

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

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

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

	}

?>
				</select>
			</td>
		</tr>
		<tr>
			<td width="30%" class="bggrey">Titre de la newsletter</td>
			<td width="70%"><table>
				<tr class="bggrey">
					<td>Français</td>
					<td>Anglais</td>
				</tr>
				<tr>
					<td width="50%"><input name="newsletter_archives_name_fr" type="text" id="newsletter_archives_name_fr" value="<?php echo $newsletter_archives_name_fr; ?>" /></td>
					<td width="50%"><input name="newsletter_archives_name_en" type="text" id="newsletter_archives_name_en" value="<?php echo $newsletter_archives_name_en; ?>" /></td>
				</tr>
			</table></td>
		</tr>
		<tr>
			<td class="bggrey">Fichier</td>
			<td><input type="file" name="newsletter_filename" id="newsletter_filename" /><input name="newsletter_filename_temp" type="hidden" id="newsletter_filename_temp" value="<?php echo $newsletter_archives_filename; ?>" /> 
				(2 Mo maximum)</td>
		</tr>
<?php

	if($newsletter_archives_id) {

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

	}

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

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