<?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);
	
	$content_id = isset($_GET['content_id']) ? (is_numeric($_GET['content_id']) ? $_GET['content_id'] : 0) : 0;
	
	$error = array();
	
	if(isset($_POST['content_submit']) and $content_id != 0) {
		
		if(isset($_FILES['content_filename']['name']) and $_FILES['content_filename']['name'] != '') {
			if($_FILES['content_filename']['type'] == 'image/jpg' or $_FILES['content_filename']['type'] == 'image/jpeg') {
				$size = getimagesize($_FILES['content_filename']['tmp_name']);
				if($size[0] <= 300 and $_FILES['content_filename']['size'] <= 1000000) {
					move_uploaded_file($_FILES['content_filename']['tmp_name'], ROOT_PATH . 'imgs/meet/' . $_POST['content_filename_temp'] . '.jpg');
				} else {
					$error[] = 'Erreur, l\'image ne respect pas les conditions demandées.';
				}
			} else {
				$error[] = 'L\'mages doit être au format JPG.';
			}
		}
		
		if(count($error) <= 0) {
			$content_text_en = trim($_POST['content_text_en']);
			$sql = 'UPDATE ' . CONTENT_TABLE . ' SET ' . $db->sql_insert_update('UPDATE', array(
				'content_text_fr'	=>	(string) $_POST['content_text_fr'],
				'content_text_en'	=>	(string) empty($content_text_en) ? NULL : $content_text_en
			)) . ' WHERE content_id = ' . $content_id;
			if(mysql_query($sql)) {
				$error[] = 'Le contenu a bien été mis à jour.';
			}
		}
	}

	if($content_id) {
		$sql = 'SELECT * FROM ' . CONTENT_TABLE . ' WHERE content_id = ' . $content_id;
		$result = mysql_query($sql);
		$content = mysql_fetch_assoc($result);
		
		$content_text_fr = $content['content_text_fr'];
		$content_text_en = $content['content_text_en'];
		$content_img = $content['content_img'];
	} else {
		$content_text_fr = '';
		$content_text_en = '';
		$content_img = false;
	}
	
?>
<!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_text_fr','content_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');

	if(count($error) > 0) {

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

	}

?>
<h1>Page de contenu</h1>
<form name="content_edit_form" action="" method="get">
	<table>
	<tr>
		<td width="30%" class="bggrey">Sélectionner le contenu à modifier</td>
		<td width="70%">
			<select name="content_id" id="content_id" onchange="this.form.submit();">
				<option value="0">Sélectionner</option>
				<option value="3"<?php echo ($content_id == 3) ? ' selected="selected"' : ''; ?>>Associés</option>
				<option value="4"<?php echo ($content_id == 4) ? ' selected="selected"' : ''; ?>>Partenaires</option>
				<option value="5"<?php echo ($content_id == 5) ? ' selected="selected"' : ''; ?>>Veille Juridique - Introduction</option>
				<option value="6"<?php echo ($content_id == 6) ? ' selected="selected"' : ''; ?>>Veille Juridique - Une base de données règlementaires</option>
				<option value="7"<?php echo ($content_id == 7) ? ' selected="selected"' : ''; ?>>Veille Juridique - Une veille réglementaire anticipative et stratégique</option>
				<option value="9"<?php echo ($content_id == 9) ? ' selected="selected"' : ''; ?>>Newsletter</option>
				<option value="8"<?php echo ($content_id == 8) ? ' selected="selected"' : ''; ?>>Nous contacter</option>
				<option value="10"<?php echo ($content_id == 10) ? ' selected="selected"' : ''; ?>>Mentions légales</option>
			</select>
		</td>
	</tr>
</table>
</form>
<form action="" method="post" enctype="multipart/form-data" name="content_form">
	<table>
		<tr>
			<td width="30%" class="bggrey">Contenu</td>
			<td width="70%"><table>
				<tr class="bggrey">
					<td>Français</td>
					<td>Anglais</td>
				</tr>
				<tr>
					<td width="50%"><textarea name="content_text_fr" rows="10" id="content_text_fr"><?php echo $content_text_fr; ?></textarea></td>
					<td width="50%"><textarea name="content_text_en" rows="10" id="content_text_en"><?php echo $content_text_en; ?></textarea></td>
				</tr>
			</table></td>
		</tr>
<?php
		
	if($content_img) {
		
?>
		<tr>
			<td class="bggrey">Photo</td>
			<td><input type="file" name="content_filename" id="content_filename" /> <input name="content_filename_temp" type="hidden" id="content_filename_temp" value="<?php echo $content_img; ?>" /> 
				Largeur maximale : 200px (1Mo maximum)</td>
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td><img src="<?php echo BASE_PATH; ?>/imgs/meet/<?php echo $content_img; ?>.jpg" width="200" alt="" /></td>
		</tr>
<?php
		
	}
		
?>
		<tr>
			<td><input type="submit" name="content_submit" id="content_submit" value="Valider les modifications" class="bouton" /></td>
			<td>&nbsp;</td>
		</tr>
	</table>
</form>
</body>
</html>
<?php

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