HummingBirdAnimeClient/src/Controller/AnimeCollection.php

213 lines
5.2 KiB
PHP
Raw Normal View History

2016-10-20 22:09:36 -04:00
<?php declare(strict_types=1);
/**
2017-02-15 16:13:32 -05:00
* Hummingbird Anime List Client
2015-11-16 11:40:01 -05:00
*
2018-08-22 13:48:27 -04:00
* An API client for Kitsu to manage anime and manga watch lists
2015-11-16 11:40:01 -05:00
*
2018-10-01 11:35:51 -04:00
* PHP version 7.1
2016-08-30 10:01:18 -04:00
*
2017-02-15 16:13:32 -05:00
* @package HummingbirdAnimeClient
2016-08-30 10:01:18 -04:00
* @author Timothy J. Warren <tim@timshomepage.net>
2018-01-15 14:43:15 -05:00
* @copyright 2015 - 2018 Timothy J. Warren
2016-08-30 10:01:18 -04:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2018-10-01 11:35:51 -04:00
* @version 4.1
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\Controller;
2015-09-15 13:19:29 -04:00
use Aviat\AnimeClient\Controller as BaseController;
2017-02-15 15:56:10 -05:00
use Aviat\AnimeClient\Model\{
Anime as AnimeModel,
AnimeCollection as AnimeCollectionModel
};
2016-10-20 22:32:17 -04:00
use Aviat\Ion\Di\ContainerInterface;
2015-06-26 16:39:10 -04:00
/**
* Controller for Anime collection pages
*/
final class AnimeCollection extends BaseController {
/**
* The anime collection model
2017-02-15 11:30:16 -05:00
* @var AnimeCollectionModel $animeCollectionModel
*/
2017-02-15 11:30:16 -05:00
private $animeCollectionModel;
2015-05-22 12:36:26 -04:00
/**
* The anime API model
2017-02-15 11:30:16 -05:00
* @var AnimeModel $animeModel
*/
2017-02-15 11:30:16 -05:00
private $animeModel;
/**
* Constructor
2015-09-14 19:54:34 -04:00
*
* @param ContainerInterface $container
2018-02-02 09:50:58 -05:00
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
*/
2015-09-17 23:11:18 -04:00
public function __construct(ContainerInterface $container)
2015-05-22 12:36:26 -04:00
{
parent::__construct($container);
2017-02-15 11:30:16 -05:00
$this->animeModel = $container->get('anime-model');
$this->animeCollectionModel = $container->get('anime-collection-model');
$this->baseData = array_merge($this->baseData, [
'collection_type' => 'anime',
2015-10-09 14:34:55 -04:00
'menu_name' => 'collection',
'other_type' => 'manga',
2018-12-21 15:52:34 -05:00
'url_type' => 'anime',
2015-09-14 15:49:20 -04:00
]);
2015-05-22 12:36:26 -04:00
}
/**
* Search for anime
*
2018-02-02 09:50:58 -05:00
* @throws \Aviat\Ion\Exception\DoubleRenderException
* @return void
*/
2018-11-09 10:38:35 -05:00
public function search(): void
{
$queryParams = $this->request->getQueryParams();
$query = $queryParams['query'];
2017-02-15 11:30:16 -05:00
$this->outputJSON($this->animeModel->search($query));
}
/**
* Show the anime collection page
*
2015-09-14 19:54:34 -04:00
* @param string $view
2018-02-02 09:50:58 -05:00
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @throws \InvalidArgumentException
* @return void
*/
2018-11-09 10:38:35 -05:00
public function index($view): void
2015-05-22 12:36:26 -04:00
{
2017-02-15 15:35:41 -05:00
$viewMap = [
'' => 'cover',
'list' => 'list'
];
2017-02-15 15:35:41 -05:00
$data = $this->animeCollectionModel->getCollection();
2017-02-15 15:35:41 -05:00
$this->outputHTML('collection/' . $viewMap[$view], [
2015-10-06 11:38:20 -04:00
'title' => $this->config->get('whose_list') . "'s Anime Collection",
'sections' => $data,
2017-02-15 15:35:41 -05:00
'genres' => $this->animeCollectionModel->getGenreList()
]);
}
2015-06-17 08:50:01 -04:00
/**
* Show the anime collection add/edit form
*
2015-10-12 14:27:20 -04:00
* @param integer|null $id
2018-02-02 09:50:58 -05:00
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @throws \Aura\Router\Exception\RouteNotFound
* @throws \InvalidArgumentException
* @return void
*/
2018-11-09 10:38:35 -05:00
public function form($id = NULL): void
{
$this->checkAuth();
2017-02-16 14:30:06 -05:00
$this->setSessionRedirect();
2018-02-02 09:50:58 -05:00
$action = $id === NULL ? 'Add' : 'Edit';
2017-03-30 16:16:40 -04:00
$urlAction = strtolower($action);
2017-03-30 16:16:40 -04:00
$this->outputHTML('collection/' . $urlAction, [
'action' => $action,
'action_url' => $this->url->generate("anime.collection.{$urlAction}.post"),
2017-03-24 09:58:27 -04:00
'title' => $this->formatTitle(
$this->config->get('whose_list') . "'s Anime Collection",
$action
),
'media_items' => $this->animeCollectionModel->getMediaTypeList(),
2018-02-02 09:50:58 -05:00
'item' => ($action === 'Edit') ? $this->animeCollectionModel->get($id) : []
]);
}
/**
* Update a collection item
*
2018-02-02 09:50:58 -05:00
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @throws \InvalidArgumentException
* @return void
*/
2018-12-12 15:31:59 -05:00
public function edit(): void
{
$this->checkAuth();
$data = $this->request->getParsedBody();
if (array_key_exists('hummingbird_id', $data))
{
// @TODO verify data was updated correctly
2017-02-15 11:30:16 -05:00
$this->animeCollectionModel->update($data);
$this->setFlashMessage('Successfully updated collection item.', 'success');
}
else
{
$this->setFlashMessage('Failed to update collection item', 'error');
}
2017-02-16 14:30:06 -05:00
$this->sessionRedirect();
}
/**
* Add a collection item
*
2018-02-02 09:50:58 -05:00
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @throws \InvalidArgumentException
* @return void
*/
2018-11-09 10:38:35 -05:00
public function add(): void
{
$this->checkAuth();
$data = $this->request->getParsedBody();
if (array_key_exists('id', $data))
{
// Check for existing entry
if ($this->animeCollectionModel->get($data['id']) !== FALSE)
{
$this->setFlashMessage('Anime already exists, can not create duplicate', 'info');
}
else
{
// @TODO actually verify that collection item was added
$this->animeCollectionModel->add($data);
$this->setFlashMessage('Successfully added collection item', 'success');
}
}
else
{
$this->setFlashMessage('Failed to add collection item.', 'error');
}
2017-02-16 14:30:06 -05:00
$this->sessionRedirect();
}
/**
* Remove a collection item
*
* @return void
*/
2018-11-09 10:38:35 -05:00
public function delete(): void
{
$this->checkAuth();
$data = $this->request->getParsedBody();
if ( ! array_key_exists('hummingbird_id', $data))
{
2018-02-02 09:50:58 -05:00
$this->redirect('/anime-collection/view', 303);
}
// @TODO verify that item was actually deleted
2017-02-15 11:30:16 -05:00
$this->animeCollectionModel->delete($data);
2018-02-02 09:50:58 -05:00
$this->setFlashMessage('Successfully removed anime from collection.', 'success');
2018-02-02 09:50:58 -05:00
$this->redirect('/anime-collection/view', 303);
}
2015-05-22 12:36:26 -04:00
}
// End of AnimeCollection.php