2016-02-14 19:37:41 +00:00
|
|
|
// 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"]')
|
2016-02-15 14:43:47 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
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"]')
|
2016-02-14 19:37:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-02-15 14:43:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [1] view -> AFFICHAGE DES UTILISATEURS
|
|
|
|
=========================================================*/
|
|
|
|
|
2016-02-14 21:50:07 +00:00
|
|
|
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');
|
2016-02-16 13:25:06 +00:00
|
|
|
autofill_edit(pageManager.vars[1]);
|
2016-02-14 21:50:07 +00:00
|
|
|
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');
|
2016-02-16 13:25:06 +00:00
|
|
|
autofill_remove(pageManager.vars[1]);
|
2016-02-14 21:50:07 +00:00
|
|
|
navSubMenu('remove');
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-14 19:37:41 +00:00
|
|
|
|
2016-02-15 14:43:47 +00:00
|
|
|
/* [2] create -> Creation d'utilisateur
|
|
|
|
=========================================================*/
|
2016-02-14 19:37:41 +00:00
|
|
|
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'),
|
2016-02-14 20:22:07 +00:00
|
|
|
lastname: document.querySelector(section.create.text + '#create_lastname'),
|
2016-02-14 19:37:41 +00:00
|
|
|
mail: document.querySelector(section.create.text + '#create_mail'),
|
|
|
|
password: document.querySelector(section.create.text + '#create_password'),
|
2016-02-14 20:22:07 +00:00
|
|
|
status: document.querySelector(section.create.text + '#create_status'),
|
2016-02-14 19:37:41 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2016-02-14 20:22:07 +00:00
|
|
|
|
|
|
|
/* (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){
|
2016-02-14 20:58:49 +00:00
|
|
|
if( answer.ModuleError == 0 ) // Tout s'est bien deroule
|
|
|
|
console.log('Utilisateur cree!');
|
|
|
|
else // Erreur
|
|
|
|
console.error('ModuleError::'+answer.ModuleError);
|
|
|
|
|
2016-02-14 20:22:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}, false);
|
|
|
|
|
2016-02-15 14:43:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [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'),
|
2016-02-15 22:07:20 +00:00
|
|
|
|
2016-02-15 14:43:47 +00:00
|
|
|
submit: document.querySelector(section.remove.text + '#remove_submit')
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-02-15 22:07:20 +00:00
|
|
|
/* (1) Gestion de l'auto-remplissage par url */
|
2016-02-16 13:25:06 +00:00
|
|
|
function autofill_remove(id_user){
|
2016-02-15 14:43:47 +00:00
|
|
|
// On recupere le code + username de l'utilisateur selectionne
|
|
|
|
var user_data = null;
|
|
|
|
|
2016-02-16 13:25:06 +00:00
|
|
|
api.send({path:'userDefault/getById', data: [id_user]}, function(answer){
|
2016-02-15 14:43:47 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
2016-02-16 13:25:06 +00:00
|
|
|
}if( pageManager.vars.length >= 2 && !isNaN(pageManager.vars[1]) ) autofill_remove(pageManager.vars[1]);
|
2016-02-15 22:07:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (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);
|
2016-02-15 14:43:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [4] edit -> Modification d'utilisateur
|
|
|
|
=========================================================*/
|
|
|
|
if( section.edit.element != null ){
|
|
|
|
|
|
|
|
// Definition des champs
|
|
|
|
section.edit.input = {
|
2016-02-16 13:25:06 +00:00
|
|
|
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')
|
|
|
|
},
|
|
|
|
|
2016-02-15 14:43:47 +00:00
|
|
|
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')
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-02-16 08:35:37 +00:00
|
|
|
/* (1) Gestion de l'auto-remplissage par url */
|
2016-02-16 13:25:06 +00:00
|
|
|
function autofill_edit(id_user){
|
2016-02-15 14:43:47 +00:00
|
|
|
// On recupere le code + username de l'utilisateur selectionne
|
|
|
|
var user_data = null;
|
|
|
|
|
2016-02-16 13:25:06 +00:00
|
|
|
// 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: ''
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-02-15 14:43:47 +00:00
|
|
|
|
2016-02-16 13:25:06 +00:00
|
|
|
// On enregistre l'UID de l'utilisateur
|
|
|
|
section.edit.input.search.id.value = user_data.id_user;
|
|
|
|
|
2016-02-15 14:43:47 +00:00
|
|
|
// Auto-remplissage du formulaire
|
2016-02-16 08:03:12 +00:00
|
|
|
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;
|
2016-02-16 13:25:06 +00:00
|
|
|
});
|
|
|
|
// 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);
|
|
|
|
|
2016-02-15 14:43:47 +00:00
|
|
|
|
2016-02-16 08:35:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* (n) Gestion de l'envoi du formulaire */
|
|
|
|
section.edit.input.submit.addEventListener('click', function(e){
|
|
|
|
// On annule l'envoi de base (PHP)
|
|
|
|
e.preventDefault();
|
|
|
|
|
2016-02-16 13:51:19 +00:00
|
|
|
// Si l'id est defini
|
|
|
|
if( section.edit.input.search.id.value != '' ){
|
|
|
|
|
|
|
|
var request = {
|
|
|
|
path: 'userDefault/edit', // On veut creer un utilisateur
|
|
|
|
data: [
|
|
|
|
section.edit.input.search.id.value,
|
|
|
|
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
|
|
|
|
]
|
|
|
|
};
|
2016-02-16 08:35:37 +00:00
|
|
|
|
2016-02-16 13:51:19 +00:00
|
|
|
api.send(request, function(answer){
|
2016-02-16 18:59:55 +00:00
|
|
|
console.log( answer );
|
2016-02-16 13:51:19 +00:00
|
|
|
if( answer.ModuleError == 0 && answer.status == true ) // Tout s'est bien deroule
|
|
|
|
console.log('Utilisateur modifie!');
|
|
|
|
else // Erreur
|
|
|
|
console.error('ModuleError::'+answer.ModuleError);
|
|
|
|
});
|
2016-02-16 08:35:37 +00:00
|
|
|
|
2016-02-16 13:51:19 +00:00
|
|
|
}
|
2016-02-16 08:35:37 +00:00
|
|
|
|
|
|
|
}, false);
|
|
|
|
|
2016-02-14 19:37:41 +00:00
|
|
|
}
|