80 lines
1.5 KiB
PHP
80 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Rennokki\QueryCache\Traits\QueryCacheable;
|
|
|
|
use App\Traits\ScopeActive;
|
|
|
|
class Echoarea extends Model
|
|
{
|
|
use SoftDeletes,ScopeActive,QueryCacheable;
|
|
|
|
private const CACHE_TIME = 3600;
|
|
|
|
protected $dates = [ 'last_message' ];
|
|
|
|
/* RELATIONS */
|
|
|
|
public function addresses()
|
|
{
|
|
return $this->belongsToMany(Address::class);
|
|
}
|
|
|
|
public function domain()
|
|
{
|
|
return $this->belongsTo(Domain::class);
|
|
}
|
|
|
|
public function echomail()
|
|
{
|
|
return $this->hasMany(Echomail::class)
|
|
->orderBy('datetime','ASC');
|
|
}
|
|
|
|
/* METHODS */
|
|
|
|
public function messages_count(int $period): int
|
|
{
|
|
$eo = Echomail::cacheFor(self::CACHE_TIME)
|
|
->where('echoarea_id',$this->id);
|
|
|
|
$dt = Carbon::now()->startOfday();
|
|
|
|
switch ($period) {
|
|
case 1: // day
|
|
$eo->where('datetime','>=',$dt->subDay());
|
|
break;
|
|
case 7: // week
|
|
$eo->where('datetime','>=',$dt->subWeek());
|
|
break;
|
|
case 30: // month
|
|
$eo->where('datetime','>=',$dt->subMonth());
|
|
break;
|
|
default:
|
|
return 0;
|
|
}
|
|
|
|
return $eo->count();
|
|
}
|
|
|
|
/**
|
|
* Number of messages waiting for address
|
|
*
|
|
* @param Address $ao
|
|
* @return Collection
|
|
*/
|
|
public function waiting(Address $ao): Collection
|
|
{
|
|
return $this->echomail()
|
|
->join('echomail_seenby',['echomail_seenby.echomail_id'=>'echomails.id'])
|
|
->whereNull('sent_at')
|
|
->whereNotNull('export_at')
|
|
->where('address_id',$ao->id)
|
|
->get();
|
|
}
|
|
} |