HummingBirdAnimeClient/src/Controller/Collection.php

192 lines
4.3 KiB
PHP
Raw Normal View History

2016-10-20 22:09:36 -04:00
<?php declare(strict_types=1);
/**
2016-12-20 12:58:37 -05:00
* Anime List Client
2015-11-16 11:40:01 -05:00
*
2016-12-20 12:58:37 -05:00
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
2015-11-16 11:40:01 -05:00
*
2016-10-20 22:09:36 -04:00
* PHP version 7
2016-08-30 10:01:18 -04:00
*
2016-12-20 12:58:37 -05:00
* @package AnimeListClient
2016-08-30 10:01:18 -04:00
* @author Timothy J. Warren <tim@timshomepage.net>
2017-01-11 10:30:53 -05:00
* @copyright 2015 - 2017 Timothy J. Warren
2016-08-30 10:01:18 -04:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2016-12-20 12:58:37 -05:00
* @version 4.0
2015-11-16 11:40:01 -05:00
* @link https://github.com/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\AnimeClient\UrlGenerator;
use Aviat\Ion\Di\ContainerInterface;
2015-06-26 16:39:10 -04:00
/**
* Controller for Anime collection pages
*/
class Collection 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;
/**
2017-02-15 11:30:16 -05:00
* Data to be sent to all routes in this controller
* @var array $baseData
*/
2017-02-15 11:30:16 -05:00
protected $baseData;
/**
* Url Generator class
* @var UrlGenerator
*/
protected $urlGenerator;
/**
* Constructor
2015-09-14 19:54:34 -04:00
*
* @param ContainerInterface $container
*/
2015-09-17 23:11:18 -04:00
public function __construct(ContainerInterface $container)
2015-05-22 12:36:26 -04:00
{
parent::__construct($container);
$this->urlGenerator = $container->get('url-generator');
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, [
2015-10-09 14:34:55 -04:00
'menu_name' => 'collection',
'url_type' => 'anime',
'other_type' => 'manga',
'config' => $this->config,
2015-09-14 15:49:20 -04:00
]);
2015-05-22 12:36:26 -04:00
}
/**
* Search for anime
*
* @return void
*/
public function search()
{
$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
* @return void
*/
public function index($view)
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
* @return void
*/
public function form($id = NULL)
{
$this->set_session_redirect();
$action = (is_null($id)) ? "Add" : "Edit";
$this->outputHTML('collection/' . strtolower($action), [
'action' => $action,
2017-02-15 15:56:10 -05:00
'action_url' => $this->urlGenerator->fullUrl('collection/' . strtolower($action)),
2015-12-08 14:52:59 -05:00
'title' => $this->config->get('whose_list') . " Anime Collection &middot; {$action}",
2017-02-15 11:30:16 -05:00
'media_items' => $this->animeCollectionModel->get_media_type_list(),
'item' => ($action === "Edit") ? $this->animeCollectionModel->get($id) : []
]);
}
/**
* Update a collection item
*
* @return void
*/
public function edit()
{
$data = $this->request->getParsedBody();
if (array_key_exists('hummingbird_id', $data))
{
2017-02-15 11:30:16 -05:00
$this->animeCollectionModel->update($data);
$this->set_flash_message('Successfully updated collection item.', 'success');
}
else
{
$this->set_flash_message('Failed to update collection item', 'error');
}
$this->session_redirect();
}
/**
* Add a collection item
*
* @return void
*/
public function add()
{
$data = $this->request->getParsedBody();
if (array_key_exists('id', $data))
{
2017-02-15 11:30:16 -05:00
$this->animeCollectionModel->add($data);
$this->set_flash_message('Successfully added collection item', 'success');
}
else
{
$this->set_flash_message('Failed to add collection item.', 'error');
}
$this->session_redirect();
}
/**
* Remove a collection item
*
* @return void
*/
public function delete()
{
$data = $this->request->getParsedBody();
if ( ! array_key_exists('hummingbird_id', $data))
{
$this->redirect("/collection/view", 303);
}
2017-02-15 11:30:16 -05:00
$this->animeCollectionModel->delete($data);
$this->set_flash_message("Successfully removed anime from collection.", 'success');
$this->redirect("/collection/view", 303);
}
2015-05-22 12:36:26 -04:00
}
// End of CollectionController.php