Files
thetool/application/Voicenumberblock/Voicenumberblock.php

128 lines
3.3 KiB
PHP

<?php
class Voicenumberblock extends mfBaseModel {
protected $forcestr = ['comment'];
private $number_first;
private $number_last;
private $base;
private $short_prefix;
private $short_first;
private $short_last;
private $files;
private $numbers;
protected function beforeUpdate($data) {
if(!array_key_exists("edit_by", $data)) {
$me = new User();
$me->loadMe();
$data["edit_by"] = $me->id;
}
return $data;
}
public function isNumberInBlock($number) {
return ($number >= $this->first && $number <= $this->last);
}
public static function findBlock($number) {
// resolve number from right to left to find fitting block
$minLength = 4;
$number = preg_replace('/^\+/', '',$number);
$try = $number;
while(strlen($try) >= $minLength) {
$block = VoicenumberblockModel::getFirst(['prefix' => $try]);
if($block) {
break;
}
$try = substr($try, 0, strlen($try)-1);
}
if(!$block) {
return false;
}
return $block;
}
public function getVoicenumber($number) {
if(!$this->isNumberInBlock($number)) return false;
$vn = VoicenumberModel::getFirst(["number" => $number]);
if(!$vn) {
$vn = VoicenumberModel::create([
"voicenumberblock_id" => $this->id,
"active" => 1,
"activated_date" => date("U"),
"number" => $number
]);
}
return $vn;
}
public function getFreeNumbers() {
if(!$this->id) return false;
$free = [];
$block_numbers = VoicenumberModel::search(["block_id" => $this->id], false, "number");
$pnumber = new Voicenumber();
$pnumber->voicenumberblock_id = $this->id;
foreach(range($this->first, $this->last) as $num) {
if(array_key_exists($num, $block_numbers)) {
$bn = $block_numbers[$num];
if($bn->active || $bn->disabled || $bn->ported_in || $bn->ported_out || $bn->orderproduct_id || $bn->contrat_id) {
continue;
}
}
$pnumber->number = $num;
$free[] = $pnumber->getPrettyNumber();
}
return $free;
}
public function getProperty($name) {
if($this->$name == null) {
if($name == "files") {
$this->files = VoiceblocknumberFileModel::search(["voicenumberblock_id" => $this->id]);
return $this->files;
}
if($name == "numbers") {
$this->numbers = VoicenumberModel::search(["voicenumberblock_id" => $this->id]);
return $this->numbers;
}
if($name == "short_prefix") {
$this->short_prefix = substr($this->prefix, strlen($this->countrycode.$this->areacode));
return $this->short_prefix;
}
if($name == "short_first") {
$this->short_first = substr($this->first, strlen($this->countrycode.$this->areacode));
return $this->short_first;
}
if($name == "short_last") {
$this->short_last = substr($this->last, strlen($this->countrycode.$this->areacode));
return $this->short_last;
}
$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;
}
}