Version 5.1 - All the GraphQL #32

Closed
timw4mail wants to merge 1160 commits from develop into master
8 changed files with 233 additions and 119 deletions
Showing only changes of commit 52b562f455 - Show all commits

View File

@ -2,6 +2,7 @@
<main> <main>
<h2>Add Anime to your List</h2> <h2>Add Anime to your List</h2>
<form action="<?= $action_url ?>" method="post"> <form action="<?= $action_url ?>" method="post">
<?php include realpath(__DIR__ . '/../js-warning.php') ?>
<section> <section>
<div class="cssload-loader" hidden="hidden"> <div class="cssload-loader" hidden="hidden">
<div class="cssload-inner cssload-one"></div> <div class="cssload-inner cssload-one"></div>

View File

@ -0,0 +1,11 @@
<select name="media_id[]" id="media_id" multiple size="13">
<?php foreach ($media_items as $group => $items): ?>
<optgroup label='<?= $group ?>'>
<?php foreach ($items as $id => $name): ?>
<option <?= in_array($id, ($item['media_id'] ?? []), FALSE) ? 'selected="selected"' : '' ?> value="<?= $id ?>">
<?= $name ?>
</option>
<?php endforeach ?>
</optgroup>
<?php endforeach ?>
</select>

View File

@ -2,6 +2,7 @@
<main> <main>
<h2>Add <?= ucfirst($collection_type) ?> to your Collection</h2> <h2>Add <?= ucfirst($collection_type) ?> to your Collection</h2>
<form action="<?= $action_url ?>" method="post"> <form action="<?= $action_url ?>" method="post">
<?php include realpath(__DIR__ . '/../js-warning.php') ?>
<section> <section>
<div class="cssload-loader" hidden="hidden"> <div class="cssload-loader" hidden="hidden">
<div class="cssload-inner cssload-one"></div> <div class="cssload-inner cssload-one"></div>
@ -16,13 +17,9 @@
<table class="invisible form"> <table class="invisible form">
<tbody> <tbody>
<tr> <tr>
<td><label for="media_id">Media</label></td> <td class="align-right"><label for="media_id">Media</label></td>
<td> <td class='align-left'>
<select name="media_id" id="media_id"> <?php include '_media-list.php' ?>
<?php foreach($media_items as $id => $name): ?>
<option value="<?= $id ?>"><?= $name ?></option>
<?php endforeach ?>
</select>
</td> </td>
</tr> </tr>
<tr> <tr>

View File

@ -24,11 +24,7 @@
<tr> <tr>
<td class="align-right"><label for="media_id">Media</label></td> <td class="align-right"><label for="media_id">Media</label></td>
<td class="align-left"> <td class="align-left">
<select name="media_id" id="media_id"> <?php include '_media-list.php' ?>
<?php foreach($media_items as $id => $name): ?>
<option <?= $item['media_id'] === $id ? 'selected="selected"' : '' ?> value="<?= $id ?>"><?= $name ?></option>
<?php endforeach ?>
</select>
</td> </td>
</tr> </tr>
<tr> <tr>

6
app/views/js-warning.php Normal file
View File

