Resolves #10, adds ability to delete from anime collection

This commit is contained in:
Timothy Warren 2016-04-14 19:10:03 -04:00
parent 174569e9ef
commit 2772a5715d
6 changed files with 156 additions and 107 deletions

View File

@ -1,6 +1,6 @@
<main>
<?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 if (empty($sections)): ?>
<h3>There's nothing here!</h3>
@ -11,7 +11,7 @@
<section class="media-wrap">
<?php foreach($items as $item): ?>
<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">
<a href="<?= $url->generate('anime.details', ['id' => $item['slug']]) ?>">
<?= $item['title'] ?>

View File

@ -13,7 +13,7 @@
</th>
<th>
<article class="media">
<?= $helper->img($item['cover_image']); ?>
<?= $helper->img($urlGenerator->asset_url('images', 'anime', basename($item['cover_image']))); ?>
</article>
</th>
</tr>
@ -45,12 +45,22 @@
</tbody>
</table>
</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>
<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>
<?php endif ?>

View File

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

View File

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

View File

@ -19,19 +19,7 @@ use Aviat\Ion\Di\ContainerInterface;
/**
* Model for getting anime collection data
*/
class AnimeCollection extends DB {
/**
* Anime API Model
* @var object $anime_model
*/
private $anime_model;
/**
* Whether the database is valid for querying
* @var bool
*/
private $valid_database = FALSE;
class AnimeCollection extends Collection {
/**
* Constructor
@ -42,91 +30,10 @@ class AnimeCollection extends DB {
{
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
$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
*
@ -265,6 +172,7 @@ class AnimeCollection extends DB {
/**
* Remove a colleciton item
*
* @param array $data
* @return void
*/
@ -276,6 +184,9 @@ class AnimeCollection extends DB {
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');
}

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