HummingBirdAnimeClient/src/Model/AnimeCollection.php

304 lines
6.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
*
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
*
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>
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
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\Model;
2017-01-16 13:49:51 -05:00
use Aviat\AnimeClient\API\Kitsu;
2015-09-17 23:11:18 -04:00
use Aviat\Ion\Di\ContainerInterface;
2016-10-20 22:32:17 -04:00
use Aviat\Ion\Json;
use PDO;
2015-06-26 16:39:10 -04:00
/**
* Model for getting anime collection data
*/
class AnimeCollection extends Collection {
/**
* Anime API Model
* @var object $animeModel
*/
protected $animeModel;
/**
* Create the collection model
*
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container)
{
parent::__construct($container);
$this->animeModel = $container->get('anime-model');
}
2015-06-11 12:54:54 -04:00
/**
* Get collection from the database, and organize by media type
*
* @return array
*/
2017-02-15 15:35:41 -05:00
public function getCollection()
{
2017-02-15 16:30:14 -05:00
$rawCollection = $this->getCollectionFromDatabase();
2015-06-11 12:54:54 -04:00
$collection = [];
2017-02-15 16:30:14 -05:00
foreach ($rawCollection as $row)
2015-06-11 12:54:54 -04:00
{
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
*/
2017-02-15 15:35:41 -05:00
public function getMediaTypeList()
{
2016-08-30 10:01:18 -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 item from collection for editing
*
* @param int $id
* @return array
*/
2017-02-15 15:35:41 -05:00
public function getCollectionEntry($id)
{
$query = $this->db->from('anime_set')
->where('hummingbird_id', (int)$id)
->get();
return $query->fetch(PDO::FETCH_ASSOC);
}
2015-06-11 12:54:54 -04:00
/**
* Get full collection from the database
*
* @return array
*/
2017-02-15 16:30:14 -05:00
private function getCollectionFromDatabase()
2015-06-11 12:54:54 -04:00
{
2017-02-15 15:35:41 -05:00
if ( ! $this->validDatabase)
2015-11-05 11:26:03 -05:00
{
return [];
}
2015-10-12 14:27:20 -04:00
$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')
2015-06-11 12:54:54 -04:00
->from('anime_set a')
->join('media', 'media.id=a.media_id', 'inner')
->order_by('media')
->order_by('title')
->get();
return $query->fetchAll(PDO::FETCH_ASSOC);
2015-06-11 12:54:54 -04:00
}
/**
* Add an item to the anime collection
*
* @param array $data
* @return void
*/
public function add($data)
{
2017-02-15 15:35:41 -05:00
$anime = (object)$this->animeModel->getAnimeById($data['id']);
$this->db->set([
'hummingbird_id' => $data['id'],
'slug' => $anime->slug,
'title' => array_shift($anime->titles),
2017-01-16 13:49:51 -05:00
'alternate_title' => implode('<br />', $anime->titles),
'show_type' => $anime->show_type,
'age_rating' => $anime->age_rating,
2017-01-16 13:49:51 -05:00
'cover_image' => $anime->cover_image,
'episode_count' => $anime->episode_count,
'episode_length' => $anime->episode_length,
'media_id' => $data['media_id'],
'notes' => $data['notes']
])->insert('anime_set');
2017-02-15 15:35:41 -05:00
$this->updateGenre($data['id']);
}
/**
* Update a collection item
*
* @param array $data
* @return void
*/
public function update($data)
{
// If there's no id to update, don't update
2015-11-05 11:26:03 -05:00
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('anime_set');
}
/**
2016-08-30 10:01:18 -04:00
* Remove a collection item
*
* @param array $data
* @return void
*/
public function delete($data)
{
// 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_anime_set_link');
$this->db->where('hummingbird_id', $data['hummingbird_id'])
->delete('anime_set');
}
/**
* Get the details of a collection item
*
2017-02-15 16:30:14 -05:00
* @param int $kitsuId
* @return array
*/
2017-02-15 16:30:14 -05:00
public function get($kitsuId)
{
$query = $this->db->from('anime_set')
2017-02-15 16:30:14 -05:00
->where('hummingbird_id', $kitsuId)
->get();
return $query->fetch(PDO::FETCH_ASSOC);
}
2015-06-11 12:54:54 -04:00
/**
* Update genre information for selected anime
2015-06-11 12:54:54 -04:00
*
2017-02-15 16:30:14 -05:00
* @param int $animeId The current anime
2015-06-11 12:54:54 -04:00
* @return void
*/
2017-02-15 16:30:14 -05:00
private function updateGenre($animeId)
2015-06-11 12:54:54 -04:00
{
2017-02-15 16:30:14 -05:00
$genreInfo = $this->getGenreData();
extract($genreInfo);
// Get api information
2017-02-15 16:30:14 -05:00
$anime = $this->animeModel->getAnimeById($animeId);
2015-06-11 12:54:54 -04:00
foreach ($anime['genres'] as $genre)
{
// Add genres that don't currently exist
2017-01-16 13:49:51 -05:00
if ( ! in_array($genre, $genres))
{
2017-01-16 13:49:51 -05:00
$this->db->set('genre', $genre)
->insert('genres');
2017-01-16 13:49:51 -05:00
$genres[] = $genre;
}
// Update link table
// Get id of genre to put in link table
2017-02-15 16:30:14 -05:00
$flippedGenres = array_flip($genres);
2017-02-15 16:30:14 -05:00
$insertArray = [
'hummingbird_id' => $animeId,
'genre_id' => $flippedGenres[$genre]
];
2017-02-15 16:30:14 -05:00
if (array_key_exists($animeId, $links))
{
2017-02-15 16:30:14 -05:00
if ( ! in_array($flippedGenres[$genre], $links[$animeId]))
{
2017-02-15 16:30:14 -05:00
$this->db->set($insertArray)->insert('genre_anime_set_link');
}
}
else
{
2017-02-15 16:30:14 -05:00
$this->db->set($insertArray)->insert('genre_anime_set_link');
}
}
}
/**
* Get list of existing genres
*
* @return array
*/
2017-02-15 15:35:41 -05:00
private function getGenreData()
{
$genres = [];
2015-06-11 12:54:54 -04:00
$links = [];
// Get existing genres
$query = $this->db->select('id, genre')
->from('genres')
->get();
foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $genre)
2015-06-11 12:54:54 -04:00
{
$genres[$genre['id']] = $genre['genre'];
}
// Get existing link table entries
$query = $this->db->select('hummingbird_id, genre_id')
->from('genre_anime_set_link')
->get();
foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $link)
2015-06-11 12:54:54 -04:00
{
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 AnimeCollectionModel.php