2017-09-14 16:18:13 -04:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* Hummingbird Anime List Client
|
|
|
|
*
|
2018-08-22 13:48:27 -04:00
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
2017-09-14 16:18:13 -04:00
|
|
|
*
|
2019-12-03 15:17:25 -05:00
|
|
|
* PHP version 7.2
|
2017-09-14 16:18:13 -04:00
|
|
|
*
|
|
|
|
* @package HummingbirdAnimeClient
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2019-12-06 09:16:35 -05:00
|
|
|
* @copyright 2015 - 2019 Timothy J. Warren
|
2017-09-14 16:18:13 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2019-12-06 09:16:35 -05:00
|
|
|
* @version 4.2
|
2017-09-14 16:18:13 -04:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient\Model;
|
|
|
|
|
|
|
|
use Aviat\Ion\Di\ContainerInterface;
|
|
|
|
use PDO;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Model for getting anime collection data
|
|
|
|
*/
|
2018-08-08 10:12:45 -04:00
|
|
|
final class MangaCollection extends Collection {
|
2017-09-14 16:18:13 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Manga API Model
|
2018-01-16 14:58:07 -05:00
|
|
|
* @var Manga $mangaModel
|
2017-09-14 16:18:13 -04:00
|
|
|
*/
|
|
|
|
protected $mangaModel;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the collection model
|
|
|
|
*
|
|
|
|
* @param ContainerInterface $container
|
|
|
|
*/
|
|
|
|
public function __construct(ContainerInterface $container)
|
|
|
|
{
|
|
|
|
parent::__construct($container);
|
|
|
|
$this->mangaModel = $container->get('manga-model');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get collection from the database, and organize by media type
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-01-18 16:21:45 -05:00
|
|
|
public function getCollection(): array
|
2017-09-14 16:18:13 -04:00
|
|
|
{
|
|
|
|
$rawCollection = $this->getCollectionFromDatabase();
|
|
|
|
|
|
|
|
$collection = [];
|
|
|
|
|
|
|
|
foreach ($rawCollection as $row)
|
|
|
|
{
|
|
|
|
if (array_key_exists($row['media'], $collection))
|
|
|
|
{
|
|
|
|
$collection[$row['media']][] = $row;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$collection[$row['media']] = [$row];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $collection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get list of media types
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-01-18 16:21:45 -05:00
|
|
|
public function getMediaTypeList(): array
|
2017-09-14 16:18:13 -04:00
|
|
|
{
|
|
|
|
$output = [];
|
|
|
|
|
|
|
|
$query = $this->db->select('id, type')
|
|
|
|
->from('media')
|
|
|
|
->get();
|
|
|
|
|
|
|
|
foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $row)
|
|
|
|
{
|
|
|
|
$output[$row['id']] = $row['type'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get full collection from the database
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-01-18 16:21:45 -05:00
|
|
|
private function getCollectionFromDatabase(): array
|
2017-09-14 16:18:13 -04:00
|
|
|
{
|
|
|
|
if ( ! $this->validDatabase)
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = $this->db->select('hummingbird_id, slug, title, alternate_title, show_type,
|
|
|
|
age_rating, episode_count, episode_length, cover_image, notes, media.type as media')
|
|
|
|
->from('manga_set a')
|
|
|
|
->join('media', 'media.id=a.media_id', 'inner')
|
|
|
|
->order_by('media')
|
|
|
|
->order_by('title')
|
|
|
|
->get();
|
|
|
|
|
|
|
|
return $query->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an item to the anime collection
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-01-18 16:21:45 -05:00
|
|
|
public function add($data): void
|
2017-09-14 16:18:13 -04:00
|
|
|
{
|
|
|
|
$anime = (object)$this->mangaModel->getMangaById($data['id']);
|
|
|
|
$this->db->set([
|
|
|
|
'hummingbird_id' => $data['id'],
|
|
|
|
'slug' => $anime->slug,
|
|
|
|
'title' => array_shift($anime->titles),
|
|
|
|
'alternate_title' => implode('<br />', $anime->titles),
|
|
|
|
'show_type' => $anime->show_type,
|
|
|
|
'age_rating' => $anime->age_rating,
|
|
|
|
'cover_image' => $anime->cover_image,
|
|
|
|
'episode_count' => $anime->episode_count,
|
|
|
|
'episode_length' => $anime->episode_length,
|
|
|
|
'media_id' => $data['media_id'],
|
|
|
|
'notes' => $data['notes']
|
|
|
|
])->insert('manga_set');
|
|
|
|
|
|
|
|
$this->updateGenre($data['id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a collection item
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-01-18 16:21:45 -05:00
|
|
|
public function update($data): void
|
2017-09-14 16:18:13 -04:00
|
|
|
{
|
|
|
|
// If there's no id to update, don't update
|
|
|
|
if ( ! array_key_exists('hummingbird_id', $data))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$id = $data['hummingbird_id'];
|
|
|
|
unset($data['hummingbird_id']);
|
|
|
|
|
|
|
|
$this->db->set($data)
|
|
|
|
->where('hummingbird_id', $id)
|
|
|
|
->update('manga_set');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a collection item
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-01-18 16:21:45 -05:00
|
|
|
public function delete($data): void
|
2017-09-14 16:18:13 -04:00
|
|
|
{
|
|
|
|
// If there's no id to update, don't delete
|
|
|
|
if ( ! array_key_exists('hummingbird_id', $data))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->db->where('hummingbird_id', $data['hummingbird_id'])
|
|
|
|
->delete('genre_manga_set_link');
|
|
|
|
|
|
|
|
$this->db->where('hummingbird_id', $data['hummingbird_id'])
|
|
|
|
->delete('manga_set');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the details of a collection item
|
|
|
|
*
|
2018-01-18 16:21:45 -05:00
|
|
|
* @param string $kitsuId
|
2017-09-14 16:18:13 -04:00
|
|
|
* @return array
|
|
|
|
*/
|
2018-01-18 16:21:45 -05:00
|
|
|
public function get($kitsuId): array
|
2017-09-14 16:18:13 -04:00
|
|
|
{
|
|
|
|
$query = $this->db->from('manga_set')
|
|
|
|
->where('hummingbird_id', $kitsuId)
|
|
|
|
->get();
|
|
|
|
|
|
|
|
return $query->fetch(PDO::FETCH_ASSOC);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update genre information for selected manga
|
|
|
|
*
|
2018-01-18 16:21:45 -05:00
|
|
|
* @param string $mangaId The current manga
|
2017-09-14 16:18:13 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
2018-01-18 16:21:45 -05:00
|
|
|
private function updateGenre($mangaId): void
|
2017-09-14 16:18:13 -04:00
|
|
|
{
|
|
|
|
$genreInfo = $this->getGenreData();
|
2018-01-18 16:21:45 -05:00
|
|
|
$genres = $genreInfo['genres'];
|
|
|
|
$links = $genreInfo['links'];
|
2017-09-14 16:18:13 -04:00
|
|
|
|
|
|
|
// Get api information
|
|
|
|
$manga = $this->mangaModel->getMangaById($mangaId);
|
|
|
|
|
2018-01-16 14:58:07 -05:00
|
|
|
foreach ($manga['genres'] as $genre)
|
2017-09-14 16:18:13 -04:00
|
|
|
{
|
|
|
|
// Add genres that don't currently exist
|
2018-01-18 16:21:45 -05:00
|
|
|
if ( ! \in_array($genre, $genres, TRUE))
|
2017-09-14 16:18:13 -04:00
|
|
|
{
|
|
|
|
$this->db->set('genre', $genre)
|
|
|
|
->insert('genres');
|
|
|
|
|
|
|
|
$genres[] = $genre;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update link table
|
|
|
|
// Get id of genre to put in link table
|
|
|
|
$flippedGenres = array_flip($genres);
|
|
|
|
|
|
|
|
$insertArray = [
|
|
|
|
'hummingbird_id' => $mangaId,
|
|
|
|
'genre_id' => $flippedGenres[$genre]
|
|
|
|
];
|
|
|
|
|
|
|
|
if (array_key_exists($mangaId, $links))
|
|
|
|
{
|
2018-01-18 16:21:45 -05:00
|
|
|
if ( ! \in_array($flippedGenres[$genre], $links[$mangaId], TRUE))
|
2017-09-14 16:18:13 -04:00
|
|
|
{
|
|
|
|
$this->db->set($insertArray)->insert('genre_manga_set_link');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->db->set($insertArray)->insert('genre_manga_set_link');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get list of existing genres
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-01-18 16:21:45 -05:00
|
|
|
private function getGenreData(): array
|
2017-09-14 16:18:13 -04:00
|
|
|
{
|
|
|
|
$genres = [];
|
|
|
|
$links = [];
|
|
|
|
|
|
|
|
// Get existing genres
|
|
|
|
$query = $this->db->select('id, genre')
|
|
|
|
->from('genres')
|
|
|
|
->get();
|
|
|
|
foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $genre)
|
|
|
|
{
|
|
|
|
$genres[$genre['id']] = $genre['genre'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get existing link table entries
|
|
|
|
$query = $this->db->select('hummingbird_id, genre_id')
|
|
|
|
->from('genre_manga_set_link')
|
|
|
|
->get();
|
|
|
|
foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $link)
|
|
|
|
{
|
|
|
|
if (array_key_exists($link['hummingbird_id'], $links))
|
|
|
|
{
|
|
|
|
$links[$link['hummingbird_id']][] = $link['genre_id'];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$links[$link['hummingbird_id']] = [$link['genre_id']];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'genres' => $genres,
|
|
|
|
'links' => $links
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// End of MangaCollectionModel.php
|