From 05b33cf155c0cd64e51d7bfe77ecf197fb4a7c04 Mon Sep 17 00:00:00 2001 From: paranarimasu <33796518+paranarimasu@users.noreply.github.com> Date: Tue, 26 Dec 2017 16:56:21 -0600 Subject: [PATCH] Add Video Listing with Pagination --- app/Http/Controllers/VideosController.php | 11 +- .../2017_12_26_221942_create_natural_sort.php | 108 ++++++++++++++++++ resources/views/layouts/nav.blade.php | 1 + resources/views/videos.blade.php | 19 +++ routes/web.php | 4 +- 5 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 database/migrations/2017_12_26_221942_create_natural_sort.php create mode 100644 resources/views/videos.blade.php diff --git a/app/Http/Controllers/VideosController.php b/app/Http/Controllers/VideosController.php index e08173538..bdd69f74b 100644 --- a/app/Http/Controllers/VideosController.php +++ b/app/Http/Controllers/VideosController.php @@ -8,8 +8,17 @@ use Illuminate\Support\Facades\Storage; class VideosController extends Controller { + + public function index() { + $videos = Video::orderByRaw('udf_NaturalSortFormat(alias, 10, ".")')->paginate(50); + + return view('videos', [ + 'videos' => $videos + ]); + } + // Source: https://stackoverflow.com/q/36778167 - public function do($alias) { + public function show($alias) { set_time_limit(0); $video = Video::where('alias', $alias)->firstOrFail(); diff --git a/database/migrations/2017_12_26_221942_create_natural_sort.php b/database/migrations/2017_12_26_221942_create_natural_sort.php new file mode 100644 index 000000000..de0334b13 --- /dev/null +++ b/database/migrations/2017_12_26_221942_create_natural_sort.php @@ -0,0 +1,108 @@ + 0 AND tmp_position < position) THEN SET position = tmp_position; END IF; + SET tmp_position = LOCATE('1', instring); IF (tmp_position > 0 AND tmp_position < position) THEN SET position = tmp_position; END IF; + SET tmp_position = LOCATE('2', instring); IF (tmp_position > 0 AND tmp_position < position) THEN SET position = tmp_position; END IF; + SET tmp_position = LOCATE('3', instring); IF (tmp_position > 0 AND tmp_position < position) THEN SET position = tmp_position; END IF; + SET tmp_position = LOCATE('4', instring); IF (tmp_position > 0 AND tmp_position < position) THEN SET position = tmp_position; END IF; + SET tmp_position = LOCATE('5', instring); IF (tmp_position > 0 AND tmp_position < position) THEN SET position = tmp_position; END IF; + SET tmp_position = LOCATE('6', instring); IF (tmp_position > 0 AND tmp_position < position) THEN SET position = tmp_position; END IF; + SET tmp_position = LOCATE('7', instring); IF (tmp_position > 0 AND tmp_position < position) THEN SET position = tmp_position; END IF; + SET tmp_position = LOCATE('8', instring); IF (tmp_position > 0 AND tmp_position < position) THEN SET position = tmp_position; END IF; + SET tmp_position = LOCATE('9', instring); IF (tmp_position > 0 AND tmp_position < position) THEN SET position = tmp_position; END IF; + + IF (position = 5000) THEN RETURN 0; END IF; + RETURN position; + END"); + + DB::unprepared("DROP FUNCTION IF EXISTS `udf_NaturalSortFormat`; + CREATE FUNCTION `udf_NaturalSortFormat` (`instring` varchar(4000), `numberLength` int, `sameOrderChars` char(50)) + RETURNS varchar(4000) + LANGUAGE SQL + DETERMINISTIC + NO SQL + SQL SECURITY INVOKER + BEGIN + DECLARE sortString varchar(4000); + DECLARE numStartIndex int; + DECLARE numEndIndex int; + DECLARE padLength int; + DECLARE totalPadLength int; + DECLARE i int; + DECLARE sameOrderCharsLen int; + + SET totalPadLength = 0; + SET instring = TRIM(instring); + SET sortString = instring; + SET numStartIndex = udf_FirstNumberPos(instring); + SET numEndIndex = 0; + SET i = 1; + SET sameOrderCharsLen = CHAR_LENGTH(sameOrderChars); + + WHILE (i <= sameOrderCharsLen) DO + SET sortString = REPLACE(sortString, SUBSTRING(sameOrderChars, i, 1), ' '); + SET i = i + 1; + END WHILE; + + WHILE (numStartIndex <> 0) DO + SET numStartIndex = numStartIndex + numEndIndex; + SET numEndIndex = numStartIndex; + + WHILE (udf_FirstNumberPos(SUBSTRING(instring, numEndIndex, 1)) = 1) DO + SET numEndIndex = numEndIndex + 1; + END WHILE; + + SET numEndIndex = numEndIndex - 1; + + SET padLength = numberLength - (numEndIndex + 1 - numStartIndex); + + IF padLength < 0 THEN + SET padLength = 0; + END IF; + + SET sortString = INSERT(sortString, numStartIndex + totalPadLength, 0, REPEAT('0', padLength)); + + SET totalPadLength = totalPadLength + padLength; + SET numStartIndex = udf_FirstNumberPos(RIGHT(instring, CHAR_LENGTH(instring) - numEndIndex)); + END WHILE; + + RETURN sortString; + END"); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + DB::unprepared("DROP FUNCTION IF EXISTS `udf_FirstNumberPos`"); + DB::unprepared("DROP FUNCTION IF EXISTS `udf_NaturalSortFormat`"); + } +} diff --git a/resources/views/layouts/nav.blade.php b/resources/views/layouts/nav.blade.php index dc511ab85..19728f2b0 100644 --- a/resources/views/layouts/nav.blade.php +++ b/resources/views/layouts/nav.blade.php @@ -4,5 +4,6 @@
A simple and consistent repository of anime opening and ending themes
+