clrghouz/app/Models/Node.php
2021-06-13 01:32:22 +10:00

72 lines
1.3 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Node extends Model
{
protected $casts = [
'is_zc'=>'boolean',
'is_rc'=>'boolean',
'is_hub'=>'boolean',
'is_host'=>'boolean',
];
protected $fillable = ['zone_id','host_id','node_id','point_id'];
/* SCOPES */
public function scopeHost()
{
}
/* RELATIONS */
/**
* Node nodelist flags
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function flags()
{
return $this->belongsToMany(Flag::class);
}
public function zone()
{
return $this->belongsTo(Zone::class);
}
/* ATTRIBUTES */
/**
* Render the node name in full 5D
*
* @return string
*/
public function getFTNAttribute()
{
return $this->zone_id
? sprintf('%s:%s/%s.%s',$this->zone->zone_id,$this->host_id,$this->node_id,$this->point_id)
: '-';
}
/**
* Get this nodes uplink
*/
public function getUplinkAttribute()
{
// @todo Need to work this out properly
return static::where('zone_id','10')->where('host_id',1)->where('node_id',0)->where('point_id',0)->first();
}
/* METHODS */
public function hasFlag($relation,$model)
{
return (bool) $this->{$relation}()
->wherePivot($model->getForeignKey(),$model->{$model->getKeyName()})
->count();
}
}