// 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(); 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(); 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(){ if( pageManager.vars.length >= 2 && !isNaN(pageManager.vars[1]) ){ // On recupere le code + username de l'utilisateur selectionne var user_data = null; api.send({path:'userDefault/getById', data: [pageManager.vars[1]]}, 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; } }); }}autofill_remove(); /* (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 = { 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(){ if( pageManager.vars.length >= 2 && !isNaN(pageManager.vars[1]) ){ // On recupere le code + username de l'utilisateur selectionne var user_data = null; api.send({path:'userDefault/getById', data: [pageManager.vars[1]]}, function(answer){ if( answer.ModuleError == 0 ){ // Aucune erreur user_data = answer.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; } }); }}autofill_edit(); /* (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); }