<?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 = $associes = $collaborateurs = $color = $error = array();

	if(isset($_POST['associes_submit'])) {
		foreach($_POST['associes_members'] as $key => $value) {
			$sql = 'UPDATE ' . MOZAIC_TABLE . ' SET ' . $db->sql_insert_update('UPDATE', array(
				'members_id'		=>	(int) $_POST['associes_members'][$key],
				'mozaic_colors_id'	=>	(int) $_POST['associes_colors'][$key]
			)) . ' WHERE mozaic_id = ' . $key;
			mysql_query($sql);
		}
		$error[] = 'Mise à jour des associés.';
	}

	if(isset($_POST['collaborateurs_submit'])) {
		foreach($_POST['collaborateurs_members'] as $key => $value) {
			$sql = 'UPDATE ' . MOZAIC_TABLE . ' SET ' . $db->sql_insert_update('UPDATE', array(
				'members_id'		=>	(int) $_POST['collaborateurs_members'][$key],
				'mozaic_colors_id'	=>	(int) $_POST['collaborateurs_colors'][$key]
			)) . ' WHERE mozaic_id = ' . $key;
			mysql_query($sql);
		}
		$error[] = 'Mise à jour des collaborateurs.';
	}

	$sql = 'SELECT members_id, members_name FROM ' . MEMBERS_TABLE . ' ORDER BY members_name ASC';
	$result = mysql_query($sql);
	while($row = mysql_fetch_assoc($result)) {
		$members[$row['members_id']] = array(
			'members_name'	=> $row['members_name']
		);
	}

	mysql_free_result($result);
	
	$sql = 'SELECT * FROM ' . MOZAIC_TABLE . ' WHERE category_id = 14';
	$result = mysql_query($sql);
	while($row = mysql_fetch_assoc($result)) {
		$associes[] = array(
			'mozaic_id'			=> $row['mozaic_id'],
			'members_id'		=> $row['members_id'],
			'category_id'		=> $row['category_id'],
			'mozaic_colors_id'	=> $row['mozaic_colors_id']
		);
	}
	mysql_free_result($result);
	
	$sql = 'SELECT * FROM ' . MOZAIC_TABLE . ' WHERE category_id = 15';
	$result = mysql_query($sql);
	while($row = mysql_fetch_assoc($result)) {
		$collaborateurs[] = array(
			'mozaic_id'			=> $row['mozaic_id'],
			'members_id'		=> $row['members_id'],
			'category_id'		=> $row['category_id'],
			'mozaic_colors_id'	=> $row['mozaic_colors_id']
		);
	}
	mysql_free_result($result);

	$sql = 'SELECT * FROM ' . MOZAIC_COLORS_TABLE;
	$result = mysql_query($sql);
	while($row = mysql_fetch_assoc($result)) {
		$color[$row['mozaic_colors_id']] = array(
			'mozaic_colors_code'	=> $row['mozaic_colors_code']
		);
	}
	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>
</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>Associés et collaborateurs</h1>
<h2>Associés</h2>
<form name="associes_form" action="" method="post">
	<table style="width: auto;">
<?php
	$k = 0;
	for($i = 0; $i < 5; $i++) {

?>
		<tr>
<?php

		for($j = 0; $j < 3; $j++) {

?>
			<td height="128">
				<select name="associes_members[<?php echo $associes[$k]['mozaic_id']; ?>]" id="associes_members">
					<option value="0">Aucun</option>
<?php

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

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

	}

?>
				</select>
				<br />
				<select name="associes_colors[<?php echo $associes[$k]['mozaic_id']; ?>]" id="associes_colors">
<?php

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

?>
					<option value="<?php echo $key; ?>" style="background-color: #<?php echo $value['mozaic_colors_code']; ?>;"<?php echo (($associes[$k]['mozaic_colors_id'] == $key) ? ' selected="selected"' : ''); ?>>Couleur <?php echo $key; ?></option>
<?php

	}

?>
				</select>
			</td>
<?php
			$k++;
		}

?>
		</tr>
<?php

	}

?>
	</table>
	<table>
		<tr>
			<td><input type="submit" name="associes_submit" id="associes_submit" value="Valider" class="bouton" /></td>
		</tr>
	</table>
</form>
<h2>Collaborateurs</h2>
<form name="collaborateurs_form" action="" method="post">
	<table style="width: auto;">
<?php
	$k = 0;
	for($i = 0; $i < 4; $i++) {

?>
		<tr>
<?php

		for($j = 0; $j < 7; $j++) {

?>
			<td height="128">
				<select name="collaborateurs_members[<?php echo $collaborateurs[$k]['mozaic_id']; ?>]" id="associes_members">
					<option value="0">Aucun</option>
<?php

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

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

	}

?>
				</select>
				<br />
				<select name="collaborateurs_colors[<?php echo $collaborateurs[$k]['mozaic_id']; ?>]" id="associes_colors">
<?php

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

?>
					<option value="<?php echo $key; ?>" style="background-color: #<?php echo $value['mozaic_colors_code']; ?>;"<?php echo (($collaborateurs[$k]['mozaic_colors_id'] == $key) ? ' selected="selected"' : ''); ?>>Couleur <?php echo $key; ?></option>
<?php

	}

?>
				</select>
			</td>
<?php
			$k++;
		}

?>
		</tr>
<?php

	}

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

	unset($members, $associes, $collaborateurs, $color, $error);
	$db->sql_close();
	
?>