Files
thetool/application/Contractjournal/ContractjournalController.php

86 lines
2.7 KiB
PHP

<?php
class ContractjournalController extends mfBaseController {
protected function init() {
$this->needlogin=true;
$me = new User();
$me->loadMe();
$this->me = $me;
$this->layout()->set("me",$me);
if(!$me->is(["Admin"])) {
$this->redirect("Dashboard");
}
}
protected function saveAction() {
$r = $this->request;
//var_dump($r->get());exit;
$contract_id = $r->contract_id;
if(!is_numeric($contract_id) || $contract_id < 1) {
$this->layout()->setFlash("Contract nicht gefunden", "error");
$this->redirect("Contract");
}
$contract = new Contract($contract_id);
if(!$contract->id) {
$this->layout()->setFlash("Contract nicht gefunden", "error");
$this->redirect("Contract");
}
$journal_data = [];
$journal_data["contract_id"] = $contract_id;
$journal_data["text"] = ($r->text) ? $r->text : null;
$journal_data["type"] = "text";
if($r->type == "phone") $journal_data["type"] = "phone";
if($r->type == "file") $journal_data["type"] = "file";
if(in_array($r->type, ["text", "phone"])) {
if(!$journal_data['text']) {
$this->layout()->setFlash("Journaleintrag darf nicht leer sein", "error");
$this->redirect("Contract", "view", ['contract_id' => $contract_id]);
}
}
if($r->type == "file") {
try {
// returns File object or throws Exception on error
$file = mfUpload::handleFormUpload("journal_file");
} catch (Exception $ex) {
$this->layout()->setFlash("Fehler beim Dateiupload: ".$ex->getMessage(), "error");
$this->redirect("Contract", "view", ['contract_id' => $contract_id]);
}
$cf = ContractFileModel::create([
'contract_id' => $contract_id,
'file_id' => $file->id,
'name' => $file->name,
]);
$contractfile_id = $cf->save();
if(!$contractfile_id) {
$this->layout()->setFlash("Fehler beim Speichern der hochgeladenen Datei", "error");
$this->redirect("Contract", "view", ['contract_id' => $contract_id]);
}
$journal_data['type'] = "file";
$journal_data['value'] = $contractfile_id;
}
$journal = ContractjournalModel::create($journal_data);
$journal_id = $journal->save();
if(!$journal_id) {
$this->layout()->setFlash("Fehler beim Speichern des Journaleintrags", "error");
$this->redirect("Contract", "view", ['contract_id' => $contract_id]);
}
$this->layout()->setFlash("Journaleintrag erfolgreich gespeichert", "success");
$this->redirect("Contract", "view", ['contract_id' => $contract_id]);
}
}