1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Seongbae\Discuss\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Auth; |
7
|
|
|
|
8
|
|
|
class Channel extends Model |
9
|
|
|
{ |
10
|
|
|
protected $fillable = [ |
11
|
|
|
'name', |
12
|
|
|
'slug' |
13
|
|
|
]; |
14
|
|
|
|
15
|
|
|
public $timestamps = false; |
16
|
|
|
|
17
|
|
|
public function getCssClassesAttribute() |
18
|
|
|
{ |
19
|
|
|
$classes = config('discuss.channel_classes'); |
20
|
|
|
|
21
|
|
|
if (array_key_exists($this->slug, $classes)) |
22
|
|
|
return 'btn '.$classes[$this->slug]; |
23
|
|
|
else |
24
|
|
|
return 'btn btn-outline-primary btn-sm '; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function subscribers() |
28
|
|
|
{ |
29
|
|
|
return $this->morphedByMany(config('discuss.user_type'), 'user', 'thread_subscription','item_id','user_id')->where('item_type','channel'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function subscribersExcept() |
33
|
|
|
{ |
34
|
|
|
return $this->subscribers()->where('user_id', '<>', Auth::id()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function updateSubscription($user) |
38
|
|
|
{ |
39
|
|
|
if (! $user->channelSubscriptions->contains($this)) { |
40
|
|
|
$user->channelSubscriptions()->save($this, ['item_type' => 'channel']); |
41
|
|
|
} else { |
42
|
|
|
$user->channelSubscriptions()->detach($this); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function subscribe($user) |
47
|
|
|
{ |
48
|
|
|
if (! $user->channelSubscriptions->contains($this)) { |
49
|
|
|
$user->channelSubscriptions()->save($this, ['item_type' => 'channel']); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function resolveChildRouteBinding($childType, $value, $field) |
54
|
|
|
{ |
55
|
|
|
// TODO: Implement resolveChildRouteBinding() method. |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getRouteKeyName() |
59
|
|
|
{ |
60
|
|
|
return 'slug'; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|