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;
|
2021-05-13 12:40:21 +00:00
|
|
|
|
|
|
|
use App\Traits\ScopeActive;
|
|
|
|
|
|
|
|
class Domain extends Model
|
|
|
|
{
|
2021-06-15 12:19:14 +00:00
|
|
|
use HasFactory,ScopeActive;
|
2021-06-14 05:46:18 +00:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-06-14 05:46:18 +00:00
|
|
|
public function zones()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Zone::class);
|
|
|
|
}
|
2021-06-14 11:33:18 +00:00
|
|
|
|
|
|
|
/* 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));
|
|
|
|
}
|
2021-10-26 12:19:55 +00:00
|
|
|
|
|
|
|
/* METHODS */
|
|
|
|
|
2021-11-20 06:58:46 +00:00
|
|
|
public function daily_area_stats(): Collection
|
|
|
|
{
|
|
|
|
if (! $this->echoareas->count())
|
|
|
|
return collect();
|
|
|
|
|
|
|
|
$where = ['echoarea_id'=>$this->echoareas->pluck('id')->toArray()];
|
|
|
|
|
|
|
|
$echostats = Echomail::countGroupBy(['datetime',['datetime'=>'%Y-%m-%d']],$where);
|
|
|
|
|
|
|
|
return $echostats
|
|
|
|
->sortBy(function($item) { return $item->id->datetime; })
|
|
|
|
->map(function($item) { return ['x'=>Carbon::createFromFormat('Y-m-d',$item->id->datetime)->timestamp*1000,'y'=>$item->count]; })
|
|
|
|
->values();
|
|
|
|
}
|
|
|
|
|
2021-10-26 12:19:55 +00:00
|
|
|
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'));
|
|
|
|
|
2021-11-20 06:58:46 +00:00
|
|
|
$echostats = Echomail::countGroupBy(['echoarea_id'],$where->toArray());
|
2021-10-26 12:19:55 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
2021-05-13 12:40:21 +00:00
|
|
|
}
|