From f33e16b1984cfdf3d1e9ce0271377354824c495e Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Fri, 9 Sep 2022 20:10:51 +0200 Subject: [PATCH] add similar posts --- assets/css/app.scss | 27 +++++++-- assets/js/app/quick-post.js | 2 +- config/packages/liip_imagine.yaml | 6 ++ src/Controller/Blog/PostController.php | 3 + src/Repository/Blog/PostRepository.php | 61 +++++++++++++++++++++ src/Repository/Blog/PostRepositoryQuery.php | 4 +- templates/page/post/_post.html.twig | 31 +++++++++++ yarn.lock | 2 +- 8 files changed, 128 insertions(+), 8 deletions(-) diff --git a/assets/css/app.scss b/assets/css/app.scss index 895b97a..2150af6 100644 --- a/assets/css/app.scss +++ b/assets/css/app.scss @@ -336,6 +336,11 @@ pre[class*="language-"] { .body { padding: 25px 40px; max-width: $content-max-width; + + &.body-full { + max-width: 100%; + } + margin-left: auto; margin-right: auto; @@ -804,10 +809,6 @@ $links: ( } } -.meshes { - padding: 0 20px 20px 20px; -} - .mesh { border: 1px solid $color-hr-border; border-radius: 10px; @@ -817,6 +818,7 @@ $links: ( img { border-top-left-radius: 10px; border-top-right-radius: 10px; + width: 100%; } } @@ -846,6 +848,23 @@ $links: ( } } + +.meshes { + padding: 0 20px 20px 20px; + + &--posts { + padding-left: 0; + padding-right: 0; + + .mesh-preview { + img { + width: 100%; + max-height: 200px; + } + } + } +} + .ejs-link { margin: 10px auto; border: 2px solid $color-very-light-grey; diff --git a/assets/js/app/quick-post.js b/assets/js/app/quick-post.js index a9e254b..1fb5b87 100644 --- a/assets/js/app/quick-post.js +++ b/assets/js/app/quick-post.js @@ -6,7 +6,7 @@ class QuickPost { init() { const doc = this.window.document - const images = doc.querySelectorAll('.quick-image img') + const images = doc.querySelectorAll('.quick-image img, .mesh-preview img') for (let i = 0, len = images.length; i < len; i++) { (function(image) { diff --git a/config/packages/liip_imagine.yaml b/config/packages/liip_imagine.yaml index 812e1d2..e3cd592 100644 --- a/config/packages/liip_imagine.yaml +++ b/config/packages/liip_imagine.yaml @@ -19,6 +19,12 @@ liip_imagine: max: [600, 600] crop: size: [600, 270] + post_preview_filter: + filters: + downscale: + max: [600, 600] + crop: + size: [600, 300] site_avatar: filters: downscale: diff --git a/src/Controller/Blog/PostController.php b/src/Controller/Blog/PostController.php index 8004f91..ff8e25e 100644 --- a/src/Controller/Blog/PostController.php +++ b/src/Controller/Blog/PostController.php @@ -85,8 +85,11 @@ class PostController extends PageController $this->addFlash('error', 'Le formulaire n\'est pas valide.'); } + $similarPosts = $this->postQuery->getRepository()->findSimilarPosts($post, 3); + return $this->defaultRender($this->siteRequest->getPage()->getTemplate(), [ 'post' => $post, + 'similarPosts' => $similarPosts, 'form' => $form->createView(), ]); } diff --git a/src/Repository/Blog/PostRepository.php b/src/Repository/Blog/PostRepository.php index 78aa905..acd84ab 100644 --- a/src/Repository/Blog/PostRepository.php +++ b/src/Repository/Blog/PostRepository.php @@ -17,4 +17,65 @@ class PostRepository extends ServiceEntityRepository { return $this->getEntityManager(); } + + public function findSimilarPosts(Post $post, int $quantity = 5): array + { + $query1 = $this->createQueryBuilder('p'); + $query2 = $this->createQueryBuilder('p'); + + $query1->orderBy('p.publishedAt', 'DESC'); + + foreach ($post->getTags() as $k => $entity) { + $query1 + ->orWhere('p.tags LIKE :tag'.$k) + ->setParameter(':tag'.$k, '%"'.$entity['label'].'"%') + ; + } + + $query2 + ->orderBy('p.publishedAt', 'DESC') + ->andWhere('p.status = 1') + ->join('p.categories', 'c') + ->where('c.id IN (:ids)') + ->setParameter(':ids', array_map( + fn ($e) => $e->getId(), + $post->getCategories()->getValues() + )) + ; + + $search1 = $query1->getQuery()->getResult(); + $search2 = $query2->getQuery()->getResult(); + $now = new \DateTime(); + $posts = []; + + foreach ([$search1, $search2] as $results) { + foreach ($results as $result) { + if (count($posts) === $quantity) { + continue 2; + } + + if ($result->getId() === $post->getId()) { + continue; + } + + if (Post::DRAFT === $result->getStatus()) { + continue; + } + + if (null === $result->getPublishedAt()) { + continue; + } + + if ($result->getPublishedAt() > $now) { + continue; + } + + if (!isset($posts[$result->getId()])) { + $posts[$result->getId()] = $result; + } + } + } + + return $posts; + } } diff --git a/src/Repository/Blog/PostRepositoryQuery.php b/src/Repository/Blog/PostRepositoryQuery.php index d5623ed..0def03f 100644 --- a/src/Repository/Blog/PostRepositoryQuery.php +++ b/src/Repository/Blog/PostRepositoryQuery.php @@ -18,7 +18,7 @@ class PostRepositoryQuery extends RepositoryQuery parent::__construct($repository, 'p', $paginator); } - public function inCategory(Category $category, bool $strict = true) + public function inCategory(Category $category, bool $strict = true): self { $c = 'c'.mt_rand(); @@ -45,7 +45,7 @@ class PostRepositoryQuery extends RepositoryQuery return $this; } - public function published() + public function published(): self { return $this ->andWhere('.status = 1') diff --git a/templates/page/post/_post.html.twig b/templates/page/post/_post.html.twig index 79751bd..684e23f 100644 --- a/templates/page/post/_post.html.twig +++ b/templates/page/post/_post.html.twig @@ -104,6 +104,37 @@ {% if full %} + {% if similarPosts is defined and similarPosts|length > 0 %} +
+
+
+ {% for item in similarPosts %} +
+
+ {% set url = safe_path('blog_menu_post', {post: item.id, slug: item.slug, _domain: _domain}) %} + + {%- if item.image -%} + + {%- endif -%} + +

+ + {{- item.title -}} + +

+
+
+ {% endfor %} +
+
+
+ {% endif %} +

diff --git a/yarn.lock b/yarn.lock index c7facf0..1f83712 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6461,7 +6461,7 @@ parseurl@~1.3.2, parseurl@~1.3.3: particles.js@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/particles.js/-/particles.js-2.0.0.tgz#21386c4328d6c7f96780a201e96eedfc09c736f6" - integrity sha1-IThsQyjWx/lngKIB6W7t/AnHNvY= + integrity sha512-8e0JIqkRbMMPlFBnF9f+92hX1s07jdkd3tqB8uHE9L+cwGGjIYjQM7QLgt0FQ5MZp6SFFYYDm/Y48pqK3ZvJOQ== particlesjs@^2.2.3: version "2.2.3"