<?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);
	
	$metiers_id = isset($_GET['metiers_id']) ? (is_numeric($_GET['metiers_id']) ? $_GET['metiers_id'] : 0) : 0;
	$metiers_details_edit_id = isset($_POST['metiers_details_edit_id']) ? (is_numeric($_POST['metiers_details_edit_id']) ? $_POST['metiers_details_edit_id'] : 0) : 0;
	$metiers_details_edit_type = isset($_POST['metiers_details_edit_type']) ? (is_numeric($_POST['metiers_details_edit_type']) ? $_POST['metiers_details_edit_type'] : 0) : 0;
	$metier = $metiers = $error = array();

	if(isset($_POST['metiers_submit']) and isset($_POST['metiers_del']) and $metiers_id != 0) {
		$sql_1 = 'DELETE FROM ' . METIERS_TABLE . ' WHERE metiers_id = ' . $metiers_id . ' LIMIT 1';
		$sql_2 = 'DELETE FROM ' . METIERS_DETAILS_TABLE . ' WHERE metiers_id = ' . $metiers_id;
		if(mysql_query($sql_1) and mysql_query($sql_2)) {
			$error[] = 'La catégorie métier et les métiers associés à celle-ci ont été supprimé.';
		}
	} elseif(isset($_POST['metiers_submit']) and $metiers_id != 0) {
		$metiers_name_en = trim($_POST['metiers_name_en']);
		$sql = 'UPDATE ' . METIERS_TABLE . ' SET ' . $db->sql_insert_update('UPDATE', array(
			'metiers_name_fr'	=>	(string) $_POST['metiers_name_fr'],
			'metiers_name_en'	=>	(string) empty($metiers_name_en) ? NULL : $metiers_name_en,
			'metiers_sort'		=>	(int) $_POST['metiers_sort']
		)) . ' WHERE metiers_id = ' . $metiers_id;
		if(mysql_query($sql)) {
			$error[] = 'La catégorie métier a bien été mise à jour.';
		}
	} elseif(isset($_POST['metiers_submit']) and $metiers_id == 0) {
		$metiers_name_en = trim($_POST['metiers_name_en']);
		$sql = 'INSERT INTO ' . METIERS_TABLE . $db->sql_insert_update('INSERT', array(
			'metiers_name_fr'	=>	(string) $_POST['metiers_name_fr'],
			'metiers_name_en'	=>	(string) empty($metiers_name_en) ? NULL : $metiers_name_en,
			'metiers_sort'		=>	(int) $_POST['metiers_sort']
		));
		if(mysql_query($sql)) {
			$error[] = 'La catégorie métier a bien été ajouté.';
		}
	}

	if(isset($_POST['metiers_details_add_submit'])) {
		$metiers_details_title_en = trim($_POST['metiers_details_title_en']);
		$metiers_details_text_en = trim($_POST['metiers_details_text_en']);
		$sql = 'INSERT INTO ' . METIERS_DETAILS_TABLE . $db->sql_insert_update('INSERT', array(
			'metiers_id'				=>	(int) $_POST['metiers_details_add_id'],
			'metiers_details_type'		=>	(int) $_POST['metiers_details_add_type'],
			'metiers_details_title_fr'	=>	(string)$_POST['metiers_details_title_fr'],
			'metiers_details_title_en'	=>	(string) empty($metiers_details_title_en) ? NULL : $metiers_details_title_en,
			'metiers_details_text_fr'	=>	(string) $_POST['metiers_details_text_fr'],
			'metiers_details_text_en'	=>	(string) empty($metiers_details_text_en) ? NULL : $metiers_details_text_en,
			'metiers_details_sort'		=>	(int) $_POST['metiers_details_add_sort']
		));
		if(mysql_query($sql)) {
			$error[] = 'La catégorie métier a bien été ajouté.';
		}
	}

	if(isset($_POST['metiers_details_submit'])) {
		foreach($_POST['metiers_details_type'] as $key => $value) {
			if(isset($_POST['metiers_details_del'][$key])) {
				$sql = 'DELETE FROM ' . METIERS_DETAILS_TABLE . ' WHERE metiers_details_id = ' . $key . ' LIMIT 1';
				if(mysql_query($sql)) {
					$error[0] = 'Métiers mis à jour.';
				}
			} else {
				$metiers_details_title_en = trim($_POST['metiers_details_title_en'][$key]);
				$metiers_details_text_en = trim($_POST['metiers_details_text_en'][$key]);
				$sql = 'UPDATE ' . METIERS_DETAILS_TABLE . ' SET ' . $db->sql_insert_update('UPDATE', array(
					'metiers_id'				=>	(int) $_POST['metiers_details_id'][$key],
					'metiers_details_type'		=>	(int) $_POST['metiers_details_type'][$key],
					'metiers_details_title_fr'	=>	(string)$_POST['metiers_details_title_fr'][$key],
					'metiers_details_title_en'	=>	(string) empty($metiers_details_title_en) ? NULL : $metiers_details_title_en,
					'metiers_details_text_fr'	=>	(string) $_POST['metiers_details_text_fr'][$key],
					'metiers_details_text_en'	=>	(string) empty($metiers_details_text_en) ? NULL : $metiers_details_text_en,
					'metiers_details_sort'		=>	(int) $_POST['metiers_details_sort'][$key]
				)) . ' WHERE metiers_details_id = ' . $key;
				if(mysql_query($sql)) {
					$error[0] = 'Métiers mis à jour.';
				}
			}
		}
	}


	$sql = 'SELECT * FROM ' . METIERS_TABLE . ' ORDER BY metiers_sort ASC';
	$result = mysql_query($sql);
	while($row = mysql_fetch_assoc($result)) {
		$metier[$row['metiers_id']] = array(
			'metiers_name_fr'	=>	$row['metiers_name_fr'],
			'metiers_name_en'	=>	$row['metiers_name_en'],
			'metiers_sort'		=>	$row['metiers_sort']
		);
	}
	mysql_free_result($result);
	
	
	if(isset($_POST['metiers_details_edit_submit'])) {
		$sql = 'SELECT * FROM ' . METIERS_DETAILS_TABLE . ' WHERE metiers_id = ' . $metiers_details_edit_id .' AND metiers_details_type = ' . $metiers_details_edit_type .' ORDER BY metiers_details_sort ASC';
		$result = mysql_query($sql);
		while($row = mysql_fetch_assoc($result)) {
			$metiers[$row['metiers_details_id']] = array(
				'metiers_id'				=>	$row['metiers_id'],
				'metiers_details_type'		=>	$row['metiers_details_type'],
				'metiers_details_title_fr'	=>	$row['metiers_details_title_fr'],
				'metiers_details_title_en'	=>	$row['metiers_details_title_en'],
				'metiers_details_text_fr'	=>	$row['metiers_details_text_fr'],
				'metiers_details_text_en'	=>	$row['metiers_details_text_en'],
				'metiers_details_sort'		=>	$row['metiers_details_sort']
			);
		}
		mysql_free_result($result);
	}
	
	if($metiers_id) {
		$sql = 'SELECT * FROM ' . METIERS_TABLE . ' WHERE metiers_id = ' . $metiers_id;
		$result = mysql_query($sql);
		$categorys = mysql_fetch_assoc($result);
		mysql_free_result($result);

		$metiers_name_fr = $categorys['metiers_name_fr'];
		$metiers_name_en = $categorys['metiers_name_en'];
		$metiers_sort = $categorys['metiers_sort'];
	} else {
		$metiers_name_fr = '';
		$metiers_name_en = '';
		$metiers_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 =[
<?php

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

?>
		'metiers_details_text_fr[<?php echo $key; ?>]',
		'metiers_details_text_en[<?php echo $key; ?>]',
<?php

	}

?>		
	'metiers_details_text_fr', 'metiers_details_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>
<?php

	}

