2016-10-20 22:09:36 -04:00
|
|
|
<?php declare(strict_types=1);
|
2015-06-16 11:11:35 -04:00
|
|
|
/**
|
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
|
|
|
*
|
2021-02-04 11:57:01 -05:00
|
|
|
* PHP version 8
|
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>
|
2021-01-13 01:52:03 -05:00
|
|
|
* @copyright 2015 - 2021 Timothy J. Warren
|
2016-08-30 10:01:18 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2020-12-10 17:06:50 -05:00
|
|
|
* @version 5.2
|
2017-03-07 20:53:58 -05:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
2017-01-11 10:34:24 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient\Controller;
|
2015-09-14 10:54:50 -04:00
|
|
|
|
2019-12-09 13:13:31 -05:00
|
|
|
use Aura\Router\Exception\RouteNotFound;
|
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;
|
2019-12-09 13:13:31 -05:00
|
|
|
use Aviat\Ion\Di\Exception\ContainerException;
|
|
|
|
use Aviat\Ion\Di\Exception\NotFoundException;
|
|
|
|
use Aviat\Ion\Exception\DoubleRenderException;
|
2015-06-26 16:39:10 -04:00
|
|
|
|
2019-12-09 14:34:23 -05:00
|
|
|
use InvalidArgumentException;
|
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
2015-09-14 10:54:50 -04:00
|
|
|
* Controller for Anime collection pages
|
2015-06-11 16:44:52 -04:00
|
|
|
*/
|
2018-08-08 10:12:45 -04:00
|
|
|
final class AnimeCollection extends BaseController {
|
2015-06-11 16:44:52 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The anime collection model
|
2017-02-15 11:30:16 -05:00
|
|
|
* @var AnimeCollectionModel $animeCollectionModel
|
2015-06-11 16:44:52 -04:00
|
|
|
*/
|
2020-04-10 20:01:46 -04:00
|
|
|
private AnimeCollectionModel $animeCollectionModel;
|
2015-05-22 12:36:26 -04:00
|
|
|
|
2015-12-15 15:55:30 -05:00
|
|
|
/**
|
|
|
|
* The anime API model
|
2017-02-15 11:30:16 -05:00
|
|
|
* @var AnimeModel $animeModel
|
2015-12-15 15:55:30 -05:00
|
|
|
*/
|
2020-04-10 20:01:46 -04:00
|
|
|
private AnimeModel $animeModel;
|
2015-12-15 15:55:30 -05:00
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
|
|
|
* Constructor
|
2015-09-14 19:54:34 -04:00
|
|
|
*
|
2015-10-06 10:24:48 -04:00
|
|
|
* @param ContainerInterface $container
|
2019-12-09 13:13:31 -05:00
|
|
|
* @throws ContainerException
|
|
|
|
* @throws NotFoundException
|
2015-06-11 16:44:52 -04:00
|
|
|
*/
|
2015-09-17 23:11:18 -04:00
|
|
|
public function __construct(ContainerInterface $container)
|
2015-05-22 12:36:26 -04:00
|
|
|
{
|
2015-09-14 10:54:50 -04:00
|
|
|
parent::__construct($container);
|
2015-06-16 11:21:22 -04:00
|
|
|
|
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, [
|
2017-09-14 15:32:53 -04:00
|
|
|
'collection_type' => 'anime',
|
2015-10-09 14:34:55 -04:00
|
|
|
'menu_name' => 'collection',
|
2015-06-16 11:11:35 -04:00
|
|
|
'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
|
|
|
}
|
|
|
|
|
2019-07-10 10:20:37 -04:00
|
|
|
public function index(): void
|
|
|
|
{
|
|
|
|
$this->redirect('/anime-collection/view', 303);
|
|
|
|
}
|
|
|
|
|
2015-07-02 14:04:04 -04:00
|
|
|
/**
|
|
|
|
* Search for anime
|
|
|
|
*
|
2019-12-09 13:13:31 -05:00
|
|
|
* @throws DoubleRenderException
|
2015-07-02 14:04:04 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
2018-11-09 10:38:35 -05:00
|
|
|
public function search(): void
|
2015-07-02 14:04:04 -04:00
|
|
|
{
|
2016-04-05 12:06:07 -04:00
|
|
|
$queryParams = $this->request->getQueryParams();
|
|
|
|
$query = $queryParams['query'];
|
2020-07-31 16:22:32 -04:00
|
|
|
$this->outputJSON($this->animeModel->search($query), 200);
|
2015-07-02 14:04:04 -04:00
|
|
|
}
|
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
2015-09-14 10:54:50 -04:00
|
|
|
* Show the anime collection page
|
2015-06-11 16:44:52 -04:00
|
|
|
*
|
2020-12-11 10:15:24 -05:00
|
|
|
* @param string|null $view
|
2019-12-09 13:13:31 -05:00
|
|
|
* @throws ContainerException
|
|
|
|
* @throws NotFoundException
|
2019-12-09 14:34:23 -05:00
|
|
|
* @throws InvalidArgumentException
|
2015-06-11 16:44:52 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
2020-12-11 10:15:24 -05:00
|
|
|
public function view(?string $view = ''): void
|
2015-05-22 12:36:26 -04:00
|
|
|
{
|
2017-02-15 15:35:41 -05:00
|
|
|
$viewMap = [
|
2015-06-16 11:11:35 -04:00
|
|
|
'' => 'cover',
|
|
|
|
'list' => 'list'
|
|
|
|
];
|
|
|
|
|
2020-08-26 17:26:42 -04:00
|
|
|
$sections = array_merge(
|
|
|
|
['All' => $this->animeCollectionModel->getFlatCollection()],
|
|
|
|
$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",
|
2020-08-26 17:26:42 -04:00
|
|
|
'sections' => $sections,
|
2015-06-24 16:01:35 -04:00
|
|
|
]);
|
2015-06-09 18:18:53 -04:00
|
|
|
}
|
2015-06-17 08:50:01 -04:00
|
|
|
|
2015-07-02 14:04:04 -04:00
|
|
|
/**
|
|
|
|
* Show the anime collection add/edit form
|
|
|
|
*
|
2015-10-12 14:27:20 -04:00
|
|
|
* @param integer|null $id
|
2019-12-09 13:13:31 -05:00
|
|
|
* @throws ContainerException
|
|
|
|
* @throws NotFoundException
|
|
|
|
* @throws RouteNotFound
|
2019-12-09 14:34:23 -05:00
|
|
|
* @throws InvalidArgumentException
|
2015-07-02 14:04:04 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
2018-11-09 10:38:35 -05:00
|
|
|
public function form($id = NULL): void
|
2015-07-02 14:04:04 -04:00
|
|
|
{
|
2019-01-29 15:12:31 -05:00
|
|
|
$this->checkAuth();
|
2019-01-29 16:01:31 -05:00
|
|
|
|
2017-02-16 14:30:06 -05:00
|
|
|
$this->setSessionRedirect();
|
2016-01-11 13:33:56 -05:00
|
|
|
|
2018-02-02 09:50:58 -05:00
|
|
|
$action = $id === NULL ? 'Add' : 'Edit';
|
2017-03-30 16:16:40 -04:00
|
|
|
$urlAction = strtolower($action);
|
2015-07-02 14:04:04 -04:00
|
|
|
|
2017-03-30 16:16:40 -04:00
|
|
|
$this->outputHTML('collection/' . $urlAction, [
|
2015-07-02 14:04:04 -04:00
|
|
|
'action' => $action,
|
2017-09-14 15:32:53 -04:00
|
|
|
'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
|
|
|
|
),
|
2017-02-21 15:36:34 -05:00
|
|
|
'media_items' => $this->animeCollectionModel->getMediaTypeList(),
|
2018-02-02 09:50:58 -05:00
|
|
|
'item' => ($action === 'Edit') ? $this->animeCollectionModel->get($id) : []
|
2015-07-02 14:04:04 -04:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a collection item
|
|
|
|
*
|
2019-12-09 13:13:31 -05:00
|
|
|
* @throws ContainerException
|
|
|
|
* @throws NotFoundException
|
2019-12-09 14:34:23 -05:00
|
|
|
* @throws InvalidArgumentException
|
2015-07-02 14:04:04 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
2018-12-12 15:31:59 -05:00
|
|
|
public function edit(): void
|
2015-07-02 14:04:04 -04:00
|
|
|
{
|
2019-01-29 15:12:31 -05:00
|
|
|
$this->checkAuth();
|
2020-04-23 18:57:22 -04:00
|
|
|
$this->update($this->request->getParsedBody());
|
2015-07-02 14:04:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a collection item
|
|
|
|
*
|
2019-12-09 13:13:31 -05:00
|
|
|
* @throws ContainerException
|
|
|
|
* @throws NotFoundException
|
2019-12-09 14:34:23 -05:00
|
|
|
* @throws InvalidArgumentException
|
2015-07-02 14:04:04 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
2018-11-09 10:38:35 -05:00
|
|
|
public function add(): void
|
2015-07-02 14:04:04 -04:00
|
|
|
{
|
2019-01-29 15:12:31 -05:00
|
|
|
$this->checkAuth();
|
2019-01-29 16:01:31 -05:00
|
|
|
|
2016-02-17 11:36:37 -05:00
|
|
|
$data = $this->request->getParsedBody();
|
2016-01-11 13:33:56 -05:00
|
|
|
if (array_key_exists('id', $data))
|
2015-07-02 14:04:04 -04:00
|
|
|
{
|
2019-01-28 14:31:48 -05:00
|
|
|
// Check for existing entry
|
2020-04-23 18:57:22 -04:00
|
|
|
if ($this->animeCollectionModel->has($data['id']))
|
2019-01-28 14:31:48 -05:00
|
|
|
{
|
2020-04-23 18:57:22 -04:00
|
|
|
// Let's just update with the data we have
|
|
|
|
// if the entry already exists.
|
|
|
|
$data['hummingbird_id'] = $data['id'];
|
|
|
|
unset(
|
|
|
|
$data['id'],
|
|
|
|
$data['mal_id'],
|
|
|
|
$data['search']
|
|
|
|
);
|
|
|
|
|
|
|
|
// Don't overwrite notes if the box is empty
|
|
|
|
if (trim($data['notes']) === '')
|
|
|
|
{
|
|
|
|
unset($data['notes']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->update($data);
|
2019-07-10 10:20:37 -04:00
|
|
|
return;
|
2019-01-28 14:31:48 -05:00
|
|
|
}
|
2019-07-10 10:20:37 -04:00
|
|
|
|
|
|
|
$this->animeCollectionModel->add($data);
|
|
|
|
|
|
|
|
// Verify the item was added
|
|
|
|
if ($this->animeCollectionModel->wasAdded($data))
|
2019-01-28 14:31:48 -05:00
|
|
|
{
|
|
|
|
$this->setFlashMessage('Successfully added collection item', 'success');
|
2019-07-10 10:20:37 -04:00
|
|
|
$this->sessionRedirect();
|
2020-04-23 18:57:22 -04:00
|
|
|
return;
|
2019-01-28 14:31:48 -05:00
|
|
|
}
|
2016-01-11 13:33:56 -05:00
|
|
|
}
|
2020-04-23 18:57:22 -04:00
|
|
|
|
|
|
|
$this->setFlashMessage('Failed to add collection item.', 'error');
|
|
|
|
$this->redirect('/anime-collection/add', 303);
|
2015-07-02 14:04:04 -04:00
|
|
|
}
|
2015-12-15 15:55:30 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a collection item
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-11-09 10:38:35 -05:00
|
|
|
public function delete(): void
|
2015-12-15 15:55:30 -05:00
|
|
|
{
|
2019-01-29 15:12:31 -05:00
|
|
|
$this->checkAuth();
|
2019-01-29 16:01:31 -05:00
|
|
|
|
2016-02-17 11:36:37 -05:00
|
|
|
$data = $this->request->getParsedBody();
|
2016-04-14 19:10:03 -04:00
|
|
|
if ( ! array_key_exists('hummingbird_id', $data))
|
2015-12-15 15:55:30 -05:00
|
|
|
{
|
2019-07-10 10:20:37 -04:00
|
|
|
$this->setFlashMessage("Can't delete item that doesn't exist", 'error');
|
2018-02-02 09:50:58 -05:00
|
|
|
$this->redirect('/anime-collection/view', 303);
|
2015-12-15 15:55:30 -05:00
|
|
|
}
|
|
|
|
|
2017-02-15 11:30:16 -05:00
|
|
|
$this->animeCollectionModel->delete($data);
|
2019-07-10 10:20:37 -04:00
|
|
|
|
|
|
|
// Verify that item was actually deleted
|
2020-04-23 18:57:22 -04:00
|
|
|
($this->animeCollectionModel->wasDeleted($data))
|
|
|
|
? $this->setFlashMessage('Successfully removed anime from collection.', 'success')
|
|
|
|
: $this->setFlashMessage('Failed to delete item from collection.', 'error');
|
|
|
|
|
|
|
|
$this->redirect('/anime-collection/view', 303);
|
|
|
|
}
|
|
|
|
|
2020-04-30 15:33:16 -04:00
|
|
|
/**
|
|
|
|
* Update a collection item
|
|
|
|
*
|
|
|
|
* @param $data
|
|
|
|
* @throws ContainerException
|
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
2020-04-23 18:57:22 -04:00
|
|
|
protected function update($data): void
|
|
|
|
{
|
|
|
|
if (array_key_exists('hummingbird_id', $data))
|
2019-07-10 10:20:37 -04:00
|
|
|
{
|
2020-04-23 18:57:22 -04:00
|
|
|
$this->animeCollectionModel->update($data);
|
|
|
|
|
|
|
|
// Verify the item was actually updated
|
|
|
|
($this->animeCollectionModel->wasUpdated($data))
|
|
|
|
? $this->setFlashMessage('Successfully updated collection item.', 'success')
|
|
|
|
: $this->setFlashMessage('Failed to update collection item.', 'error');
|
2019-07-10 10:20:37 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-23 18:57:22 -04:00
|
|
|
$this->setFlashMessage('No item id to update. Update failed.', 'error');
|
2019-07-10 10:20:37 -04:00
|
|
|
}
|
2015-12-15 15:55:30 -05:00
|
|
|
|
2020-04-23 18:57:22 -04:00
|
|
|
$this->sessionRedirect();
|
2015-12-15 15:55:30 -05:00
|
|
|
}
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
2019-01-28 14:31:48 -05:00
|
|
|
// End of AnimeCollection.php
|