From 1b74df5269b8ae9d863fc3e4e89f3937deec7caf Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Wed, 26 Aug 2020 17:26:42 -0400 Subject: [PATCH] More components, resolve #31 --- app/bootstrap.php | 10 ++- .../anime-cover.php} | 12 +-- app/templates/manga-cover.php | 74 ++++++++++++++++++ app/templates/tabs.php | 9 +++ app/templates/vertical-tabs.php | 2 +- app/views/anime/cover.php | 2 +- app/views/collection/add.php | 2 +- app/views/collection/cover.php | 41 ++++------ app/views/collection/edit.php | 3 +- app/views/collection/list-all.php | 44 ----------- app/views/collection/list-item.php | 3 + app/views/collection/list.php | 76 +++++++++++-------- ...-select-list.php => media-select-list.php} | 0 app/views/manga/cover.php | 75 +----------------- src/AnimeClient/AnimeClient.php | 15 ++++ src/AnimeClient/Component/AnimeCover.php | 30 ++++++++ src/AnimeClient/Component/ComponentTrait.php | 28 ++++++- src/AnimeClient/Component/MangaCover.php | 31 ++++++++ src/AnimeClient/Component/Tabs.php | 6 +- .../Controller/AnimeCollection.php | 8 +- 20 files changed, 276 insertions(+), 195 deletions(-) rename app/{views/anime/cover-item.php => templates/anime-cover.php} (92%) create mode 100644 app/templates/manga-cover.php delete mode 100644 app/views/collection/list-all.php rename app/views/collection/{_media-select-list.php => media-select-list.php} (100%) create mode 100644 src/AnimeClient/Component/AnimeCover.php create mode 100644 src/AnimeClient/Component/MangaCover.php diff --git a/app/bootstrap.php b/app/bootstrap.php index 627fb5c7..149c1c2a 100644 --- a/app/bootstrap.php +++ b/app/bootstrap.php @@ -101,9 +101,11 @@ return static function (array $configArray = []): Container { }); // Create Component helpers - $container->set('component-helper', static function () { + $container->set('component-helper', static function (ContainerInterface $container) { $helper = (new HelperLocatorFactory)->newInstance(); $components = [ + 'animeCover' => Component\AnimeCover::class, + 'mangaCover' => Component\MangaCover::class, 'character' => Component\Character::class, 'media' => Component\Media::class, 'tabs' => Component\Tabs::class, @@ -112,7 +114,11 @@ return static function (array $configArray = []): Container { foreach ($components as $name => $componentClass) { - $helper->set($name, fn () => new $componentClass); + $helper->set($name, static function () use ($container, $componentClass) { + $helper = new $componentClass; + $helper->setContainer($container); + return $helper; + }); } return $helper; diff --git a/app/views/anime/cover-item.php b/app/templates/anime-cover.php similarity index 92% rename from app/views/anime/cover-item.php rename to app/templates/anime-cover.php index af680967..aef5ec2b 100644 --- a/app/views/anime/cover-item.php +++ b/app/templates/anime-cover.php @@ -1,7 +1,7 @@
isAuthenticated()): ?> @@ -31,13 +31,13 @@ 0): ?>
-
Rewatched once
+
Rewatched once
-
Rewatched twice
+
Rewatched twice
-
Rewatched thrice
+
Rewatched thrice
-
Rewatched times
+
Rewatched times
diff --git a/app/templates/manga-cover.php b/app/templates/manga-cover.php new file mode 100644 index 00000000..feb646bc --- /dev/null +++ b/app/templates/manga-cover.php @@ -0,0 +1,74 @@ +
+ isAuthenticated()): ?> + + + picture("images/manga/{$item['manga']['id']}.webp") ?> + +
+ isAuthenticated()): ?> +
+ + + Edit + + +
+ +
+
+
Rating: / 10
+
+ + +
+ + + + + +
+ + + 0): ?> +
+ +
Reread once
+ +
Reread twice
+ +
Reread thrice
+ +
Reread times
+ +
+ + +
+
+ Chapters: / + +
+ +
*/ ?> +
+ Volumes: +
+
+
+
\ No newline at end of file diff --git a/app/templates/tabs.php b/app/templates/tabs.php index 17294e63..c7f70b91 100644 --- a/app/templates/tabs.php +++ b/app/templates/tabs.php @@ -11,6 +11,11 @@ /> + + +
+ +
+ + +
+ \ No newline at end of file diff --git a/app/templates/vertical-tabs.php b/app/templates/vertical-tabs.php index 72e3827f..a36a2bb1 100644 --- a/app/templates/vertical-tabs.php +++ b/app/templates/vertical-tabs.php @@ -7,7 +7,7 @@ type="radio" role='tab' aria-controls="_" - name="staff-roles" + name="" id="" /> diff --git a/app/views/anime/cover.php b/app/views/anime/cover.php index 9c872607..96cdf359 100644 --- a/app/views/anime/cover.php +++ b/app/views/anime/cover.php @@ -20,7 +20,7 @@
isAuthenticated()) continue; ?> - + animeCover($item) ?>
diff --git a/app/views/collection/add.php b/app/views/collection/add.php index 7a8bca88..aa625fa5 100644 --- a/app/views/collection/add.php +++ b/app/views/collection/add.php @@ -19,7 +19,7 @@ - + diff --git a/app/views/collection/cover.php b/app/views/collection/cover.php index 0d4c1345..924d03e1 100644 --- a/app/views/collection/cover.php +++ b/app/views/collection/cover.php @@ -1,3 +1,4 @@ +
isAuthenticated()): ?> Add Item @@ -8,30 +9,20 @@

