Version 5.1 - All the GraphQL #32

Closed
timw4mail wants to merge 1160 commits from develop into master
6 changed files with 156 additions and 107 deletions
Showing only changes of commit 05391eceab - Show all commits

View File

@ -1,6 +1,6 @@
<main> <main>
<?php if ($auth->is_authenticated()): ?> <?php if ($auth->is_authenticated()): ?>
<a class="bracketed" href="<?= $urlGenerator->url('collection/add', 'anime') ?>">Add Item</a> <a class="bracketed" href="<?= $url->generate('collection.add.get') ?>">Add Item</a>
<?php endif ?> <?php endif ?>
<?php if (empty($sections)): ?> <?php if (empty($sections)): ?>
<h3>There's nothing here!</h3> <h3>There's nothing here!</h3>
@ -11,7 +11,7 @@
<section class="media-wrap"> <section class="media-wrap">
<?php foreach($items as $item): ?> <?php foreach($items as $item): ?>
<article class="media" id="a-<?= $item['hummingbird_id'] ?>"> <article class="media" id="a-<?= $item['hummingbird_id'] ?>">
<img src="<?= $urlGenerator->asset_url('images', 'anime', basename($item['cover_image'])) ?>" /> <img src="<?= $urlGenerator->asset_url('images', 'anime', basename($item['cover_image'])) ?>" alt="<?= $item['title'] ?> cover image" />
<div class="name"> <div class="name">
<a href="<?= $url->generate('anime.details', ['id' => $item['slug']]) ?>"> <a href="<?= $url->generate('anime.details', ['id' => $item['slug']]) ?>">
<?= $item['title'] ?> <?= $item['title'] ?>

View File

@ -13,7 +13,7 @@
</th> </th>
<th> <th>
<article class="media"> <article class="media">
<?= $helper->img($item['cover_image']); ?> <?= $helper->img($urlGenerator->asset_url('images', 'anime', basename($item['cover_image']))); ?>
</article> </article>
</th> </th>
</tr> </tr>
@ -45,12 +45,22 @@
</tbody> </tbody>
</table> </table>
</form> </form>
<fieldset>
<legend>Danger Zone</legend>
<form class="js-delete" action="<?= $url->generate('collection.delete') ?>" method="post">
<table class="form invisible">
<tbody>
<tr>
<td>&nbsp;</td>
<td>
<input type="hidden" value="<?= $item['hummingbird_id'] ?>" name="hummingbird_id" />
<button type="submit" class="danger">Delete Entry</button>
</td>
</tr>
</tbody>
</table>
</form>
</fieldset>
</main> </main>
<template id="show_list">
<article class="media">
<div class="name"><label><input type="radio" name="id" value="{{:id}}" />&nbsp;<span>{{:title}}<br />{{:alternate_title}}</span></label></div>
<img src="{{:cover_image}}" alt="{{:title}}" />
</article>
</template>
<script src="<?= $urlGenerator->asset_url('js.php/g/anime_collection') ?>"></script> <script src="<?= $urlGenerator->asset_url('js.php/g/anime_collection') ?>"></script>
<?php endif ?> <?php endif ?>

View File

@ -15,7 +15,6 @@ namespace Aviat\AnimeClient\Controller;
use Aviat\Ion\Di\ContainerInterface; use Aviat\Ion\Di\ContainerInterface;
use Aviat\AnimeClient\Controller as BaseController; use Aviat\AnimeClient\Controller as BaseController;
use Aviat\AnimeClient\Config;
use Aviat\AnimeClient\UrlGenerator; use Aviat\AnimeClient\UrlGenerator;
use Aviat\AnimeClient\Model\Anime as AnimeModel; use Aviat\AnimeClient\Model\Anime as AnimeModel;
use Aviat\AnimeClient\Model\AnimeCollection as AnimeCollectionModel; use Aviat\AnimeClient\Model\AnimeCollection as AnimeCollectionModel;
@ -174,14 +173,15 @@ class Collection extends BaseController {
public function delete() public function delete()
{ {
$data = $this->request->getParsedBody(); $data = $this->request->getParsedBody();
if ( ! array_key_exists('id', $data)) if ( ! array_key_exists('hummingbird_id', $data))
{ {
$this->redirect("collection/view", 303); $this->redirect("/collection/view", 303);
} }
$this->anime_collection_model->delete($data); $this->anime_collection_model->delete($data);
$this->set_flash_message("Successfully removed anime from collection.", 'success');
$this->redirect("collection/view", 303); $this->redirect("/collection/view", 303);
} }
} }
// End of CollectionController.php // End of CollectionController.php

