1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Hummingbird Anime Client |
4
|
|
|
* |
5
|
|
|
* An API client for Hummingbird to manage anime and manga watch lists |
6
|
|
|
* |
7
|
|
|
* @package HummingbirdAnimeClient |
8
|
|
|
* @author Timothy J. Warren |
9
|
|
|
* @copyright Copyright (c) 2015 - 2016 |
10
|
|
|
* @link https://github.com/timw4mail/HummingBirdAnimeClient |
11
|
|
|
* @license MIT |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Aviat\AnimeClient\Controller; |
15
|
|
|
|
16
|
|
|
use Aviat\Ion\Di\ContainerInterface; |
17
|
|
|
use Aviat\AnimeClient\Controller as BaseController; |
18
|
|
|
use Aviat\AnimeClient\Hummingbird\Enum\AnimeWatchingStatus; |
19
|
|
|
use Aviat\AnimeClient\Model\Anime as AnimeModel; |
20
|
|
|
use Aviat\AnimeClient\Hummingbird\Transformer\AnimeListTransformer; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Controller for Anime-related pages |
24
|
|
|
*/ |
25
|
|
|
class Anime extends BaseController { |
26
|
|
|
|
27
|
|
|
use \Aviat\Ion\StringWrapper; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The anime list model |
31
|
|
|
* @var object $model |
32
|
|
|
*/ |
33
|
|
|
protected $model; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Data to ve sent to all routes in this controller |
37
|
|
|
* @var array $base_data |
38
|
|
|
*/ |
39
|
|
|
protected $base_data; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Constructor |
43
|
|
|
* |
44
|
|
|
* @param ContainerInterface $container |
45
|
|
|
*/ |
46
|
|
|
public function __construct(ContainerInterface $container) |
47
|
|
|
{ |
48
|
|
|
parent::__construct($container); |
49
|
|
|
|
50
|
|
|
$this->model = $container->get('anime-model'); |
51
|
|
|
|
52
|
|
|
$this->base_data = array_merge($this->base_data, [ |
53
|
|
|
'menu_name' => 'anime_list', |
54
|
|
|
'url_type' => 'anime', |
55
|
|
|
'other_type' => 'manga', |
56
|
|
|
'config' => $this->config, |
57
|
|
|
]); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Show a portion, or all of the anime list |
62
|
|
|
* |
63
|
|
|
* @param string $type - The section of the list |
64
|
|
|
* @param string $view - List or cover view |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
public function index($type = "watching", $view = '') |
68
|
|
|
{ |
69
|
|
|
$type_title_map = [ |
70
|
|
|
'all' => 'All', |
71
|
|
|
'watching' => 'Currently Watching', |
72
|
|
|
'plan_to_watch' => 'Plan to Watch', |
73
|
|
|
'on_hold' => 'On Hold', |
74
|
|
|
'dropped' => 'Dropped', |
75
|
|
|
'completed' => 'Completed' |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
$model_map = [ |
79
|
|
|
'watching' => AnimeWatchingStatus::WATCHING, |
80
|
|
|
'plan_to_watch' => AnimeWatchingStatus::PLAN_TO_WATCH, |
81
|
|
|
'on_hold' => AnimeWatchingStatus::ON_HOLD, |
82
|
|
|
'all' => 'all', |
83
|
|
|
'dropped' => AnimeWatchingStatus::DROPPED, |
84
|
|
|
'completed' => AnimeWatchingStatus::COMPLETED |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
if (array_key_exists($type, $type_title_map)) |
88
|
|
|
{ |
89
|
|
|
$title = $this->config->get('whose_list') . |
90
|
|
|
"'s Anime List · {$type_title_map[$type]}"; |
91
|
|
|
} |
92
|
|
|
else |
93
|
|
|
{ |
94
|
|
|
$title = ''; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$view_map = [ |
98
|
|
|
'' => 'cover', |
99
|
|
|
'list' => 'list' |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
$data = ($type != 'all') |
103
|
|
|
? $this->model->get_list($model_map[$type]) |
104
|
|
|
: $this->model->get_all_lists(); |
105
|
|
|
|
106
|
|
|
$this->outputHTML('anime/' . $view_map[$view], [ |
107
|
|
|
'title' => $title, |
108
|
|
|
'sections' => $data |
109
|
|
|
]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Form to add an anime |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
View Code Duplication |
public function add_form() |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
$raw_status_list = AnimeWatchingStatus::getConstList(); |
120
|
|
|
|
121
|
|
|
$statuses = []; |
122
|
|
|
|
123
|
|
|
foreach ($raw_status_list as $status_item) |
124
|
|
|
{ |
125
|
|
|
$statuses[$status_item] = (string)$this->string($status_item) |
126
|
|
|
->underscored() |
127
|
|
|
->humanize() |
128
|
|
|
->titleize(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$this->set_session_redirect(); |
132
|
|
|
$this->outputHTML('anime/add', [ |
133
|
|
|
'title' => $this->config->get('whose_list') . |
134
|
|
|
"'s Anime List · Add", |
135
|
|
|
'action_url' => $this->urlGenerator->url('anime/add'), |
136
|
|
|
'status_list' => $statuses |
137
|
|
|
]); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Add an anime to the list |
142
|
|
|
* |
143
|
|
|
* @return void |
144
|
|
|
*/ |
145
|
|
|
public function add() |
146
|
|
|
{ |
147
|
|
|
$data = $this->request->post->get(); |
148
|
|
|
if ( ! array_key_exists('id', $data)) |
149
|
|
|
{ |
150
|
|
|
$this->redirect("anime/add", 303); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$result = $this->model->update($data); |
154
|
|
|
|
155
|
|
|
if ($result['statusCode'] == 201) |
156
|
|
|
{ |
157
|
|
|
$this->set_flash_message('Added new anime to list', 'success'); |
158
|
|
|
} |
159
|
|
|
else |
160
|
|
|
{ |
161
|
|
|
$this->set_flash_message('Failed to add new anime to list', 'error'); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$this->session_redirect(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Form to edit details about a series |
169
|
|
|
* |
170
|
|
|
* @param int $id |
171
|
|
|
* @param string $status |
172
|
|
|
* @return void |
173
|
|
|
*/ |
174
|
|
|
public function edit($id, $status = "all") |
175
|
|
|
{ |
176
|
|
|
$item = $this->model->get_library_item($id, $status); |
177
|
|
|
$raw_status_list = AnimeWatchingStatus::getConstList(); |
178
|
|
|
|
179
|
|
|
$statuses = []; |
180
|
|
|
|
181
|
|
|
foreach ($raw_status_list as $status_item) |
182
|
|
|
{ |
183
|
|
|
$statuses[$status_item] = (string)$this->string($status_item) |
184
|
|
|
->underscored() |
185
|
|
|
->humanize() |
186
|
|
|
->titleize(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$this->set_session_redirect($this->request->server->get('HTTP_REFERRER')); |
190
|
|
|
|
191
|
|
|
$this->outputHTML('anime/edit', [ |
192
|
|
|
'title' => $this->config->get('whose_list') . |
193
|
|
|
"'s Anime List · Edit", |
194
|
|
|
'item' => $item, |
195
|
|
|
'statuses' => $statuses, |
196
|
|
|
'action' => $this->container->get('url-generator') |
197
|
|
|
->url('/anime/update_form'), |
198
|
|
|
]); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Search for anime |
203
|
|
|
* |
204
|
|
|
* @return void |
205
|
|
|
*/ |
206
|
|
|
public function search() |
207
|
|
|
{ |
208
|
|
|
$query = $this->request->query->get('query'); |
209
|
|
|
$this->outputJSON($this->model->search($query)); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Update an anime item via a form submission |
214
|
|
|
* |
215
|
|
|
* @return void |
216
|
|
|
*/ |
217
|
|
|
public function form_update() |
218
|
|
|
{ |
219
|
|
|
$post_data = $this->request->post->get(); |
220
|
|
|
|
221
|
|
|
// Do some minor data manipulation for |
222
|
|
|
// large form-based updates |
223
|
|
|
$transformer = new AnimeListTransformer(); |
224
|
|
|
$post_data = $transformer->untransform($post_data); |
225
|
|
|
|
226
|
|
|
$full_result = $this->model->update($post_data); |
227
|
|
|
$result = $full_result['body']; |
228
|
|
|
|
229
|
|
|
if (array_key_exists('anime', $result)) |
230
|
|
|
{ |
231
|
|
|
$title = ( ! empty($result['anime']['alternate_title'])) |
232
|
|
|
? "{$result['anime']['title']} ({$result['anime']['alternate_title']})" |
233
|
|
|
: "{$result['anime']['title']}"; |
234
|
|
|
|
235
|
|
|
$this->set_flash_message("Successfully updated {$title}.", 'success'); |
236
|
|
|
} |
237
|
|
|
else |
238
|
|
|
{ |
239
|
|
|
$this->set_flash_message('Failed to update anime.', 'error'); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
$this->session_redirect(); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Update an anime item |
247
|
|
|
*/ |
248
|
|
|
public function update() |
249
|
|
|
{ |
250
|
|
|
$response = $this->model->update($this->request->post->get()); |
251
|
|
|
$this->outputJSON($response['body'], $response['statusCode']); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Remove an anime from the list |
256
|
|
|
*/ |
257
|
|
|
public function delete() |
258
|
|
|
{ |
259
|
|
|
$response = $this->model->update($this->request->post->get()); |
260
|
|
|
$this->outputJSON($response['body'], $response['statusCode']); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* View details of an anime |
265
|
|
|
* |
266
|
|
|
* @param string anime_id |
267
|
|
|
* @return void |
268
|
|
|
*/ |
269
|
|
|
public function details($anime_id) |
270
|
|
|
{ |
271
|
|
|
$data = $this->model->get_anime($anime_id); |
272
|
|
|
|
273
|
|
|
$this->outputHTML('anime/details', [ |
274
|
|
|
'title' => $data['title'], |
275
|
|
|
'data' => $data, |
276
|
|
|
]); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
// 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.