add similar posts

This commit is contained in:
Simon Vieille 2022-09-09 20:10:51 +02:00
parent e87510b130
commit f33e16b198
Signed by: deblan
GPG key ID: 579388D585F70417
8 changed files with 128 additions and 8 deletions

View file

@ -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;

View file

@ -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) {

View file

@ -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:

View file

@ -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(),
]);
}

View file

@ -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;
}
}

View file

@ -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')

View file

@ -104,6 +104,37 @@
</div>
{% if full %}
{% if similarPosts is defined and similarPosts|length > 0 %}
<div class="body">
<div class="col-12 meshes meshes--posts">
<div class="row">
{% for item in similarPosts %}
<div class="col-4 mesh-wrapper">
<div class="mesh">
{% set url = safe_path('blog_menu_post', {post: item.id, slug: item.slug, _domain: _domain}) %}
{%- if item.image -%}
<div class="mesh-preview">
<a href="{{ url }}">
{% set image = asset(item.image)|imagine_filter('post_preview_filter') %}
<img src="{{ asset('build/images/px.png') }}" data-src="{{ image }}" alt="{{ item.title }}">
</a>
</div>
{%- endif -%}
<h2 class="mesh-title">
<a href="{{- url -}}">
{{- item.title -}}
</a>
</h2>
</div>
</div>
{% endfor %}
</div>
</div>
</div>
{% endif %}
<div class="col-12">
<div class="reviews">
<hr>

View file

@ -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"