<?php
/**
* MiniClick
* Gestionnaire de tableau collaboratif
* Copyright (C) 2006 Guillaume Monet <gm@ibreizh.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 
*
* @package miniclick
* @version 5.0.0
*/
?>
<script type="text/javascript">
function checkAll(){
	var inpNodes = document.getElementById('tableau1').getElementsByTagName('input');
	for(var i in inpNodes){
		var inp = inpNodes[i];
		if (inp.type == 'checkbox'){
			inp.checked = !inp.checked;
		}
	}
}
</script>
<?php

create_droit_table($_GET['module_id']);

function create_droit_table($module_id){
	

	$module_query = "SELECT `Table` FROM MODULE WHERE Module_Id='$module_id' AND App_Id='".$_SESSION['app_id']."'";
	$modules = mysql_query($module_query);
	$module = mysql_fetch_object($modules);
	$table = $module->Table;

	$dest=$_SERVER['REQUEST_URI'];

	if($_POST['action'] == "VAL"){
		valide_table($module_id);
	}
	echo "<div id=\"tableau_droit\">";
	$headers = array("Role");
	//Description de la table
	$fields = mysql_query("DESCRIBE ".$table);
	while($field = mysql_fetch_object($fields)){
		array_push($headers,$field->Field);
	}
	//Debut de la table 
	echo "<form id=\"droit\" name=\"droit\" action=\"".stripslashes($dest)."\" method=\"POST\">";
	echo "<input type=\"hidden\" id=\"action\" name=\"action\">";
	echo "<table id=\"tableau1\" border='0' cellspacing='1' cellpadding='0'>";
	echo "<tr>";
	for($i = 0 ; $i < count($headers) ; $i++){
		echo "<th>".$headers[$i]."<br>RD | WR | WF</th>";
	}
	
	$roles = mysql_query("SELECT Role_Id,Nom FROM MINICLICK.ROLE");

	$back = 0;
	while($role = mysql_fetch_object($roles)){	
		$back = 1 - $back;
		if($back==1){
        		$colorback = "style = \"background-color: rgb(255, 255, 255);\""; 
		}else{
			$colorback = "style = \"background-color: rgb(225, 225, 225);\"";
		}
		echo "<tr $colorback>";
		echo "<td>$role->Nom</td>";
		for($i = 1 ; $i < count($headers) ; $i++){
			echo "<td>";
			$perms = mysql_query("SELECT Read_Perm,Write_Perm,Workflow_Perm FROM DROIT WHERE Module_Id='$module_id' AND Colonne='".$headers[$i]."' AND Role_Id='$role->Role_Id'");
			$perm = mysql_fetch_object($perms);
			echo "<input type=\"checkbox\" size=\"3\" name='RD".$headers[$i]."$role->Role_Id'";
			if($perm->Read_Perm == "1"){
				echo " checked ";
			}
			echo ">";
			echo "<input type=\"checkbox\" size=\"3\" name='WR".$headers[$i]."$role->Role_Id'";
			if($perm->Write_Perm == "1"){
				echo " checked ";
			}
			echo ">";
			echo "<input type=\"checkbox\" size=\"3\" name='WF".$headers[$i]."$role->Role_Id'";
			if($perm->Workflow_Perm == "1"){
				echo " checked ";
			}
			echo ">";
			echo "</td>";
		}
		echo "</tr>";
	}
	echo "</table>";
	echo "<a href='javascript:document.getElementById(\"action\").value=\"VAL\";document.getElementById(\"droit\").submit();' >Valider</a>";
	echo "</form>";
	echo "<a href=\"javascript:checkAll();\">Tout cocher</a>";
	echo "</div>";
}

function valide_table($module_id){

	$module_query = "SELECT `Table` FROM MODULE WHERE Module_Id='$module_id' AND App_Id='".$_SESSION['app_id']."'";
	$modules = mysql_query($module_query);
	$module = mysql_fetch_object($modules);
	$table = $module->Table;

	$headers = array("Role");

	//Description de la table
	$fields = mysql_query("DESCRIBE ".$table);
	while($field = mysql_fetch_object($fields)){
		array_push($headers,$field->Field);
	}	
	$roles = mysql_query("SELECT Role_Id,Nom FROM ROLE");
	while($role = mysql_fetch_object($roles)){	
		for($i = 1 ; $i < count($headers) ; $i++){

			$perms = mysql_query("SELECT * FROM DROIT WHERE Module_Id='$module_id' AND Colonne='".$headers[$i]."' AND Role_Id='$role->Role_Id'");
			$perm = mysql_fetch_object($perms);
			$rdok = "RD".$headers[$i]."$role->Role_Id";
			$wrok = "WR".$headers[$i]."$role->Role_Id";
			$wfok = "WF".$headers[$i]."$role->Role_Id";
			$val_rd = $_POST[$rdok];
			$val_wr = $_POST[$wrok];
			$val_wf = $_POST[$wfok];

			if($val_rd == ""){
				$val_rd = "0";
			}else{
				$val_rd = "1";
			}
			if($val_wr == ""){
				$val_wr = "0";
			}else{
				$val_wr = "1";
			}
			if($val_wf == ""){
				$val_wf = "0";
			}else{
				$val_wf = "1";
			}

			if($perm->Read_Perm != "" && $perm->Write_Perm != "" && $perm->Workflow_Perm != ""){
				mysql_query("UPDATE DROIT SET Read_Perm='$val_rd',Write_Perm='$val_wr',Workflow_Perm='$val_wf' WHERE Module_Id='$module_id' AND Colonne='".$headers[$i]."' AND Role_Id='$role->Role_Id'");
			}else{
				mysql_query("INSERT INTO DROIT VALUES('','$module_id','".$headers[$i]."','$role->Role_Id','$val_rd','$val_wr','$val_wf')");
			}
		}
	}
}
?>
