321 lines
9.9 KiB
PHP
321 lines
9.9 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);
|
|
|
|
$filter = [];
|
|
if(is_array($this->request->filter)) {
|
|
$filter = $this->request->filter;
|
|
}
|
|
|
|
$this->layout->set("filter", $filter);
|
|
|
|
if($filter) {
|
|
$filter = $this->getPreparedViewFilter($filter);
|
|
}
|
|
|
|
$pagination = [];
|
|
$pagination['start'] = 0;
|
|
$pagination['count'] = 20;
|
|
$pagination['maxItems'] = 0;
|
|
|
|
if(is_numeric($this->request->s)) {
|
|
$pagination['start'] = intval($this->request->s);
|
|
}
|
|
|
|
$zone_search = $filter;
|
|
$zone_search['voiceplan_id'] = $id;
|
|
|
|
$pagination['maxItems'] = VoiceplanzoneModel::count($zone_search);
|
|
$zones = VoiceplanzoneModel::search($zone_search, $pagination);
|
|
$this->layout()->set("zones", $zones);
|
|
|
|
$this->layout()->set("pagination", $pagination);
|
|
|
|
}
|
|
|
|
private function getPreparedViewFilter($filter) {
|
|
$new_filter = [];
|
|
|
|
if(array_key_exists("prefix", $filter) && $filter['prefix']) {
|
|
$prefix = $filter['prefix'];
|
|
if(substr($prefix, 0, 1) == "+") {
|
|
$prefix = substr($prefix, 1)."%";
|
|
}
|
|
$new_filter['prefix'] = $prefix;
|
|
unset($filter['prefix']);
|
|
}
|
|
|
|
foreach($filter as $name => $value) {
|
|
$new_filter[$name] = $value;
|
|
}
|
|
|
|
return $new_filter;
|
|
}
|
|
|
|
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;
|
|
$data['price_multiplicator'] = ($r->price_multiplicator) ? $r->price_multiplicator : null;
|
|
|
|
|
|
if($r->increment) {
|
|
$increments = explode("/",$r->increment);
|
|
if(count($increments) != 2 || !is_numeric($increments[0]) || !is_numeric($increments[1])) {
|
|
$this->layout()->setFlash("Ungültige Taktung", "error");
|
|
return $this->editAction();
|
|
}
|
|
|
|
$data["increment_first"] = $increments[0];
|
|
$data["increment"] = $increments[1];
|
|
} else {
|
|
$this->layout()->setFlash("Ungültige Taktung", "error");
|
|
return $this->editAction();
|
|
}
|
|
|
|
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($r->copy_from_plan_id) {
|
|
// get old voiceplan, clone zones and save destinations
|
|
$old_voiceplan = new Voiceplan($r->copy_from_plan_id);
|
|
if(!$old_voiceplan->id) {
|
|
$this->layout()->setFlash("Source Voiceplan nicht gefunden. Keine Zonen und Destinations kopiert!", "warning");
|
|
$this->redirect("Voiceplan", "view", ["id" => $id]);
|
|
}
|
|
|
|
foreach($old_voiceplan->zones as $old_zone) {
|
|
$new_zone = clone($old_zone);
|
|
$new_zone->voiceplan_id = $id;
|
|
$new_zone->increment_first = $voiceplan->increment_first;
|
|
$new_zone->increment = $voiceplan->increment;
|
|
if($new_zone->purchase_price && $voiceplan->price_multiplicator) {
|
|
$new_zone->price = (float)$new_zone->purchase_price * (float)$voiceplan->price_multiplicator;
|
|
}
|
|
if(!$new_zone->save()) {
|
|
$this->layout()->setFlash("Fehler beim Kopieren der Source Zone!", "error");
|
|
$this->redirect("Voiceplan", "view", ["id" => $id]);
|
|
}
|
|
|
|
foreach($new_zone->cloned_destinations as $cd) {
|
|
$cd->voiceplanzone_id = $new_zone->id;
|
|
if(!$cd->save()) {
|
|
$this->layout()->setFlash("Fehler beim Speichern der kopierten Destinations!", "error");
|
|
$this->redirect("Voiceplan", "view", ["id" => $id]);
|
|
}
|
|
}
|
|
}
|
|
$voiceplan->checkFixpriceZones();
|
|
|
|
} elseif(is_array($_FILES) && array_key_exists("voiceplanzonefile", $_FILES) && !$_FILES['voiceplanzonefile']['error']) {
|
|
// import zones
|
|
// look for uploaded import file
|
|
try {
|
|
// returns File object or throws Exception on error
|
|
$file = mfUpload::handleFormUpload("voiceplanzonefile");
|
|
} catch (Exception $ex) {
|
|
$this->layout()->setFlash("Fehler beim Upload des Zonenfiles: ".$ex->getMessage(), "error");
|
|
return $this->editAction();
|
|
}
|
|
|
|
$vpf = VoiceplanFileModel::create([
|
|
'voiceplan_id' => $id,
|
|
'file_id' => $file->id,
|
|
'name' => "voiceplan-zone-import-".date("Y-m-d_H-i-s").".csv",
|
|
]);
|
|
$voiceplanzonefile_id = $vpf->save();
|
|
if(!$voiceplanzonefile_id) {
|
|
$this->layout()->setFlash("Fehler beim Speichern der hochgeladenen Datei", "error");
|
|
return $this->editAction();
|
|
}
|
|
|
|
// if file was uploaded successfully, delete old destinations and zones
|
|
foreach(VoiceplandestinationModel::search(['voiceplan_id' => $id]) as $destination) {
|
|
$destination->delete();
|
|
}
|
|
foreach(VoiceplanzoneModel::search(['voiceplan_id' => $id]) as $zone) {
|
|
$zone->delete();
|
|
}
|
|
|
|
|
|
if(!$voiceplan->importZonesFromCsv($file)) {
|
|
$this->layout()->setFlash("Fehler beim Importieren der Zonen!", "error");
|
|
return $this->editAction();
|
|
}
|
|
|
|
// import destinations
|
|
if(is_array($_FILES) && array_key_exists("voiceplandestinationfile", $_FILES) && !$_FILES['voiceplandestinationfile']['error']) {
|
|
// look for uploaded import file
|
|
try {
|
|
// returns File object or throws Exception on error
|
|
$file = mfUpload::handleFormUpload("voiceplandestinationfile");
|
|
} catch (Exception $ex) {
|
|
$this->layout()->setFlash("Fehler beim Upload des Destinationfiles: ".$ex->getMessage(), "error");
|
|
return $this->editAction();
|
|
}
|
|
|
|
$vpf = VoiceplanFileModel::create([
|
|
'voiceplan_id' => $id,
|
|
'file_id' => $file->id,
|
|
'name' => "voiceplan-dest-import-".date("Y-m-d_H-i-s").".csv",
|
|
]);
|
|
$voiceplandestinationfile_id = $vpf->save();
|
|
if(!$voiceplandestinationfile_id) {
|
|
$this->layout()->setFlash("Fehler beim Speichern der hochgeladenen Datei", "error");
|
|
return $this->editAction();
|
|
}
|
|
|
|
if(!$voiceplan->importDestinationsFromCsv($file)) {
|
|
$this->layout()->setFlash("Fehler beim Importieren!", "error");
|
|
return $this->editAction();
|
|
}
|
|
|
|
if($voiceplan->import_errors) {
|
|
$this->layout()->setFlash(implode("<br />\n", $voiceplan->import_errors), "warning");
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$this->layout()->setFlash("Sprachtarifpaket erfolgreich gespeichert!", "success");
|
|
$this->redirect("Voiceplan", "Index");
|
|
|
|
}
|
|
|
|
protected function downloadPdf() {
|
|
$this->layout()->setTemplate("Voiceplan/export.pdf");
|
|
|
|
$voiceplan = new Voiceplan($this->request->id);
|
|
if(!$voiceplan->id) {
|
|
$this->layout()->setFlash("Sprachtarif nicht gefunden", "error");
|
|
$this->redirect("Voiceplan", "Index");
|
|
}
|
|
|
|
$pdf_vars = [
|
|
"voiceplan" => $voiceplan,
|
|
];
|
|
|
|
$pdf = new PdfForm("Voiceplan/export.pdf", $pdf_vars);
|
|
$title = "Xinon Sprachtarif ".$voiceplan->name;
|
|
$pdf->render("--footer-center '$title Seite [page] / [topage]' --footer-font-name OpenSans --footer-font-size 9");
|
|
$tvalue = $pdf->getReturnedValues();
|
|
$pdfname = $tvalue['filename'];
|
|
$pdf->download($pdfname);
|
|
}
|
|
} |