WIP Voiceplan Zones

This commit is contained in:
Frank Schubert
2023-10-13 11:31:45 +02:00
parent 97432a24bb
commit 33c2c63100
17 changed files with 654 additions and 93 deletions

View File

@@ -0,0 +1,33 @@
<?php
class Voiceplanzone extends mfBaseModel {
private $destinations;
private $voiceplan;
public function getProperty($name) {
if($this->$name == null) {
if(!$this->id) {
return null;
}
if($name == "destinations") {
$this->destinations = VoiceplandestinationModel::search(["voiceplanzone_id" => $this->id]);
return $this->destinations;
}
$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = new $classname($this->$idfield);
if($this->$name->id) {
return $this->$name;
} else {
return null;
}
}
return $this->$name;
}
}

View File

@@ -0,0 +1,140 @@
<?php
class VoiceplanzoneController 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 addAction() {
$this->layout()->setTemplate("Voiceplanzone/Form");
if(strtolower($this->action) == "add" && $this->request->voiceplan_id) {
$this->layout()->set("voiceplan_id", $this->request->voiceplan_id);
$this->layout()->set("voiceplan", new Voiceplan($this->request->voiceplan_id));
}
}
protected function viewAction() {
$this->layout()->setTemplate("Voiceplanzone/View");
$id = $this->request->id;
if(!is_numeric($id) || $id < 1) {
$this->layout()->setFlash("Tarifzone nicht gefunden.", "error");
$this->redirect("Voiceplan");
}
$zone = new Voiceplanzone($id);
if(!$zone->id) {
$this->layout()->setFlash("Tarifzone nicht gefunden.", "error");
$this->redirect("Voiceplan");
}
$this->layout()->set("zone", $zone);
$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(['voiceplanzone_id' => $id]);
$destinations = VoiceplandestinationModel::search(['voiceplanzone_id' => $id], $pagination);
$this->layout()->set("destinations", $destinations);
$this->layout()->set("pagination", $pagination);
}
protected function editAction() {
$id = $this->request->id;
if(!is_numeric($id) || $id < 1) {
$this->layout()->setFlash("Tarifzone nicht gefunden.", "error");
$this->redirect("Voiceplan");
}
$zone = new Voiceplanzone($id);
if(!$zone->id) {
$this->layout()->setFlash("Tarifzone nicht gefunden.", "error");
$this->redirect("Voiceplan");
}
$this->layout()->set("zone", $zone);
return $this->addAction();
}
protected function saveAction() {
$r = $this->request;
//var_dump($r);exit;
$id = $r->id;
if(is_numeric($id) && $id > 0) {
$mode = "edit";
$zone = new Voiceplanzone($id);
if(!$zone->id) {
$this->layout()->setFlash("Tarifzone nicht gefunden", "error");
$this->redirect("Voiceplan");
}
} else {
$id = false;
$mode = "add";
}
$data["voiceplan_id"] = $r->voiceplan_id;
$data["name"] = $r->name;
$data['purchase_price'] = (str_replace(",",".",$r->purchase_price)) ? str_replace(",",".",$r->purchase_price) : 0;
$data['price'] = (str_replace(",",".",$r->price)) ? str_replace(",",".",$r->price) : 0;
if($r->increment) {
$increments = explode("/",$r->increment);
if(count($increments) != 2) {
$this->layout()->setFlash("Ungültige Taktung", "error");
if($mode == "edit") {
$this->redirect("Voiceplanzone", "edit", ["id" => $id]);
} else {
$this->layout()->set("zone", VoiceplanzoneModel::create($data));
return $this->addAction();
}
}
$data["increment_first"] = $increments[0];
$data["increment"] = $increments[1];
}
if(!$data['name'] || !$data['voiceplan_id']) {
$this->layout()->setFlash("Name und Tarifpaket sind erforderlich", "error");
if($mode == "edit") {
$this->redirect("Voiceplanzone", "edit", ["id" => $id]);
} else {
$this->layout()->set("destination", VoiceplanzoneModel::create($data));
return $this->addAction();
}
}
if($mode == "edit") {
$zone->update($data);
} else {
$zone = VoiceplanzoneModel::create($data);
}
$id = $zone->save();
if(!$id) {
$this->layout()->setFlash("Fehler beim Speichern!", "error");
$this->layout()->set("zone", $zone);
return $this->addAction();
}
$this->redirect("Voiceplan", "view", ["id" => $zone->voiceplan_id]);
}
}

View File

@@ -0,0 +1,134 @@
<?php
class VoiceplanzoneModel {
public $voiceplan_id;
public $name;
public $purchase_price;
public $price;
public $increment_first;
public $increment;
public $create_by;
public $edit_by;
public $create;
public $edit;
public static function create(Array $data) {
$model = new Voiceplanzone();
foreach($data as $field => $value) {
if(property_exists(get_called_class(), $field)) {
$model ->$field = $value;
}
}
$me = mfValuecache::singleton()->get("me");
if(!$me) {
$me = new User();
$me->loadMe();
mfValuecache::singleton()->set("me", $me);
}
if($model->create_by === null) {
$model->create_by = $me->id;
}
if($model->edit_by === null) {
$model->edit_by = $me->id;
}
return $model;
}
public static function getAll() {
$items = [];
$db = FronkDB::singleton();
$res = $db->select("Voiceplanzone", "*", "1=1 ORDER BY voiceplan_id,name");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new Voiceplanzone($data);
}
}
return $items;
}
public static function getFirst() {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$res = $db->select("Voiceplanzone", "*", "$where ORDER BY voiceplan_id,name");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new Voiceplanzone($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function count($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT COUNT(*) as cnt FROM Voiceplanzone
WHERE $where
";
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
return $data->cnt;
}
return 0;
}
public static function search($filter) {
$items = [];
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM Voiceplanzone
WHERE $where
ORDER BY voiceplan_id,name";
$res = $db->query($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new Voiceplanzone($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
if(array_key_exists("voiceplan_id", $filter)) {
$voiceplan_id = $filter['voiceplan_id'];
if(is_numeric($voiceplan_id)) {
$where .= " AND voiceplan_id=$voiceplan_id";
}
}
//var_dump($filter);exit;
if(array_key_exists("name", $filter)) {
$name = FronkDB::singleton()->escape($filter['name']);
if($name) {
$where .= " AND name='$name'";
}
}
//var_dump($filter, $where);exit;
return $where;
}
}