1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* Anime List Client |
4
|
|
|
* |
5
|
|
|
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists |
6
|
|
|
* |
7
|
|
|
* PHP version 7 |
8
|
|
|
* |
9
|
|
|
* @package AnimeListClient |
10
|
|
|
* @author Timothy J. Warren <[email protected]> |
11
|
|
|
* @copyright 2015 - 2017 Timothy J. Warren |
12
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
13
|
|
|
* @version 4.0 |
14
|
|
|
* @link https://github.com/timw4mail/HummingBirdAnimeClient |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace Aviat\AnimeClient\Controller; |
18
|
|
|
|
19
|
|
|
use Aviat\AnimeClient\Controller as BaseController; |
20
|
|
|
use Aviat\AnimeClient\API\Kitsu; |
21
|
|
|
use Aviat\AnimeClient\API\Kitsu\Enum\AnimeWatchingStatus; |
22
|
|
|
use Aviat\AnimeClient\API\Kitsu\Transformer\AnimeListTransformer; |
23
|
|
|
use Aviat\Ion\Di\ContainerInterface; |
24
|
|
|
use Aviat\Ion\Json; |
25
|
|
|
use Aviat\Ion\StringWrapper; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Controller for Anime-related pages |
29
|
|
|
*/ |
30
|
|
|
class Anime extends BaseController { |
31
|
|
|
|
32
|
|
|
use StringWrapper; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The anime list model |
36
|
|
|
* @var object $model |
37
|
|
|
*/ |
38
|
|
|
protected $model; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Data to be sent to all routes in this controller |
42
|
|
|
* @var array $base_data |
43
|
|
|
*/ |
44
|
|
|
protected $base_data; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Data cache |
48
|
|
|
* @var Aviat\Ion\Cache\CacheInterface |
49
|
|
|
*/ |
50
|
|
|
protected $cache; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Constructor |
54
|
|
|
* |
55
|
|
|
* @param ContainerInterface $container |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
public function __construct(ContainerInterface $container) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
parent::__construct($container); |
60
|
|
|
|
61
|
|
|
$this->model = $container->get('anime-model'); |
62
|
|
|
|
63
|
|
|
$this->base_data = array_merge($this->base_data, [ |
64
|
|
|
'menu_name' => 'anime_list', |
65
|
|
|
'url_type' => 'anime', |
66
|
|
|
'other_type' => 'manga', |
67
|
|
|
'config' => $this->config, |
68
|
|
|
]); |
69
|
|
|
|
70
|
|
|
$this->cache = $container->get('cache'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Show a portion, or all of the anime list |
75
|
|
|
* |
76
|
|
|
* @param string|int $type - The section of the list |
77
|
|
|
* @param string $view - List or cover view |
|
|
|
|
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
public function index($type = AnimeWatchingStatus::WATCHING, string $view = NULL) |
81
|
|
|
{ |
82
|
|
|
$type_title_map = [ |
83
|
|
|
'all' => 'All', |
84
|
|
|
AnimeWatchingStatus::WATCHING => 'Currently Watching', |
85
|
|
|
AnimeWatchingStatus::PLAN_TO_WATCH => 'Plan to Watch', |
86
|
|
|
AnimeWatchingStatus::ON_HOLD => 'On Hold', |
87
|
|
|
AnimeWatchingStatus::DROPPED => 'Dropped', |
88
|
|
|
AnimeWatchingStatus::COMPLETED => 'Completed' |
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
$model_map = [ |
92
|
|
|
'watching' => AnimeWatchingStatus::WATCHING, |
93
|
|
|
'plan_to_watch' => AnimeWatchingStatus::PLAN_TO_WATCH, |
94
|
|
|
'on_hold' => AnimeWatchingStatus::ON_HOLD, |
95
|
|
|
'all' => 'all', |
96
|
|
|
'dropped' => AnimeWatchingStatus::DROPPED, |
97
|
|
|
'completed' => AnimeWatchingStatus::COMPLETED |
98
|
|
|
]; |
99
|
|
|
|
100
|
|
|
$title = (array_key_exists($type, $type_title_map)) |
101
|
|
|
? $this->config->get('whose_list') . |
102
|
|
|
"'s Anime List · {$type_title_map[$type]}" |
103
|
|
|
: ''; |
104
|
|
|
|
105
|
|
|
$view_map = [ |
106
|
|
|
'' => 'cover', |
107
|
|
|
'list' => 'list' |
108
|
|
|
]; |
109
|
|
|
|
110
|
|
|
$data = ($type !== 'all') |
111
|
|
|
? $this->model->getList($model_map[$type]) |
112
|
|
|
: $this->model->get_all_lists(); |
113
|
|
|
|
114
|
|
|
$this->outputHTML('anime/' . $view_map[$view], [ |
115
|
|
|
'title' => $title, |
116
|
|
|
'sections' => $data |
117
|
|
|
]); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Form to add an anime |
122
|
|
|
* |
123
|
|
|
* @return void |
124
|
|
|
*/ |
125
|
|
|
public function add_form() |
126
|
|
|
{ |
127
|
|
|
$statuses = [ |
128
|
|
|
AnimeWatchingStatus::WATCHING => 'Currently Watching', |
129
|
|
|
AnimeWatchingStatus::PLAN_TO_WATCH => 'Plan to Watch', |
130
|
|
|
AnimeWatchingStatus::ON_HOLD => 'On Hold', |
131
|
|
|
AnimeWatchingStatus::DROPPED => 'Dropped', |
132
|
|
|
AnimeWatchingStatus::COMPLETED => 'Completed' |
133
|
|
|
]; |
134
|
|
|
|
135
|
|
|
$this->set_session_redirect(); |
136
|
|
|
$this->outputHTML('anime/add', [ |
137
|
|
|
'title' => $this->config->get('whose_list') . |
138
|
|
|
"'s Anime List · Add", |
139
|
|
|
'action_url' => $this->urlGenerator->url('anime/add'), |
140
|
|
|
'status_list' => $statuses |
141
|
|
|
]); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Add an anime to the list |
146
|
|
|
* |
147
|
|
|
* @return void |
148
|
|
|
*/ |
149
|
|
View Code Duplication |
public function add() |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
$data = $this->request->getParsedBody(); |
152
|
|
|
if ( ! array_key_exists('id', $data)) |
153
|
|
|
{ |
154
|
|
|
$this->redirect("anime/add", 303); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$result = $this->model->createLibraryItem($data); |
158
|
|
|
|
159
|
|
|
if ($result) |
160
|
|
|
{ |
161
|
|
|
$this->set_flash_message('Added new anime to list', 'success'); |
162
|
|
|
$this->cache->clear(); |
163
|
|
|
} |
164
|
|
|
else |
165
|
|
|
{ |
166
|
|
|
$this->set_flash_message('Failed to add new anime to list', 'error'); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$this->session_redirect(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Form to edit details about a series |
174
|
|
|
* |
175
|
|
|
* @param int $id |
176
|
|
|
* @param string $status |
177
|
|
|
* @return void |
178
|
|
|
*/ |
179
|
|
|
public function edit($id, $status = "all") |
180
|
|
|
{ |
181
|
|
|
$item = $this->model->getLibraryItem($id, $status); |
182
|
|
|
$raw_status_list = AnimeWatchingStatus::getConstList(); |
183
|
|
|
|
184
|
|
|
$statuses = []; |
185
|
|
|
|
186
|
|
|
foreach ($raw_status_list as $status_item) |
187
|
|
|
{ |
188
|
|
|
$statuses[$status_item] = (string) $this->string($status_item) |
189
|
|
|
->underscored() |
190
|
|
|
->humanize() |
191
|
|
|
->titleize(); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$this->set_session_redirect(); |
195
|
|
|
|
196
|
|
|
$this->outputHTML('anime/edit', [ |
197
|
|
|
'title' => $this->config->get('whose_list') . |
198
|
|
|
"'s Anime List · Edit", |
199
|
|
|
'item' => $item, |
200
|
|
|
'statuses' => Kitsu::getStatusToSelectMap(), |
201
|
|
|
'action' => $this->container->get('url-generator') |
202
|
|
|
->url('/anime/update_form'), |
203
|
|
|
]); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Search for anime |
208
|
|
|
* |
209
|
|
|
* @return void |
210
|
|
|
*/ |
211
|
|
|
public function search() |
212
|
|
|
{ |
213
|
|
|
$queryParams = $this->request->getQueryParams(); |
214
|
|
|
$query = $queryParams['query']; |
215
|
|
|
$this->outputJSON($this->model->search($query)); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Update an anime item via a form submission |
220
|
|
|
* |
221
|
|
|
* @return void |
222
|
|
|
*/ |
223
|
|
View Code Duplication |
public function form_update() |
|
|
|
|
224
|
|
|
{ |
225
|
|
|
$data = $this->request->getParsedBody(); |
226
|
|
|
|
227
|
|
|
// Do some minor data manipulation for |
228
|
|
|
// large form-based updates |
229
|
|
|
$transformer = new AnimeListTransformer(); |
230
|
|
|
$post_data = $transformer->untransform($data); |
231
|
|
|
$full_result = $this->model->updateLibraryItem($post_data); |
232
|
|
|
|
233
|
|
|
if ($full_result['statusCode'] === 200) |
234
|
|
|
{ |
235
|
|
|
$this->set_flash_message("Successfully updated.", 'success'); |
236
|
|
|
$this->cache->clear(); |
237
|
|
|
} |
238
|
|
|
else |
239
|
|
|
{ |
240
|
|
|
$this->set_flash_message('Failed to update anime.', 'error'); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
$this->session_redirect(); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Update an anime item |
248
|
|
|
* |
249
|
|
|
* @return void |
250
|
|
|
*/ |
251
|
|
View Code Duplication |
public function update() |
|
|
|
|
252
|
|
|
{ |
253
|
|
|
if ($this->request->getHeader('content-type')[0] === 'application/json') |
254
|
|
|
{ |
255
|
|
|
$data = JSON::decode((string)$this->request->getBody()); |
256
|
|
|
} |
257
|
|
|
else |
258
|
|
|
{ |
259
|
|
|
$data = $this->request->getParsedBody(); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
$response = $this->model->updateLibraryItem($data, $data); |
263
|
|
|
|
264
|
|
|
$this->cache->clear(); |
265
|
|
|
$this->outputJSON($response['body'], $response['statusCode']); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Remove an anime from the list |
270
|
|
|
* |
271
|
|
|
* @return void |
272
|
|
|
*/ |
273
|
|
View Code Duplication |
public function delete() |
|
|
|
|
274
|
|
|
{ |
275
|
|
|
$body = $this->request->getParsedBody(); |
276
|
|
|
$response = $this->model->deleteLibraryItem($body['id'], $body['mal_id']); |
277
|
|
|
|
278
|
|
|
if ((bool)$response === TRUE) |
279
|
|
|
{ |
280
|
|
|
$this->set_flash_message("Successfully deleted anime.", 'success'); |
281
|
|
|
$this->cache->clear(); |
282
|
|
|
} |
283
|
|
|
else |
284
|
|
|
{ |
285
|
|
|
$this->set_flash_message('Failed to delete anime.', 'error'); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
$this->session_redirect(); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* View details of an anime |
293
|
|
|
* |
294
|
|
|
* @param string $anime_id |
295
|
|
|
* @return void |
296
|
|
|
*/ |
297
|
|
|
public function details($anime_id) |
298
|
|
|
{ |
299
|
|
|
$data = $this->model->getAnime($anime_id); |
300
|
|
|
|
301
|
|
|
$this->outputHTML('anime/details', [ |
302
|
|
|
'title' => 'Anime · ' . $data['titles'][0], |
303
|
|
|
'data' => $data, |
304
|
|
|
]); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
} |
308
|
|
|
// End of AnimeController.php |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.