From a0871657cafc401cc4f71b60b8da8dabde896ddd Mon Sep 17 00:00:00 2001 From: SimonGschnell Date: Mon, 22 Jul 2024 11:24:47 +0200 Subject: [PATCH] adds insertFile to the new ProfilUpdate FhcAPIController and starts a transaction when deleting the foreign key from the profil update and deleting the old file --- .../api/frontend/v1/ProfilUpdate.php | 115 ++++++++++++++++++ .../models/person/Profil_update_model.php | 15 +++ public/js/api/profilUpdate.js | 11 +- .../js/components/Calendar/CalendarModal.js | 4 +- .../Cis/Profil/ProfilModal/EditProfil.js | 4 +- 5 files changed, 139 insertions(+), 10 deletions(-) diff --git a/application/controllers/api/frontend/v1/ProfilUpdate.php b/application/controllers/api/frontend/v1/ProfilUpdate.php index 44d70afb4..4b9dedb48 100644 --- a/application/controllers/api/frontend/v1/ProfilUpdate.php +++ b/application/controllers/api/frontend/v1/ProfilUpdate.php @@ -47,6 +47,7 @@ class ProfilUpdate extends FHCAPI_Controller 'insertProfilRequest' => self::PERM_LOGGED, 'updateProfilRequest' => self::PERM_LOGGED, 'deleteProfilRequest' => self::PERM_LOGGED, + 'insertFile' => self::PERM_LOGGED, ]); // Load language phrases @@ -388,6 +389,68 @@ class ProfilUpdate extends FHCAPI_Controller } + public function insertFile($replace) + { + $replace = json_decode($replace); + + if (!count($_FILES)) { + $this->terminateWithError("No file available for upload"); + } + + //? if replace is set it contains the profil_update_id in which the attachment_id has to be replaced + if (isset($replace)) { + + $this->ProfilUpdateModel->addSelect(["attachment_id"]); + $profilUpdate = $this->ProfilUpdateModel->load([$replace]); + if (isError($profilUpdate)) { + $this->terminateWithError($this->p->t('profilUpdate', 'profilUpdate_loading_error')); + } + //? get the attachmentID + $dms_id = $this->getDataOrTerminateWithError($profilUpdate)[0]->attachment_id; + + //? delete old dms_file of Profil Update + $deleteOldFile_result = $this->deleteOldVersionFile($dms_id); + if(!$deleteOldFile_result){ + $this->terminateWithError("error while deleting the old file"); + } + } + + + $files = $_FILES['files']; + $file_count = count($files['name']); + + $res = []; + + for ($i = 0; $i < $file_count; $i++) { + $_FILES['files']['name'] = $files['name'][$i]; + $_FILES['files']['type'] = $files['type'][$i]; + $_FILES['files']['tmp_name'] = $files['tmp_name'][$i]; + $_FILES['files']['error'] = $files['error'][$i]; + $_FILES['files']['size'] = $files['size'][$i]; + + $dms = [ + "kategorie_kurzbz" => "profil_aenderung", + "version" => 0, + "name" => $_FILES['files']['name'], + "mimetype" => $_FILES['files']['type'], + "beschreibung" => $this->uid . " Profil Änderung", + "insertvon" => $this->uid, + "insertamum" => "NOW()", + ]; + + $tmp_res = $this->dmslib->upload($dms, 'files', array("jpg", "png", "pdf")); + + if(isError($tmp_res)){ + $this->addError(getError($tmp_res)); + } + + $tmp_res = $this->getDataOrTerminateWithError($tmp_res); + array_push($res, $tmp_res); + } + + $this->terminateWithSuccess($res); + } + //------------------------------------------------------------------------------------------------------------------ // Private methods @@ -584,6 +647,58 @@ class ProfilUpdate extends FHCAPI_Controller } } + private function deleteOldVersionFile($dms_id) + { + // starting the transaction + $this->db->trans_start(); + + + if (!isset($dms_id)) { + return; + } + + //? delete the file from the profilUpdate first + $profilUpdateFileDelete = $this->ProfilUpdateModel->removeFileFromProfilUpdate($dms_id); + if(isError($profilUpdateFileDelete)){ + $this->terminateWithError(getError($profilUpdateFileDelete)); + } + + //? delete all the different versions of the dms_file + $dmsVersions = $this->DmsVersionModel->loadWhere(["dms_id" => $dms_id]); + $dmsVersions = $this->getDataOrTerminateWithError($dmsVersions); + + + + $dms_versions = array_map(function ($item) { + return $item->version; + }, $dmsVersions); + + + $test_array = array(); + foreach ($dms_versions as $version) { + + $delete_result = $this->dmslib->removeVersion($dms_id, $version); + array_push($test_array, $delete_result); + + if(isError($delete_result)){ + $this->addError(getError($delete_result)); + } + } + + // transaction complete + $this->db->trans_complete(); + + if ($this->db->trans_status() === FALSE) + { + return false; + } + else + { + return true; + } + + } + } diff --git a/application/models/person/Profil_update_model.php b/application/models/person/Profil_update_model.php index 68394dd9a..ffb04b7e7 100755 --- a/application/models/person/Profil_update_model.php +++ b/application/models/person/Profil_update_model.php @@ -77,6 +77,21 @@ class Profil_update_model extends DB_Model } + //? remove File from the Profil Update + public function removeFileFromProfilUpdate($dms_id) + { + + if(!is_int($dms_id) || $dms_id < 0){ + return error("not valid dms_id"); + } + + return $this->execReadOnlyQuery(" + UPDATE public.tbl_profil_update + SET attachment_id = NULL + WHERE attachment_id = ?", [$dms_id]); + + } + /** * getProfilUpdateWithPermission diff --git a/public/js/api/profilUpdate.js b/public/js/api/profilUpdate.js index 742e4a1b1..acfe99813 100644 --- a/public/js/api/profilUpdate.js +++ b/public/js/api/profilUpdate.js @@ -45,14 +45,13 @@ export default { //TODO post request //? new requests insertFile: function (dms, replace = null) { - const url = + + return this.$fhcApi.post( FHC_JS_DATA_STORAGE_OBJECT.app_root + FHC_JS_DATA_STORAGE_OBJECT.ci_router + - `/Cis/ProfilUpdate/insertFile/${replace}`; - - return axios.post(url, dms, { - headers: { "Content-Type": "multipart/form-data" }, - }); + `/api/frontend/v1/ProfilUpdate/insertFile/${replace}`, + dms); + }, getProfilRequestFiles: function (requestID) { diff --git a/public/js/components/Calendar/CalendarModal.js b/public/js/components/Calendar/CalendarModal.js index a9a11f8f7..1d143986a 100644 --- a/public/js/components/Calendar/CalendarModal.js +++ b/public/js/components/Calendar/CalendarModal.js @@ -116,14 +116,14 @@ export default { const result = this.editData.updateID ? //? updating old attachment by replacing //* second parameter of api request insertFile checks if the file has to be replaced or not - await Vue.$fhcapi.ProfilUpdate.insertFile( + await this.$fhcApi.factory.profilUpdate.insertFile( formData, this.editData.updateID ).then((res) => { return res.data?.map((file) => file.dms_id); }) : //? fresh insert of new attachment - await Vue.$fhcapi.ProfilUpdate.insertFile(formData).then((res) => { + await this.$fhcApi.factory.profilUpdate.insertFile(formData).then((res) => { return res.data?.map((file) => file.dms_id); }); return result; diff --git a/public/js/components/Cis/Profil/ProfilModal/EditProfil.js b/public/js/components/Cis/Profil/ProfilModal/EditProfil.js index 86b704c25..1b54fe7f6 100755 --- a/public/js/components/Cis/Profil/ProfilModal/EditProfil.js +++ b/public/js/components/Cis/Profil/ProfilModal/EditProfil.js @@ -129,14 +129,14 @@ export default { const result = this.editData.updateID ? //? updating old attachment by replacing //* second parameter of api request insertFile checks if the file has to be replaced or not - await Vue.$fhcapi.ProfilUpdate.insertFile( + await this.$fhcApi.factory.profilUpdate.insertFile( formData, this.editData.updateID ).then((res) => { return res.data?.map((file) => file.dms_id); }) : //? fresh insert of new attachment - await Vue.$fhcapi.ProfilUpdate.insertFile(formData).then((res) => { + await this.$fhcApi.factory.profilUpdate.insertFile(formData).then((res) => { return res.data?.map((file) => file.dms_id); }); return result;