From 27c7f08d7f93e084805a376aeba41b66693c6979 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Tue, 8 Jan 2019 15:52:53 -0500 Subject: [PATCH] Hide missing table error on noninitialized collection, see #20 --- src/Controller/Settings.php | 11 ++----- src/Model/AnimeCollection.php | 55 +++++++++++++++++++++++++++++++++++ src/Model/Collection.php | 49 ------------------------------- 3 files changed, 58 insertions(+), 57 deletions(-) diff --git a/src/Controller/Settings.php b/src/Controller/Settings.php index 225d05b1..729595ac 100644 --- a/src/Controller/Settings.php +++ b/src/Controller/Settings.php @@ -79,16 +79,11 @@ final class Settings extends BaseController { $post = $this->request->getParsedBody(); unset($post['settings-tabs']); - // dump($post); $saved = $this->settingsModel->saveSettingsFile($post); - if ($saved) - { - $this->setFlashMessage('Saved config settings.', 'success'); - } else - { - $this->setFlashMessage('Failed to save config file.', 'error'); - } + $saved + ? $this->setFlashMessage('Saved config settings.', 'success') + : $this->setFlashMessage('Failed to save config file.', 'error'); $this->redirect($this->url->generate('settings'), 303); } diff --git a/src/Model/AnimeCollection.php b/src/Model/AnimeCollection.php index 29d91485..0c1deb37 100644 --- a/src/Model/AnimeCollection.php +++ b/src/Model/AnimeCollection.php @@ -18,6 +18,7 @@ namespace Aviat\AnimeClient\Model; use Aviat\Ion\Di\ContainerInterface; use PDO; +use PDOException; /** * Model for getting anime collection data @@ -226,6 +227,60 @@ final class AnimeCollection extends Collection { return $query->fetch(PDO::FETCH_ASSOC); } + /** + * Get genres for anime collection items + * + * @param array $filter + * @return array + */ + public function getGenreList(array $filter = []): array + { + $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; + } else + { + $output[$id] = [$genre]; + } + } + } + catch (PDOException $e) {} + + return $output; + } + /** * Get the list of genres from the database * diff --git a/src/Model/Collection.php b/src/Model/Collection.php index 6191b4b1..008b1556 100644 --- a/src/Model/Collection.php +++ b/src/Model/Collection.php @@ -74,54 +74,5 @@ class Collection extends DB { $this->validDatabase = FALSE; } } - - /** - * Get genres for anime collection items - * - * @param array $filter - * @return array - */ - public function getGenreList(array $filter = []): array - { - $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(); - - $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)) - { - $output[$id][] = $genre; - } - else - { - $output[$id] = [$genre]; - } - } - - return $output; - } - } // End of Collection.php \ No newline at end of file