Add tag support
This commit is contained in:
parent
ce10245c51
commit
ae440be40c
24 changed files with 475 additions and 15 deletions
55
src/Controller/Brain/BrainCategoryController.php
Normal file
55
src/Controller/Brain/BrainCategoryController.php
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller\Brain;
|
||||
|
||||
use App\Entity\Category;
|
||||
use App\Form\CategoryType;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
final class BrainCategoryController extends AbstractController
|
||||
{
|
||||
#[Route('/brain/category/list', name: 'brain_categories_list')]
|
||||
public function index(EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
|
||||
$categories = $entityManager->getRepository(Category::class)->findAll();
|
||||
return $this->render('brain/taxonomy/index.html.twig', [
|
||||
'taxonomy' => $categories,
|
||||
'type' => 'Category'
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/brain/category/new', name: 'brain_categories_new')]
|
||||
public function new(EntityManagerInterface $entityManager, Request $request): Response
|
||||
{
|
||||
|
||||
$form = $this->createForm(CategoryType::class);
|
||||
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted()) {
|
||||
$data = $form->getData();
|
||||
$category = new Category();
|
||||
|
||||
$category->setTitle($data->getTitle());
|
||||
|
||||
$entityManager->persist($category);
|
||||
|
||||
$entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute('brain_categories_list');
|
||||
}
|
||||
|
||||
return $this->render('brain/taxonomy/create.html.twig', [
|
||||
'action' => 'New',
|
||||
'form' => $form,
|
||||
'type' => 'Category'
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
@ -43,6 +43,10 @@ final class BrainPhotosController extends AbstractController
|
|||
$photos->setText($data->getText());
|
||||
$photos->setUrl($data->getUrl());
|
||||
|
||||
foreach ($data->getTags() as $tag) {
|
||||
$photos->addTag($tag);
|
||||
}
|
||||
|
||||
$tax = $request->request->all('photos');
|
||||
|
||||
if ($data->getCategory() == null) {
|
||||
|
|
|
|||
|
|
@ -53,6 +53,10 @@ final class BrainPostController extends AbstractController
|
|||
} else {
|
||||
$post->SetPublished(false);
|
||||
}
|
||||
|
||||
foreach ($data->getTags() as $tag) {
|
||||
$post->addTag($tag);
|
||||
}
|
||||
|
||||
$tax = $request->request->all('post');
|
||||
|
||||
|
|
|
|||
56
src/Controller/Brain/BrainTagController.php
Normal file
56
src/Controller/Brain/BrainTagController.php
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller\Brain;
|
||||
|
||||
use App\Entity\Tag;
|
||||
use App\Form\TagType;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
final class BrainTagController extends AbstractController
|
||||
{
|
||||
#[Route('/brain/tag/list', name: 'brain_tags_list')]
|
||||
public function index(EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
|
||||
$tags = $entityManager->getRepository(Tag::class)->findAll();
|
||||
|
||||
return $this->render('brain/taxonomy/index.html.twig', [
|
||||
'taxonomy' => $tags,
|
||||
'type' => 'Tag'
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/brain/tag/new', name: 'brain_tags_new')]
|
||||
public function new(EntityManagerInterface $entityManager, Request $request): Response
|
||||
{
|
||||
|
||||
$form = $this->createForm(TagType::class);
|
||||
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted()) {
|
||||
$data = $form->getData();
|
||||
$Tag = new Tag();
|
||||
|
||||
$Tag->setTitle($data->getTitle());
|
||||
|
||||
$entityManager->persist($Tag);
|
||||
|
||||
$entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute('brain_tags_list');
|
||||
}
|
||||
|
||||
return $this->render('brain/taxonomy/create.html.twig', [
|
||||
'action' => 'New',
|
||||
'form' => $form,
|
||||
'type' => 'Tag'
|
||||
]);
|
||||
}
|
||||
}
|
||||
48
src/Controller/FrontEnd/TagController.php
Normal file
48
src/Controller/FrontEnd/TagController.php
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller\FrontEnd;
|
||||
|
||||
use App\Entity\Tag;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
final class TagController extends AbstractController
|
||||
{
|
||||
#[Route('/tags', name: 'front_end_tag_list', priority: 1)]
|
||||
public function index(EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
$tags = $entityManager->getRepository(Tag::class)->findAll();
|
||||
|
||||
$tagsDisplay = [];
|
||||
|
||||
foreach ($tags as $index => $tag) {
|
||||
$tagsDisplay[] = [
|
||||
'title' => $tag->getTitle(),
|
||||
'urlSafeTitle' => strtolower(str_replace(' ', '-', $tag->getTitle())),
|
||||
'count' => $tag->getCount(),
|
||||
];
|
||||
}
|
||||
|
||||
return $this->render('front/tag/index.html.twig', [
|
||||
'tags' => $tagsDisplay
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/tags/{tagTitle}', name: 'front_end_tag_detail')]
|
||||
public function detail(EntityManagerInterface $entityManager, string $tagTitle): Response
|
||||
{
|
||||
$formattedTitle = ucwords(str_replace('-', ' ', $tagTitle));
|
||||
$tag = $entityManager->getRepository(tag::class)->findOneBy(['title' => $formattedTitle]);
|
||||
|
||||
return $this->render('front/tag/detail.html.twig', [
|
||||
'title' => $tag->getTitle(),
|
||||
'posts' => $tag->getPosts(),
|
||||
'photos' => $tag->getPhotos(),
|
||||
'count' => $tag->getCount()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
@ -40,9 +40,16 @@ class Photos
|
|||
#[ORM\Column(length: 255)]
|
||||
private ?string $thumbnail = null;
|
||||
|
||||
/**
|
||||
* @var Collection<int, Tag>
|
||||
*/
|
||||
#[ORM\ManyToMany(targetEntity: Tag::class, inversedBy: 'photos')]
|
||||
private Collection $tags;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->uploads = new ArrayCollection();
|
||||
$this->tags = new ArrayCollection();
|
||||
}
|
||||
|
||||
public function getId(): ?int
|
||||
|
|
@ -151,4 +158,28 @@ class Photos
|
|||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection<int, Tag>
|
||||
*/
|
||||
public function getTags(): Collection
|
||||
{
|
||||
return $this->tags;
|
||||
}
|
||||
|
||||
public function addTag(Tag $tag): static
|
||||
{
|
||||
if (!$this->tags->contains($tag)) {
|
||||
$this->tags->add($tag);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function removeTag(Tag $tag): static
|
||||
{
|
||||
$this->tags->removeElement($tag);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,9 +24,16 @@ class Tag
|
|||
#[ORM\ManyToMany(targetEntity: Post::class, mappedBy: 'tags')]
|
||||
private Collection $posts;
|
||||
|
||||
/**
|
||||
* @var Collection<int, Photos>
|
||||
*/
|
||||
#[ORM\ManyToMany(targetEntity: Photos::class, mappedBy: 'tags')]
|
||||
private Collection $photos;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->posts = new ArrayCollection();
|
||||
$this->photos = new ArrayCollection();
|
||||
}
|
||||
|
||||
public function getId(): ?int
|
||||
|
|
@ -72,4 +79,46 @@ class Tag
|
|||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection<int, Photos>
|
||||
*/
|
||||
public function getPhotos(): Collection
|
||||
{
|
||||
return $this->photos;
|
||||
}
|
||||
|
||||
public function addPhoto(Photos $photo): static
|
||||
{
|
||||
if (!$this->photos->contains($photo)) {
|
||||
$this->photos->add($photo);
|
||||
$photo->addTag($this);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function removePhoto(Photos $photo): static
|
||||
{
|
||||
if ($this->photos->removeElement($photo)) {
|
||||
$photo->removeTag($this);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCount() : int
|
||||
{
|
||||
return $this->posts->count() + $this->photos->count();
|
||||
}
|
||||
|
||||
public function getUrlSafeTitle() : string
|
||||
{
|
||||
return strtolower(str_replace(' ', '-', $this->title));
|
||||
}
|
||||
|
||||
public function getDisplaySafeTitle(string $urlSafeTitle) : string
|
||||
{
|
||||
return ucwords(str_replace('-', ' ', $urlSafeTitle));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
26
src/Form/CategoryType.php
Normal file
26
src/Form/CategoryType.php
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace App\Form;
|
||||
|
||||
use App\Entity\Category;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
|
||||
class CategoryType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$builder->add('title')
|
||||
->add('save', SubmitType::class);
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => Category::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
@ -5,6 +5,7 @@ namespace App\Form;
|
|||
use App\Entity\Photos;
|
||||
|
||||
use App\Form\CategoryAutocompleteField;
|
||||
use App\Form\TagAutocompleteField;
|
||||
use App\Form\PhotoType;
|
||||
use App\Form\DataTransformer\UploadDataTransformer;
|
||||
|
||||
|
|
@ -25,6 +26,7 @@ class PhotosType extends AbstractType
|
|||
->add('title')
|
||||
->add('date', DateType::class)
|
||||
->add('category', CategoryAutocompleteField::class)
|
||||
->add('tags', TagAutocompleteField::class, ['required' => false])
|
||||
->add('text')
|
||||
->add('thumbnail', FileType::class, [
|
||||
'label' => 'Thumbnail',
|
||||
|
|
|
|||
|
|
@ -8,15 +8,11 @@ use App\Form\CategoryAutocompleteField;
|
|||
use App\Form\TagAutocompleteField;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\DateType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class PostType extends AbstractType
|
||||
|
|
@ -28,7 +24,7 @@ class PostType extends AbstractType
|
|||
->add('date', DateType::class)
|
||||
->add('text', TextareaType::class)
|
||||
->add('category', CategoryAutocompleteField::class)
|
||||
//->add('tags', TagAutocompleteField::class, ['required' => false])
|
||||
->add('tags', TagAutocompleteField::class, ['required' => false])
|
||||
->add('url', TextType::class)
|
||||
->add('published')
|
||||
->add('save', SubmitType::class, ['label' => 'Save'])
|
||||
|
|
|
|||
|
|
@ -23,12 +23,6 @@ class TagAutocompleteField extends AbstractType
|
|||
'choice_label' => 'title',
|
||||
'multiple' => true,
|
||||
'required' => false,
|
||||
/* TODO this isn't natively supported collections/choice type
|
||||
so we're going to manually do it for now
|
||||
*/
|
||||
/*'tom_select_options' => [
|
||||
'create' => true,
|
||||
] */
|
||||
]);
|
||||
}
|
||||
|
||||
|
|
|
|||
27
src/Form/TagType.php
Normal file
27
src/Form/TagType.php
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace App\Form;
|
||||
|
||||
use App\Entity\Tag;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
|
||||
class TagType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$builder->add('title')
|
||||
->add('save', SubmitType::class);
|
||||
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => Tag::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue