175 lines
4.6 KiB
PHP
175 lines
4.6 KiB
PHP
<?php
|
|
|
|
class VoicenumberblockController 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() {
|
|
$this->layout()->setTemplate("Voicenumberblock/Index");
|
|
|
|
$filter = [];
|
|
if(is_array($this->request->filter)) {
|
|
$filter = $this->request->filter;
|
|
}
|
|
|
|
$this->layout->set("filter", $filter);
|
|
|
|
if($filter) {
|
|
$filter = $this->getPreparedFilter($filter);
|
|
}
|
|
|
|
$num_from = [];
|
|
$num_from_start = $this->request->nf;
|
|
$num_from_block = $this->request->nfb;
|
|
|
|
if($num_from_start && $num_from_block) {
|
|
$num_from[$num_from_block] = $num_from_start;
|
|
}
|
|
$this->layout()->set("num_from", $num_from);
|
|
|
|
// 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'] = VoicenumberblockModel::count($filter);
|
|
$blocks = VoicenumberblockModel::search($filter, $pagination);
|
|
|
|
$this->layout()->set("blocks", $blocks);
|
|
$this->layout()->set("pagination", $pagination);
|
|
|
|
}
|
|
|
|
private function getPreparedFilter($filter) {
|
|
return $filter;
|
|
}
|
|
|
|
protected function addAction() {
|
|
$this->layout()->setTemplate("Voicenumberblock/Form");
|
|
}
|
|
|
|
protected function editAction() {
|
|
$id = $this->request->id;
|
|
if(!is_numeric($id) || !$id) {
|
|
$this->layout()->setFlash("Rufnummernblock nicht gefunden", "error");
|
|
$this->redirect("Voicenumberblock");
|
|
}
|
|
|
|
$block = new Voicenumberblock($id);
|
|
if($block->id != $id) {
|
|
$this->layout()->setFlash("Rufnummernblock nicht gefunden", "error");
|
|
$this->redirect("Voicenumberblock");
|
|
}
|
|
|
|
$this->layout()->set("block", $block);
|
|
return $this->addAction();
|
|
}
|
|
|
|
protected function saveAction() {
|
|
$r = $this->request;
|
|
$id = $r->id;
|
|
//var_dump($r);exit;
|
|
if(is_numeric($id) && $id > 0) {
|
|
$mode = "edit";
|
|
$block = new Voicenumberblock($id);
|
|
if(!$block->id) {
|
|
$this->layout()->setFlash("Rufnummernblock nicht gefunden", "error");
|
|
$this->redirect("Voicenumberblock");
|
|
}
|
|
} else {
|
|
$mode = "add";
|
|
}
|
|
|
|
if(!$r->countrycode ||!$r->areacode || !$r->first || !$r->last) {
|
|
$this->layout()->setFlash("Bitte alle benötigten Felder ausfüllen!", "error");
|
|
$this->layout()->set("block", $r);
|
|
return $this->add();
|
|
}
|
|
|
|
$data = [];
|
|
$data['name'] = trim($r->name);
|
|
$data['countrycode'] = trim($r->countrycode);
|
|
$data['areacode'] = trim($r->areacode);
|
|
|
|
$data['first'] = $data['countrycode'].$data['areacode'].trim($r->first);
|
|
$data['last'] = $data['countrycode'].$data['areacode'].trim($r->last);
|
|
|
|
$prefix_length = strlen($data['first']) - strlen($data['last'] - $data['first']);
|
|
$data['prefix'] = substr($data['first'], 0, $prefix_length);
|
|
|
|
$data['comment'] = nl2br(htmlentities(trim($r->comment)));
|
|
$data['edit_by'] = $this->me->id;
|
|
|
|
if($mode == "add") {
|
|
$data['create_by'] = 1;
|
|
$block = VoicenumberblockModel::create($data);
|
|
} else {
|
|
$block->update($data);
|
|
}
|
|
|
|
$new_id = $block->save();
|
|
if(!$new_id) {
|
|
$this->layout()->setFlash("Fehler beim Speichern", "error");
|
|
$this->layout()->set("block", $block);
|
|
return $this->addAction();
|
|
}
|
|
|
|
$this->layout()->setFlash("Rufnummernblock erfolgreich gespeichert.", "success");
|
|
$this->redirect("Voicenumberblock");
|
|
|
|
}
|
|
|
|
protected function apiAction() {
|
|
$do = $this->request->do;
|
|
$data = [];
|
|
|
|
switch($do) {
|
|
case "getFreeNumbers":
|
|
$return = $this->getFreeNumbersApi();
|
|
break;
|
|
default:
|
|
$return = false;
|
|
}
|
|
|
|
if(!is_array($return) || !count($return)) {
|
|
$data = ["status" => "error"];
|
|
$this->returnJson($data);
|
|
}
|
|
$data['status'] = "OK";
|
|
$data['result'] = $return;
|
|
$this->returnJson($data);
|
|
}
|
|
|
|
private function getFreeNumbersApi() {
|
|
$block_id = $this->request->id;
|
|
if(!is_numeric($block_id) || $block_id < 1) {
|
|
return false;
|
|
}
|
|
|
|
$block = new Voicenumberblock($block_id);
|
|
if(!$block->id) {
|
|
return false;
|
|
}
|
|
|
|
$free = $block->getFreeNumbers();
|
|
$free[] = "43 3476 41122";
|
|
$free[] = "43 316 413030";
|
|
|
|
return ["numbers" => $free];
|
|
}
|
|
} |