get()->first(); if ( is_null($anime) ) { abort(404); } /** * Redirects to correct slug. */ if ( $slug != Str::slug($anime->title_pref) ) { return redirect()->route('anime', ["mal_id" => $anime->mal_id, "slug" => Str::slug($anime->title_pref)]); } $anime["stats"] = $anime->getStats()->orderBy('created_at', 'asc')->get(); foreach( $anime["stats"] as $index => $stats ) { if ( ! is_null($stats->score) ) { // Skip entries which are clearly anomalies if ( !empty($score) && ($stats->score_1 + $stats->score_2 + $stats->score_3 + $stats->score_4 + $stats->score_5 + $stats->score_6 + $stats->score_7 + $stats->score_8 + $stats->score_9 + $stats->score_10) == 0 ) { continue; } $score[] = $stats->score; $rank[] = $stats->rank; $created_at_score[] = $stats->created_at->toDateString(); } $created_at[] = $stats->created_at->toDateString(); $popularity[] = $stats->popularity; $members[] = $stats->members; $favorites[] = $stats->favorites; /** * Sometimes we have a gap in our data, so we fake it by making it the same like last day. */ if ( 0 == $stats->plan_to_watch && 0 != $index && 0 != $anime["stats"][$index-1]->plan_to_watch ) { $stats = $anime["stats"][$index-1]; } $watching[] = $stats->watching; $completed[] = $stats->completed; $onhold[] = $stats->onhold; $dropped[] = $stats->dropped; $plan_to_watch[] = $stats->plan_to_watch; $scored_by[] = $stats->scored_by; $score_1[] = $stats->score_1; $score_2[] = $stats->score_2; $score_3[] = $stats->score_3; $score_4[] = $stats->score_4; $score_5[] = $stats->score_5; $score_6[] = $stats->score_6; $score_7[] = $stats->score_7; $score_8[] = $stats->score_8; $score_9[] = $stats->score_9; $score_10[] = $stats->score_10; if ( 0 != $stats->scored_by ) { $calculated_score[] = ($stats->score_1 + (2*$stats->score_2) + (3*$stats->score_3) + (4*$stats->score_4) + (5*$stats->score_5) + (6*$stats->score_6) + (7*$stats->score_7) + (8*$stats->score_8) + (9*$stats->score_9) + (10*$stats->score_10)) / $stats->scored_by; } } if ( isset($score) ) { $anime["score"] = $score; $anime["rank"] = $rank; $anime["chart_label_score"] = $created_at_score; } $anime["popularity"] = $popularity; $anime["members"] = $members; $anime["favorites"] = $favorites; $anime["watching"] = $watching; $anime["onhold"] = $onhold; $anime["plan_to_watch"] = $plan_to_watch; $anime["completed"] = $completed; $anime["dropped"] = $dropped; $anime["scored_by"] = $scored_by; $anime["score_1"] = $score_1; $anime["score_2"] = $score_2; $anime["score_3"] = $score_3; $anime["score_4"] = $score_4; $anime["score_5"] = $score_5; $anime["score_6"] = $score_6; $anime["score_7"] = $score_7; $anime["score_8"] = $score_8; $anime["score_9"] = $score_9; $anime["score_10"] = $score_10; $anime["chart_label"] = $created_at; if ( 0 != $stats->scored_by ) { $anime["calculated_score"] = $calculated_score; } $anime["basic_data"] = DB::select('select score, rank, popularity, members, favorites from anime join stats on stats.id = ( select id from stats where anime.mal_id = stats.mal_id order by created_at desc limit 1) where anime.mal_id = ?', [$mal_id])[0]; $cached_data = view('anime', [ "anime" => $anime ])->render(); Cache::put($mal_id, $cached_data, 6000); // 1000 minutes return $cached_data; } public function showCurrentAnime() { if ( Cache::has('airing_anime') ) { return Cache::get('airing_anime'); } $anime = new Anime; $anime = $anime->setTable('view_anime_index'); #$anime = $anime->setTable('anime'); $anime = $anime->where('airing_status', env('ANIME_IS_AIRING')); $anime = $anime->orWhere( function( $q ) { $q->where('season_year', date("Y")); $q->where('season_name', getSeason()); }); $anime = $anime->orderBy('season_year', 'desc'); $anime = $anime->orderByRaw('FIELD(season_name, ' . getFieldSeasonName() . ')'); $anime = $anime->orderBy('score_today', 'desc'); $anime = $anime->orderBy('members', 'desc'); $anime = $anime->orderBy('watching', 'desc'); $anime = $anime->get(); #$anime = $anime->simplePaginate(10); $cached_data = view('list_anime', ["all_anime" => $anime, "title" => "Currently Airing"])->render(); Cache::put('airing_anime', $cached_data, 6000); // 100 minutes return $cached_data; } public function showSurprisingAnime(Request $req) { if ( Cache::has('surprising_anime' . $req->get('page')) ) { return Cache::get('surprising_anime' . $req->get('page')); } $anime = new Anime; $most_surprising_anime = $anime->setTable('view_anime_index'); $most_surprising_anime = $most_surprising_anime->paginate(12); #$most_surprising_anime = $most_surprising_anime->simplePaginate(10); #$most_surprising_anime = $most_surprising_anime->get(); #return view('surprising_anime', ["surprising_anime" => $most_surprising_anime, "title" => "Most Surprising"]); $cached_data = view('list_anime', ["all_anime" => $most_surprising_anime, "title" => "Most Surprising Anime"])->render(); Cache::put('surprising_anime' . $req->get('page'), $cached_data, 6000); // 100 minutes return $cached_data; } public function showTopAnime(Request $req) { if ( Cache::has('top_anime' . $req->get('page')) ) { return Cache::get('top_anime' . $req->get('page')); } $anime = new Anime; $anime = $anime->setTable('view_anime_index'); $anime = $anime->orderBy('score_today', 'desc'); #$top_anime = $anime->simplePaginate(10); #$top_anime = $anime->get(); $top_anime = $anime->paginate(12); #return view('top_anime', ["top_anime" => $top_anime, "title" => "Top"]); $cached_data = view('list_anime', ["all_anime" => $top_anime, "title" => "Top Anime of all Time"])->render(); Cache::put('top_anime' . $req->get('page'), $cached_data, 6000); // 100 minutes return $cached_data; } public function search(Request $request) { $search_unsafe = explode(",", $request->input("q")); $anime = new Anime; $anime = $anime->setTable('view_anime_index'); foreach($search_unsafe as $q) { $q_raw = escapeLike($q); $q = "%".$q_raw."%"; /* $anime = $anime->where('title_eng', 'like', $q) ->orWhere('title_rom', 'like', $q) ->orWhere('title_nat', 'like', $q) ->orWhere('title_pref', 'like', $q) ->orWhere('synopsis', 'like', $q); */ $anime = $anime->where('title_eng', 'like', $q) ->orWhere('mal_id', '=', $q_raw) ->orWhere('title_pref', 'like', $q) ->orWhere('synopsis', 'like', $q); } $count = $anime->count(); $anime = $anime->orderBy('season_year', 'desc'); $anime = $anime->orderByRaw('FIELD(season_name, ' . getFieldSeasonName() . ')'); $anime = $anime->orderBy('score_today', 'desc'); $anime = $anime->orderBy('members', 'desc'); $anime = $anime->orderBy('watching', 'desc'); $sanime = $anime->paginate(12); return view('list_anime', ["all_anime" => $sanime, "title" => "Search", "count" => $count]); } public function showSeason($season_year, $season_name = null) { if ( Cache::has($season_year.$season_name) ) { return Cache::get($season_year.$season_name); } $anime = new Anime; $anime = $anime->setTable('view_anime_index'); $anime = $anime->where('season_year', '=', $season_year); if ( ! is_null($season_name) ) { $season_name = ucfirst($season_name); $anime = $anime->where('season_name', '=', $season_name); } $anime = $anime->orderBy('score_today', 'desc'); $anime = $anime->orderBy('members', 'desc'); $anime = $anime->orderBy('watching', 'desc'); $sanime = $anime->get(); #$sanime = $anime->paginate(12); $cached_data = view('list_anime', ["all_anime" => $sanime, "title" => $season_name . " Season " . $season_year])->render(); Cache::set($season_year.$season_name, $cached_data, 6000); // 100 minutes return $cached_data; } public function changeSeason(Request $request) { $input= explode("-", $request->input("season")); $season_name = $input[0]; $season_year = $input[1]; switch( strtolower($season_name) ) { case("winter"): $season_name = "Winter"; break; case("spring"): $season_name = "Spring"; break; case("summer"): $season_name = "Summer"; break; case("fall"): $season_name = "Fall"; break; default: $saeson_name = getSeason(); } if ( ! preg_match("/\d{4}/", $season_year) ) { $season_year = date("Y"); } return redirect('/anime/season/' . $season_year . '/' . $season_name); } }