File: manageusers.php

Recommend this page to a friend!
  Classes of zinsou A.A.E.Mo´se  >  tinyUGatMs  >  manageusers.php  >  Download  
File: manageusers.php
Role: Auxiliary script
Content type: text/plain
Description: script for users management panel
Class: tinyUGatMs
Manage users and groups of events in a database
Author: By
Last change:
Date: 2 years ago
Size: 12,526 bytes
 

Contents

Class file image Download
<?php
session_start();
require_once('config.php');
if(isset($_SESSION['role'])&&$_SESSION['role']=="admin"){
?>
<!DOCTYPE>
	<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>MANAGE USERS</title>	

	<style type="text/css">
		
		 #other {
			font-family: verdana;
			font-weight: bold;
			font-size: 14px;
			font-style:italic;
			color: #FFFFFF;
			background-color: grey;
			width: auto;
			margin-left: 15%;
			margin-right: 15%;
			padding:2%;
		}
		
		label
		{
			display: block ; 
			width: 150px;
			float : left ;
		}
		</style>
	
	</head>
<body>
<div id="other">
<?php
$_GET["action"]=(isset($_GET["action"]))?$_GET["action"]:false;
switch ($_GET["action"]){
case "add":
 ?>
<div>
<form id="users" method="POST">
<fieldset><legend>Users ADD</legend>
<label for="name">username</label><input name="username" type="text" id="username" /><br>
<label for="password">password:</label><input type="password" name="password" id="password" /><br>
<label for="firstname">firstname</label><input name="firstname" type="text" id="firstname" /><br>
<label for="lastname">lastname</label><input name="lastname" type="text" id="lastname"  /><br>
<label for="email">email</label><input name="email" type="text" id="email"  /><br>
<!--<label for="groupid">group's id</label><input name="groupid" type="text" id="groupid" /><br>-->
<label for="groupid">group</label><select name="groupid" id="groupid">
<?php

$query=$bdd->query('SELECT idgroup,name FROM groups');

$query->execute();

while($data2=$query->fetch()){
	if($data2["idgroup"]==$data["groupid"]){
		echo'<option value='.$data2["idgroup"].' id='.$data2["name"].' selected="selected">'.$data2["name"].'</option>';
	}else{
		echo'<option value='.$data2["idgroup"].' id='.$data2["name"].'>'.$data2["name"].'</option>';
	}
}
?></select><br>
<?php
				$role = array
				('user','admin','modo'); 
				echo'<label for="role">role</label>';
				echo'<select name="role" id="role">';
				for($i=0;$i<3;$i++)
				{
					echo'<option value="'.$role[$i].'">'.$role[$i].'</option>';
				}
				echo'</select><br>';
				$statut = array
						('pending','activate'); 
						echo'<label for="statut">statut</label>';
						echo'<select name="statut" id="statut">';
						for($i=0;$i<2;$i++)
						{
							echo'<option value="'.$i.'">'.$statut[$i].'</option>';

						}
				echo'</select><br>';
		?>
</p>
</fieldset>
<p><input type="submit" value="Submit" /></p>
<div id="manage"></div></form></div>
<div><p>What do you want to do ?
<ul><li><a href="./manageusers.php?action=edit">Edit existing user ?</a></li>
<li><a href="./manageusers.php?action=add">Add a new user ?</a></li>
<li><a href="./manageusers.php?action=delete">DELETE a  user ?</a></li>
</ul>
</p></div>
<script language="JavaScript">	
	var manage=document.getElementById("manage");
	// var select=document.getElementById("select");
	var users=document.getElementById("users");
	var username=document.getElementById("username");
	var mail=document.getElementById("email");
	var password=document.getElementById("password");
	var lastname=document.getElementById("lastname");
	var firstname=document.getElementById("firstname");
	var groupid=document.getElementById("groupid");
	var role=document.getElementById("role");
	var statut=document.getElementById("statut");

	
	
	function addEvent (element,event,func) {
		if (element.attachEvent){ 
			element.attachEvent ("on"+event,func) ; 
		}else{
			element.addEventListener(event,func,true) ;
		} 
	}	
	
	
	
	function user_create(){
		manage.innerHTML='<span style="color:blue;">submitting...Please wait...</span>';
		var xhr=new XMLHttpRequest;
		xhr.open("POST","./user_properties.php");
		xhr .setRequestHeader ( "Content-Type" ,"application/x-www-form-urlencoded" );
		xhr.onreadystatechange=function(){
			if(xhr.readyState==4&&xhr.status==200){
				var resp=xhr.responseText;
					manage.innerHTML=resp;
					if(resp=="User successfully added"){
						manage.innerHTML+="<br> You will be redirected in 25 seconds";
						setInterval(window.location="./manageusers.php?action=add",25000);
					}
				
			}	
		}
		xhr.send('add=true&user='+username.value+'&mail='+mail.value+'&password='+password.value+'&groupid='+groupid.options[groupid.selectedIndex ].innerHTML+'&lastname='+lastname.value+'&firstname='+firstname.value+'&statutvalidation='+statut.options[statut.selectedIndex ].value+'&role='+role.options[role.selectedIndex ].innerHTML);
	}
	
	addEvent(users,"submit",function(e){									 
									e = e || window.event ;
									user_create();
									e.returnValue= false;
									if (e.preventDefault) {
										e.preventDefault();
									}
								}													
	);
	
</script>
<?php 
break;
case "edit": ?>

<form id="users" method="POST">
<fieldset><legend>Users management</legend>
<p><select id="groupid">
<option value="none" >none</option>

<?php

$query=$bdd->query('SELECT username FROM users');
$query->execute();
while($data=$query->fetch()){
echo'<option value='.$data["username"].' id='.$data["username"].'>'.$data["username"].'</option>';
}
?></select></p><p>
<!--<input type="submit" value="modify" />-->
</p>
</fieldset><fieldset id="propert"></fieldset></form><div id="manage" ></div>
<div><p>What do you want to do ?
<ul><li><a href="./manageusers.php?action=edit">Edit existing user ?</a></li>
<li><a href="./manageusers.php?action=add">Add a new user ?</a></li>
<li><a href="./manageusers.php?action=delete">DELETE a  user ?</a></li>
</ul>
</p></div>
<script language="JavaScript">	
	var manage=document.getElementById("manage");
	// var select=document.getElementById("select");
	var users=document.getElementById("users");
	var propert=document.getElementById("propert");
	var groupid=document.getElementById("groupid");
	
	
	function addEvent (element,event,func) {
		if (element.attachEvent){ 
			element.attachEvent ("on"+event,func) ; 
		}else{
			element.addEventListener(event,func,true) ;
		} 
	}	
	
	
	function uproperties(){
		if(groupid.options[groupid.selectedIndex ].innerHTML!=="none"){
			manage.innerHTML='<span style="color:blue;">Loading...Please wait...</span>';
			var xhr=new XMLHttpRequest;
			xhr.open("GET","./ajaxusers.php?user="+groupid.options[groupid.selectedIndex ].innerHTML );
			xhr.onreadystatechange=function(){
				if(xhr.readyState==4&&xhr.status==200){
				manage.innerHTML='';
					var resp=xhr.responseText;
						propert.innerHTML=resp;
					
				}	
			}
			xhr.send(null);
		}else{
			propert.innerHTML='This user doesn\'t exists so he doesn\'t  have any properties';
			manage.innerHTML='<span style="color:blue;">Choose another user to modify it</span>';
		}
	}
	
	addEvent(groupid,"change",function(e){									 
									e = e || window.event ;
									uproperties();
									e.returnValue= false;
									if (e.preventDefault) {
										e.preventDefault();
									}
								}													
	);
	
	function uproperties_update(){
		var username=document.getElementById("username");
		var mail=document.getElementById("email");
		var password=document.getElementById("password");
		var lastname=document.getElementById("lastname");
		var firstname=document.getElementById("firstname");
		var groupid=document.getElementById("groupid");
		var oldusername=document.getElementById("oldusername");
		var role=document.getElementById("role");
		var statut=document.getElementById("statut");
		manage.innerHTML='<span style="color:blue;">Updating...Please wait...</span>';
		var xhr=new XMLHttpRequest;
		xhr.open("POST","./user_properties.php");
		xhr .setRequestHeader ( "Content-Type" ,"application/x-www-form-urlencoded" );
		xhr.onreadystatechange=function(){
			if(xhr.readyState==4&&xhr.status==200){
				var resp=xhr.responseText;
					manage.innerHTML=resp;
					if(resp=="User successfully updated"){
						manage.innerHTML +="<br> You will be redirected in 25 seconds";
						setInterval(window.location="./manageusers.php?action=edit",25000);
					}
			}	
		}
		xhr.send('update=true&user='+username.value+'&mail='+mail.value+'&password='+password.value+'&groupid='+groupid.options[groupid.selectedIndex ].innerHTML+'&lastname='+lastname.value+'&firstname='+firstname.value+'&oldusername='+oldusername.value+'&statutvalidation='+statut.options[statut.selectedIndex ].value+'&role='+role.options[role.selectedIndex ].innerHTML);
	}
	
	addEvent(users,"submit",function(e){									 
									e = e || window.event ;
									uproperties_update();
									e.returnValue= false;
									if (e.preventDefault) {
										e.preventDefault();
									}
								}													
	);
	
</script>
<?php
break;
case "delete":
?>
Delete a user will be definitive, think about it...!
<form id="users" method="POST">
<fieldset><legend>Users management</legend>
<p><select id="select">
<option value="none" >none</option>

<?php

$query=$bdd->query('SELECT username FROM users');
$query->execute();

while($data=$query->fetch()){
echo'<option value='.$data["username"].' id='.$data["username"].'>'.$data["username"].'</option>';
}
?></select></p><p>
<!--<input type="submit" value="modify" />-->
</p>
</fieldset><p id="propert"></p></form><div id="manage" ></div><div><p>What do you want to do ?
<ul><li><a href="./manageusers.php?action=edit">Edit existing user ?</a></li>
<li><a href="./manageusers.php?action=add">Add a new user ?</a></li>
<li><a href="./manageusers.php?action=delete">DELETE a  user ?</a></li>
</ul>
</p></div>
<script language="JavaScript">	
	var manage=document.getElementById("manage");
	var select=document.getElementById("select");
	var users=document.getElementById("users");
	var propert=document.getElementById("propert");
	
	function addEvent (element,event,func) {
		if (element.attachEvent){ 
			element.attachEvent ("on"+event,func) ; 
		}else{
			element.addEventListener(event,func,true) ;
		} 
	}	
	
	
	function uproperties(){
		if(select.options[select.selectedIndex ].innerHTML!=="none"){
			manage.innerHTML='<span style="color:blue;">Loading...Please wait...</span>';
			var xhr=new XMLHttpRequest;
			xhr.open("GET","./ajaxusers.php?user="+select.options[select.selectedIndex ].innerHTML+"&delete=true"
			);
			xhr.onreadystatechange=function(){
				if(xhr.readyState==4&&xhr.status==200){
				manage.innerHTML='';
					var resp=xhr.responseText;
						propert.innerHTML=resp;
					
				}	
			}
			xhr.send(null);
		}else{
			propert.innerHTML='This user doesn\'t exist so it doesn\'t  have any properties';
			manage.innerHTML='<span style="color:blue;">Choose another group to modify it</span>';
		}
	}
	
	addEvent(select,"change",function(e){									 
									e = e || window.event ;
									uproperties();
									e.returnValue= false;
									if (e.preventDefault) {
										e.preventDefault();
									}
								}													
	);
	
	function delete_user(){
		manage.innerHTML='<span style="color:blue;">Updating...Please wait...</span>';
		var xhr=new XMLHttpRequest;
		xhr.open("POST","./user_properties.php");
		xhr .setRequestHeader ( "Content-Type" ,"application/x-www-form-urlencoded" );
		xhr.onreadystatechange=function(){
			if(xhr.readyState==4&&xhr.status==200){
				var resp=xhr.responseText;
					manage.innerHTML=resp;
				if(resp=='User deleted'){
						manage.innerHTML +="<br> You will be redirected in 25 seconds";
						setInterval(window.location="./manageusers.php?action=delete",25000);
					}
				}
			}	
		xhr.send('user='+select.options[select.selectedIndex ].innerHTML);
	}
	
	addEvent(users,"submit",function(e){									 
									e = e || window.event ;
									delete_user();
									e.returnValue= false;
									if (e.preventDefault) {
										e.preventDefault();
									}
								}													
	);
	
</script>
<?php
break;
default:
echo'<div><p>What do you want to do ?
<ul><li><a href="./manageusers.php?action=edit">Edit existing user ?</a></li>
<li><a href="./manageusers.php?action=add">Add a new user ?</a></li>
<li><a href="./manageusers.php?action=delete">DELETE a  user ?</a></li>
</ul>
</p></div>';
break;
}
}else{
		$url="./connexion.php";
		echo '<script type="text/javascript">window.location="'.$url.'";</script>';
		exit;
	}
?>
</div>
</body>
</html>

For more information send a message to info at phpclasses dot org.