clrghouz/app/Traits/GetNode.php

42 lines
772 B
PHP
Raw Normal View History

2019-04-27 13:57:39 +00:00
<?php
namespace App\Traits;
2019-05-20 07:18:18 +00:00
use Illuminate\Support\Arr;
2019-04-27 13:57:39 +00:00
use App\Models\{Node,Zone};
trait GetNode
{
/**
* Get an FTN record
* If the record doesnt exist, we'll create it
*/
2019-05-20 07:18:18 +00:00
protected function get_node(array $address,$create=TRUE)
2019-04-27 13:57:39 +00:00
{
2019-05-20 07:18:18 +00:00
if (! $z=Arr::get($address,'z'))
throw new \Exception('Zone cannot be zero');
2019-04-27 13:57:39 +00:00
$zo = Zone::firstOrCreate(['id'=>$z]);
2019-05-20 07:18:18 +00:00
$no = Node::firstOrNew([
'zone_id'=>$zo->id,
'host_id'=>Arr::get($address,'n'),
'node_id'=>Arr::get($address,'f'),
'point_id'=>Arr::get($address,'p',0)
]);
2019-04-27 13:57:39 +00:00
if (! $no->exists AND $create)
{
$no->active = FALSE;
$no->system = 'AUTO DISCOVERED';
$no->sysop = 'UNKNOWN';
$no->location = '';
$no->baud = 0;
$no->save();
}
return ($no->exists) ? $no : NULL;
}
}