1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Xetaravel\Models; |
6
|
|
|
|
7
|
|
|
use Eloquence\Behaviours\HasSlugs; |
8
|
|
|
use Illuminate\Database\Eloquent\Attributes\ObservedBy; |
9
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
10
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOne; |
11
|
|
|
use Illuminate\Support\Collection; |
12
|
|
|
use Illuminate\Support\Facades\Auth; |
13
|
|
|
use Xetaravel\Models\Presenters\DiscussCategoryPresenter; |
14
|
|
|
use Xetaravel\Observers\DiscussCategoryObserver; |
15
|
|
|
|
16
|
|
|
#[ObservedBy([DiscussCategoryObserver::class])] |
17
|
|
|
class DiscussCategory extends Model |
18
|
|
|
{ |
19
|
|
|
use DiscussCategoryPresenter; |
20
|
|
|
use HasSlugs; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The attributes that are mass assignable. |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $fillable = [ |
28
|
|
|
'title', |
29
|
|
|
'slug', |
30
|
|
|
'color', |
31
|
|
|
'is_locked', |
32
|
|
|
'level', |
33
|
|
|
'icon', |
34
|
|
|
'description' |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The accessors to append to the model's array form. |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $appends = [ |
43
|
|
|
'category_url' |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The attributes that should be cast. |
48
|
|
|
*/ |
49
|
|
|
protected function casts(): array |
50
|
|
|
{ |
51
|
|
|
return [ |
52
|
|
|
'is_locked' => 'boolean' |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Return the field to slug. |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function slugStrategy(): string |
62
|
|
|
{ |
63
|
|
|
return 'title'; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the conversations for the category. |
68
|
|
|
* |
69
|
|
|
* @return HasMany |
70
|
|
|
*/ |
71
|
|
|
public function conversations(): HasMany |
72
|
|
|
{ |
73
|
|
|
return $this->hasMany(DiscussConversation::class, 'category_id', 'id'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get the last conversation of the category. |
78
|
|
|
* |
79
|
|
|
* @return HasOne |
80
|
|
|
*/ |
81
|
|
|
public function lastConversation(): HasOne |
82
|
|
|
{ |
83
|
|
|
return $this->hasOne(DiscussConversation::class, 'id', 'last_conversation_id'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Pluck the categories by the given fields and the locked state. |
88
|
|
|
* |
89
|
|
|
* @param string $value |
90
|
|
|
* @param string|null $column |
91
|
|
|
* |
92
|
|
|
* @return Collection |
93
|
|
|
*/ |
94
|
|
|
public static function pluckLocked(string $value, string $column = null): Collection |
95
|
|
|
{ |
96
|
|
|
if (Auth::user() && Auth::user()->hasPermission('manage.discuss.conversations')) { |
|
|
|
|
97
|
|
|
return self::pluck($value, $column); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return self::where('is_locked', false)->pluck($value, $column); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|