?>
<h1>Les métiers</h1>
<h2>Ajouter une catégorie de métier</h2>
<form name="metiers_edit_form" action="" method="get">
	<table>
		<tr>
			<td width="30%" class="bggrey">Sélectionner une action</td>
			<td width="70%">
				<select name="metiers_id" id="metiers_id" onchange="this.form.submit();">
					<option value="0">Sélectionner</option>
					<option value="0">Ajouter un métier</option>
<?php

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

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

	}

?>
				</select>
			</td>
		</tr>
	</table>
</form>
<form name="metiers_form" action="" method="post">
	<table>
		<tr>
			<td width="30%" class="bggrey">Nom du métier</td>
			<td width="70%"><table>
				<tr class="bggrey">
					<td width="50%">Français</td>
					<td width="50%">Anglais</td>
				</tr>
				<tr>
					<td><input name="metiers_name_fr" type="text" id="metiers_name_fr" value="<?php echo $metiers_name_fr; ?>" /></td>
					<td><input name="metiers_name_en" type="text" id="metiers_name_en" value="<?php echo $metiers_name_en; ?>" /></td>
				</tr>
			</table></td>
		</tr>
		<tr>
			<td class="bggrey">Position</td>
			<td><input name="metiers_sort" type="text" id="metiers_sort" value="<?php echo $metiers_sort; ?>" /></td>
		</tr>
