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

	if(isset($_POST['members_submit']) and isset($_POST['members_del']) and $members_id != 0) {
		
		@unlink(ROOT_PATH . 'imgs/members/' . $_POST['members_filename_temp'] . '_t.jpg');
		@unlink(ROOT_PATH . 'imgs/members/' . $_POST['members_filename_temp'] . '.jpg');
		
		$sql = 'DELETE FROM ' . MEMBERS_TABLE . ' WHERE members_id = ' . $members_id . ' LIMIT 1';
		if(mysql_query($sql)) {
			$sql = 'UPDATE ' . MOZAIC_TABLE . ' SET ' . $db->sql_insert_update('UPDATE', array(
				'members_id'	=>	0,
			)) . ' WHERE members_id = ' . $members_id;
			mysql_query($sql);
			$error[] = 'Le membre a bien été supprimé.';
		}
	} elseif(isset($_POST['members_submit']) and $members_id != 0) {

		if($_FILES['members_filename_t']['name'] != '') {
			if(($_FILES['members_filename_t']['type'] == 'image/jpg' or $_FILES['members_filename_t']['type'] == 'image/jpeg')) {
				$size_t = getimagesize($_FILES['members_filename_t']['tmp_name']);
				if($size_t[0] == 128 and $size_t[1] == 128 and $_FILES['members_filename_t']['size'] <= 1000000 ) {
					if(!move_uploaded_file($_FILES['members_filename_t']['tmp_name'], ROOT_PATH . 'imgs/members/' . $_POST['members_filename_temp'] . '_t.jpg')) {
						$error[] = 'Erreur pendant le transfert d\'image (vignette).';
					}
				} else {
					$error[] = 'Erreur, les images ne respectent pas les conditions demandées.';
				}
			} else {
				$error[] = 'Les images doivent être au format JPG.';
			}
		}
		
		if($_FILES['members_filename']['name'] != '') {
			if(($_FILES['members_filename']['type'] == 'image/jpg' or $_FILES['members_filename']['type'] == 'image/jpeg')) {
				$size = getimagesize($_FILES['members_filename']['tmp_name']);
				if($size[0] <= 385 and $_FILES['members_filename']['size'] <= 1000000 ) {
					if(!move_uploaded_file($_FILES['members_filename']['tmp_name'], ROOT_PATH . 'imgs/members/' . $_POST['members_filename_temp'] . '.jpg')) {
						$error[] = 'Erreur pendant le transfert d\'image (grande image).';
					}
				} 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) {
			$members_job_fr = trim($_POST['members_job_fr']);
			$members_job_en = trim($_POST['members_job_en']);
			$members_contact_en = trim($_POST['members_contact_en']);
			$members_intro_en = trim($_POST['members_intro_en']);
			$members_cv_en = trim($_POST['members_cv_en']);
	
			$sql = 'UPDATE ' . MEMBERS_TABLE . ' SET ' . $db->sql_insert_update('UPDATE', array(
				'members_name'			=>	(string) $_POST['members_name'],
				'members_job_fr'		=>	(string) empty($members_job_fr) ? NULL : $members_job_fr,
				'members_job_en'		=>	(string) empty($members_job_en) ? NULL : $members_job_en,
				'members_contact_fr'	=>	(string) $_POST['members_contact_fr'],
				'members_contact_en'	=>	(string) empty($members_contact_en) ? NULL : $members_contact_en,
				'members_intro_fr'		=>	(string) $_POST['members_intro_fr'],
				'members_intro_en'		=>	(string) empty($members_intro_en) ? NULL : $members_intro_en,
				'members_cv_fr'			=>	(string) $_POST['members_cv_fr'],
				'members_cv_en'			=>	(string) empty($members_cv_en) ? NULL : $members_cv_en,
			)) . ' WHERE members_id = ' . $members_id;
			if(mysql_query($sql)) {
				$error[] = 'Le membre a bien été mis à jour.';	
			}
		}	
	} elseif(isset($_POST['members_submit']) and $members_id == 0) {
		$members_job_en = trim($_POST['members_job_en']);
		$members_contact_en = trim($_POST['members_contact_en']);
		$members_intro_en = trim($_POST['members_intro_en']);
		$members_cv_en = trim($_POST['members_cv_en']);
		
		if($_FILES['members_filename_t']['name'] != '' and $_FILES['members_filename']['name'] != '') {
			if(($_FILES['members_filename_t']['type'] == 'image/jpg' or $_FILES['members_filename_t']['type'] == 'image/jpeg') and ($_FILES['members_filename']['type'] == 'image/jpg' or $_FILES['members_filename']['type'] == 'image/jpeg')) {
				$size_t = getimagesize($_FILES['members_filename_t']['tmp_name']);
				$size = getimagesize($_FILES['members_filename']['tmp_name']);
				
				if($size_t[0] == 128 and $size_t[1] == 128 and $size[0] <= 385 and $_FILES['members_filename_t']['size'] <= 1000000 and $_FILES['members_filename']['size'] <= 1000000) {
					$filename = substr(md5(microtime()), 8, mt_rand(8, 32));
					move_uploaded_file($_FILES['members_filename_t']['tmp_name'], ROOT_PATH . 'imgs/members/' . $filename . '_t.jpg');
					move_uploaded_file($_FILES['members_filename']['tmp_name'], ROOT_PATH . 'imgs/members/' . $filename . '.jpg');
					
					$sql = 'INSERT INTO ' . MEMBERS_TABLE . $db->sql_insert_update('INSERT', array(
						'members_name'			=>	(string) $_POST['members_name'],
						'members_job_fr'		=>	(string) $_POST['members_job_fr'],
						'members_job_en'		=>	(string) empty($members_job_en) ? NULL : $members_job_en,
						'members_contact_fr'	=>	(string) $_POST['members_contact_fr'],
						'members_contact_en'	=>	(string) empty($members_contact_en) ? NULL : $members_contact_en,
						'members_intro_fr'		=>	(string) $_POST['members_intro_fr'],
						'members_intro_en'		=>	(string) empty($members_intro_en) ? NULL : $members_intro_en,
						'members_cv_fr'			=>	(string) $_POST['members_cv_fr'],
						'members_cv_en'			=>	(string) empty($members_cv_en) ? NULL : $members_cv_en,
						'members_img'			=>	(string) $filename
					));
					if(mysql_query($sql)) {
						$error[] = 'Le membre a été rentré correctement.';
					}
				} 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($members_id) {
		$sql = 'SELECT * FROM ' . MEMBERS_TABLE . ' WHERE members_id = ' . $members_id;
		$result = mysql_query($sql);
		$members = mysql_fetch_assoc($result);
		
		$members_name = $members['members_name'];
		$members_job_fr = $members['members_job_fr'];
		$members_job_en = $members['members_job_en'];
		$members_contact_fr = $members['members_contact_fr'];
		$members_contact_en = $members['members_contact_en'];
		$members_intro_fr = $members['members_intro_fr'];
		$members_intro_en = $members['members_intro_en'];
		$members_cv_fr = $members['members_cv_fr'];
		$members_cv_en = $members['members_cv_en'];
		$members_filename_temp = $members['members_img'];
	} else {
		$members_name = '';
		$members_job_fr = '';
		$members_job_en = '';
		$members_contact_fr = '';
		$members_contact_en = '';
		$members_intro_fr = '';
		$members_intro_en = '';
		$members_cv_fr = '';
		$members_cv_en = '';
		$members_filename_temp = '';
	}

	$sql = 'SELECT * FROM ' . MEMBERS_TABLE . ' ORDER BY members_name ASC';
	$result = mysql_query($sql);
	while($row = mysql_fetch_assoc($result)) {
		$member[$row['members_id']] = array(
			'members_name'	=>	$row['members_name']
		);
	}
	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 = ['members_contact_fr','members_contact_en','members_intro_fr','members_intro_en','members_cv_fr','members_cv_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>Les membres</h1>
<form name="members_edit_form" action="" method="get">
<table>
	<tr>
		<td width="30%" class="bggrey">Sélectionner une l'action</td>
		<td width="70%">
			<select name="members_id" id="members_id" onchange="this.form.submit();">
				<option value="0">Sélectionner</option>
				<option value="0">Ajouter un CV</option>
<?php

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

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

	}

?>
			</select>
		</td>
	</tr>
</table>
</form>
<form action="" method="post" enctype="multipart/form-data" name="members_form">
	<table>
		<tr>
			<td class="bggrey">Prénom et nom</td>
			<td><input type="text" name="members_name" id="members_name" value="<?php echo $members_name; ?>" /></td>
		</tr>
		<tr>
			<td width="30%" class="bggrey">Rôle</td>
			<td width="70%"><table>
				<tr class="bggrey">
					<td>Français</td>
					<td>Anglais</td>
				</tr>
				<tr>
					<td width="50%"><input name="members_job_fr" type="text" id="members_job_fr" value="<?php echo $members_job_fr; ?>" /></td>
					<td width="50%"><input name="members_job_en" type="text" id="members_job_en" value="<?php echo $members_job_en; ?>" /></td>
				</tr>
			</table></td>
		</tr>
		<tr>
			<td class="bggrey">Contact</td>
			<td><table>
				<tr>
					<td width="50%"><textarea name="members_contact_fr" rows="10" id="members_contact_fr"><?php echo $members_contact_fr; ?></textarea></td>
					<td width="50%"><textarea name="members_contact_en" rows="10" id="members_contact_en"><?php echo $members_contact_en; ?></textarea></td>
				</tr>
			</table></td>
		</tr>
		<tr>
			<td class="bggrey">Texte d'introduction</td>
			<td><table>
				<tr>
					<td width="50%"><textarea name="members_intro_fr" rows="10" id="members_intro_fr"><?php echo $members_intro_fr; ?></textarea></td>
					<td width="50%"><textarea name="members_intro_en" rows="10" id="members_intro_en"><?php echo $members_intro_en; ?></textarea></td>
				</tr>
			</table></td>
		</tr>
		<tr>
			<td class="bggrey">Texte du CV</td>
			<td><table>
				<tr>
					<td width="50%"><textarea name="members_cv_fr" rows="10" id="members_cv_fr"><?php echo $members_cv_fr; ?></textarea></td>
					<td width="50%"><textarea name="members_cv_en" rows="10" id="members_cv_en"><?php echo $members_cv_en; ?></textarea></td>
				</tr>
			</table></td>
		</tr>
		<tr>
			<td class="bggrey">Photo vignette (128 pixels de large sur 128 pixels de hauteur)</td>
			<td><input type="file" name="members_filename_t" id="members_filename_t" /> <input name="members_filename_temp" type="hidden" id="members_filename_temp" value="<?php echo $members_filename_temp; ?>" />
			Format .jpg et 1 Mo maximum</td>
		</tr>
		<tr>
			<td class="bggrey">Photo grand format (385 pixels de large maximum et illimité de hauteur)</td>
			<td><input type="file" name="members_filename" id="members_filename" /> Format .jpg et 1 Mo maximum</td>
		</tr>
<?php

	if($members_id) {

?>
		<tr>
			<td>&nbsp;</td>
			<td><img src="<?php echo BASE_PATH; ?>/imgs/members/<?php echo $members_filename_temp; ?>_t.jpg" width="128" height="128" /></td>
		</tr>
		<tr>
			<td class="bggrey">Supprimer ce membre</td>
			<td><input type="checkbox" name="members_del" id="members_del" /></td>
		</tr>
<?php

	}

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

	unset($members, $member, $error);
	$db->sql_close();
	
?>