API client for Kitsu.io, with optional Anime collection, and optional Anilist syncing.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AnimeCollection.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php declare(strict_types=1);
  2. /**
  3. * Hummingbird Anime List Client
  4. *
  5. * An API client for Kitsu to manage anime and manga watch lists
  6. *
  7. * PHP version 7.1
  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.1
  14. * @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
  15. */
  16. namespace Aviat\AnimeClient\Controller;
  17. use Aviat\AnimeClient\Controller as BaseController;
  18. use Aviat\AnimeClient\Model\{
  19. Anime as AnimeModel,
  20. AnimeCollection as AnimeCollectionModel
  21. };
  22. use Aviat\Ion\Di\ContainerInterface;
  23. /**
  24. * Controller for Anime collection pages
  25. */
  26. final class AnimeCollection extends BaseController {
  27. /**
  28. * The anime collection model
  29. * @var AnimeCollectionModel $animeCollectionModel
  30. */
  31. private $animeCollectionModel;
  32. /**
  33. * The anime API model
  34. * @var AnimeModel $animeModel
  35. */
  36. private $animeModel;
  37. /**
  38. * Constructor
  39. *
  40. * @param ContainerInterface $container
  41. * @throws \Aviat\Ion\Di\ContainerException
  42. * @throws \Aviat\Ion\Di\NotFoundException
  43. */
  44. public function __construct(ContainerInterface $container)
  45. {
  46. parent::__construct($container);
  47. $this->animeModel = $container->get('anime-model');
  48. $this->animeCollectionModel = $container->get('anime-collection-model');
  49. $this->baseData = array_merge($this->baseData, [
  50. 'collection_type' => 'anime',
  51. 'menu_name' => 'collection',
  52. 'other_type' => 'manga',
  53. 'url_type' => 'anime',
  54. ]);
  55. }
  56. /**
  57. * Search for anime
  58. *
  59. * @throws \Aviat\Ion\Exception\DoubleRenderException
  60. * @return void
  61. */
  62. public function search(): void
  63. {
  64. $queryParams = $this->request->getQueryParams();
  65. $query = $queryParams['query'];
  66. $this->outputJSON($this->animeModel->search($query));
  67. }
  68. /**
  69. * Show the anime collection page
  70. *
  71. * @param string $view
  72. * @throws \Aviat\Ion\Di\ContainerException
  73. * @throws \Aviat\Ion\Di\NotFoundException
  74. * @throws \InvalidArgumentException
  75. * @return void
  76. */
  77. public function index($view): void
  78. {
  79. $viewMap = [
  80. '' => 'cover',
  81. 'list' => 'list'
  82. ];
  83. $data = $this->animeCollectionModel->getCollection();
  84. $this->outputHTML('collection/' . $viewMap[$view], [
  85. 'title' => $this->config->get('whose_list') . "'s Anime Collection",
  86. 'sections' => $data,
  87. 'genres' => $this->animeCollectionModel->getGenreList()
  88. ]);
  89. }
  90. /**
  91. * Show the anime collection add/edit form
  92. *
  93. * @param integer|null $id
  94. * @throws \Aviat\Ion\Di\ContainerException
  95. * @throws \Aviat\Ion\Di\NotFoundException
  96. * @throws \Aura\Router\Exception\RouteNotFound
  97. * @throws \InvalidArgumentException
  98. * @return void
  99. */
  100. public function form($id = NULL): void
  101. {
  102. $this->setSessionRedirect();
  103. $action = $id === NULL ? 'Add' : 'Edit';
  104. $urlAction = strtolower($action);
  105. $this->outputHTML('collection/' . $urlAction, [
  106. 'action' => $action,
  107. 'action_url' => $this->url->generate("anime.collection.{$urlAction}.post"),
  108. 'title' => $this->formatTitle(
  109. $this->config->get('whose_list') . "'s Anime Collection",
  110. $action
  111. ),
  112. 'media_items' => $this->animeCollectionModel->getMediaTypeList(),
  113. 'item' => ($action === 'Edit') ? $this->animeCollectionModel->get($id) : []
  114. ]);
  115. }
  116. /**
  117. * Update a collection item
  118. *
  119. * @throws \Aviat\Ion\Di\ContainerException
  120. * @throws \Aviat\Ion\Di\NotFoundException
  121. * @throws \InvalidArgumentException
  122. * @return void
  123. */
  124. public function edit(): void
  125. {
  126. $data = $this->request->getParsedBody();
  127. if (array_key_exists('hummingbird_id', $data))
  128. {
  129. $this->animeCollectionModel->update($data);
  130. $this->setFlashMessage('Successfully updated collection item.', 'success');
  131. }
  132. else
  133. {
  134. $this->setFlashMessage('Failed to update collection item', 'error');
  135. }
  136. $this->sessionRedirect();
  137. }
  138. /**
  139. * Add a collection item
  140. *
  141. * @throws \Aviat\Ion\Di\ContainerException
  142. * @throws \Aviat\Ion\Di\NotFoundException
  143. * @throws \InvalidArgumentException
  144. * @return void
  145. */
  146. public function add(): void
  147. {
  148. $data = $this->request->getParsedBody();
  149. if (array_key_exists('id', $data))
  150. {
  151. $this->animeCollectionModel->add($data);
  152. $this->setFlashMessage('Successfully added collection item', 'success');
  153. }
  154. else
  155. {
  156. $this->setFlashMessage('Failed to add collection item.', 'error');
  157. }
  158. $this->sessionRedirect();
  159. }
  160. /**
  161. * Remove a collection item
  162. *
  163. * @return void
  164. */
  165. public function delete(): void
  166. {
  167. $data = $this->request->getParsedBody();
  168. if ( ! array_key_exists('hummingbird_id', $data))
  169. {
  170. $this->redirect('/anime-collection/view', 303);
  171. }
  172. $this->animeCollectionModel->delete($data);
  173. $this->setFlashMessage('Successfully removed anime from collection.', 'success');
  174. $this->redirect('/anime-collection/view', 303);
  175. }
  176. }
  177. // End of CollectionController.php