diff --git a/src/Controller/Admin/PostsController.php b/src/Controller/Admin/PostsController.php index 9a87d5d..99a1206 100644 --- a/src/Controller/Admin/PostsController.php +++ b/src/Controller/Admin/PostsController.php @@ -53,7 +53,7 @@ public function new(Request $request, FileUploader $fileUploader, Security $secu $post->setPublishedAt($date); } - $post->setAuthor($security->getUser()); + $post->addAuthor($security->getUser()); $em = $this->getDoctrine()->getManager(); $em->persist($post); $em->flush(); @@ -90,7 +90,7 @@ public function show(Post $post): Response * @Route("/{uuid}/edit", name="admin_posts_edit", methods={"GET", "POST"}, requirements={"uuid"="[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"}) * @Route("/{slug}/edit", name="admin_posts_edit_slug", methods={"GET", "POST"}) */ - public function edit(Request $request, Post $post, FileUploader $fileUploader): Response + public function edit(Request $request, Post $post, FileUploader $fileUploader, Security $security): Response { $form = $this->createForm(PostType::class, $post); $form->handleRequest($request); @@ -103,6 +103,10 @@ public function edit(Request $request, Post $post, FileUploader $fileUploader): $date = new DateTime($posted['publishedAt']); $post->setPublishedAt($date); } + $post->addAuthor($security->getUser()); + $em = $this->getDoctrine()->getManager(); + $em->persist($post); + $em->flush(); $this->getDoctrine()->getManager()->flush(); $this->addFlash('success', 'posts.success.edit'); diff --git a/src/Entity/Blog/Post.php b/src/Entity/Blog/Post.php index 73b7f58..533afd0 100644 --- a/src/Entity/Blog/Post.php +++ b/src/Entity/Blog/Post.php @@ -72,10 +72,12 @@ class Post protected $published = false; /** - * @var User - * @ORM\ManyToOne(targetEntity="App\Entity\User\User", inversedBy="posts") + * + * @var Collection|User[] + * @ORM\ManyToMany(targetEntity="App\Entity\User\User", inversedBy="posts") + * */ - protected $author; + protected $authors; /** * @var Collection|Comment[] @@ -119,6 +121,7 @@ public function __construct() { $this->uuid = Uuid::uuid4(); $this->tags = new ArrayCollection(); + $this->authors = new ArrayCollection(); $this->comments = new ArrayCollection(); } @@ -165,14 +168,16 @@ public function setContent(string $content): self return $this; } - public function getAuthor(): User + public function getAuthor(): Collection { - return $this->author; + return $this->authors; } - public function setAuthor(UserInterface $author): self + + + public function addAuthor(UserInterface $authors): self { - $this->author = $author; + $this->authors[] = $authors; return $this; } diff --git a/src/Entity/User/User.php b/src/Entity/User/User.php index 6fb9be1..898dd4a 100644 --- a/src/Entity/User/User.php +++ b/src/Entity/User/User.php @@ -130,7 +130,7 @@ class User implements UserInterface /** * @var Collection|Post[] - * @ORM\OneToMany(targetEntity="App\Entity\Blog\Post", mappedBy="author") + * @ORM\ManyToMany(targetEntity="App\Entity\Blog\Post", mappedBy="authors") */ protected $posts; diff --git a/src/Form/Blog/PostType.php b/src/Form/Blog/PostType.php index 2e5ff8d..db0306f 100644 --- a/src/Form/Blog/PostType.php +++ b/src/Form/Blog/PostType.php @@ -3,11 +3,11 @@ namespace App\Form\Blog; use App\Entity\Blog\Tag; -use DateTime; +use App\Entity\User\User; +use App\Repository\User\UserRepository; use FOS\CKEditorBundle\Form\Type\CKEditorType; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; -use Symfony\Component\Form\CallbackTransformer; use Symfony\Component\Form\Extension\Core\Type\DateType; use Symfony\Component\Form\Extension\Core\Type\FileType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; @@ -49,6 +49,18 @@ public function buildForm(FormBuilderInterface $builder, array $options) 'multiple' => true, 'required' => false, ]) + ->add('author', EntityType::class, [ + 'class' => User::class, + 'query_builder' => function (UserRepository $er) { + return $er->createQueryBuilder('u') + ->Where('u.roles = :val') + ->setParameter('val', "[\"ROLE_REDACTEUR\"]") + ->orderBy('u.id', 'ASC'); + }, + 'choice_label' => 'displayName', + 'multiple' => true, + 'required' => false, + ]) ->add('publishedAt', DateType::class, [ 'widget' => 'single_text', 'format' => 'yyyy-MM-dd', diff --git a/src/Migrations/Version20201010210915.php b/src/Migrations/Version20201010210915.php new file mode 100644 index 0000000..206f8e0 --- /dev/null +++ b/src/Migrations/Version20201010210915.php @@ -0,0 +1,43 @@ +abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); + + + $this->addSql('ALTER TABLE post_user ADD PRIMARY KEY (post_id, user_id)'); + $this->addSql('ALTER TABLE post_user ADD CONSTRAINT FK_44C6B1424B89032C FOREIGN KEY (post_id) REFERENCES post (id) ON DELETE CASCADE'); + $this->addSql('ALTER TABLE post_user ADD CONSTRAINT FK_44C6B142A76ED395 FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE'); + $this->addSql('ALTER TABLE post_user RENAME INDEX post_user_ibfk_1 TO IDX_44C6B1424B89032C'); + $this->addSql('ALTER TABLE post_user RENAME INDEX post_user_ibfk_2 TO IDX_44C6B142A76ED395'); + } + + public function down(Schema $schema) : void + { + // this down() migration is auto-generated, please modify it to your needs + $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); + $this->addSql('ALTER TABLE post_user DROP FOREIGN KEY FK_44C6B1424B89032C'); + $this->addSql('ALTER TABLE post_user DROP FOREIGN KEY FK_44C6B142A76ED395'); + $this->addSql('ALTER TABLE post_user DROP PRIMARY KEY'); + $this->addSql('ALTER TABLE post_user RENAME INDEX idx_44c6b1424b89032c TO post_user_ibfk_1'); + $this->addSql('ALTER TABLE post_user RENAME INDEX idx_44c6b142a76ed395 TO post_user_ibfk_2'); + } +} diff --git a/src/Repository/Blog/PostRepository.php b/src/Repository/Blog/PostRepository.php index 1c05f24..78394a3 100644 --- a/src/Repository/Blog/PostRepository.php +++ b/src/Repository/Blog/PostRepository.php @@ -28,7 +28,7 @@ public function findLatest(int $page = 1, array $options = []): Paginator { $qb = $this->createQueryBuilder('p') ->addSelect('a', 't') - ->innerJoin('p.author', 'a') + ->innerJoin('p.authors', 'a') ->leftJoin('p.tags', 't') ->where('p.publishedAt <= :now') ->orderBy('p.publishedAt', 'DESC') @@ -38,7 +38,7 @@ public function findLatest(int $page = 1, array $options = []): Paginator $qb->andWhere(':tag MEMBER OF p.tags')->setParameter('tag', $options['tag']); } if ($options['author'] instanceof User) { - $qb->andWhere('p.author = :author')->setParameter('author', $options['author']); + $qb->andWhere(':authors MEMBER OF p.authors')->setParameter('authors', $options['authors']); } return (new Paginator($qb))->paginate($page); @@ -47,10 +47,10 @@ public function findLatest(int $page = 1, array $options = []): Paginator public function getRecommended(int $int) { return $this->createQueryBuilder('p') - ->where('p.publishedAt <= :now') + ->where('p.publishedAt <= :now') ->orderBy('p.publishedAt', 'DESC') ->setMaxResults(3) - ->setParameter('now', new DateTime()) + ->setParameter('now', new DateTime()) ->getQuery() ->getResult(); } diff --git a/templates/admin/posts/edit.html.twig b/templates/admin/posts/edit.html.twig index 09e43a2..3e787d9 100644 --- a/templates/admin/posts/edit.html.twig +++ b/templates/admin/posts/edit.html.twig @@ -33,6 +33,13 @@ {{ form_widget(form.tags) }} + +
+
+ {{ form_label(form.author) }} + {{ form_widget(form.author) }} +
+
{{ form_row(form.save) }} diff --git a/templates/admin/posts/index.html.twig b/templates/admin/posts/index.html.twig index cc1236a..8612b11 100644 --- a/templates/admin/posts/index.html.twig +++ b/templates/admin/posts/index.html.twig @@ -30,7 +30,7 @@ diff --git a/templates/admin/posts/new.html.twig b/templates/admin/posts/new.html.twig index bbb908b..7a11e3c 100644 --- a/templates/admin/posts/new.html.twig +++ b/templates/admin/posts/new.html.twig @@ -33,6 +33,13 @@ {{ form_widget(form.tags) }}
+
+
+ {{ form_label(form.author) }} + {{ form_widget(form.author) }} +
+
+
{{ form_row(form.save) }} diff --git a/templates/admin/posts/show.html.twig b/templates/admin/posts/show.html.twig index 419ef7d..4e7985b 100644 --- a/templates/admin/posts/show.html.twig +++ b/templates/admin/posts/show.html.twig @@ -29,7 +29,7 @@

{{ post.title }}

diff --git a/templates/views/blog/_author.html.twig b/templates/views/blog/_author.html.twig index 09bb965..8e9ad6e 100644 --- a/templates/views/blog/_author.html.twig +++ b/templates/views/blog/_author.html.twig @@ -1,4 +1,6 @@ {% spaceless %} + {% for author in author %} + {% if loop.first %}
{{ author.displayName }} @@ -38,4 +40,6 @@

+ {% endif %} + {% if not loop.last %}, {% endif %}{% endfor %} {% endspaceless %} diff --git a/templates/views/blog/article.html.twig b/templates/views/blog/article.html.twig index 76e0482..04b088c 100644 --- a/templates/views/blog/article.html.twig +++ b/templates/views/blog/article.html.twig @@ -24,7 +24,8 @@

{{ article.title }}