Added Voiceplan, Admin IVT product matcher and Voicenumber update

updates number in ContractConfig
This commit is contained in:
Frank Schubert
2023-05-04 15:01:28 +02:00
parent a2b519f6f3
commit 8214acd3e9
42 changed files with 1975 additions and 207 deletions
+7
View File
@@ -38,4 +38,11 @@ class AdminController extends mfBaseController {
$this->redirect("Admin");
}
protected function ivtImportMatchProductsAction() {
$this->layout()->setTemplate("Admin/ivtimport_product_match");
$ivtproducts = IvtProductModel::getAll();
$this->layout()->set("ivtproducts", $ivtproducts);
}
}
+25 -5
View File
@@ -18,14 +18,22 @@ class ContractController extends mfBaseController {
protected function indexAction() {
$this->layout()->setTemplate("Contract/Index");
$rfilter = $this->request->filter;
iF(!is_array($rfilter)) {
$rfilter = [];
if($this->request->resetFilter) {
unset($_SESSION[MFAPPNAME.'-Contract-filter']);
}
$this->layout->set("filter", $rfilter);
$filter = [];
if(is_array($this->request->filter)) {
$filter = $this->request->filter;
$_SESSION[MFAPPNAME.'-Contract-filter'] = $filter;
} else {
if(array_key_exists(MFAPPNAME.'-Contract-filter', $_SESSION) && count($_SESSION[MFAPPNAME.'-Contract-filter'])) {
$filter = $_SESSION[MFAPPNAME.'-Contract-filter'];
}
}
$filter = $this->getPreparedFilter($rfilter);
$this->layout->set("filter", $filter);
$filter = $this->getPreparedFilter($filter);
// pagination defaults
$pagination = [];
@@ -344,6 +352,18 @@ class ContractController extends mfBaseController {
}
protected function cancelAction() {
$id = $this->request->contract_id;
if(!$id) $id = $this->request->id;
$this->layout()->setFlash("Not implemented", "error");
if($id) {
$this->redirect("Contract","view",["id" => $id]);
} else {
$this->redirect("Contract");
}
}
protected function addAction() {
$this->layout()->setTemplate("Contract/Form");
$this->layout()->set("terminations", TerminationModel::getAll());
@@ -61,39 +61,42 @@ class CpeprovisioningController extends mfBaseController
continue;
}
}
if(array_key_exists($order->id, $orderproductsprefetch)) {
foreach ($orderproductsprefetch[$order->id] as $orderproduct) {
if(!$orderproduct) continue;
if(!is_array($orderproduct)) continue;
if ($orderproduct['routerconfig_finished'] == 1) {
if (!$filter['routerconfig_finished']) continue;
} else {
if ($filter['routerconfig_finished']) continue;
}
foreach ($orderproductsprefetch[$order->id] as $orderproduct) {
$productattributes = $orderproduct['attributes'];
if ($orderproduct['routerconfig_finished'] == 1) {
if (!$filter['routerconfig_finished']) continue;
} else {
if ($filter['routerconfig_finished']) continue;
}
if (is_array($productattributes) && count($productattributes)) {
// filter out products without bras_type
if (array_key_exists("bras_type", $productattributes) && $productattributes) {
$pagination['maxItems']++;
if ($pagination['maxItems'] >= $pagination['start']+1 && $pagination['maxItems'] <= $pagination['start'] + $pagination['count']) {
$cpeproducts[] = OrderProductModel::getOne($orderproduct['id']);
}
$productattributes = $orderproduct['attributes'];
if (is_array($productattributes) && count($productattributes)) {
// filter out products without bras_type
if (array_key_exists("bras_type", $productattributes) && $productattributes) {
$pagination['maxItems']++;
if ($pagination['maxItems'] >= $pagination['start']+1 && $pagination['maxItems'] <= $pagination['start'] + $pagination['count']) {
$cpeproducts[] = OrderProductModel::getOne($orderproduct['id']);
}
} else {
$this->log->debug("no bras_type oid " . $order->id);
continue;
}
} else {
// ignore products without attributes
$this->log->debug("no attributes oid " . $order->id);
continue;
}
} else {
$this->log->debug("no bras_type oid " . $order->id);
continue;
}
} else {
// ignore products without attributes
$this->log->debug("no attributes oid " . $order->id);
continue;
}
}
}
}
+23
View File
@@ -40,6 +40,29 @@ class File extends mfBaseModel {
return false;
}
public function getFullPath() {
if(!is_numeric($this->id) || $this->id < 1) {
throw new Exception("File not found", 4040);
}
$filename = $this->store_filename;
$path = MFUPLOAD_FILE_SAVE_PATH;
$path .= ($this->subfolder) ? "/".$this->subfolder : "";
$path .= "/$filename";
if(!file_exists($path)) {
throw new Exception("File not found", 4041);
}
return $path;
}
public function getContents() {
$outname = $this->getFullPath();
return file_get_contents($outname);
}
public function getProperty($name) {
if($this->$name == null) {
+37
View File
@@ -0,0 +1,37 @@
<?php
class IvtProduct extends mfBaseModel {
protected $forcestr = [];
/**
* Takes ID or DB row as arguments
* @param id or table row $_
*/
public function __construct($_=NULL) {
$this->log = mfLoghandler::singleton();
$this->data = new stdClass();
$this->table = "products";
$this->db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
if(is_numeric($_)) {
$this->fetch($_);
} elseif(is_object($_)) {
$this->load($_);
}
}
public function save() {
return true;
}
public function __debugInfo() {
$vars = get_object_vars($this);
if(is_object($vars['db'])) $vars['db'] = "object(FronkDB)";
if(is_object($vars['log'])) $vars['log'] = 'object(mfLoghandler)';
return $vars;
}
}
@@ -0,0 +1,89 @@
<?php
class IvtProductModel {
public static function create(Array $data) {
throw new Exception("Please use IvtProductController to create IvtProducts");
}
public static function getAll() {
$items = [];
$db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
$res = $db->select("products", "*", "1=1 ORDER BY id");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new IvtProduct($data);
}
}
return $items;
}
public static function getFirst() {
$db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
$where = self::getSqlFilter($filter);
$res = $db->select("products", "*", "$where ORDER BY id LIMIT 1");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new IvtProduct($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function search($filter, $limit = false) {
$items = [];
$db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM products $where ORDER by id";
mfLoghandler::singleton()->debug($sql);
if(is_array($limit) && count($limit)) {
if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['start'].", ".$limit['count'];
} elseif(is_numeric($count)) {
$sql .= " LIMIT ".$limit['count'];
}
}
$res = $db->query($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new IvtProduct($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
/*
if(array_key_exists("status_id", $filter)) {
$status_id = $filter['status_id'];
if(is_numeric($status_id)) {
$where .= " AND IvtProduct.status_id=$status_id";
}
}
if(array_key_exists("street", $filter)) {
$street = FronkDB::singleton()->escape($filter["street"]);
if($street) {
$where .= " AND street like '%$street%'";
}
}
*/
//var_dump($filter, $where);exit;
return $where;
}
}
+2 -3
View File
@@ -330,7 +330,6 @@ class ProductController extends mfBaseController {
private function findProductApi() {
$search = trim($this->request->q);
$autocomplete = $this->request->autocomplete;
$products = [];
@@ -341,8 +340,8 @@ class ProductController extends mfBaseController {
}
}
$products = array_merge($products, ProductModel::search(["active" => 1, "name%" => $search]));
$wild_search = implode("%", explode(" ", $search));
$products = array_merge($products, ProductModel::search(["active" => 1, "name%" => $wild_search]));
if(!is_array($products) && !count($products)) {
return false;
@@ -150,6 +150,9 @@ class TerminationController extends mfBaseController {
case "setValue":
$return = $this->setValueApi();
break;
case "find":
$return = $this->findTerminationApi();
break;
default:
$return = false;
}
@@ -193,4 +196,12 @@ class TerminationController extends mfBaseController {
return ["msg" => "Saved successfully"];
}
private function findTerminationApi() {
$search = $this->request->search;
$terminations = [];
}
}
+61
View File
@@ -33,6 +33,67 @@ class Voicenumber extends mfBaseModel {
}
}
public function afterSave() {
// if contract_id is given, add number to contract
if($this->contract_id) {
if(is_array($this->contract->configvalues) && array_key_exists("voicenumberblock_voicenumber", $this->contract->configvalues)) {
$cc_item = $this->contract->configvalues["voicenumberblock_voicenumber"];
} else {
$cc_item = ContractconfigItemModel::getFirst(["name" => "voicenumberblock_voicenumber"]);
$cc_item->setContractId($this->contract_id);
}
$contract_numbers = [];
$numbers_json = $cc_item->value->json;
if($numbers_json) {
$contract_numbers = json_decode($numbers_json);
}
//var_dump($contract_numbers);
if(!in_array($this->number, $contract_numbers)) {
$contract_numbers[] = $this->number;
$cc_item->value->set($contract_numbers);
$cc_item->value->save();
}
// if contract_id was changed, remove number from old contract
if($this->_old_data->contract_id > 0 && $this->_old_data->contract_id != $this->contract_id) {
$old_contract = new Contract($this->_old_data->contract_id);
$cc_item = $old_contract->configvalues["voicenumberblock_voicenumber"];
$numbers_json = $cc_item->value->json;
if($numbers_json) {
$contract_numbers = json_decode($numbers_json);
$new_numbers = [];
foreach($contract_numbers as $cnum) {
if($this->number != $cnum) {
$new_numbers[] = $cnum;
}
}
$cc_item->value->set($new_numbers);
$cc_item->value->save();
}
}
} elseif($this->_old_data->contract_id > 0) {
// removed contract_id, so remove number from old contract
$old_contract = new Contract($this->_old_data->contract_id);
$cc_item = $old_contract->configvalues["voicenumberblock_voicenumber"];
$numbers_json = $cc_item->value->json;
if($numbers_json) {
$contract_numbers = json_decode($numbers_json);
$new_numbers = [];
foreach($contract_numbers as $cnum) {
if($this->number != $cnum) {
$new_numbers[] = $cnum;
}
}
$cc_item->value->set($new_numbers);
$cc_item->value->save();
}
}
}
public function getProperty($name) {
if($this->$name == null) {
@@ -155,12 +155,20 @@ class VoicenumberController extends mfBaseController {
$number->update($number_data);
$number_id = $number->save();
if(!$number_id) {
$this->layout()->setFlash("Fehler beim Speichern", "error");
$this->redirect("Voicenumber", "edit", ["block_id" => $block_id, "number" => $num]);
}
//var_dump($number->_old_data);exit;
// save number to contract
$this->layout()->setFlash("Rufnummer erfolgreich gespeichert", "success");
$this->redirect("Voicenumberblock", "Index", null, "block=".$block->id);
+110
View File
@@ -0,0 +1,110 @@
<?php
class Voiceplan extends mfBaseModel {
private $creator;
private $editor;
private $destinations;
public function importDestinationsFromCsv(File $file) {
if(!$this->id) {
return false;
}
try {
$i = 0;
$filename = $file->getFullPath();
$input = fopen($filename, "r");
while($csv = fgetcsv($input, 0, ",")) {
$i++;
if($i == 1) continue;
if(!trim($csv[0])) {
continue;
}
$name = trim($csv[0]);
$prefix = trim($csv[1]);
$price_ek = str_replace(",",".", trim($csv[2]));
$price_vk = str_replace(",",".", trim($csv[3]));
if(!$name || !$prefix || !$price_ek || !$price_vk) {
$this->log->warning("not Importing Voiceplandestination with empty value: destination: $name | prefix: $prefix | ek: $price_ek | vk: $price_vk");
continue;
}
$destination = VoiceplandestinationModel::getFirst(["voiceplan_id" => $this->id, "destination" => $name, "prefix" => $prefix]);
if($destination) {
$this->log->warning("Destination gibts schon, updateing prices: voiceplan_id: ".$this->id." | destination: $name | prefix: $prefix");
$destination->purchase_price = $price_ek;
$destination->price = $price_vk;
$destination->save();
continue;
}
$destination = VoiceplandestinationModel::create([
'voiceplan_id' => $this->id,
'destination' => $name,
'prefix' => $prefix,
'increment_first' => $this->increment_first,
'increment' => $this->increment,
'purchase_price' => $price_ek,
'price' => $price_vk
]);
$destination->save();
}
return true;
} catch(Exception $e) {
echo $e->getCode().": ".$e->getMessage();exit;
return false;
}
}
public function getProperty($name) {
if($this->$name == null) {
if($name == "destinations") {
$this->destinations = VoiceplandestinationModel::search(["voiceplan_id" => $this->id]);
return $this->destinations;
}
if($name == "creator") {
$user = mfValuecache::singleton()->get("Worker-id-".$this->create_by);
if($user) {
$this->creator = $user;
return $this->creator;
}
$this->creator = new User($this->create_by);
if($this->creator->id) {
mfValuecache::singleton()->set("Worker-id-".$this->create_by, $this->creator);
}
return $this->creator;
}
if($name == "editor") {
$this->editor = new User($this->edit_by);
return $this->editor;
}
$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = mfValuecache::singleton()->get("mfObjectmodel-$name-".$this->$idfield);
if($this->$name === null) {
$this->$name = new $classname($this->$idfield);
}
if($this->$name->id) {
mfValuecache::singleton()->set("mfObjectmodel-$name-".$this->$name->id, $this->$name);
return $this->$name;
} else {
return null;
}
}
return $this->$name;
}
}
@@ -0,0 +1,195 @@
<?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]);
}
}
+139
View File
@@ -0,0 +1,139 @@
<?php
class VoiceplanModel {
public $name;
public $description;
public $create_by;
public $edit_by;
public $create;
public $edit;
public static function create(Array $data) {
$model = new Voiceplan();
foreach($data as $field => $value) {
if(property_exists(get_called_class(), $field)) {
$model->$field = $value;
}
}
$me = new User();
$me->loadMe();
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("Voiceplan", "*", "1=1 ORDER BY name");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new Voiceplan($data);
}
}
return $items;
}
public static function getFirst($filter = null) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
mfLoghandler::singleton()->debug($where);
$res = $db->select("Voiceplan", "*", "$where");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new Voiceplan($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 Voiceplan
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, $limit = false) {
$items = [];
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM Voiceplan
WHERE $where
ORDER BY name";
if(is_array($limit) && count($limit)) {
if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['start'].", ".$limit['count'];
} elseif(is_numeric($count)) {
$sql .= " LIMIT ".$limit['count'];
}
}
$res = $db->query($sql);
//mfLoghandler::singleton()->debug($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new Voiceplan($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$db = FronkDB::singleton();
$where = "1=1 ";
if(!is_array($filter)) {
return $where;
}
/*if(array_key_exists("block_id", $filter)) {
$block_id = $filter['block_id'];
if(is_numeric($block_id)) {
$where .= " AND voiceplanblock_id = $block_id";
}
}*/
if(array_key_exists("name", $filter)) {
$name = $db->escape($filter['name']);
if($name) {
$where .= " AND name like '%$name%'";
}
}
//var_dump($filter, $where);exit;
return $where;
}
}
@@ -0,0 +1,39 @@
<?php
class VoiceplanFile extends mfBaseModel {
private $file;
private $creator;
private $editor;
public function getProperty($name) {
if($this->$name == null) {
if(!$this->id) {
return null;
}
if($name == "creator") {
$this->creator = new User($this->create_by);
return $this->creator;
}
if($name == "editor") {
$this->editor = new User($this->edit_by);
return $this->editor;
}
$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;
}
}
@@ -0,0 +1,149 @@
<?php
class VoiceplanFileModel {
public $voiceplan_id;
public $file_id;
public $name;
public $description;
public $create_by = null;
public $edit_by = null;
public $create = null;
public $edit = null;
public static function create(Array $data) {
$model = new VoiceplanFile();
foreach($data as $field => $value) {
if(property_exists(get_called_class(), $field)) {
$model ->$field = $value;
}
}
$me = new User();
$me->loadMe();
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 getOne($id) {
if(!is_numeric($id) || !$id) {
throw new Exception("Invalid number", 400);
}
$item = [];
$db = FronkDB::singleton();
$res = $db->select("VoiceplanFile", "*", "id=$id LIMIT 1");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new VoiceplanFile($data);
}
return $item;
}
public static function getAll() {
$items = [];
$db = FronkDB::singleton();
$res = $db->select("VoiceplanFile", "*", "1=1 ORDER BY voiceplan_id, `create`");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new VoiceplanFile($data);
}
}
return $items;
}
public static function getFirst() {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$res = $db->select("VoiceplanFile", "*", "$where ORDER BY voiceplan_id, `create`");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new VoiceplanFile($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function search($filter) {
$items = [];
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT VoiceplanFile.* FROM VoiceplanFile
LEFT JOIN File ON (VoiceplanFile.file_id = File.id)
WHERE $where
ORDER BY voiceplan_id, `create`";
$res = $db->query($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new VoiceplanFile($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
if(array_key_exists("file_id", $filter)) {
$file_id = $filter['file_id'];
if(is_numeric($file_id)) {
$where .= " AND file_id=$file_id";
}
}
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'";
}
}
if(array_key_exists("filename", $filter)) {
$filename = FronkDB::singleton()->escape($filter['filename']);
if($filename) {
$where .= " AND File.filename='$filename'";
}
}
if(array_key_exists("subfolder", $filter)) {
$subfolder = FronkDB::singleton()->escape($filter['subfolder']);
if($subfolder) {
$where .= " AND File.subfolder='$subfolder'";
}
}
//var_dump($filter, $where);exit;
return $where;
}
}
@@ -0,0 +1,6 @@
<?php
class Voiceplandestination extends mfBaseModel {
protected $forcestr = ["name", "destination", "prefix"];
}
@@ -0,0 +1,105 @@
<?php
class VoiceplandestinationController 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("Voiceplandestination/Form");
$voiceplans = VoiceplanModel::getAll();
$this->layout()->set("voiceplans", $voiceplans);
$voiceplan_id = $this->request->voiceplan_id;
if($voiceplan_id) {
$voiceplan = new Voiceplan($voiceplan_id);
$this->layout()->set("voiceplan", $voiceplan);
}
}
protected function editAction() {
$id = $this->request->id;
if(!is_numeric($id) || $id < 1) {
$this->layout()->setFlash("Destination nicht gefunden", "error");
$this->redirect("Voiceplan");
}
$destination = new Voiceplandestination($id);
if(!$destination->id) {
$this->layout()->setFlash("Destination nicht gefunden", "error");
$this->redirect("Voiceplan");
}
$this->layout()->set("destination", $destination);
return $this->addAction();
}
protected function saveAction() {
$r = $this->request;
var_dump($r);
$id = $r->id;
if(is_numeric($id) && $id > 0) {
$mode = "edit";
$destination = new Voiceplandestination($id);
if(!$destination->id) {
$this->layout()->setFlash("Destination nicht gefunden", "error");
$this->redirect("Voiceplan");
}
} else {
$id = false;
$mode = "add";
}
$data = [];
$data['voiceplan_id'] = $r->voiceplan_id;
$data['destination'] = $r->destination;
$data['prefix'] = intval($r->prefix);
$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");
$this->layout()->set("destination", VoiceplandestinationModel::create($data));
return $this->addAction();
}
$data["increment_first"] = $increments[0];
$data["increment"] = $increments[1];
}
if(!$data['destination'] || !$data['prefix'] || !$data['voiceplan_id']) {
$this->layout()->setFlash("Destination, Prefix und Tarifpaket sind erforderlich", "error");
$this->layout()->set("destination", VoiceplandestinationModel::create($data));
return $this->addAction();
}
if($mode == "edit") {
$destination->update($data);
} else {
$destination = VoiceplandestinationModel::create($data);
}
$id = $destination->save();
if(!$id) {
$this->layout()->setFlash("Fehler beim Speichern!", "error");
$this->layout()->set("destination", $destination);
return $this->addAction();
}
$this->redirect("Voiceplan", "view", ["id" => $destination->voiceplan_id]);
}
}
@@ -0,0 +1,159 @@
<?php
class VoiceplandestinationModel {
public $voiceplan_id;
public $description;
public $destination;
public $prefix;
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 Voiceplandestination();
foreach($data as $field => $value) {
if(property_exists(get_called_class(), $field)) {
$model->$field = $value;
}
}
$me = new User();
$me->loadMe();
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("Voiceplandestination", "*", "1=1 ORDER BY destination,prefix");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new Voiceplandestination($data);
}
}
return $items;
}
public static function getFirst($filter = null) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
mfLoghandler::singleton()->debug($where);
$res = $db->select("Voiceplandestination", "*", "$where ORDER BY destination,prefix");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new Voiceplandestination($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 Voiceplandestination
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, $limit = false) {
$items = [];
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM Voiceplandestination
WHERE $where
ORDER BY destination,prefix";
if(is_array($limit) && count($limit)) {
if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['start'].", ".$limit['count'];
} elseif(is_numeric($count)) {
$sql .= " LIMIT ".$limit['count'];
}
}
$res = $db->query($sql);
//mfLoghandler::singleton()->debug($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new Voiceplandestination($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$db = FronkDB::singleton();
$where = "1=1 ";
if(!is_array($filter)) {
return $where;
}
if(array_key_exists("voiceplan_id", $filter)) {
$voiceplan_id = $filter['voiceplan_id'];
if(is_numeric($voiceplan_id)) {
$where .= " AND voiceplan_id = $voiceplan_id";
}
}
if(array_key_exists("name", $filter)) {
$name = $db->escape($filter['name']);
if($name) {
$where .= " AND name like '%$name%'";
}
}
if(array_key_exists("destination", $filter)) {
$destination = $db->escape($filter['destination']);
if($destination) {
$where .= " AND destination like '%$destination%'";
}
}
if(array_key_exists("prefix", $filter)) {
$prefix = $db->escape($filter['prefix']);
if($prefix) {
$where .= " AND prefix like '%$prefix%'";
}
}
//var_dump($filter, $where);exit;
return $where;
}
}