adding actual useful feature

This commit is contained in:
x 2024-06-10 00:27:00 +02:00
parent 7040a9851d
commit 0ddb0dae53
4 changed files with 74 additions and 163 deletions

View File

@ -7,9 +7,10 @@ use App\Models\Video;
use App\Models\Banner; use App\Models\Banner;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use App\Http\Requests; use App\Http\Requests;
use Cviebrock\EloquentTaggable\Services\TagService;
class TagviewController extends Controller class TagviewController extends Controller
{ {
/** /**
* Display a listing of the resource. * Display a listing of the resource.
@ -36,154 +37,41 @@ class TagviewController extends Controller
// //
} }
public function tagview($tag, $id = null) public function showVideo($shortname, $id = null)
{ {
// return $shortname;
if (is_null($id)) { if (is_null($id)) {
// $x = Video::with('tags')->get(); $video = Video::getRandom($shortname, true);
// return $x; if ($video instanceof HasMany) {
$x = Video::orderByRaw("RAND()")->with('tags')->whereHas('tags', function ($query) use ($tag) { $video = $video->first();
$query->where('normalized', $tag);
})->first();
// $x = Video::orderByRaw("RAND()")->with('tags')->get()->first();
$responseArray = json_decode($x, true);
$normalizedTags = array_column($responseArray['tags'], 'normalized');
$variableToCompare = $tag;
if (in_array($variableToCompare, $normalizedTags)) {
// tag exists
return redirect('t/' . $tag . '/' . $x->id);
} else {
// tag does not exist on video
return 404;
} }
else {
return redirect()->back()->with('error', 'tag is empty.');
}
return redirect('/t/' . $shortname . '/' . $video->id);
} else { } else {
// return $id; // Don't filter on specific video.
$video = Video::where('id', $id)->with('tags')->get(); // TODO: Add warning page
return view('tagviewdev', [ $video = Video::withAnyTags($shortname)->find($id);
'video' => $video,
'related' => $video,
'tag' => $tag
]);
} }
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())]);
} }
// public function tagview($tag, $id = null)
// {
// $x = Video::withAllTags($tag)->first();
// // return $x;
// // id 1
// // file "1.webm"
// // videotitle "su"
// // interpret null
// // songtitle null
// // imgsource null
// // category_id 8
// // user_id 1
// // created_at "2024-02-22 00:31:49"
// // updated_at "2024-02-22 00:31:49"
// // deleted_at null
// // hash "3cdd7e0aa1a1cf175e06f22da2f485091f3b9f22"
// // tag_id 2
// // taggable_id 1
// // taggable_type "App\\Models\\Video"
// // name "misc"
// // normalized "misc"
// if(!$id) {
// // gibs keine...
// $z = Video::getAllTags();
// return $z;
// } else {
// $y = $x->id;
// return $y;
// }
// }
// public function tagview($tag, $id = null)
// {
// // $tag_in = Video::withAnyTags($tag)->first();
// // return $tag_in;
// $video = Video::getRandom($tag->id);
// if ($video instanceof HasMany) {
// $video = $video->first();
// }
// // $video = Video::getRandom($tag->id);
// if (is_null($id)) {
// $video = Video::getRandom($tag);
// } else {
// }
// }
// public function helloworld($tag, $id = null)
// {
// if(!$tag) {
// return redirect()->back()->with('error', 'Tag not found');
// }
// if (is_null($id)) {
// $tag = Video::withAnyTags($tag)->first();
// return redirect('t/' . $tag->normalized . '/' . $tag->id);
// } else {
// $tag = Video::withAnyTags($tag)->first();
// $video = Video::with('tags')->find($tag->id);
// return view('tagviewdev', [
// 'id' => $tag->id,
// 'video' => $video,
// 'tag' => $tag->name,
// 'related' => $video->name,
// 'banner' => 'banner'
// ]);
// }
// if(!$id) {
// // is not null
// // implement redirect to t/$tag/$id
// } else {
// $tag = Video::withAnyTags($tag)->first();
// // reverse lookup of $video thanks to the api for tag
// $video = Video::with('tags')->find($tag->id);
// $tags = $video->tags;
// return view('tagviewdev', [
// 'video' => $video,
// 'tags' => $tags,
// 'related' => $tag->name
// ]);
// //return redirect('t/' . $tag . '/' . );
// }
// $blub = $tag->id;
// return $blub;
// if (is_null($id)) {
// $video = Video::getRandom($tag);
// if ($video instanceof HasMany) {
// $video = $video->first();
// }
// else {
// return redirect()->back()->with('error', '404');
// }
// return redirect('t/' . $tag . '/' . $video->id);
// } else {
// // Don't filter on specific video.
// // TODO: Add warning page
// // $video = $tag->videos()->find($id);
// }
// //return $tag;
// return view('tagviewdev', [
// 'tag' => $tag,
// 'related' => $tag,
// ]);
/** /**
* Store a newly created resource in storage. * Store a newly created resource in storage.
* *

View File

@ -188,7 +188,7 @@ Route::group(["middleware" => "theme"], function() {
Route::get('{shortname}/{id}', 'CategoryController@showVideo')->where(['shortname' => '[a-z][a-z0-9]+', 'id' => '[0-9]+']); Route::get('{shortname}/{id}', 'CategoryController@showVideo')->where(['shortname' => '[a-z][a-z0-9]+', 'id' => '[0-9]+']);
##Tag View anybody? ##Tag View anybody?
Route::get('t/{tag}', 'TagviewController@tagview')->where('tag', '[a-z][a-z0-9]+'); Route::get('t/{tag}', 'TagviewController@showVideo')->where('tag', '[a-z][a-z0-9]+');
Route::get('t/{tag}/{id}', 'TagviewController@tagview')->where(['tag' => '[a-z][a-z0-9]+', 'id' => '[0-9]+']); Route::get('t/{tag}/{id}', 'TagviewController@showVideo')->where(['tag' => '[a-z][a-z0-9]+', 'id' => '[0-9]+']);
}); });

View File

@ -71,31 +71,43 @@ class Video extends Model
return $this->belongsToMany(User::class, 'favorites', 'video_id', 'user_id'); 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) { 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; return static::filtered()->orderBy('id', 'ASC')->first()->id;
} }
public static function getLastId($related = null) { public static function getLastId($related = null, $isTag = false) {
if ($related) { 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; return static::select('id')->filtered()->orderBy('id', 'DESC')->first()->id;
} }
public function getNext($related = null) { public function getNext($related = null, $isTag = false) {
if ($related) { 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 { } else {
return static::filtered()->where('id', '>', $this->id)->orderBy('id', 'ASC')->first(); 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) { 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 { } else {
return static::filtered()->where('id', '<', $this->id)->orderBy('id', 'DESC')->first(); return static::filtered()->where('id', '<', $this->id)->orderBy('id', 'DESC')->first();
} }
@ -224,14 +236,24 @@ public function blurryThumb() {
} }
} }
public static function getRandom($related = null) { public static function getRandom($related = null, $tag = false) {
if ($related) { 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) { if ($id < 0) {
return redirect()->back()->with('error', 'no videos found'); return redirect()->back()->with('error', 'no videos found');
} }
$id = mt_rand(0, $id); $id = mt_rand(0,$id);
return $related->videos()->filtered()->skip($id); return Video::withAnyTags($related)->filtered()->skip($id);
} }
$id = static::filtered()->countScoped()->count() - 1; $id = static::filtered()->countScoped()->count() - 1;
if ($id < 0) { if ($id < 0) {
@ -241,6 +263,7 @@ public function blurryThumb() {
return static::filtered()->skip($id); return static::filtered()->skip($id);
} }
public static function getSingleRandom($related = null) { public static function getSingleRandom($related = null) {
$query = $related ? $related->videos()->filtered() : static::filtered(); $query = $related ? $related->videos()->filtered() : static::filtered();
$count = $query->countScoped()->count(); $count = $query->countScoped()->count();

View File

@ -41,20 +41,20 @@
?> ?>
<div class="text-center" style="position: unset;"> <div class="text-center" style="position: unset;">
@if($related) @if($related)
@if(($prev = $video->getPrev($related)) === null) @if(($prev = $video->getPrev($related,$isTag)) === null)
<a class="first" href="#" style="visibility: hidden;"> first</a> <a class="first" href="#" style="visibility: hidden;"> first</a>
<a id="prev" href="#" style="visibility: hidden;"> prev</a> | <a id="prev" href="#" style="visibility: hidden;"> prev</a> |
@else @else
<a class="first" href="{{url($related->baseurl(), $video->getFirstId($related))}}"> first</a> <a class="first" href="{{url($isTag ? '/t/' . $related : $related->baseurl(), $video->getFirstId($related,$isTag))}}"> first</a>
<a id="prev" href="{{url($related->baseurl(), [$prev->id])}}"> prev</a> | <a id="prev" href="{{url($isTag ? '/t/' . $related : $related->baseurl(), [$prev->id])}}"> prev</a> |
@endif @endif
<a href="{{url($related->baseurl())}}">{!!$related->displayName()!!}</a> <a href="{{url($isTag ? '/t/' . $related : $related->baseurl())}}">{!!$isTag ? $related:$related->displayName()!!}</a>
@if(($next = $video->getNext($related)) === null) @if(($next = $video->getNext($related,$isTag)) === null)
| <a id="next" href="#" style="visibility: hidden;">next </a> | <a id="next" href="#" style="visibility: hidden;">next </a>
<a class="last" href="#" style="visibility: hidden;">last </a> <a class="last" href="#" style="visibility: hidden;">last </a>
@else @else
| <a id="next" href="{{url($related->baseurl(), [$next->id])}}">next </a> | <a id="next" href="{{url($isTag ? '/t/' . $related : $related->baseurl(), [$next->id])}}">next </a>
<a class="last" href="{{url($related->baseurl(), $video->getLastId($related))}}">last </a> <a class="last" href="{{url($isTag ? '/t/' . $related : $related->baseurl(), $video->getLastId($related,$isTag))}}">last </a>
@endif @endif
@else @else
@if(($prev = $video->getPrev()) === null) @if(($prev = $video->getPrev()) === null)