Files
thetool/application/Voiceplan/VoiceplanController.php
Frank Schubert 8214acd3e9 Added Voiceplan, Admin IVT product matcher and Voicenumber update
updates number in ContractConfig
2023-05-04 15:01:28 +02:00

195 lines
5.3 KiB
PHP

<?php
class VoiceplanController extends mfBaseController {
protected function init() {
$this->needlogin=true;
$me = new User();
$me->loadMe();
$this->me = $me;
$this->layout()->set("me",$me);
if(!$me->isAdmin()) {
$this->redirect("Dashboard");
}
}
protected function indexAction() {
$filter = [];
if(is_array($this->request->filter)) {
$filter = $this->request->filter;
}
$this->layout->set("filter", $filter);
if($filter) {
$filter = $this->getPreparedFilter($filter);
}
// pagination defaults
$pagination = [];
$pagination['start'] = 0;
$pagination['count'] = 20;
$pagination['maxItems'] = 0;
if(is_numeric($this->request->s)) {
$pagination['start'] = intval($this->request->s);
}
$pagination['maxItems'] = VoiceplanModel::count($filter);
$voiceplans = VoiceplanModel::getAll();
$this->layout()->set("voiceplans", $voiceplans);
$this->layout()->set("pagination", $pagination);
}
private function getPreparedFilter($filter) {
return $filter;
}
protected function viewAction() {
$this->layout()->setTemplate("Voiceplan/View");
$id = $this->request->id;
if(!is_numeric($id) || $id < 1) {
$this->layout()->setFlash("Tarifpaket nicht gefunden.", "error");
$this->redirect("Voiceplan");
}
$plan = new Voiceplan($id);
if(!$plan->id) {
$this->layout()->setFlash("Tarifpaket nicht gefunden.", "error");
$this->redirect("Voiceplan");
}
$this->layout()->set("plan", $plan);
$pagination = [];
$pagination['start'] = 0;
$pagination['count'] = 20;
$pagination['maxItems'] = 0;
if(is_numeric($this->request->s)) {
$pagination['start'] = intval($this->request->s);
}
$pagination['maxItems'] = VoiceplandestinationModel::count(['voiceplan_id' => $id]);
$destinations = VoiceplandestinationModel::search(['voiceplan_id' => $id], $pagination);
$this->layout()->set("destinations", $destinations);
$this->layout()->set("pagination", $pagination);
}
protected function addAction() {
$this->layout()->setTemplate("Voiceplan/Form");
}
protected function editAction() {
$id = $this->request->id;
if(!is_numeric($id) || $id < 1) {
$this->layout()->setFlash("Tarifpaket nicht gefunden.", "error");
$this->redirect("Voiceplan");
}
$plan = new Voiceplan($id);
if(!$plan->id) {
$this->layout()->setFlash("Tarifpaket nicht gefunden.", "error");
$this->redirect("Voiceplan");
}
$this->layout()->set("plan", $plan);
return $this->addAction();
}
protected function saveAction() {
$r = $this->request;
//var_dump($r);exit;
$id = $r->id;
if(is_numeric($id) && $id > 0) {
$mode = "edit";
$voiceplan = new Voiceplan($id);
if(!$voiceplan->id) {
$this->layout()->setFlash("Tarifpaket nicht gefunden", "error");
$this->redirect("Voiceplan");
}
} else {
$id = false;
$mode = "add";
}
$data = [];
$data['name'] = $r->name;
$data['description'] = $r->description;
if($r->increment) {
$increments = explode("/",$r->increment);
if(count($increments) != 2) {
$this->layout()->setFlash("Ungültige Taktung", "error");
return $this->editAction();
}
$data["increment_first"] = $increments[0];
$data["increment"] = $increments[1];
}
if(!$data['name']) {
$this->layout()->setFlash("Bitte Name eingeben", "error");
return $this->editAction();
}
if($mode == "edit") {
$voiceplan->update($data);
} else {
$voiceplan = VoiceplanModel::create($data);
}
$id = $voiceplan->save();
if(!$id) {
$this->layout()->setFlash("Fehler beim Speichern!", "error");
return $this->editAction();
}
if(is_array($_FILES) && array_key_exists("voiceplanfile", $_FILES) && !$_FILES['voiceplanfile']['error']) {
// look for uploaded import file
try {
// returns File object or throws Exception on error
$file = mfUpload::handleFormUpload("voiceplanfile");
} catch (Exception $ex) {
$this->layout()->setFlash("Fehler beim Dateiupload: ".$ex->getMessage(), "error");
return $this->editAction();
}
$vpf = VoiceplanFileModel::create([
'voiceplan_id' => $id,
'file_id' => $file->id,
'name' => "voiceplan-import-".date("Y-m-d_H-i-s").".csv",
]);
$voiceplanfile_id = $vpf->save();
if(!$voiceplanfile_id) {
$this->layout()->setFlash("Fehler beim Speichern der hochgeladenen Datei", "error");
return $this->editAction();
}
// if file was uploaded successfully, delete old destinations
foreach(VoiceplandestinationModel::search(['voiceplan_id' => $id]) as $destination) {
$destination->delete();
}
if(!$voiceplan->importDestinationsFromCsv($file)) {
$this->layout()->setFlash("Fehler beim Importieren!", "error");
return $this->editAction();
}
}
$this->layout()->setFlash("Sprachtarifpaket erfolgreich gespeichert!", "success");
$this->redirect("Voiceplan", "view", ["id" => $id]);
}
}