clrghouz/app/Models/Domain.php
Deon George 3f5668292f
All checks were successful
Create Docker Image / Build Docker Image (x86_64) (push) Successful in 36s
Create Docker Image / Build Docker Image (arm64) (push) Successful in 1m34s
Create Docker Image / Final Docker Image Manifest (push) Successful in 10s
More optimisations for users dashboard
2024-04-21 00:28:50 +10:00

127 lines
3.5 KiB
PHP

<?php
namespace App\Models;
use AgliPanci\LaravelCase\Facades\CaseBuilder;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use App\Casts\CompressedString;
use App\Traits\{QueryCacheableConfig,ScopeActive};
class Domain extends Model
{
use HasFactory,ScopeActive,QueryCacheableConfig;
private const CACHE_TIME = 3600;
private const STATS_MONTHS = 6;
protected $casts = [
'homepage' => CompressedString::class,
];
/* SCOPES */
/**
* Only query active records
*/
public function scopePublic($query)
{
$user = Auth::user();
return $query
->when(((! $user) || (! $user->isAdmin())),function($query) { return $query->where('public',TRUE)->active(); });
}
/* RELATIONS */
public function echoareas()
{
return $this->hasMany(Echoarea::class);
}
public function fileareas()
{
return $this->hasMany(Filearea::class);
}
public function nodelist_filearea()
{
return $this->belongsTo(Filearea::class);
}
public function zones()
{
return $this->hasMany(Zone::class);
}
/* ATTRIBUTES */
public function getHomePageAttribute(?string $value): string
{
//0xFD2FB528
return $this->castAttribute('homepage',$value) ?: 'No available information at the moment.';
}
/* METHODS */
public function echoarea_stats(): Collection
{
$dt = Carbon::now()->startOfday();
$case = CaseBuilder::whenRaw("datetime >= '?'",$dt->subDay()->format('Y-m-d'))->thenRaw("'day'")
->whenRaw("datetime >= '?'",$dt->subDays(7)->format('Y-m-d'))->thenRaw("'week'")
->whenRaw("datetime >= '?'",$dt->subMonth()->format('Y-m-d'))->thenRaw("'month'")
->elseRaw("'all'");
return Echoarea::cacheFor(self::CACHE_TIME)
->select(['echoareas.id','name','description','active',DB::raw('count(echomails.id) AS count'),DB::raw('max(datetime) as last_message')])
->selectRaw($case->toRaw().' AS stats')
->join('echomails',['echomails.echoarea_id'=>'echoareas.id'],NULL,NULL,'left outer')
->where('domain_id',$this->id)
->groupBy('echoareas.id')
->groupBy('echoareas.name')
->groupBy('stats')
->orderBy('echoareas.name')
->orderBy('last_message','DESC')
->get();
}
public function echoarea_total_daily(Collection $systems=NULL): Collection
{
return Cache::remember(md5(sprintf('%d-%s',$this->id,$systems?->pluck('id')->join(','))),self::CACHE_TIME,function() use ($systems) {
return DB::query()
->select(['echoareas.name','echoareas.show',DB::raw('COUNT(*) AS count'),DB::raw('datetime::date AS date')])
->from($this->getTable())
->join('echoareas',['echoareas.domain_id'=>'domains.id'])
->join('echomails',['echomails.echoarea_id'=>'echoareas.id'])
->where('domains.id',$this->id)
->where('echomails.datetime','>=',Carbon::now()->subMonths(self::STATS_MONTHS)->startOfMonth())
->when($systems?->count(),function($query) use ($systems) { return $query->whereIn('echomails.fftn_id',$systems->pluck('addresses')->flatten()->pluck('id')); })
->groupBy(['echoareas.name','echoareas.show','date'])
->orderBy('echoareas.name')
->orderBy('date')
->get();
});
}
public function isManaged(): bool
{
return our_address()->pluck('zone.domain')->pluck('id')->contains($this->id);
}
/**
* Determine if this zone is managed by this host
*
* @return bool
* @deprecated use self::isManaged();
*/
public function managed(): bool
{
return our_address($this)->count() > 0;
}
}