From bc6854a8e515ecb3cdf40cc6b9eb820061b36a70 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Mon, 20 Mar 2017 13:14:01 -0400 Subject: [PATCH] Show characters on anime details page --- app/views/anime/details.php | 25 ++++++++++++++++++++++++- src/API/JsonAPI.php | 5 ++++- src/API/Kitsu/Model.php | 7 +++++-- src/Controller/Anime.php | 10 ++++++++++ 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/app/views/anime/details.php b/app/views/anime/details.php index 9d14e390..c9390200 100644 --- a/app/views/anime/details.php +++ b/app/views/anime/details.php @@ -74,7 +74,30 @@ - */ ?> + + + */ ?> +
+ 0): ?> +

Characters

+
+ + +
+ generate('character', ['slug' => $char['slug']]) ?> + a($link, $char['name']); ?> +
+ + img($char['image']['original'], [ + 'width' => '225' + ]) ?> + +
+ + +
+ +
\ No newline at end of file diff --git a/src/API/JsonAPI.php b/src/API/JsonAPI.php index dc0475e0..2776c4ec 100644 --- a/src/API/JsonAPI.php +++ b/src/API/JsonAPI.php @@ -167,7 +167,10 @@ class JsonAPI { foreach ($data['data'] as $item) { - $organized[$key][] = $item['id']; + if (is_array($item) && array_key_exists('id', $item)) + { + $organized[$key][] = $item['id']; + } } } diff --git a/src/API/Kitsu/Model.php b/src/API/Kitsu/Model.php index 24de9b0c..bf4eb724 100644 --- a/src/API/Kitsu/Model.php +++ b/src/API/Kitsu/Model.php @@ -127,6 +127,7 @@ class Model { */ public function getCharacter(string $slug): array { + // @todo catch non-existent characters and show 404 $data = $this->getRequest('/characters', [ 'query' => [ 'filter' => [ @@ -196,7 +197,9 @@ class Model { { // @TODO catch non-existent anime $baseData = $this->getRawMediaData('anime', $slug); - return $this->animeTransformer->transform($baseData); + $transformed = $this->animeTransformer->transform($baseData); + $transformed['included'] = $baseData['included']; + return $transformed; } /** @@ -659,7 +662,7 @@ class Model { 'slug' => $slug ], 'include' => ($type === 'anime') - ? 'genres,mappings,streamingLinks' + ? 'genres,mappings,streamingLinks,animeCharacters.character' : 'genres,mappings', ] ]; diff --git a/src/Controller/Anime.php b/src/Controller/Anime.php index 6e391a89..5c81a861 100644 --- a/src/Controller/Anime.php +++ b/src/Controller/Anime.php @@ -261,9 +261,19 @@ class Anime extends BaseController { public function details(string $animeId) { $data = $this->model->getAnime($animeId); + $characters = []; + + foreach($data['included'] as $included) + { + if ($included['type'] === 'characters') + { + $characters[$included['id']] = $included['attributes']; + } + } $this->outputHTML('anime/details', [ 'title' => 'Anime · ' . $data['titles'][0], + 'characters' => $characters, 'data' => $data, ]); }