View File

@ -173,6 +173,7 @@ class Anime extends API {
* *
* @param string $name * @param string $name
* @return array * @return array
* @throws RuntimeException
*/ */
public function search($name) public function search($name)
{ {

View File

@ -19,19 +19,7 @@ use Aviat\Ion\Di\ContainerInterface;
/** /**
* Model for getting anime collection data * Model for getting anime collection data
*/ */
class AnimeCollection extends DB { class AnimeCollection extends Collection {
/**
* Anime API Model
* @var object $anime_model
*/
private $anime_model;
/**
* Whether the database is valid for querying
* @var bool
*/
private $valid_database = FALSE;
/** /**
* Constructor * Constructor
@ -42,91 +30,10 @@ class AnimeCollection extends DB {
{ {
parent::__construct($container); parent::__construct($container);
try
{
$this->db = \Query($this->db_config['collection']);
}
catch (\PDOException $e)
{
$this->valid_database = FALSE;
return FALSE;
}
$this->anime_model = $container->get('anime-model');
// Is database valid? If not, set a flag so the
// app can be run without a valid database
if ($this->db_config['collection']['type'] === 'sqlite')
{
$db_file_name = $this->db_config['collection']['file'];
if ($db_file_name !== ':memory:' && file_exists($db_file_name))
{
$db_file = file_get_contents($db_file_name);
$this->valid_database = (strpos($db_file, 'SQLite format 3') === 0);
}
else
{
$this->valid_database = FALSE;
}
}
else
{
$this->valid_database = TRUE;
}
// Do an import if an import file exists // Do an import if an import file exists
$this->json_import(); $this->json_import();
} }
/**
* Get genres for anime collection items
*
* @param array $filter
* @return array
*/
public function get_genre_list($filter = [])
{
$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 = [];
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))
{
array_push($output[$id], $genre);
}
else
{
$output[$id] = [$genre];
}
}
return $output;
}
/** /**
* Get collection from the database, and organize by media type * Get collection from the database, and organize by media type
* *
@ -265,6 +172,7 @@ class AnimeCollection extends DB {
/** /**
* Remove a colleciton item * Remove a colleciton item
*
* @param array $data * @param array $data
* @return void * @return void
*/ */
@ -276,6 +184,9 @@ class AnimeCollection extends DB {
return; return;
} }
$this->db->where('hummingbird_id', $data['hummingbird_id'])
->delete('genre_anime_set_link');
$this->db->where('hummingbird_id', $data['hummingbird_id']) $this->db->where('hummingbird_id', $data['hummingbird_id'])
->delete('anime_set'); ->delete('anime_set');
} }

View File

@ -0,0 +1,127 @@
<?php
/**
* Hummingbird Anime Client
*
* An API client for Hummingbird to manage anime and manga watch lists
*
* @package HummingbirdAnimeClient
* @author Timothy J. Warren
* @copyright Copyright (c) 2015 - 2016
* @link https://github.com/timw4mail/HummingBirdAnimeClient
* @license MIT
*/
namespace Aviat\AnimeClient\Model;
use Aviat\Ion\Di\ContainerInterface;
/**
* Base model for anime and manga collections
*/
class Collection extends DB {
/**
* Anime API Model
* @var object $anime_model
*/
protected $anime_model;
/**
* Whether the database is valid for querying
* @var bool
*/
protected $valid_database = FALSE;
/**
* Create a new collection object
*
* @param ContainerInterface $container
* @return boolean
*/
public function __construct(ContainerInterface $container)
{
parent::__construct($container);
try
{
$this->db = \Query($this->db_config['collection']);
}
catch (\PDOException $e)
{
$this->valid_database = FALSE;
return FALSE;
}
$this->anime_model = $container->get('anime-model');
// Is database valid? If not, set a flag so the
// app can be run without a valid database
if ($this->db_config['collection']['type'] === 'sqlite')
{
$db_file_name = $this->db_config['collection']['file'];
if ($db_file_name !== ':memory:' && file_exists($db_file_name))
{
$db_file = file_get_contents($db_file_name);
$this->valid_database = (strpos($db_file, 'SQLite format 3') === 0);
}
else
{
$this->valid_database = FALSE;
}
}
else
{
$this->valid_database = TRUE;
}
}
/**
* Get genres for anime collection items
*
* @param array $filter
* @return array
*/
public function get_genre_list($filter = [])
{
$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 = [];
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))
{
array_push($output[$id], $genre);
}
else
{
$output[$id] = [$genre];
}
}
return $output;
}
}
// End of Collection.php