clrghouz/app/Models/Domain.php
2021-10-26 23:19:55 +11:00

81 lines
1.5 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use App\Traits\ScopeActive;
class Domain extends Model
{
use HasFactory,ScopeActive;
/* SCOPES */
/**
* Only query active records
*/
public function scopePublic($query)
{
return $query->where('public',TRUE);
}
/* RELATIONS */
public function echoareas()
{
return $this->hasMany(Echoarea::class);
}
public function fileareas()
{
return $this->hasMany(Filearea::class);
}
public function zones()
{
return $this->hasMany(Zone::class);
}
/* CASTS */
public function getHomePageAttribute($value)
{
return $value ? gzuncompress(base64_decode($value)) : 'No available information at the moment.';
}
public function setHomePageAttribute($value)
{
$this->attributes['homepage'] = base64_encode(gzcompress($value,9));
}
/* METHODS */
public function stats(System $o=NULL): Collection
{
if (! $this->echoareas->count())
return collect();
$where = collect(['echoarea_id'=>$this->echoareas->pluck('id')->toArray()]);
if ($o)
$where->put('fftn_id',$o->addresses()->pluck('id'));
$echostats = Echomail::countGroupBy('echoarea_id',$where->toArray());
return $this->echoareas->map(function($item) use ($echostats) {
$stats = $echostats->filter(function($x) use ($item) {
return $x->id->echoarea_id == $item->id;
});
$item->count = 0;
foreach ($stats as $o)
$item->count += $o->count;
return $item;
});
}
}