1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* App\Models\UserSerie. |
10
|
|
|
* |
11
|
|
|
* @property int $id |
12
|
|
|
* @property int $users_id |
13
|
|
|
* @property int $videos_id FK to videos.id |
14
|
|
|
* @property string|null $categories List of categories for user tv shows |
15
|
|
|
* @property \Carbon\Carbon|null $created_at |
16
|
|
|
* @property \Carbon\Carbon|null $updated_at |
17
|
|
|
* @property-read User $user |
18
|
|
|
* |
19
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserSerie whereCategories($value) |
20
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserSerie whereCreatedAt($value) |
21
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserSerie whereId($value) |
22
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserSerie whereUpdatedAt($value) |
23
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserSerie whereUsersId($value) |
24
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserSerie whereVideosId($value) |
25
|
|
|
* |
26
|
|
|
* @mixin \Eloquent |
27
|
|
|
* |
28
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserSerie newModelQuery() |
29
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserSerie newQuery() |
30
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserSerie query() |
31
|
|
|
*/ |
32
|
|
|
class UserSerie extends Model |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $guarded = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
protected $dateFormat = false; |
43
|
|
|
|
44
|
|
|
public function user(): BelongsTo |
45
|
|
|
{ |
46
|
|
|
return $this->belongsTo(User::class, 'users_id'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* When a user wants to add a show to "my shows" insert it into the user series table. |
51
|
|
|
* |
52
|
|
|
* |
53
|
|
|
* @return int|\Illuminate\Database\Eloquent\Builder |
54
|
|
|
*/ |
55
|
|
|
public static function addShow($userId, $videoId, array $catID = []) |
56
|
|
|
{ |
57
|
|
|
return self::query() |
58
|
|
|
->insertGetId( |
59
|
|
|
[ |
60
|
|
|
'users_id' => $userId, |
61
|
|
|
'videos_id' => $videoId, |
62
|
|
|
'categories' => ! empty($catID) ? implode('|', $catID) : 'NULL', |
63
|
|
|
'created_at' => now(), |
64
|
|
|
'updated_at' => now(), |
65
|
|
|
] |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get all the user's "my shows". |
71
|
|
|
* |
72
|
|
|
* |
73
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|static[] |
74
|
|
|
*/ |
75
|
|
|
public static function getShows($userId) |
76
|
|
|
{ |
77
|
|
|
return self::query() |
78
|
|
|
->where('user_series.users_id', $userId) |
79
|
|
|
->select(['user_series.*', 'v.title']) |
80
|
|
|
->join('videos as v', 'v.id', '=', 'user_series.videos_id') |
81
|
|
|
->orderBy('v.title') |
|
|
|
|
82
|
|
|
->get(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Delete a tv show from the user's "my shows". |
87
|
|
|
*/ |
88
|
|
|
public static function delShow($users_id, $videos_id): void |
89
|
|
|
{ |
90
|
|
|
self::query()->where(compact('users_id', 'videos_id'))->delete(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get tv show information for a user. |
95
|
|
|
* |
96
|
|
|
* |
97
|
|
|
* @return Model|null|static |
98
|
|
|
*/ |
99
|
|
|
public static function getShow($userId, $videoId) |
100
|
|
|
{ |
101
|
|
|
return self::query() |
102
|
|
|
->where(['user_series.users_id' => $userId, 'user_series.videos_id' => $videoId]) |
103
|
|
|
->select(['user_series.*', 'v.title']) |
104
|
|
|
->leftJoin('videos as v', 'v.id', '=', 'user_series.videos_id')->first(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Delete all shows from the user's "my shows". |
109
|
|
|
*/ |
110
|
|
|
public static function delShowForUser($userId): void |
111
|
|
|
{ |
112
|
|
|
self::query()->where('users_id', $userId)->delete(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Delete TV shows from all user's "my shows" that match a TV id. |
117
|
|
|
*/ |
118
|
|
|
public static function delShowForSeries($videoId): void |
119
|
|
|
{ |
120
|
|
|
self::query()->where('videos_id', $videoId)->delete(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Update a TV show category ID for a user's "my show" TV show. |
125
|
|
|
* |
126
|
|
|
* @param array $catID List of category ID's. |
127
|
|
|
*/ |
128
|
|
|
public static function updateShow($users_id, $videos_id, array $catID = []): void |
129
|
|
|
{ |
130
|
|
|
self::query()->where(compact('users_id', 'videos_id'))->update(['categories' => ! empty($catID) ? implode('|', $catID) : 'NULL']); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|