From 15bb02de520011e9c2b1f145a81f1ae16ed934f9 Mon Sep 17 00:00:00 2001 From: Frank Schubert Date: Thu, 9 Oct 2025 15:33:24 +0200 Subject: [PATCH] Order/Form: documents can be uploaded even if order is closed already --- Layout/default/Order/Form.php | 46 ++++++++++++++-- application/File/File.php | 28 ++++------ application/Order/OrderController.php | 75 ++++++++++++++++++++++++++- lib/mvcfronk/mfUpload/mfUpload.php | 4 +- 4 files changed, 127 insertions(+), 26 deletions(-) diff --git a/Layout/default/Order/Form.php b/Layout/default/Order/Form.php index 31a7848d5..7b28f713c 100644 --- a/Layout/default/Order/Form.php +++ b/Layout/default/Order/Form.php @@ -1041,7 +1041,7 @@
- + Erlaubte Dateiendungen:
@@ -1823,9 +1823,47 @@ //$("#orderForm").detach(); - /*$("#upload-button").click(() => { - $("#orderForm").wrap('
{ + // ajax upload + + const selectedFile = document.getElementById("OrderFileUpload").files[0]; + console.log(selectedFile); + const reader = new FileReader(); + reader.onload = async () => { + const fileContent = reader.result; + //console.log(fileContent); + + formdata = new FormData(); + formdata.append("id", id?>); + formdata.append("file_name", $("#order-files select").val()); + //formdata.append("orig_filename", selectedFile.name); + formdata.append("file_description", $("#order-files textarea[name='file_description']").val()); + formdata.append("file_contents", new Blob([fileContent]), selectedFile.name); + + await fetch(" "uploadDocument"])?>", { + body: formdata, + method: "POST", + }).then(resp => { + if (resp.ok) { + return resp.json(); + } + }).then(data => { + if(data.status == "OK") { + window.location.replace(window.location.href); + } else { + window.notify("error", "Fehler beim Dateiupload!"); + } + }).catch(err => { + window.notify("error", "Fehler beim Dateiupload!"); + console.log(err); + }); + + + } + reader.readAsText(selectedFile); + + + }); $("#upload-button").show(); diff --git a/application/File/File.php b/application/File/File.php index 8887bc097..94c1f2d8c 100644 --- a/application/File/File.php +++ b/application/File/File.php @@ -40,26 +40,18 @@ class File extends mfBaseModel { public function delete() { if($this->id) { - $id = $this->id; + $id = $this->id; - // delete file in store - if($this->subfolder) { - $path = MFUPLOAD_FILE_SAVE_PATH."/".$this->subfolder."/".$this->store_filename; - } else { - $path = MFUPLOAD_FILE_SAVE_PATH."/".$this->store_filename; - } - if(!unlink($path)) { - $this->log->warn(__CLASS__."::delete(): Error unlinking file ($path)"); - } + // delete physical file + if(!unlink($this->getFullPath())) { + $this->log->warn(__CLASS__."::delete(): Error unlinking file ({$this->getFullPath()})"); + } - $where = "id=$id"; - if($this->fieldprefix && !strstr($field,"_")) { - $where = $this->fieldprefix."_id=$id"; - } - if($this->db->delete($this->table,$where)) { - if(method_exists($this, "afterDelete")) { - $this->afterDelete(); - } + $where = "id=$id"; + if($this->db->delete($this->table,$where)) { + if(method_exists($this, "afterDelete")) { + $this->afterDelete(); + } $this->data = new stdClass(); $this->id = ""; return true; diff --git a/application/Order/OrderController.php b/application/Order/OrderController.php index 986d5ba75..c8da669e9 100644 --- a/application/Order/OrderController.php +++ b/application/Order/OrderController.php @@ -239,7 +239,7 @@ class OrderController extends mfBaseController { && $lop->product->attributes['termination_required'] && $lop->termination_id ) { - $has_term = true; + $has_term = true; // is already in orders with terminations } } if(!$has_bras && !$has_voice && !$has_term) { @@ -1286,7 +1286,7 @@ class OrderController extends mfBaseController { } } - + protected function saveVorortterminAction() { if(!$this->me->is(["Admin","salespartner"])) { $this->layout()->setFlash("Keine Berechtigung", "error"); @@ -1478,5 +1478,76 @@ class OrderController extends mfBaseController { $this->redirect("Order"); } + + protected function apiAction() { + $do = $this->request->do; + $data = []; + + switch($do) { + case "uploadDocument": + $return = $this->uploadDocumentApi(); + break; + default: + $return = false; + } + + if(!is_array($return) || !count($return)) { + $data = ["status" => "error"]; + $this->returnJson($data); + } + + if(mfResponse::isResponse($return)) { + $this->returnJson($return); + } else { + $data['status'] = "OK"; + $data['result'] = $return; + $this->returnJson($data); + } + } + + private function uploadDocumentApi() { + $order_id = $this->request->id; + $filename = $this->request->file_name; + $orig_filename = $this->request->orig_filename; + $description = $this->request->file_description; + + if(!$order_id || !is_numeric($order_id) || $order_id < 1) { + return false; + } + $order = new Order($order_id); + if(!$order->id) { + return false; + } + + if(!$filename) { + $this->log->error(__METHOD__.": No filename"); + return false; + } + + $file = mfUpload::handleFormUpload("file_contents"); + + if(!$file) { + $this->log->error(__METHOD__.": Error uploading file"); + return false; + } else { + $of = []; + $of['order_id'] = $order_id; + $of['file_id'] = $file->id; + $of['name'] = $filename; + $of['description'] = $description; + + $orderfile = OrderFileModel::create($of); + if(!$orderfile->save()) { + $file->delete(); + $this->log->error(__METHOD__.": Error saving OrderFile object: ".print_r($orderfile, true)); + } else { + // send email based on file type + $order->sendFileuploadEmail($orderfile); + } + + } + + return ["status" => "OK", "message" => "File uploaded successfully"]; + } } diff --git a/lib/mvcfronk/mfUpload/mfUpload.php b/lib/mvcfronk/mfUpload/mfUpload.php index 128d7f624..19faca0cb 100644 --- a/lib/mvcfronk/mfUpload/mfUpload.php +++ b/lib/mvcfronk/mfUpload/mfUpload.php @@ -37,7 +37,7 @@ class mfUpload { } if($randomFileName) { - $this->filename = $this->getRandomFilename().'-'.$this->filename; + $this->filename = self::getRandomFilename().'-'.$this->filename; } $this->size = $this->upload->getFileSize(); @@ -184,7 +184,7 @@ class mfUpload { return true; } - public function getRandomFilename() { + public static function getRandomFilename() { $length = 20; $characters = '0123456789abcdef'; $string = '';