2021-05-13 12:40:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2021-11-20 06:58:46 +00:00
|
|
|
use Carbon\Carbon;
|
2021-06-15 12:19:14 +00:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2021-05-13 12:40:21 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2021-10-26 12:19:55 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2022-01-01 05:59:35 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2021-05-13 12:40:21 +00:00
|
|
|
|
2022-10-30 12:42:30 +00:00
|
|
|
use App\Casts\CompressedString;
|
2022-01-01 05:59:35 +00:00
|
|
|
use App\Traits\{QueryCacheableConfig,ScopeActive};
|
2021-05-13 12:40:21 +00:00
|
|
|
|
|
|
|
class Domain extends Model
|
|
|
|
{
|
2022-01-01 05:59:35 +00:00
|
|
|
use HasFactory,ScopeActive,QueryCacheableConfig;
|
|
|
|
|
2021-11-26 05:58:50 +00:00
|
|
|
private const CACHE_TIME = 3600;
|
2021-12-03 00:24:23 +00:00
|
|
|
private const STATS_MONTHS = 6;
|
2021-06-14 05:46:18 +00:00
|
|
|
|
2022-10-30 12:42:30 +00:00
|
|
|
protected $casts = [
|
|
|
|
'homepage' => CompressedString::class,
|
|
|
|
];
|
|
|
|
|
2021-06-14 11:33:18 +00:00
|
|
|
/* SCOPES */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Only query active records
|
|
|
|
*/
|
|
|
|
public function scopePublic($query)
|
|
|
|
{
|
|
|
|
return $query->where('public',TRUE);
|
|
|
|
}
|
|
|
|
|
2021-06-14 05:46:18 +00:00
|
|
|
/* RELATIONS */
|
|
|
|
|
2021-08-11 13:45:30 +00:00
|
|
|
public function echoareas()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Echoarea::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fileareas()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Filearea::class);
|
|
|
|
}
|
|
|
|
|
2022-11-04 06:20:22 +00:00
|
|
|
public function nodelist_filearea()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Filearea::class);
|
|
|
|
}
|
|
|
|
|
2021-06-14 05:46:18 +00:00
|
|
|
public function zones()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Zone::class);
|
|
|
|
}
|
2021-06-14 11:33:18 +00:00
|
|
|
|
2022-01-05 13:19:57 +00:00
|
|
|
/* ATTRIBUTES */
|
2021-06-14 11:33:18 +00:00
|
|
|
|
2022-12-01 12:51:43 +00:00
|
|
|
public function getHomePageAttribute(?string $value): string
|
2021-06-14 11:33:18 +00:00
|
|
|
{
|
2022-10-23 09:11:03 +00:00
|
|
|
//0xFD2FB528
|
2022-10-30 12:42:30 +00:00
|
|
|
return $this->castAttribute('homepage',$value) ?: 'No available information at the moment.';
|
2021-06-14 11:33:18 +00:00
|
|
|
}
|
2021-10-26 12:19:55 +00:00
|
|
|
|
|
|
|
/* METHODS */
|
|
|
|
|
2022-01-05 13:19:57 +00:00
|
|
|
/**
|
|
|
|
* Get some message area stats for this domain
|
|
|
|
*
|
|
|
|
* @param bool $byarea
|
|
|
|
* @param Collection|NULL $systems
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function daily_area_stats(bool $byarea=FALSE,Collection $systems=NULL): Collection
|
2021-11-20 06:58:46 +00:00
|
|
|
{
|
|
|
|
if (! $this->echoareas->count())
|
|
|
|
return collect();
|
|
|
|
|
2022-01-05 13:19:57 +00:00
|
|
|
$echostats = Echomail::cacheFor(self::CACHE_TIME)->select([DB::raw('datetime::date as date'),'echoarea_id','echoareas.name',DB::raw('COUNT(*)')])
|
|
|
|
->join('echoareas',['echoareas.id'=>'echomails.echoarea_id'])
|
|
|
|
->join('domains',['domains.id'=>'echoareas.domain_id'])
|
|
|
|
->where('domain_id',$this->id)
|
|
|
|
->when($systems?->count(),function($query) use ($systems) { return $query->whereIn('fftn_id',$systems->pluck('addresses')->flatten()->pluck('id')->toArray()); })
|
2022-01-01 05:59:35 +00:00
|
|
|
->where('datetime','>=',Carbon::now()->subMonths(self::STATS_MONTHS)->startOfMonth())
|
2022-01-05 13:19:57 +00:00
|
|
|
->groupBy(['echoarea_id','echoareas.name','date'])
|
|
|
|
->orderBy('date')
|
2022-01-14 08:15:37 +00:00
|
|
|
->orderBy('echoareas.name')
|
2022-01-05 13:19:57 +00:00
|
|
|
->with(['echoarea'])
|
2022-01-01 05:59:35 +00:00
|
|
|
->get();
|
2021-11-26 05:58:50 +00:00
|
|
|
|
2022-01-05 13:19:57 +00:00
|
|
|
if ($byarea)
|
2021-11-26 05:58:50 +00:00
|
|
|
return $echostats
|
2022-01-14 08:15:37 +00:00
|
|
|
->sortBy('name')
|
2022-01-05 13:19:57 +00:00
|
|
|
->groupBy(['echoarea_id'])
|
|
|
|
->map(function($item,$key) {
|
|
|
|
return [
|
|
|
|
'name' => $item->first()->echoarea->name,
|
|
|
|
'data' => $item->groupby('date')->map(function($item) {
|
|
|
|
return [
|
|
|
|
'x' => Carbon::create($item->first()->date)->timestamp*1000,
|
|
|
|
'y' => $item->sum('count')
|
|
|
|
];
|
|
|
|
})->values(),
|
|
|
|
'dashStyle' => 'ShortDot',
|
|
|
|
];
|
|
|
|
})->values();
|
|
|
|
|
|
|
|
else
|
2021-11-26 05:58:50 +00:00
|
|
|
return $echostats
|
2022-01-05 13:19:57 +00:00
|
|
|
->groupBy('date')
|
|
|
|
->map(function($item) { return ['x'=>Carbon::create($item->first()->date)->timestamp*1000,'y'=>$item->sum('count')]; })
|
2021-11-26 05:58:50 +00:00
|
|
|
->values();
|
2021-11-26 05:16:33 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 13:19:57 +00:00
|
|
|
/**
|
|
|
|
* Get the latest message in each echomail area
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function latest_echomail_message(): Collection
|
2021-10-26 12:19:55 +00:00
|
|
|
{
|
2022-01-05 13:19:57 +00:00
|
|
|
return Echoarea::cacheFor(self::CACHE_TIME)
|
|
|
|
->select([
|
|
|
|
'echoareas.*',DB::raw('max(datetime) as last_message')
|
|
|
|
])
|
|
|
|
->leftJoin('echomails',['echomails.echoarea_id'=>'echoareas.id'])
|
|
|
|
->where('domain_id',$this->id)
|
|
|
|
->groupBy('echoareas.id')
|
|
|
|
->orderBy('echoareas.name')
|
|
|
|
->get();
|
2021-10-26 12:19:55 +00:00
|
|
|
}
|
2022-11-25 10:44:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if this zone is managed by this host
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function managed(): bool
|
|
|
|
{
|
|
|
|
static $so = NULL;
|
|
|
|
|
|
|
|
if (is_null($so))
|
|
|
|
$so = Setup::findOrFail(config('app.id'));
|
|
|
|
|
|
|
|
return $so
|
|
|
|
->system
|
|
|
|
->addresses
|
|
|
|
->where('zone.domain.active',TRUE)
|
|
|
|
->pluck('zone.domain_id')
|
|
|
|
->contains($this->id);
|
|
|
|
}
|
2021-05-13 12:40:21 +00:00
|
|
|
}
|