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

This commit is contained in:
SimonGschnell
2024-07-22 11:24:47 +02:00
parent 0cc6222175
commit a0871657ca
5 changed files with 139 additions and 10 deletions
@@ -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;
}
}
}
@@ -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
+5 -6
View File
@@ -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) {
@@ -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;
@@ -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;