changes how files are attached/replaced to a profil update

This commit is contained in:
SimonGschnell
2024-02-09 13:55:08 +01:00
parent cda13ac705
commit 43492add0b
10 changed files with 395 additions and 345 deletions
+231 -3
View File
@@ -16,8 +16,15 @@ class ProfilUpdate extends Auth_Controller
'acceptProfilRequest'=>['student/stammdaten:rw','mitarbeiter/stammdaten:rw'],
'denyProfilRequest'=>['student/stammdaten:rw','mitarbeiter/stammdaten:rw'],
'show'=>['student/stammdaten:r','mitarbeiter/stammdaten:r'],
'insertProfilRequest' => ['student/anrechnung_beantragen:r', 'user:r'],
'updateProfilRequest' => ['student/anrechnung_beantragen:r', 'user:r'],
'deleteProfilRequest' => ['student/anrechnung_beantragen:r', 'user:r'],
'selectProfilRequest' => ['student/anrechnung_beantragen:r', 'user:r'],
'insertFile' => ['student/anrechnung_beantragen:r', 'user:r'],
'getProfilRequestFiles' => ['student/anrechnung_beantragen:r', 'user:r'],
]);
@@ -26,6 +33,12 @@ class ProfilUpdate extends Auth_Controller
$this->load->model('person/Adresse_model','AdresseModel');
$this->load->model('person/Adressentyp_model', 'AdressenTypModel');
$this->load->model('person/Person_model','PersonModel');
$this->load->library('DmsLib');
//? put the uid and pid inside the controller for reusability
$this->uid = getAuthUID();
$this->pid = getAuthPersonID();
}
@@ -45,6 +58,221 @@ class ProfilUpdate extends Auth_Controller
echo json_encode($res);
}
public function insertFile($replace){
$replace = json_decode($replace);
if(!count($_FILES)){
echo json_encode([]);
return;
}
//? 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"]);
$attachmentID = $this->ProfilUpdateModel->load([$replace]);
if(isError($attachmentID)){
return json_encode(error("Error loading ProfilUpdate resource"));
}
//? get the attachmentID
$dms_id = hasData($attachmentID) ? getData($attachmentID)[0]->attachment_id : null;
//? delete old dms_file of Profil Update
$this->deleteOldVersionFile($dms_id);
}
$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');
$tmp_res = hasData($tmp_res)? getData($tmp_res) : null;
array_push($res,$tmp_res);
}
echo json_encode($res);
}
private function deleteOldVersionFile($dms_id){
if(!isset($dms_id)){
return;
}
//? collect all the results of the deleted versions in an array
$res = array();
//? delete all the different versions of the dms_file
$dmsVersions = $this->DmsVersionModel->loadWhere(["dms_id"=>$dms_id]);
$dmsVersions = hasData($dmsVersions) ? getData($dmsVersions) : null;
if(isset($dmsVersions)){
$zwischen_res = array_map(function($item){ return $item->version;},$dmsVersions);
foreach($zwischen_res as $version){
array_push($res, $this->DmsVersionModel->delete([$dms_id,$version]));
}
}else{
echo json_encode(error("No version of the file has been found"));
}
//? returns a result for each deleted dms_file
return $res;
}
public function selectProfilRequest(){
$_GET = json_decode($this->input->raw_input_stream, true);
$uid = $this->input->get('uid');
$id = $this->input->get('id');
$whereClause=['uid'=> $this->uid];
if(isset($uid)) $whereClause['uid'] = $uid;
if(isset($id)) $whereClause['id'] = $id;
$res= $this->ProfilUpdateModel->getProfilUpdatesWhere($whereClause);
echo json_encode($res);
}
public function getProfilRequestFiles(){
$id = json_decode($this->input->raw_input_stream);
$this->ProfilUpdateModel->addSelect(["attachment_id"]);
$attachmentID = $this->ProfilUpdateModel->load([$id]);
if(isError($attachmentID)){
return json_encode(error("Error loading ProfilUpdate resource"));
}
//? get the attachmentID
$dms_id = hasData($attachmentID) ? getData($attachmentID)[0]->attachment_id : null;
//? get the name to the file
$this->DmsVersionModel->addSelect(["name", "dms_id"]);
$attachment = $this->DmsVersionModel->load([$dms_id,0]);
if(isError($attachment)){
return json_encode(error("Error loading DmsVersion resource"));
}
$attachment = hasData($attachment) ? getData($attachment) : null;
//? returns {name:..., dms_id:...}
echo json_encode($attachment);
}
public function insertProfilRequest()
{
$json = json_decode($this->input->raw_input_stream);
$payload = $json->payload;
$identifier = property_exists($json->payload,"kontakt_id")? "kontakt_id" : (property_exists($json->payload,"adresse_id")? "adresse_id" : null);
$name = $this->PersonModel->getFullName($this->uid);
if(isError($name)){
// error handling
var_dump($name);
return;
}
$data = ["topic"=>$json->topic,"uid" => $this->uid, "name"=>getData($name), "requested_change" => json_encode($payload), "insertamum" => "NOW()", "insertvon"=>$this->uid,"status"=>"pending" ];
//? insert fileID in the dataset if sent with post request
if(isset($json->fileID)){
$data['attachment_id'] = $json->fileID;
}
//? loops over all updateRequests from a user to validate if the new request is valid
$res = $this->ProfilUpdateModel->getProfilUpdatesWhere(["uid"=>$this->uid]);
$res = hasData($res) ? getData($res) : null;
if($res){
$pending_changes = array_filter($res, function($element) {
return $element->status == 'pending';
});
foreach($pending_changes as $update_request){
$existing_change = json_decode($update_request->requested_change);
//? the user can add as many new kontakt/adresse as he likes
if( !isset($payload->add) && property_exists($existing_change,$identifier) && property_exists($payload,$identifier) && $existing_change->$identifier == $payload->$identifier){
//? the kontakt_id / adresse_id of a change has to be unique
echo json_encode(error("cannot change the same resource twice"));
return;
}
elseif(!$identifier && $update_request->topic == $json->topic ){
//? if it is not a delete or add request than the topic has to be unique
echo json_encode(error("A request to change " . $json->topic . " is already open"));
return;
}
}}
$insertID = $this->ProfilUpdateModel->insert($data);
if(isError($insertID)){
//catch error
}else{
$insertID = hasData($insertID)? getData($insertID): null;
$editTimestamp = $this->ProfilUpdateModel->getTimestamp($insertID);
$date = success(date_create($editTimestamp)->format('d.m.Y'));
echo json_encode($date);
}
}
public function updateProfilRequest()
{
$json = json_decode($this->input->raw_input_stream);
$updateData = ["requested_change" => json_encode($json->payload), "updateamum" => "NOW()", "updatevon" => $this->uid];
if(isset($json->fileID)){
$updateData['attachment_id'] = json_decode($json->fileID);
}
$updateID =$this->ProfilUpdateModel->update([$json->ID],$updateData);
//? insert fileID in the dataset if sent with post request
if(isError($updateID)){
//catch error
}else{
$updateID = hasData($updateID)? getData($updateID)[0]: null;
$editTimestamp = $this->ProfilUpdateModel->getTimestamp($updateID,true);
$date = success(date_create($editTimestamp)->format('d.m.Y'));
echo json_encode($date);
}
}
public function deleteProfilRequest(){
$json = json_decode($this->input->raw_input_stream);
$delete_res = $this->ProfilUpdateModel->delete([$json]);
echo json_encode($delete_res);
}
public function getProfilUpdateWithPermission($status=null){
@@ -125,11 +353,11 @@ class ProfilUpdate extends Auth_Controller
}
private function updateRequestedChange($id, $requested_change){
return $this->ProfilUpdateModel->update([$id], ['requested_change'=>json_encode($requested_change)]);
return $this->update([$id], ['requested_change'=>json_encode($requested_change)]);
}
private function setStatusOnUpdateRequest($id, $status, $status_message ){
return $this->ProfilUpdateModel->update([$id], ["status"=>$status,"status_timestamp"=>"NOW()","status_message"=>$status_message]);
return $this->update([$id], ["status"=>$status,"status_timestamp"=>"NOW()","status_message"=>$status_message]);
}