368 lines
10 KiB
JavaScript
368 lines
10 KiB
JavaScript
// On referencie toutes les sections
|
|
var section = {
|
|
view: {
|
|
text: '#CONTAINER > section[data-sublink="view"] ',
|
|
element: document.querySelector('#CONTAINER > section[data-sublink="view"]')
|
|
},
|
|
|
|
create: {
|
|
text: '#CONTAINER > section[data-sublink="create"] ',
|
|
element: document.querySelector('#CONTAINER > section[data-sublink="create"]')
|
|
},
|
|
|
|
remove: {
|
|
text: '#CONTAINER > section[data-sublink="remove"] ',
|
|
element: document.querySelector('#CONTAINER > section[data-sublink="remove"]')
|
|
},
|
|
|
|
edit: {
|
|
text: '#CONTAINER > section[data-sublink="edit"] ',
|
|
element: document.querySelector('#CONTAINER > section[data-sublink="edit"]')
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [1] view -> AFFICHAGE DES UTILISATEURS
|
|
=========================================================*/
|
|
|
|
if( section.view.element != null ){
|
|
|
|
// On recupere tous les liens
|
|
section.view.link = {
|
|
edit: document.querySelectorAll(section.view.text + '.link_edit[data-user]'),
|
|
remove: document.querySelectorAll(section.view.text + '.link_remove[data-user]')
|
|
};
|
|
|
|
// On gere la "redirection" vers la modification
|
|
for( var i = 0 ; i < section.view.link.edit.length ; i++ ){
|
|
|
|
section.view.link.edit[i].addEventListener('click', function(e){
|
|
pageManager.vars[1] = e.target.getData('user');
|
|
autofill_edit(pageManager.vars[1]);
|
|
navSubMenu('edit');
|
|
}, false);
|
|
|
|
}
|
|
|
|
// On gere la "redirection" vers la suppression
|
|
for( var i = 0 ; i < section.view.link.remove.length ; i++ ){
|
|
|
|
section.view.link.remove[i].addEventListener('click', function(e){
|
|
pageManager.vars[1] = e.target.getData('user');
|
|
autofill_remove(pageManager.vars[1]);
|
|
navSubMenu('remove');
|
|
}, false);
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] create -> Creation d'utilisateur
|
|
=========================================================*/
|
|
if( section.create.element != null ){
|
|
|
|
// On recupere tous les champs
|
|
section.create.input = {
|
|
code: document.querySelector(section.create.text + '#create_code'),
|
|
username: document.querySelector(section.create.text + '#create_username'),
|
|
firstname: document.querySelector(section.create.text + '#create_firstname'),
|
|
lastname: document.querySelector(section.create.text + '#create_lastname'),
|
|
mail: document.querySelector(section.create.text + '#create_mail'),
|
|
password: document.querySelector(section.create.text + '#create_password'),
|
|
status: document.querySelector(section.create.text + '#create_status'),
|
|
|
|
submit: document.querySelector(section.create.text + '#create_submit')
|
|
}
|
|
|
|
// On initialise le gestionnaire des saisies
|
|
var createChecker = new inputChecker();
|
|
|
|
|
|
/* (1) Gestion du code RFID */
|
|
createChecker.append( section.create.input.code, format_code, '01-AB-CD-23');
|
|
section.create.input.code.addEventListener('keyup', function(e){
|
|
e.target.value = e.target.value.toUpperCase();
|
|
if( !createChecker.check(e.target) )
|
|
createChecker.correct(e.target, false);
|
|
|
|
}, false);
|
|
|
|
|
|
/* (n) Gestion de l'envoi du formulaire */
|
|
section.create.input.submit.addEventListener('click', function(e){
|
|
// On annule l'envoi de base (PHP)
|
|
e.preventDefault();
|
|
|
|
var request = {
|
|
path: 'userDefault/create', // On veut creer un utilisateur
|
|
data: [
|
|
section.create.input.code.value,
|
|
section.create.input.username.value,
|
|
section.create.input.firstname.value,
|
|
section.create.input.lastname.value,
|
|
section.create.input.mail.value,
|
|
section.create.input.password.value,
|
|
section.create.input.status.value
|
|
]
|
|
};
|
|
|
|
api.send(request, function(answer){
|
|
if( answer.ModuleError == 0 ) // Tout s'est bien deroule
|
|
console.log('Utilisateur cree!');
|
|
else // Erreur
|
|
console.error('ModuleError::'+answer.ModuleError);
|
|
|
|
});
|
|
|
|
|
|
|
|
}, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] remove -> Suppression d'utilisateur
|
|
=========================================================*/
|
|
if( section.remove.element != null ){
|
|
|
|
// Definition des champs
|
|
section.remove.input = {
|
|
code: document.querySelector(section.remove.text + '#remove_code'),
|
|
username: document.querySelector(section.remove.text + '#remove_username'),
|
|
|
|
submit: document.querySelector(section.remove.text + '#remove_submit')
|
|
};
|
|
|
|
|
|
/* (1) Gestion de l'auto-remplissage par url */
|
|
function autofill_remove(id_user){
|
|
// On recupere le code + username de l'utilisateur selectionne
|
|
var user_data = null;
|
|
|
|
api.send({path:'userDefault/getById', data: [id_user]}, function(answer){
|
|
if( answer.ModuleError == 0 ){ // Aucune erreur
|
|
user_data = answer.user;
|
|
|
|
// Auto-remplissage du formulaire
|
|
section.remove.input.code.value = user_data.code;
|
|
section.remove.input.username.value = user_data.username;
|
|
}
|
|
});
|
|
}if( pageManager.vars.length >= 2 && !isNaN(pageManager.vars[1]) ) autofill_remove(pageManager.vars[1]);
|
|
|
|
|
|
|
|
|
|
/* (n) Gestion de l'envoi du formulaire */
|
|
section.remove.input.submit.addEventListener('click', function(e){
|
|
// On annule l'envoi de base (PHP)
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
/* (1) On recupere l'UID de l'utilisateur */
|
|
var getuid = {};
|
|
|
|
|
|
// recuperation par CODE
|
|
if( section.remove.input.code.value != '' ){
|
|
getuid.path = 'userDefault/getByCode';
|
|
getuid.data = [section.remove.input.code.value];
|
|
|
|
// recuperation par USERNAME
|
|
}else if( section.remove.input.username.value != '' ){
|
|
getuid.path = 'userDefault/getByUsername';
|
|
getuid.data = [section.remove.input.username.value];
|
|
}
|
|
|
|
// On envoie la requete de demande d'uid
|
|
api.send(getuid, function(uid_answer){
|
|
if( uid_answer.ModuleError == 0 ){ // Si aucune erreur
|
|
|
|
/* (2) On supprime l'utilisateur */
|
|
var request = {
|
|
path: 'userDefault/delete', // On veut supprimer cet utilisateur
|
|
data: [uid_answer.user.id_user]
|
|
};
|
|
|
|
api.send(request, function(answer){
|
|
if( answer.ModuleError == 0 && answer.status == true ) // Tout s'est bien deroule
|
|
console.log('Utilisateur supprime!');
|
|
else // Erreur
|
|
console.error('ModuleError::'+answer.ModuleError);
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, false);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [4] edit -> Modification d'utilisateur
|
|
=========================================================*/
|
|
if( section.edit.element != null ){
|
|
|
|
// Definition des champs
|
|
section.edit.input = {
|
|
search: {
|
|
keyword: document.querySelector(section.edit.text + '#edit_search_keyword'),
|
|
id: document.querySelector(section.edit.text + '#edit_search_id'),
|
|
submit: document.querySelector(section.edit.text + '#edit_search_submit')
|
|
},
|
|
|
|
code: document.querySelector(section.edit.text + '#edit_code'),
|
|
username: document.querySelector(section.edit.text + '#edit_username'),
|
|
firstname: document.querySelector(section.edit.text + '#edit_firstname'),
|
|
lastname: document.querySelector(section.edit.text + '#edit_lastname'),
|
|
mail: document.querySelector(section.edit.text + '#edit_mail'),
|
|
password: document.querySelector(section.edit.text + '#edit_password'),
|
|
status: document.querySelector(section.edit.text + '#edit_status'),
|
|
|
|
submit: document.querySelector(section.edit.text + '#edit_submit')
|
|
};
|
|
|
|
|
|
/* (1) Gestion de l'auto-remplissage par url */
|
|
function autofill_edit(id_user){
|
|
// On recupere le code + username de l'utilisateur selectionne
|
|
var user_data = null;
|
|
|
|
// SI @id_user est un entier
|
|
if( !isNaN(id_user) ){
|
|
api.send({path:'userDefault/getById', data: [id_user]}, function(answer){
|
|
if( answer.ModuleError == 0 ){ // Si aucune erreur
|
|
user_data = answer.user;
|
|
|
|
}else{ // Si erreuron utilise un jeu de donnees vides
|
|
user_data = {
|
|
id_user: '',
|
|
code: '',
|
|
username: '',
|
|
firstname: '',
|
|
lastname: '',
|
|
mail: '',
|
|
status: ''
|
|
};
|
|
}
|
|
|
|
|
|
// On enregistre l'UID de l'utilisateur
|
|
section.edit.input.search.id.value = user_data.id_user;
|
|
|
|
// Auto-remplissage du formulaire
|
|
section.edit.input.code.value = user_data.code;
|
|
section.edit.input.username.value = user_data.username;
|
|
section.edit.input.firstname.value = user_data.firstname;
|
|
section.edit.input.lastname.value = user_data.lastname;
|
|
section.edit.input.mail.value = user_data.mail;
|
|
section.edit.input.password.value = '';
|
|
section.edit.input.status.value = user_data.status;
|
|
});
|
|
// Si @id_user est incoherent
|
|
}
|
|
|
|
|
|
}if( pageManager.vars.length >= 2 && !isNaN(pageManager.vars[1]) ) autofill_edit(pageManager.vars[1]);
|
|
|
|
|
|
|
|
/* (2) Gestion de la recherche de l'utilisateur */
|
|
var lastsearch = null;
|
|
var searchindex = 0;
|
|
section.edit.input.search.submit.addEventListener('click', function(e){
|
|
// On annule l'envoi de base (PHP)
|
|
e.preventDefault();
|
|
|
|
// Si le champ n'est pas vide, on lance la recherche
|
|
if( section.edit.input.search.keyword.value != '' ){
|
|
// Redaction de la requete
|
|
var getuid = {
|
|
path: 'userDefault/search',
|
|
data: [section.edit.input.search.keyword.value]
|
|
};
|
|
|
|
// On envoie la requete
|
|
api.send(getuid, function(uidanswer){
|
|
if( uidanswer.ModuleError == 0 && uidanswer.users.length > 0 ){ // si on a trouve qqch
|
|
// Si on trouve le meme utilisateur que lors de la derniere recherche
|
|
if( lastsearch == uidanswer.users[searchindex].id_user )
|
|
searchindex = (searchindex+1) % uidanswer.users.length; // on affiche le prochain (decalage cyclique)
|
|
|
|
lastsearch = uidanswer.users[searchindex].id_user;
|
|
section.edit.input.search.id.value = lastsearch;
|
|
// on auto-remplit le formulaire
|
|
autofill_edit(lastsearch);
|
|
|
|
}else{
|
|
autofill_edit(null);
|
|
}
|
|
});
|
|
}
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
|
|
/* (n) Gestion de l'envoi du formulaire */
|
|
section.edit.input.submit.addEventListener('click', function(e){
|
|
// On annule l'envoi de base (PHP)
|
|
e.preventDefault();
|
|
|
|
var request = {
|
|
path: 'userDefault/edit', // On veut creer un utilisateur
|
|
data: [
|
|
section.edit.input.code.value,
|
|
section.edit.input.username.value,
|
|
section.edit.input.firstname.value,
|
|
section.edit.input.lastname.value,
|
|
section.edit.input.mail.value,
|
|
section.edit.input.password.value,
|
|
section.edit.input.status.value
|
|
]
|
|
};
|
|
|
|
api.send(request, function(answer){
|
|
if( answer.ModuleError == 0 && answer.status == true ) // Tout s'est bien deroule
|
|
console.log('Utilisateur modifie!');
|
|
else // Erreur
|
|
console.error('ModuleError::'+answer.ModuleError);
|
|
|
|
});
|
|
|
|
}, false);
|
|
|
|
} |