-
- - $items): ?> - - -
-
- - - -
-
- - - - - -
-
- - - -
-
-
+ tabs('collection-tab', $sections, static function ($items) use ($auth, $collection_type, $helper, $url, $component) { + $rendered = []; + foreach ($items as $item) + { + $rendered[] = renderTemplate(__DIR__ . '/cover-item.php', [ + 'auth' => $auth, + 'collection_type' => $collection_type, + 'helper' => $helper, + 'item' => $item, + 'url' => $url, + ]); + } + + return implode('', array_map('mb_trim', $rendered)); + }, 'media-wrap', true) ?>
diff --git a/app/views/collection/edit.php b/app/views/collection/edit.php index c3b8f2b0..65129799 100644 --- a/app/views/collection/edit.php +++ b/app/views/collection/edit.php @@ -1,3 +1,4 @@ + isAuthenticated()): ?>

Edit Anime Collection Item

@@ -24,7 +25,7 @@ - + diff --git a/app/views/collection/list-all.php b/app/views/collection/list-all.php deleted file mode 100644 index b14db2b1..00000000 --- a/app/views/collection/list-all.php +++ /dev/null @@ -1,44 +0,0 @@ - - -
- - - - isAuthenticated()): ?> - - - - - - - - - - - - - generate($collection_type . '.collection.edit.get', ['id' => $item['hummingbird_id']]); ?> - - isAuthenticated()): ?> - - - - - - - - - - - - - -
 TitleMediaEpisode CountEpisode LengthShow TypeAge RatingNotesGenres
- Edit - - - - - ' . $item['alternate_title'] . '' : '' ?> - 1) ? $item['episode_count'] : '-' ?>
-
\ No newline at end of file diff --git a/app/views/collection/list-item.php b/app/views/collection/list-item.php index 9c8ac967..4fd1191a 100644 --- a/app/views/collection/list-item.php +++ b/app/views/collection/list-item.php @@ -11,6 +11,9 @@ ' . $item['alternate_title'] . '' : '' ?> + + + 1) ? $item['episode_count'] : '-' ?> diff --git a/app/views/collection/list.php b/app/views/collection/list.php index 3fa1be75..ec3fd5e8 100644 --- a/app/views/collection/list.php +++ b/app/views/collection/list.php @@ -1,4 +1,4 @@ - +
isAuthenticated()): ?> Add Item @@ -9,38 +9,48 @@

