2021-05-13 12:40:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2023-07-30 10:14:38 +00:00
|
|
|
use AgliPanci\LaravelCase\Facades\CaseBuilder;
|
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;
|
2023-11-24 13:10:21 +00:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2024-04-20 12:03:47 +00:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
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)
|
|
|
|
{
|
2023-11-24 13:10:21 +00:00
|
|
|
$user = Auth::user();
|
|
|
|
|
|
|
|
return $query
|
|
|
|
->when(((! $user) || (! $user->isAdmin())),function($query) { return $query->where('public',TRUE)->active(); });
|
2021-06-14 11:33:18 +00:00
|
|
|
}
|
|
|
|
|
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 */
|
|
|
|
|
2023-07-30 10:14:38 +00:00
|
|
|
public function echoarea_stats(): Collection
|
2021-10-26 12:19:55 +00:00
|
|
|
{
|
2023-07-30 10:14:38 +00:00
|
|
|
$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'");
|
|
|
|
|
2022-01-05 13:19:57 +00:00
|
|
|
return Echoarea::cacheFor(self::CACHE_TIME)
|
2023-08-03 23:53:40 +00:00
|
|
|
->select(['echoareas.id','name','description','active',DB::raw('count(echomails.id) AS count'),DB::raw('max(datetime) as last_message')])
|
2023-07-30 10:14:38 +00:00
|
|
|
->selectRaw($case->toRaw().' AS stats')
|
2023-08-03 23:53:40 +00:00
|
|
|
->join('echomails',['echomails.echoarea_id'=>'echoareas.id'],NULL,NULL,'left outer')
|
2022-01-05 13:19:57 +00:00
|
|
|
->where('domain_id',$this->id)
|
|
|
|
->groupBy('echoareas.id')
|
2023-07-30 10:14:38 +00:00
|
|
|
->groupBy('echoareas.name')
|
|
|
|
->groupBy('stats')
|
2022-01-05 13:19:57 +00:00
|
|
|
->orderBy('echoareas.name')
|
2023-08-03 23:53:40 +00:00
|
|
|
->orderBy('last_message','DESC')
|
2022-01-05 13:19:57 +00:00
|
|
|
->get();
|
2021-10-26 12:19:55 +00:00
|
|
|
}
|
2022-11-25 10:44:03 +00:00
|
|
|
|
2024-04-20 12:03:47 +00:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-04-13 12:41:58 +00:00
|
|
|
public function isManaged(): bool
|
|
|
|
{
|
|
|
|
return our_address()->pluck('zone.domain')->pluck('id')->contains($this->id);
|
|
|
|
}
|
|
|
|
|
2022-11-25 10:44:03 +00:00
|
|
|
/**
|
|
|
|
* Determine if this zone is managed by this host
|
|
|
|
*
|
|
|
|
* @return bool
|
2024-04-13 12:41:58 +00:00
|
|
|
* @deprecated use self::isManaged();
|
2022-11-25 10:44:03 +00:00
|
|
|
*/
|
|
|
|
public function managed(): bool
|
|
|
|
{
|
2023-12-18 04:13:16 +00:00
|
|
|
return our_address($this)->count() > 0;
|
2022-11-25 10:44:03 +00:00
|
|
|
}
|
2021-05-13 12:40:21 +00:00
|
|
|
}
|