davadmin/admin/scripts/users.js

190 lines
8.3 KiB
JavaScript
Raw Normal View History

2007-08-28 10:18:04 +02:00
function handleerror(xmldata) {
if ($('error', xmldata).size()) {
var msg = 'Es ist ein Fehler aufgetreten:\n';
$('error', xmldata).find('errormsg').each(function(i) {
msg += $(this).text();
});
alert(msg);
$('#content').show();
return true;
}
return false;
}
function updateusers(xmldata) {
var uid = $(xmldata).find('uid').text();
var username = $(xmldata).find('username').text();
var firstname = $(xmldata).find('firstname').text();
var lastname = $(xmldata).find('lastname').text();
var loggedin = $(xmldata).find('loggedin').text();
var htmltext = '<td>' + username + '</td><td>' + lastname + ', ' + firstname + '</td><td><a id="edit' + uid + '" class="editlink" href="#" title="Die Daten dieses Nutzers bearbeiten"><img class="actionicon" src="images/edit.png" width="16" height="16" alt="bearbeiten"/></a>';
if (loggedin == '0') {
htmltext = htmltext + '<a id="delete' + uid + '" class="deletelink" href="#" title="Die Daten dieses Nutzers löschen"><img class="actionicon" src="images/delete.png" width="16" height="16" alt="löschen" /></a>';
}
htmltext = htmltext + '</td>';
$('#usertable').find('tr#uid' + uid).empty().append(htmltext);
if (!($('#usertable').find('tr#uid' + uid).size())) {
var rows = $('#usertable').find('tr');
var inserted = false;
for (var i = 0; !inserted && i < rows.length; i++) {
if ($(rows[i]).find('td:first').text() > username) {
$(rows[i]).before(
'<tr id="uid' + uid + '">' + htmltext + '</tr>');
inserted = true;
}
}
if (!inserted) {
$(rows[rows.length-1]).before(
'<tr id="uid' + uid + '">' + htmltext + '</tr>');
}
}
$('#usertable').find('tr#uid' + uid).find('a.editlink').click(function() {
edituser(this.id.substr(4));
});
$('#usertable').find('tr#uid' + uid).find('a.deletelink').click(function() {
deleteuser(this.id.substr(6));
});
$('#content').show();
}
function getEditUserForm(title, username, firstname, lastname, groups, userid) {
var retval;
retval = '<form action="#" id="userform"><fieldset class="dynaform"><legend><img id="closer" src="images/x.png" width="16" height="16" alt="Schließen" /> ' + title + '</legend><div class="formelement"><label for="input-username">Nutzername:</label><input id="input-username" type="text" name="username" value="' + username + '" ';
if (userid != null) {
retval = retval + ' readonly="readonly"';
}
retval = retval + '/></div><div class="formelement"><label for="input-firstname">Vorname:</label><input id="input-firstname" type="text" name="firstname" value="' + firstname + '" /></div><div class="formelement"><label for="input-lastname">Nachname:</label><input id="input-lastname" type="text" name="lastname" value="' + lastname + '" /></div><div class="formelement"><label for="pwd1">Passwort:</label><input type="password" name="pwd1" /></div><div class="formelement"><label for="pwd2">Wiederholung:</label><input type="password" name="pwd2" /></div><div class="formelement"><label for="groups">Gruppen:</label><input type="text" name="groups" class="form-autocomplete" id="input-groups" value="' + groups + '" /><input type="hidden" class="autocomplete" id="input-groups-autocomplete" value="getgroups.php" /></div><div class="formactions"><input type="submit" name="submit" value="Absenden" /></div></fieldset></form>';
return retval;
}
function displayusereditor(title, userid, username, firstname, lastname, groups) {
$('#content').hide();
$('#usereditor').hide().empty().append(getEditUserForm(title, username, firstname, lastname, groups, userid)).show();
DAV.autocompleteAutoAttach();
$('#closer').click(function() {
$('#usereditor').hide().empty();
$('#content').show();
});
$('#userform').find('#input-username').focus();
$('#userform').submit(function() {
var params;
if (userid == null) {
if (!this.username.value.match(/^[a-zA-Z0-9]{2,}$/)) {
alert('Der Nutzername muss aus mindestens 2 Buchstaben oder Ziffern bestehen und darf keine sonstigen Zeichen enthalten.');
this.username.focus();
return false;
}
}
if (userid == null || this.pwd1.value.length > 0) {
if (this.pwd1.value.length < 8) {
alert('Das Passwort muss mindestens 8 Zeichen lang sein!');
this.pwd1.focus();
return false;
}
if (this.pwd1.value != this.pwd2.value) {
alert('Passwort und Wiederholung müssen übereinstimmen!');
this.pwd2.focus();
return false;
}
}
if (!this.groups.value.match(/^([0-9a-zA-z]+[,\s]*)+$/)) {
alert('Die Gruppenangabe muss eine durch Kommata getrennte Liste von Gruppennamen, die aus Buchstaben und Ziffern zusammengesetzt sein können, sein.');
this.groups.focus();
return false;
}
if (userid == null) {
params = {method : 'submituser',
username : this.username.value,
firstname : this.firstname.value,
lastname : this.lastname.value,
password : this.pwd1.value,
groups : this.groups.value};
} else {
params = {method : 'submituser',
uid : userid,
username : this.username.value,
firstname : this.firstname.value,
lastname : this.lastname.value,
password : this.pwd1.value,
groups : this.groups.value};
}
$.post(
"users.php", params,
function(retval) {
$('div#usereditor').hide().empty();
if (!handleerror(retval)) {
updateusers(retval);
}
});
return false;
});
}
function deleteuserdialog(userid, username, firstname, lastname) {
$("#usereditor").hide().empty();
var msg = 'Soll der Nutzer ' + firstname + ' ' + lastname +
' mit dem Login ' + username + ' wirklich gelöscht werden?';
if (confirm(msg) == true) {
$.post(
"users.php",
{method : 'deleteuser',
uid : userid},
function(retval) {
if (!handleerror(retval)) {
var deluid = $('uid:first', retval).text();
$('#usertable').find('tr#uid' + deluid).remove();
}
});
}
}
function deleteuser(userid) {
$.get(
"users.php",
{uid : userid,
method : 'getuserdata'},
function(retval) {
if (!handleerror(retval)) {
var username, lastname, firstname;
username = $("username:first", retval).text();
lastname = $("lastname:first", retval).text();
firstname = $("firstname:first", retval).text();
deleteuserdialog(userid, username, firstname, lastname);
}
});
}
function edituser(userid) {
$.get(
"users.php",
{uid : userid,
method : 'getuserdata'},
function(retval) {
if (!handleerror(retval)) {
var username, lastname, firstname, groups;
username = $("username:first", retval).text();
lastname = $("lastname:first", retval).text();
firstname = $("firstname:first", retval).text();
groups = $("groups:first", retval).text();
displayusereditor('Nutzerdaten bearbeiten', userid, username,
firstname, lastname, groups);
}
});
}
function newuser() {
displayusereditor('Neuen Nutzer anlegen', null, '', '', '', '');
}
$(function() {
$("a.editlink").each(function(i) {
$(this).click(function() { edituser(this.id.substr(4)); });
});
$("a.newlink").each(function(i) {
$(this).click(function() { newuser(); });
});
$("a.deletelink").each(function(i) {
$(this).click(function() { deleteuser(this.id.substr(6)); });
});
});