API client for Kitsu.io, with optional Anime collection, and optional Anilist syncing.
anime
manga
kitsu
anilist
single-user

AnimeCollection.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php declare(strict_types=1);
  2. /**
  3. * Hummingbird 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 HummingbirdAnimeClient
  10. * @author Timothy J. Warren <tim@timshomepage.net>
  11. * @copyright 2015 - 2018 Timothy J. Warren
  12. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  13. * @version 4.0
  14. * @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
  15. */
  16. namespace Aviat\AnimeClient\Model;
  17. use Aviat\Ion\Di\ContainerInterface;
  18. use PDO;
  19. /**
  20. * Model for getting anime collection data
  21. */
  22. final class AnimeCollection extends Collection {
  23. /**
  24. * Anime API Model
  25. * @var Anime $animeModel
  26. */
  27. protected $animeModel;
  28. /**
  29. * Create the collection model
  30. *
  31. * @param ContainerInterface $container
  32. */
  33. public function __construct(ContainerInterface $container)
  34. {
  35. parent::__construct($container);
  36. $this->animeModel = $container->get('anime-model');
  37. }
  38. /**
  39. * Get collection from the database, and organize by media type
  40. *
  41. * @return array
  42. */
  43. public function getCollection(): array
  44. {
  45. $rawCollection = $this->getCollectionFromDatabase();
  46. $collection = [];
  47. foreach ($rawCollection as $row)
  48. {
  49. if (array_key_exists($row['media'], $collection))
  50. {
  51. $collection[$row['media']][] = $row;
  52. }
  53. else
  54. {
  55. $collection[$row['media']] = [$row];
  56. }
  57. }
  58. return $collection;
  59. }
  60. /**
  61. * Get list of media types
  62. *
  63. * @return array
  64. */
  65. public function getMediaTypeList(): array
  66. {
  67. $output = [];
  68. $query = $this->db->select('id, type')
  69. ->from('media')
  70. ->get();
  71. foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $row)
  72. {
  73. $output[$row['id']] = $row['type'];
  74. }
  75. return $output;
  76. }
  77. /**
  78. * Get item from collection for editing
  79. *
  80. * @param string $id
  81. * @return array
  82. */
  83. public function getCollectionEntry($id): array
  84. {
  85. $query = $this->db->from('anime_set')
  86. ->where('hummingbird_id', $id)
  87. ->get();
  88. return $query->fetch(PDO::FETCH_ASSOC);
  89. }
  90. /**
  91. * Get full collection from the database
  92. *
  93. * @return array
  94. */
  95. private function getCollectionFromDatabase(): array
  96. {
  97. if ( ! $this->validDatabase)
  98. {
  99. return [];
  100. }
  101. $query = $this->db->select('hummingbird_id, slug, title, alternate_title, show_type,
  102. age_rating, episode_count, episode_length, cover_image, notes, media.type as media')
  103. ->from('anime_set a')
  104. ->join('media', 'media.id=a.media_id', 'inner')
  105. ->order_by('media')
  106. ->order_by('title')
  107. ->get();
  108. return $query->fetchAll(PDO::FETCH_ASSOC);
  109. }
  110. /**
  111. * Add an item to the anime collection
  112. *
  113. * @param array $data
  114. * @return void
  115. */
  116. public function add($data): void
  117. {
  118. $anime = (object)$this->animeModel->getAnimeById($data['id']);
  119. $this->db->set([
  120. 'hummingbird_id' => $data['id'],
  121. 'slug' => $anime->slug,
  122. 'title' => array_shift($anime->titles),
  123. 'alternate_title' => implode('<br />', $anime->titles),
  124. 'show_type' => $anime->show_type,
  125. 'age_rating' => $anime->age_rating,
  126. 'cover_image' => $anime->cover_image,
  127. 'episode_count' => $anime->episode_count,
  128. 'episode_length' => $anime->episode_length,
  129. 'media_id' => $data['media_id'],
  130. 'notes' => $data['notes']
  131. ])->insert('anime_set');
  132. $this->updateGenre($data['id']);
  133. }
  134. /**
  135. * Update a collection item
  136. *
  137. * @param array $data
  138. * @return void
  139. */
  140. public function update($data): void
  141. {
  142. // If there's no id to update, don't update
  143. if ( ! array_key_exists('hummingbird_id', $data))
  144. {
  145. return;
  146. }
  147. $id = $data['hummingbird_id'];
  148. unset($data['hummingbird_id']);
  149. $this->db->set($data)
  150. ->where('hummingbird_id', $id)
  151. ->update('anime_set');
  152. }
  153. /**
  154. * Remove a collection item
  155. *
  156. * @param array $data
  157. * @return void
  158. */
  159. public function delete($data): void
  160. {
  161. // If there's no id to update, don't delete
  162. if ( ! array_key_exists('hummingbird_id', $data))
  163. {
  164. return;
  165. }
  166. $this->db->where('hummingbird_id', $data['hummingbird_id'])
  167. ->delete('genre_anime_set_link');
  168. $this->db->where('hummingbird_id', $data['hummingbird_id'])
  169. ->delete('anime_set');
  170. }
  171. /**
  172. * Get the details of a collection item
  173. *
  174. * @param int $kitsuId
  175. * @return array
  176. */
  177. public function get($kitsuId): array
  178. {
  179. $query = $this->db->from('anime_set')
  180. ->where('hummingbird_id', $kitsuId)
  181. ->get();
  182. return $query->fetch(PDO::FETCH_ASSOC);
  183. }
  184. /**
  185. * Update genre information for selected anime
  186. *
  187. * @param string $animeId The current anime
  188. * @return void
  189. */
  190. private function updateGenre($animeId): void
  191. {
  192. $genreInfo = $this->getGenreData();
  193. $genres = $genreInfo['genres'];
  194. $links = $genreInfo['links'];
  195. // Get api information
  196. $anime = $this->animeModel->getAnimeById($animeId);
  197. foreach ($anime['genres'] as $genre)
  198. {
  199. // Add genres that don't currently exist
  200. if ( ! \in_array($genre, $genres, TRUE))
  201. {
  202. $this->db->set('genre', $genre)
  203. ->insert('genres');
  204. $genres[] = $genre;
  205. }
  206. // Update link table
  207. // Get id of genre to put in link table
  208. $flippedGenres = array_flip($genres);
  209. $insertArray = [
  210. 'hummingbird_id' => $animeId,
  211. 'genre_id' => $flippedGenres[$genre]
  212. ];
  213. if (array_key_exists($animeId, $links))
  214. {
  215. if ( ! \in_array($flippedGenres[$genre], $links[$animeId], TRUE))
  216. {
  217. $this->db->set($insertArray)->insert('genre_anime_set_link');
  218. }
  219. }
  220. else
  221. {
  222. $this->db->set($insertArray)->insert('genre_anime_set_link');
  223. }
  224. }
  225. }
  226. /**
  227. * Get list of existing genres
  228. *
  229. * @return array
  230. */
  231. private function getGenreData(): array
  232. {
  233. $genres = [];
  234. $links = [];
  235. // Get existing genres
  236. $query = $this->db->select('id, genre')
  237. ->from('genres')
  238. ->get();
  239. foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $genre)
  240. {
  241. $genres[$genre['id']] = $genre['genre'];
  242. }
  243. // Get existing link table entries
  244. $query = $this->db->select('hummingbird_id, genre_id')
  245. ->from('genre_anime_set_link')
  246. ->get();
  247. foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $link)
  248. {
  249. if (array_key_exists($link['hummingbird_id'], $links))
  250. {
  251. $links[$link['hummingbird_id']][] = $link['genre_id'];
  252. }
  253. else
  254. {
  255. $links[$link['hummingbird_id']] = [$link['genre_id']];
  256. }
  257. }
  258. return [
  259. 'genres' => $genres,
  260. 'links' => $links
  261. ];
  262. }
  263. }
  264. // End of AnimeCollectionModel.php