diff --git a/static/app/bib_nom/edit/bibNom-form-controler.js b/static/app/bib_nom/edit/bibNom-form-controler.js index fc8caa5f..077086ba 100644 --- a/static/app/bib_nom/edit/bibNom-form-controler.js +++ b/static/app/bib_nom/edit/bibNom-form-controler.js @@ -14,6 +14,8 @@ function($scope, $routeParams, $http, $uibModal, locationHistoryService, $locati self.showMediaForm=false; self.form = $scope.form; self.userRightLevel = 0; + self.hideInpnButton = backendCfg.hideInpnButton; + self.inpnLoading = false; //----------------------Gestion des droits---------------// if (loginSrv.getCurrentUser()) { @@ -27,9 +29,32 @@ function($scope, $routeParams, $http, $uibModal, locationHistoryService, $locati var toasterMsg = { - 'saveSuccess':{"title":"Taxon enregistré", "msg": "Le taxon a été enregistré avec succès"}, - 'saveError':{"title":"Erreur d'enregistrement"}, - } + saveSuccess: { + title: "Taxon enregistré", + msg: "Le taxon a été enregistré avec succès", + }, + saveError: { title: "Erreur d'enregistrement" }, + mediaInserted: { title: "API INPN", msg: "Media inséré avec succès" }, + infoInserted: { + title: "API INPN", + msg: "Information recupérée avec succès", + }, + infoPresent: { + title: "API INPN", + msg: "Il y a déjà une description pour ce taxon, veuillez d'abord la supprimer", + }, + inpnError: { + title: "API INPN", + msg: "Impossible d'accéder à l'API de l'INPN", + }, + inpnNotFound: { + title: "API INPN", + msg: "Aucune description trouvée pour ce taxon sur l'API de l'INPN", + }, + }; + + const inpnURL = "https://taxref.mnhn.fr/api/taxa/"; + var getTaxonsInfo = function (cd_nom) { $http.get(backendCfg.api_url + "bibnoms/taxoninfo/"+cd_nom+"?forcePath=True").then(function(response) { if (response.data) { @@ -114,11 +139,17 @@ function($scope, $routeParams, $http, $uibModal, locationHistoryService, $locati } }); //------------------------------ Sauvegarde du formulaire ----------------------------------/ - self.submit = function() { + function postBibNom() { var params = self.bibNom; - var url = backendCfg.api_url +"bibnoms/"; - if(action == 'edit'){url=url+self.bibNom.id_nom;} - $http.post(url, params, { withCredentials: true }) + var url = backendCfg.api_url + "bibnoms/"; + if (action == "edit") { + url = url + self.bibNom.id_nom; + } + return $http.post(url, params, { withCredentials: true }); + } + + self.submit = function() { + postBibNom() .then(function(response) { var data = response.data if (data.success == true) { @@ -155,5 +186,126 @@ function($scope, $routeParams, $http, $uibModal, locationHistoryService, $locati return response.data; }); }; + //------------------------------ Recupération des infos de l'inpn ----------------------------------/ + function getAtlasDescription() { + return attribut = self.attributsDefList + ?.map((item) => item.attributs)[0] + ?.filter((item) => item.nom_attribut == "atlas_description")[0]; + } + + function setInfo(cd_nom) { + url = inpnURL + `${cd_nom}/factsheet`; + if (self.attributsDefList) { + const attribut = getAtlasDescription() + if (attribut !== undefined) { + $http + .get(url) + .then((response) => { + self.bibNom.attributs_values[attribut.id_attribut] = + response.data.text || ""; + postBibNom() + .then(() => + toaster.pop( + "success", + toasterMsg.saveSuccess.title, + toasterMsg.saveSuccess.msg, + 5000, + "trustedHtml" + ) + ) + .catch(() => + toaster.pop( + "error", + toasterMsg.saveError.title, + response.data.message, + 5000, + "trustedHtml" + ) + ) + .finally(() => (self.inpnLoading = false)); + }) + .catch((error) => { + let title = toasterMsg.inpnError.title; + let msg = toasterMsg.inpnError.msg; + let toasterType = "error"; + + if (error.status == 404) { + title = toasterMsg.inpnNotFound.title; + msg = toasterMsg.inpnNotFound.msg; + toasterType = "warning"; + } + + toaster.pop(toasterType, title, msg, 5000, "trustedHtml"); + }) + .finally(() => (self.inpnLoading = false)); + } + } + } + + function setMedia(cd_nom) { + url = inpnURL + `${cd_nom}/media`; + has_error = false; + $http.get(url).then((response) => { + response.data?._embedded?.media.forEach((media) => { + const url = media?._links?.file?.href; + if (url && !self.bibNom.medias.map((item) => item.url).includes(url)) { + payload = { + cd_ref: cd_nom, + chemin: null, + id_type: 1, + is_public: true, + isFile: false, + titre: media?.title || "", + auteur: media?.copyright, + url: url, + }; + $http + .post(backendCfg.api_url + "tmedias/", payload) + .then((response) => { + const newMedia = response.data?.media + if (newMedia !== undefined) { + self.bibNom.medias.push(newMedia) + } + toaster.pop( + "info", + toasterMsg.mediaInserted.title, + toasterMsg.mediaInserted.msg, + 2000, + "trustedHtml" + ); + }) + .catch(() => + toaster.pop( + "error", + toasterMsg.inpnError.title, + toasterMsg.inpnError.msg, + 5000, + "trustedHtml" + ) + ) + .finally(() => (self.inpnLoading = false)); + } + }); + }); + } + + self.getINPNInfo = function () { + const cd_nom = self.bibNom.cd_nom; + self.inpnLoading = true; + const attribut = getAtlasDescription(); + if (!self.bibNom.attributs_values[attribut.id_attribut]) { + setInfo(cd_nom); + } else { + self.inpnLoading = false; + toaster.pop( + "info", + toasterMsg.infoPresent.title, + toasterMsg.infoPresent.msg, + 5000, + "trustedHtml" + ); + } + setMedia(cd_nom); + }; } ]); diff --git a/static/app/bib_nom/edit/bibNom-form-tpl.html b/static/app/bib_nom/edit/bibNom-form-tpl.html index c0a16d39..10689e4e 100644 --- a/static/app/bib_nom/edit/bibNom-form-tpl.html +++ b/static/app/bib_nom/edit/bibNom-form-tpl.html @@ -15,6 +15,10 @@ +
diff --git a/static/app/constants.js.sample b/static/app/constants.js.sample index 0faecd9e..dd9c9890 100644 --- a/static/app/constants.js.sample +++ b/static/app/constants.js.sample @@ -4,5 +4,6 @@ app.constant("backendCfg", { "user_admin_privilege":6, "user_high_privilege":4, "user_medium_privilege":3, - "user_low_privilege":2 + "user_low_privilege":2, + "hideInpnButton": false })