- -
- $items): ?> - - - -
- - - - isAuthenticated()): ?> - - - - - - - - - - - - - - -
 TitleEpisode CountEpisode LengthShow TypeAge RatingNotesGenres
-
- - - - -
+ tabs('collection-tab', $sections, static function ($items, $section) use ($auth, $helper, $url, $collection_type) { + $hasNotes = colNotEmpty($items, 'notes'); + $hasMedia = $section === 'All'; + $firstTh = ($auth->isAuthenticated()) ? ' ' : ''; + $mediaTh = ($hasMedia) ? 'Media' : ''; + $noteTh = ($hasNotes) ? 'Notes' : ''; + + $rendered = []; + foreach ($items as $item) + { + $rendered[] = renderTemplate(__DIR__ . '/list-item.php', [ + 'auth' => $auth, + 'collection_type' => $collection_type, + 'hasMedia' => $hasMedia, + 'hasNotes' => $hasNotes, + 'helper' => $helper, + 'item' => $item, + 'url' => $url, + ]); + } + $rows = implode('', array_map('mb_trim', $rendered)); + + return << + + + {$firstTh} + Title + {$mediaTh} + Episode Count + Episode Length + Show Type + Age Rating + {$noteTh} + Genres + + + {$rows} + +HTML; + + }) ?>
\ No newline at end of file diff --git a/app/views/collection/_media-select-list.php b/app/views/collection/media-select-list.php similarity index 100% rename from app/views/collection/_media-select-list.php rename to app/views/collection/media-select-list.php diff --git a/app/views/manga/cover.php b/app/views/manga/cover.php index 7d3edbae..8d4822c7 100644 --- a/app/views/manga/cover.php +++ b/app/views/manga/cover.php @@ -19,80 +19,7 @@

html($name) ?>

