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); } /* ATTRIBUTES */ 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 */ /** * 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 { if (! $this->echoareas->count()) return collect(); $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()); }) ->where('datetime','>=',Carbon::now()->subMonths(self::STATS_MONTHS)->startOfMonth()) ->groupBy(['echoarea_id','echoareas.name','date']) ->orderBy('date') ->orderBy('echoareas.name') ->with(['echoarea']) ->get(); if ($byarea) return $echostats ->sortBy('name') ->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 return $echostats ->groupBy('date') ->map(function($item) { return ['x'=>Carbon::create($item->first()->date)->timestamp*1000,'y'=>$item->sum('count')]; }) ->values(); } /** * Get the latest message in each echomail area * * @return Collection */ public function latest_echomail_message(): Collection { 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(); } }