Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blog UiElement of articles #5

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions src/Form/Type/ArticleSelectionElementType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/*
* This file is part of Monsieur Biz's Blog plugin for Sylius.
* (c) Monsieur Biz <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace MonsieurBiz\SyliusBlogPlugin\Form\Type;

use MonsieurBiz\SyliusBlogPlugin\Entity\Article;
use MonsieurBiz\SyliusBlogPlugin\Entity\ArticleInterface;
use MonsieurBiz\SyliusBlogPlugin\Repository\ArticleRepositoryInterface;
use Sylius\Bundle\ResourceBundle\Form\DataTransformer\ResourceToIdentifierTransformer;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Locale\Context\LocaleContextInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\ReversedTransformer;
use Symfony\Component\Validator\Constraints as Assert;

final class ArticleSelectionElementType extends AbstractType
{
public function __construct(
private readonly ArticleRepositoryInterface $articleRepository,
private readonly ChannelContextInterface $channelContext,
private readonly LocaleContextInterface $localeContext,
) {
}

/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$articles = $this->articleRepository->createShopListQueryBuilderByType(
$this->localeContext->getLocaleCode(),
ArticleInterface::BLOG_TYPE,
$this->channelContext->getChannel(),
null
);

$articles = $articles->orderBy('translation.title')->getQuery()->getResult();

$builder
->add('article', EntityType::class, [
'class' => Article::class,
'label' => 'monsieurbiz_blog.ui_element.articles_selection_ui_element.fields.article',
'choice_label' => fn (Article $article) => $article->getTitle(),
'choice_value' => fn (?Article $article) => $article?->getId(),
'required' => true,
'choices' => $articles,
])
delyriand marked this conversation as resolved.
Show resolved Hide resolved
->add('position', IntegerType::class, [
'label' => 'monsieurbiz_blog.ui_element.articles_selection_ui_element.fields.position',
'required' => true,
'constraints' => [
new Assert\NotBlank(),
new Assert\GreaterThan(0),
],
])
;

$builder->get('article')->addModelTransformer(
new ReversedTransformer(new ResourceToIdentifierTransformer($this->articleRepository, 'id')),
);
}
}
43 changes: 43 additions & 0 deletions src/Form/Type/ArticlesDisplayType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of Monsieur Biz's Blog plugin for Sylius.
* (c) Monsieur Biz <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace MonsieurBiz\SyliusBlogPlugin\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;

final class ArticlesDisplayType extends AbstractType
{
public const MULTIPLE_WITH_IMAGE = 'multiple_with_image';

public const MULTIPLE_WITHOUT_IMAGE = 'multiple_without_image';

public const SINGLE = 'single';

/**
* @SuppressWarnings(PHPMD.UnusedFormalParameters)
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('display', ChoiceType::class, [
'label' => 'monsieurbiz_blog.articles.display.label',
'required' => true,
'choices' => [
'monsieurbiz_blog.articles.display.choices.multiple_with_image' => self::MULTIPLE_WITH_IMAGE,
'monsieurbiz_blog.articles.display.choices.multiple_without_image' => self::MULTIPLE_WITHOUT_IMAGE,
'monsieurbiz_blog.articles.display.choices.single' => self::SINGLE,
],
])
;
}
}
112 changes: 112 additions & 0 deletions src/Form/Type/UiElement/ArticlesByTagsUiElementType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

/*
* This file is part of Monsieur Biz's Blog plugin for Sylius.
* (c) Monsieur Biz <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace MonsieurBiz\SyliusBlogPlugin\Form\Type\UiElement;

use MonsieurBiz\SyliusBlogPlugin\Entity\Tag;
use MonsieurBiz\SyliusBlogPlugin\Form\Type\ArticlesDisplayType;
use MonsieurBiz\SyliusBlogPlugin\Repository\TagRepositoryInterface;
use MonsieurBiz\SyliusRichEditorPlugin\Attribute\AsUiElement;
use MonsieurBiz\SyliusRichEditorPlugin\Attribute\TemplatesUiElement;
use Sylius\Component\Locale\Context\LocaleContextInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints as Assert;

#[AsUiElement(
code: 'monsieurbiz_blog.articles_by_tags_ui_element',
icon: 'tags',
title: 'monsieurbiz_blog.ui_element.articles_by_tags_ui_element.title',
description: 'monsieurbiz_blog.ui_element.articles_by_tags_ui_element.description',
uiElement: 'MonsieurBiz\SyliusBlogPlugin\UiElement\ArticlesByTagsUiElement',
templates: new TemplatesUiElement(
adminRender: '@MonsieurBizSyliusBlogPlugin/Admin/UiElement/articles_by_tags.html.twig',
frontRender: '@MonsieurBizSyliusBlogPlugin/Shop/UiElement/articles_by_tags.html.twig',
),
wireframe: 'articles-by-tags',
tags: [],
)]
class ArticlesByTagsUiElementType extends AbstractType
{
public function __construct(
private readonly TagRepositoryInterface $tagRepository,
private readonly LocaleContextInterface $localeContext,
) {
}

/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$tags = $this->tagRepository->createListQueryBuilder(
$this->localeContext->getLocaleCode(),
);

$tags = $tags->orderBy('translation.name')->getQuery()->getResult();
etienne-monsieurbiz marked this conversation as resolved.
Show resolved Hide resolved

$builder
->add('title', TextType::class, [
'label' => 'monsieurbiz_blog.ui_element.articles_by_tags_ui_element.fields.title',
'required' => false,
])
->add('display', ArticlesDisplayType::class, [
'label' => false, // already defined in the ArticlesDisplayType
])
->add('tags', EntityType::class, [
'label' => 'monsieurbiz_blog.ui_element.articles_by_tags_ui_element.fields.tags',
'required' => false,
'class' => Tag::class,
'choice_label' => fn (Tag $tag) => $tag->getName(),
'choice_value' => fn (?Tag $tag) => $tag?->getId(),
'choices' => $tags,
'multiple' => true,
])
->add('limit', IntegerType::class, [
'label' => 'monsieurbiz_blog.ui_element.articles_by_tags_ui_element.fields.limit',
'help' => 'monsieurbiz_blog.ui_element.articles_by_tags_ui_element.help.limit',
'required' => true,
'constraints' => [
new Assert\NotBlank(),
new Assert\GreaterThan(0),
],
])
->add('btnLabel', TextType::class, [
'label' => 'monsieurbiz_blog.ui_element.articles_by_tags_ui_element.fields.btn_label',
'required' => false,
])
->add('btnUrl', TextType::class, [
'label' => 'monsieurbiz_blog.ui_element.articles_by_tags_ui_element.fields.btn_url',
'required' => false,
])
;

$builder->get('tags')->addModelTransformer(
new CallbackTransformer(
function ($tagsAsArray) {
return $this->tagRepository->findBy(['id' => $tagsAsArray ?? []]);
},
function ($tagsAsString) {
$tags = [];
foreach ($tagsAsString as $tag) {
$tags[] = $tag->getId();
}

return $tags;
}
),
);
}
}
93 changes: 93 additions & 0 deletions src/Form/Type/UiElement/ArticlesSelectionUiElementType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

/*
* This file is part of Monsieur Biz's Blog plugin for Sylius.
* (c) Monsieur Biz <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace MonsieurBiz\SyliusBlogPlugin\Form\Type\UiElement;

use MonsieurBiz\SyliusBlogPlugin\Form\Type\ArticlesDisplayType;
use MonsieurBiz\SyliusBlogPlugin\Form\Type\ArticleSelectionElementType;
use MonsieurBiz\SyliusRichEditorPlugin\Attribute\AsUiElement;
use MonsieurBiz\SyliusRichEditorPlugin\Attribute\TemplatesUiElement;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Validator\Constraints as Assert;

#[AsUiElement(
code: 'monsieurbiz_blog.articles_selection_ui_element',
icon: 'newspaper',
title: 'monsieurbiz_blog.ui_element.articles_selection_ui_element.title',
description: 'monsieurbiz_blog.ui_element.articles_selection_ui_element.description',
uiElement: 'MonsieurBiz\SyliusBlogPlugin\UiElement\ArticlesSelectionUiElement',
templates: new TemplatesUiElement(
adminRender: '@MonsieurBizSyliusBlogPlugin/Admin/UiElement/articles_selection.html.twig',
frontRender: '@MonsieurBizSyliusBlogPlugin/Shop/UiElement/articles_selection.html.twig',
),
wireframe: 'articles-selection',
tags: [],
)]
class ArticlesSelectionUiElementType extends AbstractType
{
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('title', TextType::class, [
'label' => 'monsieurbiz_blog.ui_element.articles_selection_ui_element.fields.title',
'required' => false,
])
->add('display', ArticlesDisplayType::class, [
'label' => false, // already defined in the ArticlesDisplayType
])
->add('articles', CollectionType::class, [
'label' => 'monsieurbiz_blog.ui_element.articles_selection_ui_element.fields.articles',
'entry_type' => ArticleSelectionElementType::class,
'prototype_name' => '__article_selection__',
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'delete_empty' => true,
'attr' => [
'class' => 'ui segment secondary collection--flex',
],
'constraints' => [
new Assert\Count(['min' => 1]),
],
])
->add('btnLabel', TextType::class, [
'label' => 'monsieurbiz_blog.ui_element.articles_selection_ui_element.fields.btn_label',
'required' => false,
])
->add('btnUrl', TextType::class, [
maximehuran marked this conversation as resolved.
Show resolved Hide resolved
'label' => 'monsieurbiz_blog.ui_element.articles_selection_ui_element.fields.btn_url',
'required' => false,
])
maximehuran marked this conversation as resolved.
Show resolved Hide resolved
;
}

/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function finishView(FormView $view, FormInterface $form, array $options): void
{
usort($view['articles']->children, function (FormView $articleA, FormView $articleB) {
return match (true) {
!$articleA->offsetExists('position') => -1,
!$articleB->offsetExists('position') => 1,
default => $articleA['position']->vars['data'] <=> $articleB['position']->vars['data']
};
});
}
}
4 changes: 2 additions & 2 deletions src/Form/Type/UiElement/CaseStudiesUiElementType.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
#[AsUiElement(
code: 'monsieurbiz_blog.case_studies_ui_element',
icon: 'crosshairs',
uiElement: 'MonsieurBiz\SyliusBlogPlugin\UiElement\CaseStudiesUiElement',
title: 'monsieurbiz_blog.ui_element.case_studies_ui_element.title',
description: 'monsieurbiz_blog.ui_element.case_studies_ui_element.description',
uiElement: 'MonsieurBiz\SyliusBlogPlugin\UiElement\CaseStudiesUiElement',
etienne-monsieurbiz marked this conversation as resolved.
Show resolved Hide resolved
templates: new TemplatesUiElement(
adminRender: '@MonsieurBizSyliusBlogPlugin/Admin/UiElement/case_studies.html.twig',
frontRender: '@MonsieurBizSyliusBlogPlugin/Shop/UiElement/case_studies.html.twig',
),
tags: [],
wireframe: 'case-studies',
tags: [],
)]
class CaseStudiesUiElementType extends AbstractType
{
Expand Down
25 changes: 25 additions & 0 deletions src/Repository/ArticleRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,31 @@ public function findAllEnabledAndPublishedByTag(string $localeCode, string $type
;
}

public function findAllEnabledAndPublishedByTags(string $localeCode, string $type, ChannelInterface $channel, array $tags, int $limit): array
{
$queryBuilder = $this->createListQueryBuilderByType($localeCode, $type)
->andWhere(':channel MEMBER OF ba.channels')
->andWhere('ba.enabled = true')
->andWhere('ba.state = :state')
->setParameter('channel', $channel)
->setParameter('state', ArticleInterface::STATE_PUBLISHED)
->setMaxResults($limit)
->orderBy('ba.publishedAt', 'DESC')
;

if (!empty($tags)) {
$queryBuilder
->andWhere(':tags MEMBER OF ba.tags')
->setParameter('tags', $tags)
;
}

return $queryBuilder
->getQuery()
->getResult()
;
}

public function findOnePublishedBySlug(string $slug, string $localeCode, string $type, ChannelInterface $channel): ?ArticleInterface
{
return $this->createListQueryBuilderByType($localeCode, $type)
Expand Down
5 changes: 5 additions & 0 deletions src/Repository/ArticleRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public function createShopListQueryBuilderByType(string $localeCode, string $typ
*/
public function findAllEnabledAndPublishedByTag(string $localeCode, string $type, ChannelInterface $channel, TagInterface $tag, int $limit): array;

/**
* @return ArticleInterface[]
*/
public function findAllEnabledAndPublishedByTags(string $localeCode, string $type, ChannelInterface $channel, array $tags, int $limit): array;

public function findOnePublishedBySlug(string $slug, string $localeCode, string $type, ChannelInterface $channel): ?ArticleInterface;

public function findAllEnabledAndPublishedByAuthor(string $localeCode, string $type, ChannelInterface $channel, AuthorInterface $author, int $limit): array;
Expand Down
Loading
Loading