-
- isAuthenticated()): ?> - - - picture("images/manga/{$item['manga']['id']}.webp") ?> - -
- isAuthenticated()): ?> -
- - - Edit - - -
- -
-
-
Rating: / 10
-
- - -
- - - - - -
- - - 0): ?> -
- -
Reread once
- -
Reread twice
- -
Reread thrice
- -
Reread times
- -
- - -
-
- Chapters: / - -
- -
*/ ?> -
- Volumes: -
-
-
-
+ mangaCover($item, $name) ?>
diff --git a/src/AnimeClient/AnimeClient.php b/src/AnimeClient/AnimeClient.php index eb35e2f9..c9c70216 100644 --- a/src/AnimeClient/AnimeClient.php +++ b/src/AnimeClient/AnimeClient.php @@ -369,4 +369,19 @@ function clearCache(CacheInterface $cache): bool : TRUE; return $cleared && $saved; +} + +/** + * Render a PHP code template as a string + * + * @param string $path + * @param array $data + * @return string + */ +function renderTemplate(string $path, array $data): string +{ + ob_start(); + extract($data, EXTR_OVERWRITE); + include $path; + return ob_get_clean(); } \ No newline at end of file diff --git a/src/AnimeClient/Component/AnimeCover.php b/src/AnimeClient/Component/AnimeCover.php new file mode 100644 index 00000000..d34db72f --- /dev/null +++ b/src/AnimeClient/Component/AnimeCover.php @@ -0,0 +1,30 @@ + + * @copyright 2015 - 2020 Timothy J. Warren + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @version 5.1 + * @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient + */ + +namespace Aviat\AnimeClient\Component; + +use Aviat\AnimeClient\Types\AnimeListItem; + +final class AnimeCover { + use ComponentTrait; + + public function __invoke(AnimeListItem $item): string + { + return $this->render('anime-cover.php', [ + 'item' => $item, + ]); + } +} \ No newline at end of file diff --git a/src/AnimeClient/Component/ComponentTrait.php b/src/AnimeClient/Component/ComponentTrait.php index d4f909ae..1e71ae4e 100644 --- a/src/AnimeClient/Component/ComponentTrait.php +++ b/src/AnimeClient/Component/ComponentTrait.php @@ -16,15 +16,35 @@ namespace Aviat\AnimeClient\Component; +use Aviat\Ion\Di\ContainerAware; +use const TEMPLATE_DIR; +use function Aviat\AnimeClient\renderTemplate; + /** * Shared logic for component-based functionality, like Tabs */ trait ComponentTrait { + use ContainerAware; + + /** + * Render a template with common container values + * + * @param string $path + * @param array $data + * @return string + */ public function render(string $path, array $data): string { - ob_start(); - extract($data, EXTR_OVERWRITE); - include \TEMPLATE_DIR . '/' .$path; - return ob_get_clean(); + $container = $this->getContainer(); + $helper = $container->get('html-helper'); + + $baseData = [ + 'auth' => $container->get('auth'), + 'escape' => $helper->escape(), + 'helper' => $helper, + 'url' => $container->get('aura-router')->getGenerator(), + ]; + + return renderTemplate(TEMPLATE_DIR . '/' . $path, array_merge($baseData, $data)); } } \ No newline at end of file diff --git a/src/AnimeClient/Component/MangaCover.php b/src/AnimeClient/Component/MangaCover.php new file mode 100644 index 00000000..422e4ecc --- /dev/null +++ b/src/AnimeClient/Component/MangaCover.php @@ -0,0 +1,31 @@ + + * @copyright 2015 - 2020 Timothy J. Warren + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @version 5.1 + * @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient + */ + +namespace Aviat\AnimeClient\Component; + +use Aviat\AnimeClient\Types\MangaListItem; + +final class MangaCover { + use ComponentTrait; + + public function __invoke(MangaListItem $item, string $name): string + { + return $this->render('manga-cover.php', [ + 'item' => $item, + 'name' => $name, + ]); + } +} \ No newline at end of file diff --git a/src/AnimeClient/Component/Tabs.php b/src/AnimeClient/Component/Tabs.php index 91c9d572..f30276e8 100644 --- a/src/AnimeClient/Component/Tabs.php +++ b/src/AnimeClient/Component/Tabs.php @@ -26,13 +26,16 @@ final class Tabs { * also used to generate id attributes * @param array $tabData The data used to create the tab content, indexed by the tab label * @param callable $cb The function to generate the tab content + * @param string $className + * @param bool $hasSectionWrapper * @return string */ public function __invoke( string $name, array $tabData, callable $cb, - string $className = 'content media-wrap flex flex-wrap flex-justify-start' + string $className = 'content media-wrap flex flex-wrap flex-justify-start', + bool $hasSectionWrapper = false ): string { return $this->render('tabs.php', [ @@ -40,6 +43,7 @@ final class Tabs { 'data' => $tabData, 'callback' => $cb, 'className' => $className, + 'hasSectionWrapper' => $hasSectionWrapper, ]); } } \ No newline at end of file diff --git a/src/AnimeClient/Controller/AnimeCollection.php b/src/AnimeClient/Controller/AnimeCollection.php index 547f2aa0..10aaf9d6 100644 --- a/src/AnimeClient/Controller/AnimeCollection.php +++ b/src/AnimeClient/Controller/AnimeCollection.php @@ -101,10 +101,14 @@ final class AnimeCollection extends BaseController { 'list' => 'list' ]; + $sections = array_merge( + ['All' => $this->animeCollectionModel->getFlatCollection()], + $this->animeCollectionModel->getCollection() + ); + $this->outputHTML('collection/' . $viewMap[$view], [ 'title' => $this->config->get('whose_list') . "'s Anime Collection", - 'sections' => $this->animeCollectionModel->getCollection(), - 'all' => $this->animeCollectionModel->getFlatCollection(), + 'sections' => $sections, ]); }