From 9da848ccb6d2413cd4c7e05c3ee129bd043cdc2c Mon Sep 17 00:00:00 2001 From: Jonas Leder <jonas@jonasled.de> Date: Tue, 14 Feb 2023 09:53:55 +0100 Subject: [PATCH 1/4] rename blog template --- src/Controller/BlogController.php | 2 +- templates/pages/{blog.html.twig => blog-entry.html.twig} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename templates/pages/{blog.html.twig => blog-entry.html.twig} (100%) diff --git a/src/Controller/BlogController.php b/src/Controller/BlogController.php index cceb53b3..b4c5f30c 100644 --- a/src/Controller/BlogController.php +++ b/src/Controller/BlogController.php @@ -17,7 +17,7 @@ class BlogController extends AbstractController if (!$blogEntry) { throw $this->createNotFoundException(); } - return $this->render('pages/blog.html.twig', [ + return $this->render('pages/blog-entry.html.twig', [ 'blog' => $blogEntry ]); } diff --git a/templates/pages/blog.html.twig b/templates/pages/blog-entry.html.twig similarity index 100% rename from templates/pages/blog.html.twig rename to templates/pages/blog-entry.html.twig -- GitLab From fe229af8ae52b247c2ea1491618006f9950824e6 Mon Sep 17 00:00:00 2001 From: Jonas Leder <jonas@jonasled.de> Date: Tue, 14 Feb 2023 11:26:14 +0100 Subject: [PATCH 2/4] add page to display all blog entries --- assets/styles/app.scss | 3 +- assets/styles/lib/_pagination.scss | 19 ++ composer.json | 3 + composer.lock | 228 +++++++++++++++++++- config/bundles.php | 1 + src/Controller/BlogController.php | 21 +- symfony.lock | 3 + templates/components/blog_preview.html.twig | 2 +- templates/components/footer.html.twig | 2 +- templates/pages/blog.html.twig | 24 +++ 10 files changed, 301 insertions(+), 5 deletions(-) create mode 100644 assets/styles/lib/_pagination.scss create mode 100644 templates/pages/blog.html.twig diff --git a/assets/styles/app.scss b/assets/styles/app.scss index c6318ff2..d0976935 100644 --- a/assets/styles/app.scss +++ b/assets/styles/app.scss @@ -15,4 +15,5 @@ @import 'lib/_sellingTable'; @import 'lib/_skills'; @import 'lib/_basicLightbox'; -@import 'lib/_pwgen'; \ No newline at end of file +@import 'lib/_pwgen'; +@import 'lib/_pagination'; \ No newline at end of file diff --git a/assets/styles/lib/_pagination.scss b/assets/styles/lib/_pagination.scss new file mode 100644 index 00000000..bcc9fd6a --- /dev/null +++ b/assets/styles/lib/_pagination.scss @@ -0,0 +1,19 @@ +.pagination { + display: inline-block; + a { + color: $text-color; + float: left; + padding: 8px 16px; + text-decoration: none; + + &.active { + background-color: $accent-color; + color: black; + } + + &:hover { + background-color: $accent-color-2; + color: black; + } + } +} \ No newline at end of file diff --git a/composer.json b/composer.json index c14f702a..3d4fd08e 100644 --- a/composer.json +++ b/composer.json @@ -8,6 +8,7 @@ "ext-ctype": "*", "ext-iconv": "*", "aws/aws-sdk-php": "^3.257", + "babdev/pagerfanta-bundle": "^3.7", "doctrine/doctrine-bundle": "^2.8", "doctrine/doctrine-migrations-bundle": "^3.2", "doctrine/orm": "^2.14", @@ -15,6 +16,8 @@ "erusev/parsedown": "^1.7", "gregwar/captcha": "^1.1", "knplabs/knp-time-bundle": "^1.20", + "pagerfanta/doctrine-orm-adapter": "^3.7", + "pagerfanta/twig": "^3.7", "symfony/console": "6.2.*", "symfony/dotenv": "6.2.*", "symfony/flex": "^2", diff --git a/composer.lock b/composer.lock index 9f066fa4..753e981d 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "b7ed8d333430e393ceaf403cf82be662", + "content-hash": "a8a88884414c630f8a91b748f1b3c1d1", "packages": [ { "name": "aws/aws-crt-php", @@ -150,6 +150,89 @@ }, "time": "2023-01-13T19:29:27+00:00" }, + { + "name": "babdev/pagerfanta-bundle", + "version": "v3.7.0", + "source": { + "type": "git", + "url": "https://github.com/BabDev/PagerfantaBundle.git", + "reference": "d2760ebc3d22db10f219544323bea432dedc9eb0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/BabDev/PagerfantaBundle/zipball/d2760ebc3d22db10f219544323bea432dedc9eb0", + "reference": "d2760ebc3d22db10f219544323bea432dedc9eb0", + "shasum": "" + }, + "require": { + "pagerfanta/core": "^3.1", + "php": "^7.4 || ^8.0", + "symfony/config": "^4.4 || ^5.4 || ^6.0", + "symfony/dependency-injection": "^4.4 || ^5.4 || ^6.0", + "symfony/deprecation-contracts": "^2.1 || ^3.0", + "symfony/http-foundation": "^4.4 || ^5.4 || ^6.0", + "symfony/http-kernel": "^4.4 || ^5.4 || ^6.0", + "symfony/polyfill-php80": "^1.15", + "symfony/property-access": "^4.4 || ^5.4 || ^6.0", + "symfony/routing": "^4.4 || ^5.4 || ^6.0" + }, + "conflict": { + "pagerfanta/twig": "<3.1", + "twig/twig": "<1.35 || >=2.0,<2.5", + "white-october/pagerfanta-bundle": "*" + }, + "require-dev": { + "doctrine/annotations": "^1.8", + "jms/serializer": "^3.0", + "jms/serializer-bundle": "^3.0 || ^4.0", + "matthiasnoback/symfony-dependency-injection-test": "^4.3", + "pagerfanta/twig": "^3.1", + "phpstan/extension-installer": "^1.1", + "phpstan/phpstan": "1.7.2", + "phpstan/phpstan-phpunit": "1.1.1", + "phpstan/phpstan-symfony": "1.2.0", + "phpunit/phpunit": "^9.5", + "symfony/phpunit-bridge": "^5.4 || ^6.0", + "symfony/serializer": "^4.4 || ^5.4 || ^6.0", + "symfony/translation": "^4.4 || ^5.4 || ^6.0", + "symfony/twig-bridge": "^4.4 || ^5.4 || ^6.0", + "symfony/twig-bundle": "^4.4 || ^5.4 || ^6.0", + "twig/twig": "^1.35 || ^2.5 || ^3.0" + }, + "suggest": { + "jms/serializer-bundle": "To use the Pagerfanta class with the JMS Serializer", + "symfony/serializer": "To use the Pagerfanta class with the Symfony Serializer", + "symfony/translation": "To use the Twig templates with translation support", + "twig/twig": "To integrate Pagerfanta with Twig through extensions" + }, + "type": "symfony-bundle", + "autoload": { + "psr-4": { + "BabDev\\PagerfantaBundle\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Bundle integrating Pagerfanta with Symfony", + "keywords": [ + "pagerfanta", + "pagination", + "symfony" + ], + "support": { + "issues": "https://github.com/BabDev/PagerfantaBundle/issues", + "source": "https://github.com/BabDev/PagerfantaBundle/tree/v3.7.0" + }, + "funding": [ + { + "url": "https://github.com/mbabker", + "type": "github" + } + ], + "time": "2022-05-27T20:58:26+00:00" + }, { "name": "doctrine/cache", "version": "2.2.0", @@ -2425,6 +2508,149 @@ }, "time": "2022-11-12T15:38:23+00:00" }, + { + "name": "pagerfanta/core", + "version": "v3.7.0", + "source": { + "type": "git", + "url": "https://github.com/Pagerfanta/core.git", + "reference": "53c0bfdffc18fb816ae5c08e4bdda0773651bacc" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Pagerfanta/core/zipball/53c0bfdffc18fb816ae5c08e4bdda0773651bacc", + "reference": "53c0bfdffc18fb816ae5c08e4bdda0773651bacc", + "shasum": "" + }, + "require": { + "ext-json": "*", + "php": "^7.4 || ^8.0", + "symfony/deprecation-contracts": "^2.1 || ^3.0", + "symfony/polyfill-php80": "^1.15" + }, + "require-dev": { + "phpunit/phpunit": "^9.5", + "symfony/phpunit-bridge": "^5.4 || ^6.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Pagerfanta\\": "./" + }, + "exclude-from-classmap": [ + "Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Core Pagerfanta API", + "keywords": [ + "pagerfanta" + ], + "support": { + "source": "https://github.com/Pagerfanta/core/tree/v3.7.0" + }, + "time": "2022-12-01T22:53:57+00:00" + }, + { + "name": "pagerfanta/doctrine-orm-adapter", + "version": "v3.7.0", + "source": { + "type": "git", + "url": "https://github.com/Pagerfanta/doctrine-orm-adapter.git", + "reference": "a76dccd512cd31c5400030e39bc0793caa13da51" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Pagerfanta/doctrine-orm-adapter/zipball/a76dccd512cd31c5400030e39bc0793caa13da51", + "reference": "a76dccd512cd31c5400030e39bc0793caa13da51", + "shasum": "" + }, + "require": { + "doctrine/orm": "^2.8", + "pagerfanta/core": "^3.0", + "php": "^7.4 || ^8.0", + "symfony/deprecation-contracts": "^2.1 || ^3.0" + }, + "require-dev": { + "doctrine/annotations": "^1.11.1", + "doctrine/cache": "^1.11 || ^2.0", + "phpunit/phpunit": "^9.5", + "symfony/cache": "^4.4 || ^5.4 || ^6.0", + "symfony/phpunit-bridge": "^5.4 || ^6.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Pagerfanta\\Doctrine\\ORM\\": "./" + }, + "exclude-from-classmap": [ + "Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Pagerfanta adapter for Doctrine ORM", + "keywords": [ + "doctrine", + "orm", + "pagerfanta" + ], + "support": { + "source": "https://github.com/Pagerfanta/doctrine-orm-adapter/tree/v3.7.0" + }, + "time": "2022-12-01T22:53:57+00:00" + }, + { + "name": "pagerfanta/twig", + "version": "v3.7.0", + "source": { + "type": "git", + "url": "https://github.com/Pagerfanta/twig.git", + "reference": "e8f60980242db0e744ef926a0263ff3722f502bf" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Pagerfanta/twig/zipball/e8f60980242db0e744ef926a0263ff3722f502bf", + "reference": "e8f60980242db0e744ef926a0263ff3722f502bf", + "shasum": "" + }, + "require": { + "pagerfanta/core": "^3.0", + "php": "^7.4 || ^8.0", + "symfony/polyfill-php80": "^1.15", + "twig/twig": "^1.35 || ^2.5 || ^3.0" + }, + "require-dev": { + "phpunit/phpunit": "^9.5", + "symfony/phpunit-bridge": "^5.4 || ^6.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Pagerfanta\\Twig\\": "./" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Twig integration for Pagerfanta", + "keywords": [ + "pagerfanta" + ], + "support": { + "source": "https://github.com/Pagerfanta/twig/tree/v3.7.0" + }, + "time": "2022-12-01T22:53:57+00:00" + }, { "name": "psr/cache", "version": "3.0.0", diff --git a/config/bundles.php b/config/bundles.php index c04e1849..daae8b61 100644 --- a/config/bundles.php +++ b/config/bundles.php @@ -15,4 +15,5 @@ return [ Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true], EasyCorp\Bundle\EasyAdminBundle\EasyAdminBundle::class => ['all' => true], Knp\Bundle\TimeBundle\KnpTimeBundle::class => ['all' => true], + BabDev\PagerfantaBundle\BabDevPagerfantaBundle::class => ['all' => true], ]; diff --git a/src/Controller/BlogController.php b/src/Controller/BlogController.php index b4c5f30c..26ef67f2 100644 --- a/src/Controller/BlogController.php +++ b/src/Controller/BlogController.php @@ -3,14 +3,17 @@ namespace App\Controller; use App\Repository\BlogRepository; +use Pagerfanta\Doctrine\ORM\QueryAdapter; +use Pagerfanta\Pagerfanta; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\Routing\Annotation\Route; class BlogController extends AbstractController { - #[Route('/blog/{id}', methods: ['GET'], name: 'blog')] + #[Route('/blog/{id}', methods: ['GET'], name: 'blogEntry')] public function getBlogEntry(int $id, BlogRepository $blogRepository): Response { $blogEntry = $blogRepository->find($id); @@ -21,4 +24,20 @@ class BlogController extends AbstractController 'blog' => $blogEntry ]); } + + #[Route('/blog', methods: ['GET'], name: 'blog')] + public function getBlog(BlogRepository $blogRepository, Request $request): Response + { + $queryBuilder = $blogRepository->createQueryBuilder('b'); + $queryBuilder->orderBy('b.date', 'DESC'); + + $pagination = Pagerfanta::createForCurrentPageWithMaxPerPage( + new QueryAdapter($queryBuilder), + $request->query->get('page', 1), + 20); + + return $this->render('pages/blog.html.twig', [ + 'pagination' => $pagination, + ]); + } } diff --git a/symfony.lock b/symfony.lock index 5bbdd4f5..4af3b1a1 100644 --- a/symfony.lock +++ b/symfony.lock @@ -1,4 +1,7 @@ { + "babdev/pagerfanta-bundle": { + "version": "v3.7.0" + }, "doctrine/doctrine-bundle": { "version": "2.8", "recipe": { diff --git a/templates/components/blog_preview.html.twig b/templates/components/blog_preview.html.twig index 3bedbcd8..87c16b4c 100644 --- a/templates/components/blog_preview.html.twig +++ b/templates/components/blog_preview.html.twig @@ -3,7 +3,7 @@ <h2>{{ this.title }}</h2> <p class="breakWord">{{ this.blog.content | striptags |u.truncate(200, ' ...', false) }}.</p> <p class="center"> - <a href="{{ path('blog', {id: this.id}) }}"> + <a href="{{ path('blogEntry', {id: this.id}) }}"> <button>Mehr lesen</button> </a> </p> diff --git a/templates/components/footer.html.twig b/templates/components/footer.html.twig index b5e6e0da..00a0a9d5 100644 --- a/templates/components/footer.html.twig +++ b/templates/components/footer.html.twig @@ -9,7 +9,7 @@ <div id="newestPost"><h3>Neueste Beiträge</h3> <ul> {% for blog in blogEntries %} - <li><a href="{{ path('blog', {id: blog.id}) }}">{{ blog.title }}</a></li> + <li><a href="{{ path('blogEntry', {id: blog.id}) }}">{{ blog.title }}</a></li> {% endfor %} </ul> </div> diff --git a/templates/pages/blog.html.twig b/templates/pages/blog.html.twig new file mode 100644 index 00000000..70f2e437 --- /dev/null +++ b/templates/pages/blog.html.twig @@ -0,0 +1,24 @@ +{% extends 'base.html.twig' %} +{% block title %}Blog{% endblock %} +{% block content %} + <ul> + {% for blogEntry in pagination %} + <li><a href="{{ path('blogEntry', {id: blogEntry.id}) }}">{{ blogEntry.date | date("d.m.Y") }} + | {{ blogEntry.title }}</a></li> + {% endfor %} + </ul> + {% if pagination.haveToPaginate %} + <div class="pagination"> + {% if pagination.hasPreviousPage %} + <a href="{{ pagerfanta_page_url(pagination, pagination.previousPage) }}">«</a> + {% endif %} + {% for pageNum in range(1, pagination.getNbPages) %} + <a class="{% if pageNum == pagination.getCurrentPage %}active{% endif %}" + href="{{ pagerfanta_page_url(pagination, pageNum) }}">{{ pageNum }}</a> + {% endfor %} + {% if pagination.hasNextPage %} + <a href="{{ pagerfanta_page_url(pagination, pagination.nextPage) }}">»</a> + {% endif %} + </div> + {% endif %} +{% endblock %} \ No newline at end of file -- GitLab From abb5efd5957ae0926840fcd1ffe2b9d6b6735724 Mon Sep 17 00:00:00 2001 From: Jonas Leder <jonas@jonasled.de> Date: Tue, 14 Feb 2023 11:39:49 +0100 Subject: [PATCH 3/4] use better rendering for blog list --- assets/styles/app.scss | 3 ++- assets/styles/lib/_blogPage.scss | 6 ++++++ templates/pages/blog.html.twig | 7 +++---- 3 files changed, 11 insertions(+), 5 deletions(-) create mode 100644 assets/styles/lib/_blogPage.scss diff --git a/assets/styles/app.scss b/assets/styles/app.scss index d0976935..6ee31845 100644 --- a/assets/styles/app.scss +++ b/assets/styles/app.scss @@ -16,4 +16,5 @@ @import 'lib/_skills'; @import 'lib/_basicLightbox'; @import 'lib/_pwgen'; -@import 'lib/_pagination'; \ No newline at end of file +@import 'lib/_pagination'; +@import 'lib/_blogPage'; \ No newline at end of file diff --git a/assets/styles/lib/_blogPage.scss b/assets/styles/lib/_blogPage.scss new file mode 100644 index 00000000..e0d31761 --- /dev/null +++ b/assets/styles/lib/_blogPage.scss @@ -0,0 +1,6 @@ +.blogPage{ + article { + width: 100%; + margin-bottom: 10px; + } +} \ No newline at end of file diff --git a/templates/pages/blog.html.twig b/templates/pages/blog.html.twig index 70f2e437..de5076bb 100644 --- a/templates/pages/blog.html.twig +++ b/templates/pages/blog.html.twig @@ -1,12 +1,11 @@ {% extends 'base.html.twig' %} {% block title %}Blog{% endblock %} {% block content %} - <ul> + <div class="blogPage"> {% for blogEntry in pagination %} - <li><a href="{{ path('blogEntry', {id: blogEntry.id}) }}">{{ blogEntry.date | date("d.m.Y") }} - | {{ blogEntry.title }}</a></li> + {{ component('blog_preview', {id: blogEntry.id}) }} {% endfor %} - </ul> + </div> {% if pagination.haveToPaginate %} <div class="pagination"> {% if pagination.hasPreviousPage %} -- GitLab From a03e33e834a9de1ca230057688444c1547370c9d Mon Sep 17 00:00:00 2001 From: Jonas Leder <jonas@jonasled.de> Date: Tue, 14 Feb 2023 11:54:36 +0100 Subject: [PATCH 4/4] fix phpstan / phpcs problems --- src/Controller/BlogController.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Controller/BlogController.php b/src/Controller/BlogController.php index 26ef67f2..a3a8beb4 100644 --- a/src/Controller/BlogController.php +++ b/src/Controller/BlogController.php @@ -33,8 +33,9 @@ class BlogController extends AbstractController $pagination = Pagerfanta::createForCurrentPageWithMaxPerPage( new QueryAdapter($queryBuilder), - $request->query->get('page', 1), - 20); + (int)$request->query->get('page', '1'), + 20 + ); return $this->render('pages/blog.html.twig', [ 'pagination' => $pagination, -- GitLab