<?php

	if($metiers_id) {

?>
		<tr>
			<td class="bggrey">Supprimer la rubrique</td>
			<td><input type="checkbox" name="metiers_del" id="metiers_del" /> 
			<span style="color:#f00;"><strong>Attention</strong> tous les métiers associés à la catégorie seront également supprimés.</span></td>
		</tr>
<?php

	}

?>
		<tr>
			<td><input type="submit" name="metiers_submit" id="metiers_submit" value="Valider" class="bouton" /></td>
			<td>&nbsp;</td>
		</tr>
	</table>
</form>
<h2>Ajouter un métier</h2>
<form name="metier_add_form" action="" method="post">
	<table>
		<tr>
			<td class="bggrey">Métier</td>
			<td>
				<select name="metiers_details_add_id" id="metiers_details_add_id">
					<option value="0">Sélectionner</option>
<?php
	
	foreach($metier as $key => $value) {
	
?>
						<option value="<?php echo $key; ?>"><?php echo $value['metiers_name_fr']; ?></option>
<?php
	
	}
	
?>
				</select>
			</td>
		</tr>
		<tr>
			<td class="bggrey">Verbe</td>
			<td>
				<select name="metiers_details_add_type" id="metiers_details_edit_type">
					<option value="0">Sélectionner</option>
					<option value="1">Anticiper</option>
					<option value="2">Imaginer</option>
					<option value="3">Défendre</option>
					<option value="4">Evaluer</option>
				</select>
			</td>
		</tr>
		<tr>
			<td class="bggrey">Titre</td>
			<td><table>
				<tr class="bggrey">
					<td width="50%">Français</td>
					<td width="50%">Anglais</td>
					</tr>
				<tr>
					<td><input name="metiers_details_title_fr" type="text" id="metiers_details_title_fr" /></td>
					<td><input name="metiers_details_title_en" type="text" id="metiers_details_title_en" /></td>
					</tr>
			</table></td>
		</tr>
		<tr>
			<td class="bggrey">Texte</td>
			<td><table>
				<tr>
					<td width="50%"><textarea name="metiers_details_text_fr" id="metiers_details_text_fr" cols="45" rows="5"></textarea></td>
					<td width="50%"><textarea name="metiers_details_text_en" id="metiers_details_text_en" cols="45" rows="5"></textarea></td>
				</tr>
			</table></td>
		</tr>
		<tr>
			<td class="bggrey">Ordre</td>
			<td><input name="metiers_details_add_sort" type="text" id="metiers_details_add_sort" /></td>
		</tr>
		<tr>
			<td width="30%"><input type="submit" name="metiers_details_add_submit" id="metiers_details_add_submit" value="Ajouter" class="bouton" /></td>
			<td width="70%">&nbsp;</td>
		</tr>
	</table>
</form>
<h2>Gestion des métiers</h2>
<form name="metier_edit_form" action="" method="post">
<table>
	<tr>
		<td class="bggrey">Métier</td>
		<td>
			<select name="metiers_details_edit_id" id="metiers_details_edit_id">
				<option value="0">Sélectionner</option>
<?php

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

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

	}

?>
			</select>
		</td>
	</tr>
	<tr>
		<td class="bggrey">Verbe</td>
		<td>
			<select name="metiers_details_edit_type" id="metiers_details_edit_type">
				<option value="0">Sélectionner</option>
				<option value="1"<?php echo (($metiers_details_edit_type  == 1) ? ' selected="selected"' : ''); ?>>Anticiper</option>
				<option value="2"<?php echo (($metiers_details_edit_type  == 2) ? ' selected="selected"' : ''); ?>>Imaginer</option>
				<option value="3"<?php echo (($metiers_details_edit_type  == 3) ? ' selected="selected"' : ''); ?>>Défendre</option>
				<option value="4"<?php echo (($metiers_details_edit_type  == 4) ? ' selected="selected"' : ''); ?>>Evaluer</option>
			</select>
		</td>
	</tr>
	<tr>
		<td width="30%"><input type="submit" name="metiers_details_edit_submit" id="metiers_details_edit_submit" value="Lancer la recherche" class="bouton" /></td>
		<td width="70%">&nbsp;</td>
	</tr>
