2016-10-20 22:09:36 -04:00
|
|
|
<?php declare(strict_types=1);
|
2015-06-16 11:11:35 -04:00
|
|
|
/**
|
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
|
2017-01-11 10:30:53 -05:00
|
|
|
*/namespace Aviat\AnimeClient\Model;
|
2015-09-14 10:54:50 -04:00
|
|
|
|
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;
|
2017-01-05 13:41:32 -05:00
|
|
|
use PDO;
|
2015-06-26 16:39:10 -04:00
|
|
|
|
2015-06-09 18:18:53 -04:00
|
|
|
/**
|
|
|
|
* Model for getting anime collection data
|
|
|
|
*/
|
2016-04-14 19:10:03 -04:00
|
|
|
class AnimeCollection extends Collection {
|
2015-06-16 15:54:10 -04:00
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
|
|
|
* Constructor
|
2015-09-16 12:25:35 -04:00
|
|
|
*
|
2015-10-06 10:24:48 -04:00
|
|
|
* @param ContainerInterface $container
|
2015-06-11 16:44:52 -04:00
|
|
|
*/
|
2015-09-17 23:11:18 -04:00
|
|
|
public function __construct(ContainerInterface $container)
|
2015-06-09 18:18:53 -04:00
|
|
|
{
|
2015-09-14 10:54:50 -04:00
|
|
|
parent::__construct($container);
|
2015-06-09 18:18:53 -04:00
|
|
|
|
2015-06-11 12:54:54 -04:00
|
|
|
// Do an import if an import file exists
|
|
|
|
$this->json_import();
|
2015-06-09 18:18:53 -04:00
|
|
|
}
|
|
|
|
|
2015-06-11 12:54:54 -04:00
|
|
|
/**
|
|
|
|
* Get collection from the database, and organize by media type
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2015-06-09 18:18:53 -04:00
|
|
|
public function get_collection()
|
|
|
|
{
|
2015-06-11 12:54:54 -04:00
|
|
|
$raw_collection = $this->_get_collection();
|
|
|
|
|
|
|
|
$collection = [];
|
|
|
|
|
2015-10-06 10:24:48 -04:00
|
|
|
foreach ($raw_collection 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;
|
|
|
|
}
|
|
|
|
|
2015-07-02 14:04:04 -04:00
|
|
|
/**
|
|
|
|
* Get list of media types
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_media_type_list()
|
|
|
|
{
|
2016-08-30 10:01:18 -04:00
|
|
|
$output = [];
|
2015-07-02 14:04:04 -04:00
|
|
|
|
|
|
|
$query = $this->db->select('id, type')
|
|
|
|
->from('media')
|
|
|
|
->get();
|
|
|
|
|
2017-01-05 13:41:32 -05:00
|
|
|
foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $row)
|
2015-07-02 14:04:04 -04:00
|
|
|
{
|
|
|
|
$output[$row['id']] = $row['type'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get item from collection for editing
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_collection_entry($id)
|
|
|
|
{
|
|
|
|
$query = $this->db->from('anime_set')
|
2015-10-06 10:24:48 -04:00
|
|
|
->where('hummingbird_id', (int)$id)
|
2015-07-02 14:04:04 -04:00
|
|
|
->get();
|
|
|
|
|
2017-01-05 13:41:32 -05:00
|
|
|
return $query->fetch(PDO::FETCH_ASSOC);
|
2015-07-02 14:04:04 -04:00
|
|
|
}
|
|
|
|
|
2015-06-11 12:54:54 -04:00
|
|
|
/**
|
|
|
|
* Get full collection from the database
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function _get_collection()
|
|
|
|
{
|
2015-11-05 11:26:03 -05:00
|
|
|
if ( ! $this->valid_database)
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
2015-06-16 15:54:10 -04:00
|
|
|
|
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();
|
|
|
|
|
2017-01-05 13:41:32 -05:00
|
|
|
return $query->fetchAll(PDO::FETCH_ASSOC);
|
2015-06-11 12:54:54 -04:00
|
|
|
}
|
|
|
|
|
2015-07-02 14:04:04 -04:00
|
|
|
/**
|
|
|
|
* Add an item to the anime collection
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function add($data)
|
|
|
|
{
|
2015-10-06 10:24:48 -04:00
|
|
|
$anime = (object)$this->anime_model->get_anime($data['id']);
|
2015-07-02 14:04:04 -04:00
|
|
|
|
2016-07-27 14:32:37 -04:00
|
|
|
$util = $this->container->get('util');
|
|
|
|
|
2015-07-02 14:04:04 -04:00
|
|
|
$this->db->set([
|
|
|
|
'hummingbird_id' => $data['id'],
|
|
|
|
'slug' => $anime->slug,
|
|
|
|
'title' => $anime->title,
|
|
|
|
'alternate_title' => $anime->alternate_title,
|
|
|
|
'show_type' => $anime->show_type,
|
|
|
|
'age_rating' => $anime->age_rating,
|
2015-10-12 14:27:20 -04:00
|
|
|
'cover_image' => basename(
|
2016-07-27 14:32:37 -04:00
|
|
|
$util->get_cached_image($anime->cover_image, $anime->slug, 'anime')
|
2015-10-12 14:27:20 -04:00
|
|
|
),
|
2015-07-02 14:04:04 -04:00
|
|
|
'episode_count' => $anime->episode_count,
|
|
|
|
'episode_length' => $anime->episode_length,
|
|
|
|
'media_id' => $data['media_id'],
|
|
|
|
'notes' => $data['notes']
|
|
|
|
])->insert('anime_set');
|
|
|
|
|
|
|
|
$this->update_genre($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;
|
|
|
|
}
|
2015-07-02 14:04:04 -04:00
|
|
|
|
|
|
|
$id = $data['hummingbird_id'];
|
|
|
|
unset($data['hummingbird_id']);
|
|
|
|
|
|
|
|
$this->db->set($data)
|
|
|
|
->where('hummingbird_id', $id)
|
|
|
|
->update('anime_set');
|
|
|
|
}
|
|
|
|
|
2015-12-15 15:55:30 -05:00
|
|
|
/**
|
2016-08-30 10:01:18 -04:00
|
|
|
* Remove a collection item
|
|
|
|
*
|
2015-12-15 15:55:30 -05:00
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2016-04-14 19:10:03 -04:00
|
|
|
$this->db->where('hummingbird_id', $data['hummingbird_id'])
|
|
|
|
->delete('genre_anime_set_link');
|
|
|
|
|
2015-12-15 15:55:30 -05:00
|
|
|
$this->db->where('hummingbird_id', $data['hummingbird_id'])
|
|
|
|
->delete('anime_set');
|
|
|
|
}
|
|
|
|
|
2015-07-02 14:04:04 -04:00
|
|
|
/**
|
|
|
|
* Get the details of a collection item
|
|
|
|
*
|
|
|
|
* @param int $hummingbird_id
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get($hummingbird_id)
|
|
|
|
{
|
|
|
|
$query = $this->db->from('anime_set')
|
|
|
|
->where('hummingbird_id', $hummingbird_id)
|
|
|
|
->get();
|
|
|
|
|
2017-01-05 13:41:32 -05:00
|
|
|
return $query->fetch(PDO::FETCH_ASSOC);
|
2015-07-02 14:04:04 -04:00
|
|
|
}
|
|
|
|
|
2015-06-11 12:54:54 -04:00
|
|
|
/**
|
|
|
|
* Import anime into collection from a json file
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function json_import()
|
|
|
|
{
|
2016-08-30 10:57:41 -04:00
|
|
|
if ( ! file_exists('import.json') OR ! $this->valid_database)
|
2015-11-05 11:26:03 -05:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-06-11 12:54:54 -04:00
|
|
|
|
2016-01-07 13:45:43 -05:00
|
|
|
$anime = Json::decodeFile("import.json");
|
2015-06-11 12:54:54 -04:00
|
|
|
|
2015-10-06 10:24:48 -04:00
|
|
|
foreach ($anime as $item)
|
2015-06-11 12:54:54 -04:00
|
|
|
{
|
2016-07-27 14:32:37 -04:00
|
|
|
$util = $this->container->get('util');
|
|
|
|
|
2015-06-11 12:54:54 -04:00
|
|
|
$this->db->set([
|
|
|
|
'hummingbird_id' => $item->id,
|
|
|
|
'slug' => $item->slug,
|
|
|
|
'title' => $item->title,
|
|
|
|
'alternate_title' => $item->alternate_title,
|
|
|
|
'show_type' => $item->show_type,
|
|
|
|
'age_rating' => $item->age_rating,
|
2015-10-12 14:27:20 -04:00
|
|
|
'cover_image' => basename(
|
2016-07-27 14:32:37 -04:00
|
|
|
$util->get_cached_image($item->cover_image, $item->slug, 'anime')
|
2015-10-12 14:27:20 -04:00
|
|
|
),
|
2015-06-11 12:54:54 -04:00
|
|
|
'episode_count' => $item->episode_count,
|
|
|
|
'episode_length' => $item->episode_length
|
|
|
|
])->insert('anime_set');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the import file
|
|
|
|
unlink('import.json');
|
|
|
|
|
|
|
|
// Update genre info
|
|
|
|
$this->update_genres();
|
2015-06-09 18:18:53 -04:00
|
|
|
}
|
|
|
|
|
2015-06-11 12:54:54 -04:00
|
|
|
/**
|
2015-07-02 14:04:04 -04:00
|
|
|
* Update genre information for selected anime
|
2015-06-11 12:54:54 -04:00
|
|
|
*
|
2015-10-09 22:29:59 -04:00
|
|
|
* @param int $anime_id The current anime
|
2015-06-11 12:54:54 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
2015-07-02 14:04:04 -04:00
|
|
|
private function update_genre($anime_id)
|
2015-06-11 12:54:54 -04:00
|
|
|
{
|
2015-07-02 14:04:04 -04:00
|
|
|
$genre_info = $this->get_genre_data();
|
|
|
|
extract($genre_info);
|
|
|
|
|
|
|
|
// Get api information
|
|
|
|
$anime = $this->anime_model->get_anime($anime_id);
|
2015-06-11 12:54:54 -04:00
|
|
|
|
2015-10-06 10:24:48 -04:00
|
|
|
foreach ($anime['genres'] as $genre)
|
2015-07-02 14:04:04 -04:00
|
|
|
{
|
|
|
|
// Add genres that don't currently exist
|
|
|
|
if ( ! in_array($genre['name'], $genres))
|
|
|
|
{
|
|
|
|
$this->db->set('genre', $genre['name'])
|
|
|
|
->insert('genres');
|
|
|
|
|
|
|
|
$genres[] = $genre['name'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update link table
|
|
|
|
// Get id of genre to put in link table
|
|
|
|
$flipped_genres = array_flip($genres);
|
|
|
|
|
|
|
|
$insert_array = [
|
|
|
|
'hummingbird_id' => $anime['id'],
|
|
|
|
'genre_id' => $flipped_genres[$genre['name']]
|
|
|
|
];
|
|
|
|
|
|
|
|
if (array_key_exists($anime['id'], $links))
|
|
|
|
{
|
|
|
|
if ( ! in_array($flipped_genres[$genre['name']], $links[$anime['id']]))
|
|
|
|
{
|
|
|
|
$this->db->set($insert_array)->insert('genre_anime_set_link');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->db->set($insert_array)->insert('genre_anime_set_link');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get list of existing genres
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function get_genre_data()
|
|
|
|
{
|
|
|
|
$genres = [];
|
2015-06-11 12:54:54 -04:00
|
|
|
$links = [];
|
|
|
|
|
|
|
|
// Get existing genres
|
|
|
|
$query = $this->db->select('id, genre')
|
|
|
|
->from('genres')
|
|
|
|
->get();
|
2017-01-05 13:41:32 -05:00
|
|
|
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();
|
2017-01-05 13:41:32 -05:00
|
|
|
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']];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-02 14:04:04 -04:00
|
|
|
return [
|
|
|
|
'genres' => $genres,
|
|
|
|
'links' => $links
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update genre information for the entire collection
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function update_genres()
|
|
|
|
{
|
2015-06-11 12:54:54 -04:00
|
|
|
// Get the anime collection
|
|
|
|
$collection = $this->_get_collection();
|
2015-10-06 10:24:48 -04:00
|
|
|
foreach ($collection as $anime)
|
2015-06-11 12:54:54 -04:00
|
|
|
{
|
|
|
|
// Get api information
|
2015-07-02 14:04:04 -04:00
|
|
|
$this->update_genre($anime['hummingbird_id']);
|
2015-06-11 12:54:54 -04:00
|
|
|
}
|
|
|
|
}
|
2015-06-09 18:18:53 -04:00
|
|
|
}
|
|
|
|
// End of AnimeCollectionModel.php
|