diff --git a/app/Http/Controllers/TagviewController.php b/app/Http/Controllers/TagviewController.php new file mode 100644 index 0000000..6c6c839 --- /dev/null +++ b/app/Http/Controllers/TagviewController.php @@ -0,0 +1,125 @@ +find($tag->id); + return $tag; + // $tag = Video::withAnyTags($tag)->first(); + // return $tag; + // $video = Video::with('tags')->find($tag->id); + // return redirect('t/' . $tag->normalized . '/' . $video->id); + } + + /** + * Show the form for creating a new resource. + * + * @return \Illuminate\Http\Response + */ + public function create() + { + // + } + + public function showVideo($shortname, $id = null) + { + // return $shortname; + + if (is_null($id)) { + $video = Video::getRandom($shortname, true); + if ($video instanceof HasMany) { + $video = $video->first(); + } + else { + return redirect()->back()->with('error', 'tag is empty.'); + } + return redirect('/t/' . $shortname . '/' . $video->id); + } else { + // Don't filter on specific video. + // TODO: Add warning page + $video = Video::withAnyTags($shortname)->find($id); + } + + if (is_null($video)) { + return redirect()->back()->with('error', 'tag is empty.'); + } + + $sfw = $video->tags->contains(function($key, $tag) { + return $tag->normalized === 'sfw'; + }); + + return view('video', [ + 'video' => $video, + 'related' => $shortname, + 'isTag' => true, + 'sfw' => $sfw, + 'banner' => Banner::getRandom($video->isSfw())]); + } + + /** + * Store a newly created resource in storage. + * + * @param \Illuminate\Http\Request $request + * @return \Illuminate\Http\Response + */ + /** + * Display the specified resource. + * + * @param int $id + * @return \Illuminate\Http\Response + */ + public function show($id) + { + // + } + + /** + * Show the form for editing the specified resource. + * + * @param int $id + * @return \Illuminate\Http\Response + */ + public function edit($id) + { + // + } + + /** + * Update the specified resource in storage. + * + * @param \Illuminate\Http\Request $request + * @param int $id + * @return \Illuminate\Http\Response + */ + public function update(Request $request, $id) + { + // + } + + /** + * Remove the specified resource from storage. + * + * @param int $id + * @return \Illuminate\Http\Response + */ + public function destroy($id) + { + // + } +} diff --git a/app/Http/routes.php b/app/Http/routes.php index 35e93db..ec1c438 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -185,4 +185,9 @@ Route::group(["middleware" => "theme"], function() { ##Category View Route::get('{shortname}', 'CategoryController@showVideo')->where('shortname', '[a-z][a-z0-9]+'); Route::get('{shortname}/{id}', 'CategoryController@showVideo')->where(['shortname' => '[a-z][a-z0-9]+', 'id' => '[0-9]+']); + + ##Tag View anybody? + Route::get('t/{tag}', 'TagviewController@showVideo')->where('tag', '[a-z][a-z0-9]+'); + Route::get('t/{tag}/{id}', 'TagviewController@showVideo')->where(['tag' => '[a-z][a-z0-9]+', 'id' => '[0-9]+']); + }); diff --git a/app/Models/Video.php b/app/Models/Video.php index 5f903e5..c22a261 100644 --- a/app/Models/Video.php +++ b/app/Models/Video.php @@ -67,31 +67,43 @@ class Video extends Model return $this->belongsToMany(User::class, 'favorites', 'video_id', 'user_id'); } - public static function getFirstId($related = null) { + public static function getFirstId($related = null, $isTag = false) { if ($related) { - return $related->videos()->filtered()->orderBy('id', 'ASC')->first()->id; + if (!$isTag) { + return $related->videos()->filtered()->orderBy('id', 'ASC')->first()->id; + } + return Video::withAnyTags($related)->filtered()->orderBy('id', 'ASC')->first()->id; } return static::filtered()->orderBy('id', 'ASC')->first()->id; } - public static function getLastId($related = null) { + public static function getLastId($related = null, $isTag = false) { if ($related) { - return $related->videos()->filtered()->orderBy('id', 'DESC')->first()->id; + if (!$isTag) { + return $related->videos()->filtered()->orderBy('id', 'DESC')->first()->id; + } + return Video::withAnyTags($related)->filtered()->orderBy('id', 'DESC')->first()->id; } return static::select('id')->filtered()->orderBy('id', 'DESC')->first()->id; } - public function getNext($related = null) { + public function getNext($related = null, $isTag = false) { if ($related) { - return $related->videos()->filtered()->where('id', '>', $this->id)->orderBy('id', 'ASC')->first(); + if (!$isTag) { + return $related->videos()->filtered()->where('id', '>', $this->id)->orderBy('id', 'ASC')->first(); + } + return Video::withAnyTags($related)->filtered()->where('id', '>', $this->id)->orderBy('id', 'ASC')->first(); } else { return static::filtered()->where('id', '>', $this->id)->orderBy('id', 'ASC')->first(); } } - public function getPrev($related = null) { + public function getPrev($related = null, $isTag = false) { if ($related) { - return $related->videos()->filtered()->where('id', '<', $this->id)->orderBy('id', 'DESC')->first(); + if (!$isTag) { + return $related->videos()->filtered()->where('id', '<', $this->id)->orderBy('id', 'DESC')->first(); + } + return Video::withAnyTags($related)->filtered()->where('id', '<', $this->id)->orderBy('id', 'DESC')->first(); } else { return static::filtered()->where('id', '<', $this->id)->orderBy('id', 'DESC')->first(); } @@ -220,14 +232,24 @@ public function blurryThumb() { } } - public static function getRandom($related = null) { + public static function getRandom($related = null, $tag = false) { if ($related) { - $id = $related->videos()->filtered()->countScoped()->count() - 1; + if (!$tag) { + $id = $related->videos()->filtered()->countScoped()->count() - 1; + if ($id < 0) { + return redirect()->back()->with('error', 'no videos found'); + } + $id = mt_rand(0, $id); + return $related->videos()->filtered()->skip($id); + } + // jetzt sind wir im tag + $id = Video::withAnyTags($related)->filtered()->countScoped()->count()-1; if ($id < 0) { return redirect()->back()->with('error', 'no videos found'); } - $id = mt_rand(0, $id); - return $related->videos()->filtered()->skip($id); + $id = mt_rand(0,$id); + return Video::withAnyTags($related)->filtered()->skip($id); + } $id = static::filtered()->countScoped()->count() - 1; if ($id < 0) { @@ -237,6 +259,16 @@ public function blurryThumb() { return static::filtered()->skip($id); } + + public static function getSingleRandom($related = null) { + $query = $related ? $related->videos()->filtered() : static::filtered(); + $count = $query->countScoped()->count(); + if ($count < 1) { + return null; // Return null if no videos found + } + return $query->inRandomOrder()->first(); + } + public function isSfw() { return !$this->tags->contains(function ($key, $tag) { return $tag->normalized === 'nsfw'; diff --git a/resources/views/layout1/video.blade.php b/resources/views/layout1/video.blade.php index 5c00afc..3ee12ad 100644 --- a/resources/views/layout1/video.blade.php +++ b/resources/views/layout1/video.blade.php @@ -33,20 +33,20 @@