@ -0,0 +1,6 @@
<noscript>
<div class="message error">
<span class="icon"></span>
This feature requires Javascript to function :(
</div>
</noscript>

View File

@ -2,6 +2,7 @@
<main> <main>
<h2>Add Manga to your List</h2> <h2>Add Manga to your List</h2>
<form action="<?= $action_url ?>" method="post"> <form action="<?= $action_url ?>" method="post">
<?php include realpath(__DIR__ . '/../js-warning.php') ?>
<section> <section>
<div class="cssload-loader" hidden="hidden"> <div class="cssload-loader" hidden="hidden">
<div class="cssload-inner cssload-one"></div> <div class="cssload-inner cssload-one"></div>

View File

@ -152,28 +152,7 @@ final class AnimeCollection extends BaseController {
public function edit(): void public function edit(): void
{ {
$this->checkAuth(); $this->checkAuth();
$this->update($this->request->getParsedBody());
$data = $this->request->getParsedBody();
if (array_key_exists('hummingbird_id', $data))
{
$this->animeCollectionModel->update($data);
// Verify the item was actually updated
if ($this->animeCollectionModel->wasUpdated($data))
{
$this->setFlashMessage('Successfully updated collection item.', 'success');
}
else
{
$this->setFlashMessage('Failed to update collection item.', 'error');
}
}
else
{
$this->setFlashMessage('No item id to update. Update failed.', 'error');
}
$this->sessionRedirect();
} }
/** /**
@ -192,11 +171,24 @@ final class AnimeCollection extends BaseController {
if (array_key_exists('id', $data)) if (array_key_exists('id', $data))
{ {
// Check for existing entry // Check for existing entry
if ($this->animeCollectionModel->get($data['id']) !== FALSE) if ($this->animeCollectionModel->has($data['id']))
{ {
// Redirect to the edit screen, because that's probably what you want! // Let's just update with the data we have
$this->setFlashMessage('Anime already exists, update instead.', 'info'); // if the entry already exists.
$this->redirect("/anime-collection/edit/{$data['id']}", 303); $data['hummingbird_id'] = $data['id'];
unset(
$data['id'],
$data['mal_id'],
$data['search']
);
// Don't overwrite notes if the box is empty
if (trim($data['notes']) === '')
{
unset($data['notes']);
}
$this->update($data);
return; return;
} }
@ -207,13 +199,12 @@ final class AnimeCollection extends BaseController {
{ {
$this->setFlashMessage('Successfully added collection item', 'success'); $this->setFlashMessage('Successfully added collection item', 'success');
$this->sessionRedirect(); $this->sessionRedirect();
return;
} }
} }
else
{ $this->setFlashMessage('Failed to add collection item.', 'error');
$this->setFlashMessage('Failed to add collection item.', 'error'); $this->redirect('/anime-collection/add', 303);
$this->redirect('/anime-collection/add', 303);
}
} }
/** /**
@ -235,16 +226,30 @@ final class AnimeCollection extends BaseController {
$this->animeCollectionModel->delete($data); $this->animeCollectionModel->delete($data);
// Verify that item was actually deleted // Verify that item was actually deleted
if ($this->animeCollectionModel->wasDeleted($data)) ($this->animeCollectionModel->wasDeleted($data))
{ ? $this->setFlashMessage('Successfully removed anime from collection.', 'success')
$this->setFlashMessage('Successfully removed anime from collection.', 'success'); : $this->setFlashMessage('Failed to delete item from collection.', 'error');
}
else
{
$this->setFlashMessage('Failed to delete item from collection.', 'error');
}
$this->redirect('/anime-collection/view', 303); $this->redirect('/anime-collection/view', 303);
} }
protected function update($data): void
{
if (array_key_exists('hummingbird_id', $data))
{
$this->animeCollectionModel->update($data);
// Verify the item was actually updated
($this->animeCollectionModel->wasUpdated($data))
? $this->setFlashMessage('Successfully updated collection item.', 'success')
: $this->setFlashMessage('Failed to update collection item.', 'error');
}
else
{
$this->setFlashMessage('No item id to update. Update failed.', 'error');
}
$this->sessionRedirect();
}
} }
// End of AnimeCollection.php // End of AnimeCollection.php

View File

@ -19,6 +19,7 @@ namespace Aviat\AnimeClient\Model;
use Aviat\Ion\Di\ContainerInterface; use Aviat\Ion\Di\ContainerInterface;
use PDO; use PDO;
use PDOException; use PDOException;
use function in_array;
/** /**
* Model for getting anime collection data * Model for getting anime collection data
@ -80,7 +81,7 @@ final class AnimeCollection extends Collection {
return []; return [];
} }
$output = []; $flatList = [];
$query = $this->db->select('id, type') $query = $this->db->select('id, type')
->from('media') ->from('media')
@ -88,49 +89,28 @@ final class AnimeCollection extends Collection {
foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $row) foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $row)
{ {
$output[$row['id']] = $row['type']; $flatList[$row['id']] = $row['type'];
} }
return $output; // Organize the media types into groups
} return [
'Common' => [
/** 2 => $flatList[2], // Blu-ray
* Get full collection from the database 3 => $flatList[3], // DVD
* 7 => $flatList[7], // Digital
* @return array 4 => $flatList[4], // Bootleg
*/ ],
private function getCollectionFromDatabase(): array 'Retro' => [
{ 5 => $flatList[5], // LaserDisc
if ( ! $this->validDatabase) 6 => $flatList[6], // VHS
{ 9 => $flatList[9], // Betamax
return []; 8 => $flatList[8], // Video CD
} ],
'Other' => [
$query = $this->db->select('hummingbird_id, slug, title, alternate_title, show_type, 10 => $flatList[10], // UMD
age_rating, episode_count, episode_length, cover_image, notes, media.type as media') 11 => $flatList[11], // Other
->from('anime_set a') ]
->join('media', 'media.id=a.media_id', 'inner') ];
->order_by('media')
->order_by('title')
->group_by('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;
} }
/** /**
@ -146,18 +126,16 @@ final class AnimeCollection extends Collection {
return; return;
} }
$id = $data['id'];
// Check that the anime doesn't already exist // Check that the anime doesn't already exist
$existing = $this->get($id); if ($this->has($data['id']))
if ( ! empty($existing))
{ {
return; return;
} }
$id = $data['id'];
$anime = (object)$this->animeModel->getAnimeById($id); $anime = (object)$this->animeModel->getAnimeById($id);
$this->db->set([ $this->db->set([
'hummingbird_id' => $data['id'], 'hummingbird_id' => $id,
'slug' => $anime->slug, 'slug' => $anime->slug,
'title' => array_shift($anime->titles), 'title' => array_shift($anime->titles),
'alternate_title' => implode('<br />', $anime->titles), 'alternate_title' => implode('<br />', $anime->titles),
@ -166,11 +144,12 @@ final class AnimeCollection extends Collection {
'cover_image' => $anime->cover_image, 'cover_image' => $anime->cover_image,
'episode_count' => $anime->episode_count, 'episode_count' => $anime->episode_count,
'episode_length' => $anime->episode_length, 'episode_length' => $anime->episode_length,
'media_id' => $data['media_id'],
'notes' => $data['notes'] 'notes' => $data['notes']
])->insert('anime_set'); ])->insert('anime_set');
$this->updateGenre($id); $this->updateMediaLink($id, $data['media_id']);
$this->updateGenres($id);
} }
/** /**
@ -211,14 +190,22 @@ final class AnimeCollection extends Collection {
} }
$id = $data['hummingbird_id']; $id = $data['hummingbird_id'];
unset($data['hummingbird_id']); $media = $data['media_id'];
unset($data['hummingbird_id'], $data['media_id']);
$this->db->set($data) // If updating from the 'add' page, there
->where('hummingbird_id', $id) // might be no data to actually update in
->update('anime_set'); // the anime_set table
if ( ! empty($data))
{
$this->db->set($data)
->where('hummingbird_id', $id)
->update('anime_set');
}
// Just in case, also update genres // Update media and genres
$this->updateGenre($id); $this->updateMediaLink($id, $media);
$this->updateGenres($id);
} }
/** /**
@ -239,6 +226,16 @@ final class AnimeCollection extends Collection {
foreach ($data as $key => $value) foreach ($data as $key => $value)
{ {
if (is_array($row[$key]))
{
if ($row[$key] === $value)
{
continue;
}
return FALSE;
}
if ((string)$row[$key] !== (string)$value) if ((string)$row[$key] !== (string)$value)
{ {
return FALSE; return FALSE;
@ -285,29 +282,60 @@ final class AnimeCollection extends Collection {
return FALSE; return FALSE;
} }
$animeRow = $this->get($data['hummingbird_id']); return $this->has($data['hummingbird_id']) === FALSE;
return empty($animeRow);
} }
/** /**
* Get the details of a collection item * Get the details of a collection item
* *
* @param int $kitsuId * @param int|string $kitsuId
* @return array | false * @return array
*/ */
public function get($kitsuId) 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
{ {
if ($this->validDatabase === FALSE) if ($this->validDatabase === FALSE)
{ {
return FALSE; return FALSE;
} }
$query = $this->db->from('anime_set') $row = $this->db->select('hummingbird_id')
->from('anime_set')
->where('hummingbird_id', $kitsuId) ->where('hummingbird_id', $kitsuId)
->get(); ->get()
->fetch(PDO::FETCH_ASSOC);
return $query->fetch(PDO::FETCH_ASSOC); return ! empty($row);
} }
/** /**
@ -372,13 +400,32 @@ final class AnimeCollection extends Collection {
return $output; return $output;
} }
private function updateMediaLink(string $animeId, array $media): void
{
// 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);
}
/** /**
* Update genre information for selected anime * Update genre information for selected anime
* *
* @param string $animeId The current anime * @param string $animeId The current anime
* @return void * @return void
*/ */
private function updateGenre($animeId): void private function updateGenres($animeId): void
{ {
if ($this->validDatabase === FALSE) if ($this->validDatabase === FALSE)
{ {
@ -405,7 +452,7 @@ final class AnimeCollection extends Collection {
$animeLinks = $links[$animeId] ?? []; $animeLinks = $links[$animeId] ?? [];
if ( ! \in_array($flippedGenres[$animeGenre], $animeLinks, TRUE)) if ( ! in_array($flippedGenres[$animeGenre], $animeLinks, TRUE))
{ {
$linksToInsert[] = [ $linksToInsert[] = [
'hummingbird_id' => $animeId, 'hummingbird_id' => $animeId,
@ -416,7 +463,17 @@ final class AnimeCollection extends Collection {
if ( ! empty($linksToInsert)) if ( ! empty($linksToInsert))
{ {
$this->db->insertBatch('genre_anime_set_link', $linksToInsert); try
{
$this->db->insertBatch('genre_anime_set_link', $linksToInsert);
}
catch (PDOException $e)
{
// This often results in a unique constraint violation
// So swallow this for now
// @TODO Fix properly
}
} }
} }
@ -450,7 +507,7 @@ final class AnimeCollection extends Collection {
} }
catch (PDOException $e) catch (PDOException $e)
{ {
dump($e); // dump($e);
} }
} }
@ -520,5 +577,45 @@ final class AnimeCollection extends Collection {
return $links; return $links;
} }
/**
* 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;
}
} }
// End of AnimeCollectionModel.php // End of AnimeCollectionModel.php