2011-04-16 19:49:57 +04:00
|
|
|
$(document).ready(function(){
|
2011-04-17 01:26:55 +04:00
|
|
|
// Vars we need
|
|
|
|
var uid = "";
|
|
|
|
var gid = "";
|
2011-04-17 20:05:49 +04:00
|
|
|
var togglepassword = "";
|
|
|
|
var togglegroup = "";
|
|
|
|
|
|
|
|
//#########################################################################
|
|
|
|
// Stuff I don't understand
|
|
|
|
//#########################################################################
|
|
|
|
|
|
|
|
function doToggleGroup( group ){
|
|
|
|
$("#changegroupgid").val(group);
|
|
|
|
|
|
|
|
// Serialize the data
|
|
|
|
var post = $( "#changegroupsform" ).serialize();
|
|
|
|
// Ajax foo
|
|
|
|
$.post( 'ajax/togglegroups.php', post, function(data){
|
|
|
|
if( data.status == "success" ){
|
|
|
|
var groups = [];
|
|
|
|
$("input[x-use='togglegroup']").each( function(index){
|
|
|
|
if( $(this).attr("checked")){
|
|
|
|
groups.push($(this).val());
|
|
|
|
}
|
|
|
|
});
|
2011-04-18 11:30:37 +04:00
|
|
|
if( groups.length == 0 ){
|
|
|
|
$("#changegroups").prev().html( ' ' );
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
$("#changegroups").prev().html( groups.join(", "));
|
|
|
|
}
|
2011-04-17 20:05:49 +04:00
|
|
|
}
|
|
|
|
else{
|
2011-04-18 14:39:28 +04:00
|
|
|
printError( data.data.message );
|
2011-04-17 20:05:49 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
2011-04-17 15:15:55 +04:00
|
|
|
|
2011-04-18 14:39:28 +04:00
|
|
|
function printError( message ){
|
|
|
|
$("#errormessage").text( message );
|
|
|
|
$("#errordialog").dialog( "open" );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-04-17 15:15:55 +04:00
|
|
|
//#########################################################################
|
|
|
|
// Functions for editing the dom after user manipulation
|
|
|
|
//#########################################################################
|
|
|
|
|
|
|
|
// Manipulating the page after crteating a user
|
|
|
|
function userCreated( username, groups ){
|
2011-04-18 11:30:37 +04:00
|
|
|
// We need at least a space for showing the div
|
|
|
|
if( groups == "" ){
|
2011-04-18 15:47:18 +04:00
|
|
|
groups = ' ';
|
2011-04-18 11:30:37 +04:00
|
|
|
}
|
|
|
|
|
2011-04-17 15:15:55 +04:00
|
|
|
// Add user to table
|
2011-04-18 11:30:37 +04:00
|
|
|
var newrow = '<tr x-uid="'+username+'"><td x-use="username"><div x-use="usernamediv">'+username+'</div></td>';
|
|
|
|
newrow = newrow+'<td x-use="usergroups"><div x-use="usergroupsdiv">'+groups+'</td>';
|
|
|
|
newrow = newrow+'<td><a class="removeuserbutton" href="">remove</a></td></tr>';
|
2011-04-17 15:15:55 +04:00
|
|
|
$("#usertable").append( newrow );
|
|
|
|
|
|
|
|
// Clear forms
|
|
|
|
$("input[x-use='createuserfield']").val( "" );
|
|
|
|
$("input[x-use='createusercheckbox']").attr( "checked", false );
|
|
|
|
}
|
|
|
|
|
|
|
|
function userRemoved( username ){
|
2011-04-17 20:05:49 +04:00
|
|
|
$( "tr[x-uid='"+username+"']" ).remove();
|
2011-04-17 15:15:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function groupCreated( groupname ){
|
2011-04-17 20:05:49 +04:00
|
|
|
var newrow = '<tr x-gid="'+groupname+'"><td>' + groupname + '</td>';
|
2011-04-17 15:15:55 +04:00
|
|
|
newrow = newrow + '<td><a class="removegroupbutton" href="">remove</a></td></tr>';
|
|
|
|
$("#grouptable").append( newrow );
|
|
|
|
|
|
|
|
// Delete form content
|
|
|
|
$("input[x-use='creategroupfield']").val( "" );
|
|
|
|
|
|
|
|
// Add group option to Create User and Edit User
|
2011-04-18 15:12:30 +04:00
|
|
|
var createuser = '<input x-use="createusercheckbox" x-gid="'+groupname+'" type="checkbox" name="groups[]" value="'+groupname+'" /> <span x-gid="'+groupname+'">'+groupname+'<br /></span>';
|
2011-04-17 15:15:55 +04:00
|
|
|
$("#createusergroups").append( createuser );
|
2011-04-18 15:12:30 +04:00
|
|
|
var changeuser = '<input x-use="togglegroup" x-gid="'+groupname+'" type="checkbox" name="groups[]" value="'+groupname+'" /> <span x-use="togglegroup" x-gid="'+groupname+'">'+groupname+'<br /></span>';
|
2011-04-17 20:05:49 +04:00
|
|
|
$("#changegroupsform").append( changeuser );
|
2011-04-17 15:15:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function groupRemoved( groupname ){
|
|
|
|
// Delete the options
|
2011-04-17 20:05:49 +04:00
|
|
|
$( "tr[x-gid='"+groupname+"']" ).remove();
|
2011-04-17 15:15:55 +04:00
|
|
|
$( "span[x-gid='"+groupname+"']" ).remove();
|
|
|
|
$( "input[x-gid='"+groupname+"']" ).remove();
|
|
|
|
|
|
|
|
// remove it from user list
|
2011-04-18 15:12:30 +04:00
|
|
|
$( "div[x-use='usergroupsdiv']" ).each(function(index){
|
2011-04-17 15:15:55 +04:00
|
|
|
var content = $(this).text();
|
|
|
|
var list = content.split( ", " );
|
|
|
|
var newlist = [];
|
|
|
|
for( var i = 0; i < list.length; i++ ){
|
|
|
|
var temp = list[i];
|
|
|
|
if( temp != groupname ){
|
|
|
|
newlist.push( temp );
|
|
|
|
}
|
2011-04-16 19:49:57 +04:00
|
|
|
}
|
2011-04-17 15:15:55 +04:00
|
|
|
var newstring = newlist.join( ", " );
|
|
|
|
$(this).html( newstring )
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
2011-04-16 19:49:57 +04:00
|
|
|
|
2011-04-17 20:05:49 +04:00
|
|
|
//#########################################################################
|
|
|
|
// Editing the users properties by clicking the cell
|
|
|
|
//#########################################################################
|
|
|
|
|
|
|
|
// Password (clicking on user name)
|
2011-07-20 03:18:43 +04:00
|
|
|
$("span[x-use='usernamediv']").live( "click", function(){
|
2011-04-17 20:05:49 +04:00
|
|
|
if( togglepassword == "" || $(this).parent().parent().attr("x-uid") != togglepassword ){
|
|
|
|
togglepassword = $(this).parent().parent().attr("x-uid");
|
|
|
|
// Set the username!
|
|
|
|
$("#changepassworduid").val(togglepassword);
|
|
|
|
$("#changepasswordpwd").val("");
|
|
|
|
$(this).parent().append( $('#changepassword') );
|
|
|
|
$('#changepassword').show();
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
$('#changepassword').hide();
|
|
|
|
togglepassword = "";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#changepasswordbutton").click( function(){
|
|
|
|
// Serialize the data
|
|
|
|
var post = $( "#changepasswordform" ).serialize();
|
|
|
|
// Ajax foo
|
|
|
|
$.post( 'ajax/changepassword.php', post, function(data){
|
|
|
|
if( data.status == "success" ){
|
|
|
|
togglepassword = "";
|
|
|
|
$('#changepassword').hide();
|
|
|
|
}
|
|
|
|
else{
|
2011-04-18 14:39:28 +04:00
|
|
|
printError( data.data.message );
|
2011-04-17 20:05:49 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Groups
|
2011-04-18 11:30:37 +04:00
|
|
|
$("div[x-use='usergroupsdiv']").live( "click", function(){
|
2011-04-17 20:05:49 +04:00
|
|
|
if( togglegroup == "" || $(this).parent().parent().attr("x-uid") != togglegroup){
|
|
|
|
togglegroup = $(this).parent().parent().attr("x-uid");
|
|
|
|
var groups = $(this).text();
|
|
|
|
groups = groups.split(", ");
|
|
|
|
$("input[x-use='togglegroup']").each( function(index){
|
|
|
|
var check = false;
|
|
|
|
// Group checked?
|
|
|
|
for( var i = 0; i < groups.length; i++ ){
|
|
|
|
if( $(this).val() == groups[i] ){
|
|
|
|
check = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check/uncheck
|
|
|
|
if( check ){
|
|
|
|
$(this).attr("checked","checked");
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
$(this).removeAttr("checked");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$("#changegroupuid").val(togglegroup);
|
2011-07-20 03:18:43 +04:00
|
|
|
$(this).empty();
|
2011-04-17 20:05:49 +04:00
|
|
|
$(this).parent().append( $('#changegroups') );
|
|
|
|
$('#changegroups').show();
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
var groups = [];
|
|
|
|
$("input[x-use='togglegroup']").each( function(index){
|
|
|
|
if( $(this).attr("checked")){
|
|
|
|
groups.push($(this).val());
|
|
|
|
}
|
|
|
|
});
|
2011-04-18 11:30:37 +04:00
|
|
|
if( groups.length == 0 ){
|
|
|
|
$("#changegroups").prev().html( ' ' );
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
$("#changegroups").prev().html( groups.join(", "));
|
|
|
|
}
|
2011-04-17 20:05:49 +04:00
|
|
|
$('#changegroups').hide();
|
|
|
|
togglegroup = "";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$("span[x-use='togglegroup']").live( "click", function(){
|
|
|
|
if( $(this).prev().attr("checked")){
|
|
|
|
$(this).prev().removeAttr("checked")
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
$(this).prev().attr("checked","checked")
|
|
|
|
}
|
|
|
|
doToggleGroup( $(this).attr("x-gid"));
|
|
|
|
});
|
|
|
|
|
|
|
|
$("input[x-use='togglegroup']").live( "click", function(){
|
|
|
|
doToggleGroup( $(this).attr("x-gid"));
|
|
|
|
});
|
2011-04-17 15:15:55 +04:00
|
|
|
//#########################################################################
|
|
|
|
// Clicking on buttons
|
|
|
|
//#########################################################################
|
|
|
|
|
2011-04-16 19:49:57 +04:00
|
|
|
|
2011-04-17 15:15:55 +04:00
|
|
|
// Create a new user
|
|
|
|
$( "#createuserbutton" )
|
|
|
|
.click(function(){
|
2011-07-06 02:34:24 +04:00
|
|
|
if(!$( "#createuserbutton" ).data('active')){
|
|
|
|
$( "#createuserbutton" ).data('active',true);
|
|
|
|
|
|
|
|
// Create the post data
|
|
|
|
var post = $( "#createuserdata" ).serialize();
|
|
|
|
|
|
|
|
// Ajax call
|
|
|
|
$.post( 'ajax/createuser.php', post, function(data){
|
|
|
|
$( "#createuserbutton" ).data('active',false);
|
|
|
|
|
|
|
|
// If it says "success" then we are happy
|
|
|
|
if( data.status == "success" ){
|
|
|
|
userCreated( data.data.username, data.data.groups );
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
printError( data.data.message );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2011-04-17 15:15:55 +04:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
$( ".removeuserbutton" ).live( 'click', function() {
|
2011-04-17 20:05:49 +04:00
|
|
|
uid = $( this ).parent().parent().attr( 'x-uid' );
|
2011-04-17 15:15:55 +04:00
|
|
|
$("#deleteuserusername").html(uid);
|
|
|
|
$("#deleteusernamefield").val(uid);
|
|
|
|
$("#removeuserform").dialog( "open" );
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
$( "#creategroupbutton" )
|
|
|
|
.click(function(){
|
|
|
|
// Serialize the data
|
|
|
|
var post = $( "#creategroupdata" ).serialize();
|
|
|
|
// Ajax foo
|
|
|
|
$.post( 'ajax/creategroup.php', post, function(data){
|
|
|
|
if( data.status == "success" ){
|
|
|
|
groupCreated( data.data.groupname );
|
|
|
|
}
|
|
|
|
else{
|
2011-04-18 14:39:28 +04:00
|
|
|
printError( data.data.message );
|
2011-04-17 15:15:55 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
$( ".removegroupbutton" ).live( 'click', function(){
|
2011-04-17 20:05:49 +04:00
|
|
|
gid = $( this ).parent().parent().attr( 'x-gid' );
|
2011-04-17 15:15:55 +04:00
|
|
|
$("#removegroupgroupname").html(gid);
|
|
|
|
$("#removegroupnamefield").val(gid);
|
|
|
|
$("#removegroupform").dialog( "open" );
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
//#########################################################################
|
|
|
|
// Dialogs
|
|
|
|
//#########################################################################
|
|
|
|
|
2011-04-18 14:39:28 +04:00
|
|
|
// Removing users
|
|
|
|
$( "#errordialog" ).dialog({
|
|
|
|
autoOpen: false,
|
|
|
|
modal: true,
|
|
|
|
buttons: {
|
|
|
|
OK: function() {
|
|
|
|
$( this ).dialog( "close" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-04-17 03:04:23 +04:00
|
|
|
// Removing users
|
2011-04-17 15:15:55 +04:00
|
|
|
$( "#removeuserform" ).dialog({
|
2011-04-16 19:49:57 +04:00
|
|
|
autoOpen: false,
|
|
|
|
height: 300,
|
|
|
|
width: 350,
|
|
|
|
modal: true,
|
|
|
|
buttons: {
|
|
|
|
"Remove user": function() {
|
2011-04-17 03:04:23 +04:00
|
|
|
var post = $( "#removeuserdata" ).serialize();
|
|
|
|
$.post( 'ajax/removeuser.php', post, function(data){
|
|
|
|
if( data.status == "success" ){
|
2011-04-17 15:15:55 +04:00
|
|
|
userRemoved( uid );
|
2011-04-17 03:04:23 +04:00
|
|
|
}
|
|
|
|
else{
|
2011-04-18 14:39:28 +04:00
|
|
|
printError( data.data.message );
|
2011-04-17 03:04:23 +04:00
|
|
|
}
|
|
|
|
});
|
2011-04-16 19:49:57 +04:00
|
|
|
$( this ).dialog( "close" );
|
|
|
|
},
|
|
|
|
Cancel: function() {
|
|
|
|
$( this ).dialog( "close" );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
close: function() {
|
2011-04-17 03:04:23 +04:00
|
|
|
true;
|
2011-04-16 19:49:57 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-04-17 03:04:23 +04:00
|
|
|
|
2011-04-16 19:49:57 +04:00
|
|
|
// Dialog for adding users
|
2011-04-17 15:15:55 +04:00
|
|
|
$( "#removegroupform" ).dialog({
|
2011-04-16 19:49:57 +04:00
|
|
|
autoOpen: false,
|
|
|
|
height: 300,
|
|
|
|
width: 350,
|
|
|
|
modal: true,
|
|
|
|
buttons: {
|
2011-04-17 01:26:55 +04:00
|
|
|
"Remove group": function(){
|
2011-04-17 03:04:23 +04:00
|
|
|
var post = $( "#removegroupdata" ).serialize();
|
|
|
|
$.post( 'ajax/removegroup.php', post, function(data){
|
|
|
|
if( data.status == "success" ){
|
2011-04-17 15:15:55 +04:00
|
|
|
groupRemoved( gid );
|
2011-04-17 03:04:23 +04:00
|
|
|
}
|
|
|
|
else{
|
2011-04-18 14:39:28 +04:00
|
|
|
printError( data.data.message );
|
2011-04-17 03:04:23 +04:00
|
|
|
}
|
2011-04-17 01:26:55 +04:00
|
|
|
});
|
2011-04-16 19:49:57 +04:00
|
|
|
$( this ).dialog( "close" );
|
|
|
|
},
|
|
|
|
Cancel: function() {
|
|
|
|
$( this ).dialog( "close" );
|
|
|
|
}
|
|
|
|
},
|
2011-04-17 01:26:55 +04:00
|
|
|
close: function(){
|
2011-04-17 03:04:23 +04:00
|
|
|
true;
|
2011-04-16 19:49:57 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
} );
|
2011-04-17 15:15:55 +04:00
|
|
|
|