1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Alish\ActiveableModel\Traits; |
5
|
|
|
|
6
|
|
|
use Alish\ActiveableModel\Models\ActiveableModel; |
7
|
|
|
use Alish\ActiveableModel\Scopes\ActiveableScope; |
8
|
|
|
use Illuminate\Database\Eloquent\Builder; |
9
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
10
|
|
|
use Illuminate\Support\Arr; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @method static Builder actives() |
14
|
|
|
* @property bool is_active |
15
|
|
|
*/ |
16
|
|
|
trait IsActiveable |
17
|
|
|
{ |
18
|
|
|
protected bool $defaultStatus = true; |
|
|
|
|
19
|
|
|
|
20
|
|
|
protected static function bootHasStatus() { |
21
|
|
|
static::addGlobalScope(new ActiveableScope()); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function activeStates(): MorphMany |
25
|
|
|
{ |
26
|
|
|
return $this->morphMany(ActiveableModel::class, 'object'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function scopeActives(Builder $query) |
30
|
|
|
{ |
31
|
|
|
return $query->where(function ($query) { |
32
|
|
|
$query->where(function ($query) { |
33
|
|
|
$query->select('is_active') |
34
|
|
|
->from('statuses') |
35
|
|
|
->whereColumn('object_id', $this->getTable() . '.id') |
36
|
|
|
->where('object_type', get_class($this)) |
37
|
|
|
->latest('created_at') |
38
|
|
|
->latest('id') |
39
|
|
|
->limit(1); |
40
|
|
|
}, '=', 1) |
41
|
|
|
->when( |
42
|
|
|
$this->defaultStatus, |
43
|
|
|
fn($query) => $query->orWhere( |
44
|
|
|
fn($query) => $query->doesntHave('statuses') |
45
|
|
|
) |
46
|
|
|
); |
47
|
|
|
}); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function isActive(): bool |
51
|
|
|
{ |
52
|
|
|
/** @var ActiveableModel $latestStatus */ |
53
|
|
|
$latestStatus = $this->activeStates() |
54
|
|
|
->latest('created_at') |
55
|
|
|
->first(); |
56
|
|
|
|
57
|
|
|
if (is_null($latestStatus)) { |
58
|
|
|
return $this->defaultStatus; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $latestStatus->is_active; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function activate(): bool |
65
|
|
|
{ |
66
|
|
|
$this->activeStates()->create([ |
67
|
|
|
'is_active' => true |
68
|
|
|
]); |
69
|
|
|
|
70
|
|
|
$this->setAttribute('is_active', true); |
71
|
|
|
|
72
|
|
|
return true; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function deactivate(): bool |
76
|
|
|
{ |
77
|
|
|
$this->activeStates()->create([ |
78
|
|
|
'is_active' => false |
79
|
|
|
]); |
80
|
|
|
|
81
|
|
|
$this->setAttribute('is_active', false); |
82
|
|
|
|
83
|
|
|
return true; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getIsActiveAttribute() |
87
|
|
|
{ |
88
|
|
|
if (Arr::has($this->attributes, 'is_active')) { |
89
|
|
|
|
90
|
|
|
if (is_null($this->attributes['is_active'])) { |
91
|
|
|
return $this->defaultStatus; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return (bool) $this->attributes['is_active']; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->setAttribute('is_active', $isActive = $this->isActive()); |
98
|
|
|
|
99
|
|
|
return is_null($isActive) ? $this->defaultStatus : $isActive; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|