summaryrefslogtreecommitdiff
path: root/app/Http/Controllers/AnimeController.php
blob: 265617ec9c09fd34dbd2c3b550902265b982797d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

use App\Anime;
use App\AnimeStats;

use Carbon\Carbon;

class AnimeController extends Controller {
	/**
        * Shows the index page.
        *
        * @return Response
        */
        public function showAnime($mal_id, $slug = "") {

		if ( Cache::has($mal_id) ) {
			return Cache::get($mal_id);
		}

		$anime = Anime::where('mal_id', $mal_id)->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) ) {
				$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);
	}
}