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
|
|
|
*
|
2020-04-10 15:39:39 -04:00
|
|
|
* PHP version 7.4
|
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>
|
2020-01-08 15:39:49 -05:00
|
|
|
* @copyright 2015 - 2020 Timothy J. Warren
|
2016-08-30 10:01:18 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2020-04-10 15:39:39 -04:00
|
|
|
* @version 5
|
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\Model;
|
2015-09-14 10:54:50 -04:00
|
|
|
|
2015-09-17 23:11:18 -04:00
|
|
|
use Aviat\Ion\Di\ContainerInterface;
|
2017-01-05 13:41:32 -05:00
|
|
|
use PDO;
|
2019-01-08 15:52:53 -05:00
|
|
|
use PDOException;
|
2020-04-23 18:57:22 -04:00
|
|
|
use function in_array;
|
2015-06-26 16:39:10 -04:00
|
|
|
|
2015-06-09 18:18:53 -04:00
|
|
|
/**
|
|
|
|
* Model for getting anime collection data
|
|
|
|
*/
|
2018-08-08 10:12:45 -04:00
|
|
|
final class AnimeCollection extends Collection {
|
2015-06-16 15:54:10 -04:00
|
|
|
|
2017-09-14 16:18:13 -04:00
|
|
|
/**
|
|
|
|
* Anime API Model
|
2018-01-16 14:58:07 -05:00
|
|
|
* @var Anime $animeModel
|
2017-09-14 16:18:13 -04:00
|
|
|
*/
|
2020-04-10 20:01:46 -04:00
|
|
|
protected Anime $animeModel;
|
2017-09-14 16:18:13 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2018-01-18 16:21:45 -05:00
|
|
|
public function getCollection(): array
|
2015-06-09 18:18:53 -04:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2015-07-02 14:04:04 -04:00
|
|
|
/**
|
|
|
|
* Get list of media types
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-01-18 16:21:45 -05:00
|
|
|
public function getMediaTypeList(): array
|
2015-07-02 14:04:04 -04:00
|
|
|
{
|
2019-12-09 16:17:25 -05:00
|
|
|
if ($this->validDatabase === FALSE)
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-04-23 18:57:22 -04:00
|
|
|
$flatList = [];
|
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
|
|
|
{
|
2020-04-23 18:57:22 -04:00
|
|
|
$flatList[$row['id']] = $row['type'];
|
2018-08-21 17:09:42 -04:00
|
|
|
}
|
|
|
|
|
2020-04-23 18:57:22 -04:00
|
|
|
// Organize the media types into groups
|
|
|
|
return [
|
|
|
|
'Common' => [
|
|
|
|
2 => $flatList[2], // Blu-ray
|
|
|
|
3 => $flatList[3], // DVD
|
|
|
|
7 => $flatList[7], // Digital
|
|
|
|
4 => $flatList[4], // Bootleg
|
|
|
|
],
|
|
|
|
'Retro' => [
|
|
|
|
5 => $flatList[5], // LaserDisc
|
|
|
|
6 => $flatList[6], // VHS
|
|
|
|
9 => $flatList[9], // Betamax
|
|
|
|
8 => $flatList[8], // Video CD
|
|
|
|
],
|
|
|
|
'Other' => [
|
|
|
|
10 => $flatList[10], // UMD
|
|
|
|
11 => $flatList[11], // Other
|
|
|
|
]
|
|
|
|
];
|
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
|
|
|
|
*/
|
2018-01-18 16:21:45 -05:00
|
|
|
public function add($data): void
|
2015-07-02 14:04:04 -04:00
|
|
|
{
|
2019-12-09 16:17:25 -05:00
|
|
|
if ($this->validDatabase === FALSE)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-22 10:21:58 -05:00
|
|
|
// Check that the anime doesn't already exist
|
2020-04-23 18:57:22 -04:00
|
|
|
if ($this->has($data['id']))
|
2019-01-22 10:21:58 -05:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-23 18:57:22 -04:00
|
|
|
$id = $data['id'];
|
2019-01-22 10:21:58 -05:00
|
|
|
$anime = (object)$this->animeModel->getAnimeById($id);
|
2020-04-23 20:03:55 -04:00
|
|
|
|
2015-07-02 14:04:04 -04:00
|
|
|
$this->db->set([
|
2020-04-23 18:57:22 -04:00
|
|
|
'hummingbird_id' => $id,
|
2015-07-02 14:04:04 -04:00
|
|
|
'slug' => $anime->slug,
|
2017-01-16 14:03:30 -05:00
|
|
|
'title' => array_shift($anime->titles),
|
2017-01-16 13:49:51 -05:00
|
|
|
'alternate_title' => implode('<br />', $anime->titles),
|
2015-07-02 14:04:04 -04:00
|
|
|
'show_type' => $anime->show_type,
|
|
|
|
'age_rating' => $anime->age_rating,
|
2017-01-16 13:49:51 -05:00
|
|
|
'cover_image' => $anime->cover_image,
|
2015-07-02 14:04:04 -04:00
|
|
|
'episode_count' => $anime->episode_count,
|
|
|
|
'episode_length' => $anime->episode_length,
|
|
|
|
'notes' => $data['notes']
|
|
|
|
])->insert('anime_set');
|
|
|
|
|
2020-04-23 18:57:22 -04:00
|
|
|
$this->updateMediaLink($id, $data['media_id']);
|
|
|
|
$this->updateGenres($id);
|
2019-07-10 10:20:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verify that an item was added
|
|
|
|
*
|
2019-12-09 14:41:04 -05:00
|
|
|
* @param array|null|object $data
|
2019-07-10 10:20:37 -04:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function wasAdded($data): bool
|
|
|
|
{
|
2019-12-09 16:17:25 -05:00
|
|
|
if ($this->validDatabase === FALSE)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:20:37 -04:00
|
|
|
$row = $this->get($data['id']);
|
|
|
|
|
|
|
|
return ! empty($row);
|
2015-07-02 14:04:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a collection item
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-01-18 16:21:45 -05:00
|
|
|
public function update($data): void
|
2015-07-02 14:04:04 -04:00
|
|
|
{
|
2019-12-09 16:17:25 -05:00
|
|
|
if ($this->validDatabase === FALSE)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-02 14:04:04 -04:00
|
|
|
// 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'];
|
2020-04-23 18:57:22 -04:00
|
|
|
$media = $data['media_id'];
|
|
|
|
unset($data['hummingbird_id'], $data['media_id']);
|
2015-07-02 14:04:04 -04:00
|
|
|
|
2020-04-23 18:57:22 -04:00
|
|
|
// If updating from the 'add' page, there
|
|
|
|
// might be no data to actually update in
|
|
|
|
// the anime_set table
|
|
|
|
if ( ! empty($data))
|
|
|
|
{
|
|
|
|
$this->db->set($data)
|
|
|
|
->where('hummingbird_id', $id)
|
|
|
|
->update('anime_set');
|
|
|
|
}
|
2019-07-10 10:20:37 -04:00
|
|
|
|
2020-04-23 18:57:22 -04:00
|
|
|
// Update media and genres
|
|
|
|
$this->updateMediaLink($id, $media);
|
|
|
|
$this->updateGenres($id);
|
2019-07-10 10:20:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verify that the collection item was updated
|
|
|
|
*
|
2019-12-09 14:41:04 -05:00
|
|
|
* @param array|null|object $data
|
|
|
|
*
|
2019-07-10 10:20:37 -04:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function wasUpdated($data): bool
|
|
|
|
{
|
2019-12-09 16:17:25 -05:00
|
|
|
if ($this->validDatabase === FALSE)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:20:37 -04:00
|
|
|
$row = $this->get($data['hummingbird_id']);
|
|
|
|
|
|
|
|
foreach ($data as $key => $value)
|
|
|
|
{
|
2020-04-23 18:57:22 -04:00
|
|
|
if (is_array($row[$key]))
|
|
|
|
{
|
2020-04-28 12:13:35 -04:00
|
|
|
continue;
|
2020-04-23 18:57:22 -04:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:20:37 -04:00
|
|
|
if ((string)$row[$key] !== (string)$value)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
2015-07-02 14:04:04 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2018-01-18 16:21:45 -05:00
|
|
|
public function delete($data): void
|
2015-12-15 15:55:30 -05:00
|
|
|
{
|
2019-12-09 16:17:25 -05:00
|
|
|
if ($this->validDatabase === FALSE)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-15 15:55:30 -05:00
|
|
|
// If there's no id to update, don't delete
|
|
|
|
if ( ! array_key_exists('hummingbird_id', $data))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-23 20:03:55 -04:00
|
|
|
$this->db->beginTransaction();
|
|
|
|
|
2016-04-14 19:10:03 -04:00
|
|
|
$this->db->where('hummingbird_id', $data['hummingbird_id'])
|
|
|
|
->delete('genre_anime_set_link');
|
|
|
|
|
2020-04-23 20:03:55 -04:00
|
|
|
$this->db->where('hummingbird_id', $data['hummingbird_id'])
|
|
|
|
->delete('anime_set_media_link');
|
|
|
|
|
2015-12-15 15:55:30 -05:00
|
|
|
$this->db->where('hummingbird_id', $data['hummingbird_id'])
|
|
|
|
->delete('anime_set');
|
2020-04-23 20:03:55 -04:00
|
|
|
|
|
|
|
$this->db->commit();
|
2015-12-15 15:55:30 -05:00
|
|
|
}
|
|
|
|
|
2019-12-09 14:41:04 -05:00
|
|
|
/**
|
|
|
|
* @param array|null|object $data
|
2019-12-09 16:17:25 -05:00
|
|
|
* @return bool
|
2019-12-09 14:41:04 -05:00
|
|
|
*/
|
2019-07-10 10:20:37 -04:00
|
|
|
public function wasDeleted($data): bool
|
|
|
|
{
|
2019-12-09 16:17:25 -05:00
|
|
|
if ($this->validDatabase === FALSE)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-04-23 18:57:22 -04:00
|
|
|
return $this->has($data['hummingbird_id']) === FALSE;
|
2019-07-10 10:20:37 -04:00
|
|
|
}
|
|
|
|
|
2015-07-02 14:04:04 -04:00
|
|
|
/**
|
|
|
|
* Get the details of a collection item
|
|
|
|
*
|
2020-04-23 18:57:22 -04:00
|
|
|
* @param int|string $kitsuId
|
|
|
|
* @return array
|
2015-07-02 14:04:04 -04:00
|
|
|
*/
|
2020-04-23 18:57:22 -04:00
|
|
|
public function get($kitsuId): array
|
|
|
|
{
|
|
|
|
if ($this->validDatabase === FALSE)
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the main row data
|
|
|
|
$row = $this->db->from('anime_set')
|
|
|
|
->where('hummingbird_id', $kitsuId)
|
|
|
|
->get()
|
|
|
|
->fetch(PDO::FETCH_ASSOC);
|
|
|
|
|
|
|
|
// Get the media ids
|
|
|
|
$mediaRows = $this->db->select('media_id')
|
|
|
|
->from('anime_set_media_link')
|
|
|
|
->where('hummingbird_id', $kitsuId)
|
|
|
|
->get()
|
|
|
|
->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
|
|
|
|
$row['media_id'] = array_column($mediaRows, 'media_id');
|
|
|
|
|
|
|
|
return $row;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does this anime already exist in the collection?
|
|
|
|
*
|
|
|
|
* @param int|string $kitsuId
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function has($kitsuId): bool
|
2015-07-02 14:04:04 -04:00
|
|
|
{
|
2019-12-09 16:17:25 -05:00
|
|
|
if ($this->validDatabase === FALSE)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-04-23 18:57:22 -04:00
|
|
|
$row = $this->db->select('hummingbird_id')
|
|
|
|
->from('anime_set')
|
2017-02-15 16:30:14 -05:00
|
|
|
->where('hummingbird_id', $kitsuId)
|
2020-04-23 18:57:22 -04:00
|
|
|
->get()
|
|
|
|
->fetch(PDO::FETCH_ASSOC);
|
2015-07-02 14:04:04 -04:00
|
|
|
|
2020-04-23 18:57:22 -04:00
|
|
|
return ! empty($row);
|
2015-07-02 14:04:04 -04:00
|
|
|
}
|
|
|
|
|
2019-01-08 15:52:53 -05:00
|
|
|
/**
|
|
|
|
* Get genres for anime collection items
|
|
|
|
*
|
|
|
|
* @param array $filter
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getGenreList(array $filter = []): array
|
|
|
|
{
|
2019-12-09 16:17:25 -05:00
|
|
|
if ($this->validDatabase === FALSE)
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-01-08 15:52:53 -05:00
|
|
|
$output = [];
|
|
|
|
|
|
|
|
// Catch the missing table PDOException
|
|
|
|
// so that the collection does not show an
|
|
|
|
// error by default
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$this->db->select('hummingbird_id, genre')
|
|
|
|
->from('genre_anime_set_link gl')
|
|
|
|
->join('genres g', 'g.id=gl.genre_id', 'left');
|
|
|
|
|
|
|
|
|
|
|
|
if ( ! empty($filter))
|
|
|
|
{
|
|
|
|
$this->db->whereIn('hummingbird_id', $filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = $this->db->orderBy('hummingbird_id')
|
|
|
|
->orderBy('genre')
|
|
|
|
->get();
|
|
|
|
|
|
|
|
foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $row)
|
|
|
|
{
|
|
|
|
$id = $row['hummingbird_id'];
|
|
|
|
$genre = $row['genre'];
|
|
|
|
|
|
|
|
// Empty genre names aren't useful
|
|
|
|
if (empty($genre))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists($id, $output))
|
|
|
|
{
|
|
|
|
$output[$id][] = $genre;
|
2019-07-10 10:20:37 -04:00
|
|
|
}
|
|
|
|
else
|
2019-01-08 15:52:53 -05:00
|
|
|
{
|
|
|
|
$output[$id] = [$genre];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (PDOException $e) {}
|
|
|
|
|
2020-04-21 19:22:56 -04:00
|
|
|
$this->db->resetQuery();
|
2019-07-10 10:20:37 -04:00
|
|
|
|
2019-01-08 15:52:53 -05:00
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2020-04-23 18:57:22 -04:00
|
|
|
private function updateMediaLink(string $animeId, array $media): void
|
|
|
|
{
|
2020-04-28 12:13:35 -04:00
|
|
|
$this->db->beginTransaction();
|
|
|
|
|
2020-04-23 18:57:22 -04:00
|
|
|
// Delete the old entries
|
|
|
|
$this->db->where('hummingbird_id', $animeId)
|
|
|
|
->delete('anime_set_media_link');
|
|
|
|
|
|
|
|
// Add the new entries
|
|
|
|
$entries = [];
|
|
|
|
foreach ($media as $id)
|
|
|
|
{
|
|
|
|
$entries[] = [
|
|
|
|
'hummingbird_id' => $animeId,
|
|
|
|
'media_id' => $id,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->db->insertBatch('anime_set_media_link', $entries);
|
2020-04-28 12:13:35 -04:00
|
|
|
|
|
|
|
$this->db->commit();
|
2020-04-23 18:57:22 -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
|
|
|
*
|
2018-01-18 16:21:45 -05:00
|
|
|
* @param string $animeId The current anime
|
2015-06-11 12:54:54 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
2020-04-23 18:57:22 -04:00
|
|
|
private function updateGenres($animeId): void
|
2015-06-11 12:54:54 -04:00
|
|
|
{
|
2019-12-09 16:17:25 -05:00
|
|
|
if ($this->validDatabase === FALSE)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:20:37 -04:00
|
|
|
// Get api information
|
|
|
|
$anime = $this->animeModel->getAnimeById($animeId);
|
|
|
|
|
|
|
|
$this->addNewGenres($anime['genres']);
|
|
|
|
|
2017-02-15 16:30:14 -05:00
|
|
|
$genreInfo = $this->getGenreData();
|
2018-01-18 16:21:45 -05:00
|
|
|
$genres = $genreInfo['genres'];
|
|
|
|
$links = $genreInfo['links'];
|
2015-07-02 14:04:04 -04:00
|
|
|
|
2019-07-10 10:20:37 -04:00
|
|
|
$linksToInsert = [];
|
2015-06-11 12:54:54 -04:00
|
|
|
|
2019-07-10 10:20:37 -04:00
|
|
|
foreach ($anime['genres'] as $animeGenre)
|
2015-07-02 14:04:04 -04:00
|
|
|
{
|
|
|
|
// Update link table
|
|
|
|
// Get id of genre to put in link table
|
2017-02-15 16:30:14 -05:00
|
|
|
$flippedGenres = array_flip($genres);
|
2019-07-10 10:20:37 -04:00
|
|
|
$genreId = $flippedGenres[$animeGenre];
|
2015-07-02 14:04:04 -04:00
|
|
|
|
2019-07-10 10:20:37 -04:00
|
|
|
$animeLinks = $links[$animeId] ?? [];
|
2015-07-02 14:04:04 -04:00
|
|
|
|
2020-04-23 18:57:22 -04:00
|
|
|
if ( ! in_array($flippedGenres[$animeGenre], $animeLinks, TRUE))
|
2015-07-02 14:04:04 -04:00
|
|
|
{
|
2019-07-10 10:20:37 -04:00
|
|
|
$linksToInsert[] = [
|
|
|
|
'hummingbird_id' => $animeId,
|
|
|
|
'genre_id' => $genreId,
|
|
|
|
];
|
2015-07-02 14:04:04 -04:00
|
|
|
}
|
|
|
|
}
|
2019-07-10 10:20:37 -04:00
|
|
|
|
|
|
|
if ( ! empty($linksToInsert))
|
|
|
|
{
|
2020-04-28 12:13:35 -04:00
|
|
|
$this->db->insertBatch('genre_anime_set_link', $linksToInsert);
|
2019-07-10 10:20:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add genres to the database
|
|
|
|
*
|
|
|
|
* @param array $genres
|
|
|
|
*/
|
|
|
|
private function addNewGenres(array $genres): void
|
|
|
|
{
|
2019-12-09 16:17:25 -05:00
|
|
|
if ($this->validDatabase === FALSE)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:20:37 -04:00
|
|
|
$existingGenres = $this->getExistingGenres();
|
|
|
|
$newGenres = array_diff($genres, $existingGenres);
|
|
|
|
|
|
|
|
$insert = [];
|
|
|
|
|
|
|
|
foreach ($newGenres as $genre)
|
|
|
|
{
|
|
|
|
$insert[] = [
|
|
|
|
'genre' => $genre,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2020-04-21 19:22:56 -04:00
|
|
|
$this->db->insertBatch('genres', $insert);
|
2019-07-10 10:20:37 -04:00
|
|
|
}
|
|
|
|
catch (PDOException $e)
|
|
|
|
{
|
2020-04-23 18:57:22 -04:00
|
|
|
// dump($e);
|
2019-07-10 10:20:37 -04:00
|
|
|
}
|
2015-07-02 14:04:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get list of existing genres
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-01-18 16:21:45 -05:00
|
|
|
private function getGenreData(): array
|
2019-01-28 14:31:48 -05:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'genres' => $this->getExistingGenres(),
|
|
|
|
'links' => $this->getExistingGenreLinkEntries(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getExistingGenres(): array
|
2015-07-02 14:04:04 -04:00
|
|
|
{
|
2019-12-09 16:17:25 -05:00
|
|
|
if ($this->validDatabase === FALSE)
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2015-07-02 14:04:04 -04:00
|
|
|
$genres = [];
|
2015-06-11 12:54:54 -04:00
|
|
|
|
|
|
|
// Get existing genres
|
|
|
|
$query = $this->db->select('id, genre')
|
|
|
|
->from('genres')
|
|
|
|
->get();
|
2019-07-10 10:20:37 -04:00
|
|
|
|
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'];
|
|
|
|
}
|
|
|
|
|
2020-04-21 19:22:56 -04:00
|
|
|
$this->db->resetQuery();
|
2019-07-10 10:20:37 -04:00
|
|
|
|
2019-01-28 14:31:48 -05:00
|
|
|
return $genres;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getExistingGenreLinkEntries(): array
|
|
|
|
{
|
2019-12-09 16:17:25 -05:00
|
|
|
if ($this->validDatabase === FALSE)
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-01-28 14:31:48 -05:00
|
|
|
$links = [];
|
|
|
|
|
2015-06-11 12:54:54 -04:00
|
|
|
$query = $this->db->select('hummingbird_id, genre_id')
|
|
|
|
->from('genre_anime_set_link')
|
|
|
|
->get();
|
2019-07-10 10:20:37 -04:00
|
|
|
|
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'];
|
2020-04-21 19:22:56 -04:00
|
|
|
}
|
|
|
|
else
|
2015-06-11 12:54:54 -04:00
|
|
|
{
|
|
|
|
$links[$link['hummingbird_id']] = [$link['genre_id']];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 19:22:56 -04:00
|
|
|
$this->db->resetQuery();
|
2019-07-10 10:20:37 -04:00
|
|
|
|
2019-01-28 14:31:48 -05:00
|
|
|
return $links;
|
2015-07-02 14:04:04 -04:00
|
|
|
}
|
2020-04-23 18:57:22 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get full collection from the database
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getCollectionFromDatabase(): array
|
|
|
|
{
|
|
|
|
if ( ! $this->validDatabase)
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = $this->db->select('a.hummingbird_id, slug, title, alternate_title, show_type,
|
|
|
|
age_rating, episode_count, episode_length, cover_image, notes, media.type as media')
|
|
|
|
->from('anime_set a')
|
|
|
|
->join('anime_set_media_link ml', 'ml.hummingbird_id=a.hummingbird_id', 'inner')
|
|
|
|
->join('media', 'media.id=ml.media_id', 'inner')
|
|
|
|
->orderBy('media')
|
|
|
|
->orderBy('title')
|
|
|
|
->groupBy('a.hummingbird_id, media.type')
|
|
|
|
->get();
|
|
|
|
|
|
|
|
// Add genres associated with each item
|
|
|
|
$rows = $query->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
$genres = $this->getGenreList();
|
|
|
|
|
|
|
|
foreach($rows as &$row)
|
|
|
|
{
|
|
|
|
$id = $row['hummingbird_id'];
|
|
|
|
|
|
|
|
$row['genres'] = array_key_exists($id, $genres)
|
|
|
|
? $genres[$id]
|
|
|
|
: [];
|
|
|
|
|
|
|
|
sort($row['genres']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rows;
|
|
|
|
}
|
2015-06-09 18:18:53 -04:00
|
|
|
}
|
|
|
|
// End of AnimeCollectionModel.php
|