</table>
</form>
<br />
<form action="" method="post" name="metier_form">
	<table>
<?php

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

?>
		<tr>
			<td class="bggrey">Métier</td>
			<td>
				<select name="metiers_details_id[<?php echo $key; ?>]" id="metiers_details_id[<?php echo $key; ?>]">
<?php

		foreach($metier as $mkey => $mvalue) {

?>
					<option value="<?php echo $mkey; ?>"<?php echo (($value['metiers_id'] == $mkey) ? ' selected="selected"' : ''); ?>><?php echo $mvalue['metiers_name_fr']; ?></option>
<?php

		}

?>
				</select>
			</td>
		</tr>
		<tr>
			<td class="bggrey">Verbe</td>
			<td>
				<select name="metiers_details_type[<?php echo $key; ?>]" id="metiers_details_type[<?php echo $key; ?>]">
					<option value="1"<?php echo (($metiers_details_edit_type  == 1) ? ' selected="selected"' : ''); ?>>Anticiper</option>
					<option value="2"<?php echo (($metiers_details_edit_type  == 2) ? ' selected="selected"' : ''); ?>>Imaginer</option>
					<option value="3"<?php echo (($metiers_details_edit_type  == 3) ? ' selected="selected"' : ''); ?>>Défendre</option>
					<option value="4"<?php echo (($metiers_details_edit_type  == 4) ? ' selected="selected"' : ''); ?>>Evaluer</option>
				</select>
			</td>
		</tr>
		<tr>
			<td width="30%" class="bggrey">Titre</td>
			<td width="70%">
				<table>
					<tr class="bggrey">
						<td width="50%">Français</td>
						<td width="50%">Anglais</td>
					</tr>
					<tr>
						<td><input name="metiers_details_title_fr[<?php echo $key; ?>]" type="text" id="metiers_details_title_fr[<?php echo $key; ?>]" value="<?php echo $value['metiers_details_title_fr']; ?>" /></td>
						<td><input name="metiers_details_title_en[<?php echo $key; ?>]" type="text" id="metiers_details_title_en[<?php echo $key; ?>]" value="<?php echo $value['metiers_details_title_en']; ?>" /></td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td class="bggrey">Texte</td>
			<td>
				<table>
					<tr>
						<td width="50%"><textarea name="metiers_details_text_fr[<?php echo $key; ?>]" id="metiers_details_text_fr[<?php echo $key; ?>]" cols="45" rows="5"><?php echo $value['metiers_details_text_fr']; ?></textarea></td>
						<td width="50%"><textarea name="metiers_details_text_en[<?php echo $key; ?>]" id="metiers_details_text_en[<?php echo $key; ?>]" cols="45" rows="5"><?php echo $value['metiers_details_text_en']; ?></textarea></td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td class="bggrey">Ordre</td>
			<td><input name="metiers_details_sort[<?php echo $key; ?>]" type="text" id="metiers_details_sort[<?php echo $key; ?>]" value="<?php echo $value['metiers_details_sort']; ?>" /></td>
		</tr>
<?php

		if($metiers_details_edit_id and $metiers_details_edit_type) {

?>
		<tr>
			<td class="bggrey">Supprimer</td>
			<td><input type="checkbox" name="metiers_details_del[<?php echo $key; ?>]" id="metiers_details_del[<?php echo $key; ?>]" /></td>
		</tr>
<?php

		}

?>
		<tr>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
		</tr>
<?php

	}

	if(($metiers_details_edit_id and $metiers_details_edit_type)) {

?>
		<tr>
			<td><input type="submit" name="metiers_details_submit" id="metiers_details_submit" value="Valider" class="bouton" /></td>
			<td>&nbsp;</td>
		</tr>
<?php

		}

?>
	</table>
</form>
</body>
</html>
<?php

	unset($metier, $metiers, $error);
	$db->sql_close();
	
?>