2016-10-20 22:09:36 -04:00
|
|
|
<?php declare(strict_types=1);
|
2016-04-14 19:10:03 -04:00
|
|
|
/**
|
2017-02-15 16:13:32 -05:00
|
|
|
* Hummingbird Anime List Client
|
2016-04-14 19:10:03 -04:00
|
|
|
*
|
2016-12-20 12:58:37 -05:00
|
|
|
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
|
2016-04-14 19:10:03 -04: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
|
2016-04-14 19:10:03 -04:00
|
|
|
* @link https://github.com/timw4mail/HummingBirdAnimeClient
|
2017-01-11 10:34:24 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient\Model;
|
2016-04-14 19:10:03 -04:00
|
|
|
|
2017-02-15 15:35:41 -05:00
|
|
|
use Aviat\AnimeClient\Model\DB;
|
|
|
|
use Aviat\Ion\Di\{ContainerAware, ContainerInterface};
|
2017-01-09 20:36:48 -05:00
|
|
|
use PDO;
|
2017-01-05 13:41:32 -05:00
|
|
|
use PDOException;
|
2016-04-14 19:10:03 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Base model for anime and manga collections
|
|
|
|
*/
|
|
|
|
class Collection extends DB {
|
2017-01-16 13:49:51 -05:00
|
|
|
|
|
|
|
use ContainerAware;
|
2016-04-14 19:10:03 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Anime API Model
|
2017-02-15 15:35:41 -05:00
|
|
|
* @var object $animeModel
|
2016-04-14 19:10:03 -04:00
|
|
|
*/
|
2017-02-15 15:35:41 -05:00
|
|
|
protected $animeModel;
|
2016-04-14 19:10:03 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the database is valid for querying
|
2016-08-30 10:57:41 -04:00
|
|
|
* @var boolean
|
2016-04-14 19:10:03 -04:00
|
|
|
*/
|
2017-02-15 15:35:41 -05:00
|
|
|
protected $validDatabase = FALSE;
|
2016-04-14 19:10:03 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new collection object
|
|
|
|
*
|
|
|
|
* @param ContainerInterface $container
|
|
|
|
*/
|
|
|
|
public function __construct(ContainerInterface $container)
|
2017-02-15 15:35:41 -05:00
|
|
|
{
|
|
|
|
parent::__construct($container);
|
2016-04-14 19:10:03 -04:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2017-02-15 15:35:41 -05:00
|
|
|
$this->db = \Query($this->dbConfig['collection']);
|
2016-04-14 19:10:03 -04:00
|
|
|
}
|
2017-01-05 13:41:32 -05:00
|
|
|
catch (PDOException $e)
|
2016-04-14 19:10:03 -04:00
|
|
|
{
|
2017-02-15 15:35:41 -05:00
|
|
|
//$this->validDatabase = FALSE;
|
2017-01-16 13:49:51 -05:00
|
|
|
//return FALSE;
|
2016-04-14 19:10:03 -04:00
|
|
|
}
|
2017-02-15 15:35:41 -05:00
|
|
|
$this->animeModel = $container->get('anime-model');
|
2016-04-14 19:10:03 -04:00
|
|
|
|
|
|
|
// Is database valid? If not, set a flag so the
|
|
|
|
// app can be run without a valid database
|
2017-02-15 15:35:41 -05:00
|
|
|
if ($this->dbConfig['collection']['type'] === 'sqlite')
|
2016-04-14 19:10:03 -04:00
|
|
|
{
|
2017-02-15 15:35:41 -05:00
|
|
|
$dbFileName = $this->dbConfig['collection']['file'];
|
2016-04-14 19:10:03 -04:00
|
|
|
|
2017-02-15 15:35:41 -05:00
|
|
|
if ($dbFileName !== ':memory:' && file_exists($dbFileName))
|
2016-04-14 19:10:03 -04:00
|
|
|
{
|
2017-02-15 15:35:41 -05:00
|
|
|
$dbFile = file_get_contents($dbFileName);
|
|
|
|
$this->validDatabase = (strpos($dbFile, 'SQLite format 3') === 0);
|
2016-04-14 19:10:03 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-15 15:35:41 -05:00
|
|
|
$this->validDatabase = FALSE;
|
2016-04-14 19:10:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-15 15:35:41 -05:00
|
|
|
$this->validDatabase = TRUE;
|
2016-04-14 19:10:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get genres for anime collection items
|
|
|
|
*
|
|
|
|
* @param array $filter
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-02-15 15:35:41 -05:00
|
|
|
public function getGenreList($filter = [])
|
2016-04-14 19:10:03 -04:00
|
|
|
{
|
|
|
|
$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->where_in('hummingbird_id', $filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = $this->db->order_by('hummingbird_id')
|
|
|
|
->order_by('genre')
|
|
|
|
->get();
|
|
|
|
|
|
|
|
$output = [];
|
|
|
|
|
2017-01-05 13:41:32 -05:00
|
|
|
foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $row)
|
2016-04-14 19:10:03 -04:00
|
|
|
{
|
|
|
|
$id = $row['hummingbird_id'];
|
|
|
|
$genre = $row['genre'];
|
|
|
|
|
|
|
|
// Empty genre names aren't useful
|
|
|
|
if (empty($genre))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists($id, $output))
|
|
|
|
{
|
|
|
|
array_push($output[$id], $genre);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$output[$id] = [$genre];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
// End of Collection.php
|