Files
thetool/application/Voicenumber/Voicenumber.php
2025-01-24 15:04:24 +01:00

159 lines
4.8 KiB
PHP

<?php
class Voicenumber extends mfBaseModel {
private $block;
private $orderproduct;
private $contract;
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 loadFromBlock(Voicenumberblock $block) {
$this->voicenumberblock_id = $block->id;
//$this->countrycode = $block->countrycode;
//$this->areacode = $block->areacode;
//$this->number_prepend_zero = $block->number_prepend_zero;
return true;
}
public function getPrettyNumber($include_cc = true) {
if(!$this->voicenumberblock_id) return $this->number;
$block = $this->getProperty("block");
$prefix = "";
$return = "";
if($include_cc) {
$prefix = $this->block->countrycode." ";
$return = $this->block->countrycode." ";
} else {
$return = "0";
}
$prefix .= $this->block->areacode;
$return .= $this->block->areacode." ".substr($this->number, strlen($prefix)-1);
return $return;
}
public function beforeSave() {
if($this->ported_from && $this->port_in_date) {
$this->ported_in = 1;
} else {
$this->ported_in = 0;
}
if($this->ported_to && $this->port_out_date) {
$this->ported_out = 1;
} else {
$this->ported_out = 0;
}
//var_dump($this);exit;
if(is_array($this->_old_data) && count($this->_old_data)) {
if($this->active == 1 && !$this->_old_data->active) {
$this->activated_date = date("U");
}
}
}
public function afterSave() {
// TODO: Move to some ContractConfig add number function
// if contract_id is given, add number to contract
if($this->contract_id) {
$contract = new Contract($this->contract_id);
if(is_array($contract->configvalues) && array_key_exists("voicenumberblock_voicenumber", $contract->configvalues)) {
$cc_item = $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(property_exists($this->_old_data, "contract_id") && $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(property_exists($this->_old_data, "contract_id") && $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) {
if($name == "block") {
$this->block = new Voicenumberblock($this->voicenumberblock_id);
return $this->block;
}
if($name == "orderproduct") {
$this->orderproduct = new Orderproduct($this->orderproduct_id);
return $this->orderproduct;
}
if($name == "contract") {
$this->contract = new Contract($this->contract_id);
return $this->contract;
}
$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;
}
}