<?php
/* $Id: rights_manage.php,v 1.3 2004/07/15 13:24:06 k-fish Exp $ */
error_reporting  (E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
$myEnv["module"] = "calendar";
$myEnv["output"] = "html";

header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"'); //MSIE6 P3P header
include("../../config.inc.php");
include(INCLUDEPATH . "container.inc.php");
//include(INCLUDEPATH . "mSelectBox.class.php");

// adding file revision into global array
if(function_exists("revisionInit")) revisionInit("\$Revision: 1.3 $", __FILE__);

$action = (isset($_GET["action"])) ? $_GET["action"] : (IsSet($_POST["action"]) ? $_POST["action"] : "");

if (!isset($action)) $action = "view";
    
$currentuserid = $_SESSION["MGW"]->userid;


// ====================================
// CREATE / UPDATE RIGHTS
// ====================================
if (isset($action) && $action == "set"){

//set new rights

$sql = "SELECT id, ".$conn->Concat("firstname", "' '", "lastname")." AS name
		FROM mgw_users
		WHERE NOT(level=".UDELETED.") AND id !='$currentuserid'
		ORDER BY name";
if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
while($row = $res->FetchRow()) {
	$userid = $row["id"];
	//select current rights:
	$sql2 ="SELECT a.*, b.id, ".$conn->Concat("b.firstname", "' '", "b.lastname")." AS name
		FROM mgw_calendar_rights a, mgw_users b
		WHERE NOT(b.level=".UDELETED.") AND a.id_owner='$currentuserid' AND a.id_user='$userid' AND a.id_user = b.id";
	if(!$res2 = $conn->Execute($sql2)) exit(showSQLerror($sql2, $conn->ErrorMsg(), __LINE__, __FILE__));
	$row2 = $res2->FetchRow();
	
	
	
	if (IsSet($_POST["right_".$userid]) && $_POST["right_".$userid]!=""){
		    
		$new_modify_right = ($_POST["right_".$userid]=="M" ? 1 : 0);
		$new_view_all_right = ($_POST["right_".$userid]=="V" ? 1 : 0);
		$new_default_right = ($_POST["right_".$userid]=="D" ? 1 : 0);
				
			//if record exits : 
			if ($res2->NumRows()>0) {
			    			
				$old_modify_right = $row2["modify"];
				$old_view_all_right = $row2["view_all"];
				$old_default_right = ($old_modify_right != 1 && $old_view_all_right !=1) ? 1 : 0;
				//only if there are modifications : update or delete
				if ( ! ($new_modify_right==$old_modify_right && 
						$new_view_all_right==$old_view_all_right && 
						$old_default_right==$new_default_right
						)
					){
					if ($new_default_right==1) { //default -> delete record in mgw_calendar_rights
					    $sql_delete = "	DELETE FROM mgw_calendar_rights 
										WHERE id_owner='$currentuserid' AND id_user='$userid'";
						if(!$res_delete = $conn->Execute($sql_delete)) exit(showSQLerror($sql_delete, $conn->ErrorMsg(), __LINE__, __FILE__));
					}
					else{
						$sql_update = "	UPDATE mgw_calendar_rights 
										SET modify='$new_modify_right', view_all='$new_view_all_right'
										WHERE id_owner='$currentuserid' AND id_user='$userid'";
						if(!$res_update = $conn->Execute($sql_update)) exit(showSQLerror($sql_update, $conn->ErrorMsg(), __LINE__, __FILE__));
					}
				}
			}	
			//else : insert if right != default
			else if ($new_default_right!=1) {
			     $sql_insert = "	INSERT INTO mgw_calendar_rights (id_owner, id_user, modify, view_all) 
								VALUES ('$currentuserid','$userid','$new_modify_right','$new_view_all_right')";
				if(!$res_insert = $conn->Execute($sql_insert)) exit(showSQLerror($sql_insert, $conn->ErrorMsg(), __LINE__, __FILE__));	
			}
		}// end if isset....
	}//end while		

	unset($action);
	$modif_ok = 1; // there should be a complete checking... 
	$action="view"; // now : show new rights...
}


// ====================================
// SHOW EXISTING RIGHTS
// ====================================
if (isset($action) && $action == "view"){

//get existing rights:
$ar_rights = array();

$sql = "SELECT id, ".$conn->Concat("firstname", "' '", "lastname")." AS name
		FROM mgw_users
		WHERE NOT(level=".UDELETED.") AND id !='$currentuserid'
		ORDER BY name";
if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
while($row = $res->FetchRow()) {
	
	$userid = $row["id"];
		
	$sql2 ="SELECT a.*, b.id, ".$conn->Concat("b.firstname", "' '", "b.lastname")." AS name
		FROM mgw_calendar_rights a, mgw_users b
		WHERE NOT(b.level=".UDELETED.") AND a.id_owner='$currentuserid' AND a.id_user='$userid' AND a.id_user = b.id";
	if(!$res2 = $conn->Execute($sql2)) exit(showSQLerror($sql2, $conn->ErrorMsg(), __LINE__, __FILE__));
	$row2 = $res2->FetchRow();
	
	$ar_rights[]= array("id" => $userid,
						"name" => $row["name"],
						"modify" => ($row2["modify"]=="1" ? "CHECKED" : ""),
						"view_all" => ($row2["view_all"]=="1" ? "CHECKED" : ""),
						"default" => ($row2["view_all"]!="1" && $row2["modify"]!="1" ? "CHECKED" : ""),
						);	

}

$back = /*(ereg("rights_manage.php",$_SERVER["HTTP_REFERER"]) ?*/ "index.php" ;/*: $_SERVER["HTTP_REFERER"]);*/
if (isset($modif_ok) && $modif_ok==1) {
	$smarty->assign("warning_message",Lang::getLanguageString("gentemp_operation_completed"));    
}
if (isset($modif_ok) && $modif_ok==0) {
	$smarty->assign("warning_message",Lang::getLanguageString("!--ERROR--!")); //TODO : make a translation
}
$smarty->assign("urlback",$back);
$smarty->assign("tab_rights",$ar_rights);
$smarty->display("calendar_rights_manage.tpl");
}

?>