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

update - multiple authors #96

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 6 additions & 2 deletions src/Controller/Admin/PostsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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');
Expand Down
19 changes: 12 additions & 7 deletions src/Entity/Blog/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down Expand Up @@ -119,6 +121,7 @@ public function __construct()
{
$this->uuid = Uuid::uuid4();
$this->tags = new ArrayCollection();
$this->authors = new ArrayCollection();
$this->comments = new ArrayCollection();
}

Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
16 changes: 14 additions & 2 deletions src/Form/Blog/PostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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',
Expand Down
43 changes: 43 additions & 0 deletions src/Migrations/Version20201010210915.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20201010210915 extends AbstractMigration
{
public function getDescription() : string
{
return '';
}

public function up(Schema $schema) : void
{
// this up() 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 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');
}
}
8 changes: 4 additions & 4 deletions src/Repository/Blog/PostRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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);
Expand All @@ -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();
}
Expand Down
7 changes: 7 additions & 0 deletions templates/admin/posts/edit.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
{{ form_widget(form.tags) }}
</div>
</div>

<div class="row">
<div class="input-field col s12">
{{ form_label(form.author) }}
{{ form_widget(form.author) }}
</div>
</div>
<div class="row">
<div class="input-field">
{{ form_row(form.save) }}
Expand Down
2 changes: 1 addition & 1 deletion templates/admin/posts/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<td>
<div class="post__infos">
<p>{{ post.title }}</p>
<p>par <span class="post__author">{{ post.author.displayName }}</span></p>
<p>par {% for author in post.author %} <span class="post__author">{{ author.displayName }}</span> {% if not loop.last %}, {% endif %}{% endfor %}</p>
</div>
</td>
<td>
Expand Down
7 changes: 7 additions & 0 deletions templates/admin/posts/new.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
{{ form_widget(form.tags) }}
</div>
</div>
<div class="row">
<div class="input-field col s12">
{{ form_label(form.author) }}
{{ form_widget(form.author) }}
</div>
</div>

<div class="row">
<div class="input-field">
{{ form_row(form.save) }}
Expand Down
2 changes: 1 addition & 1 deletion templates/admin/posts/show.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<div class="col">
<h2 class="article__second_title">{{ post.title }}</h2>
<p class="article__metadata">
Posté par <a class="article__author">{{ post.author.displayName }}</a> le {{ post.publishedAt |date('d M Y') }} dans<span>
Posté par {% for author in post.author %} <a class="article__author">{{ author.displayName }}</a> {% if not loop.last %}, {% endif %}{% endfor %} le {{ post.publishedAt |date('d M Y') }} dans<span>
{% for tag in post.tags %}<a class="article__tag">{{ tag.name }}</a>{% if not loop.last %}, {% endif %}{% endfor %}
</span>
</p>
Expand Down
4 changes: 4 additions & 0 deletions templates/views/blog/_author.html.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{% spaceless %}
{% for author in author %}
{% if loop.first %}
<div class="row">
<div class="col s3">
<img src="{{ author.picture ? asset('uploads/members/' ~ author.picture) : asset('build/img/blog/WAITING.png') }}" alt="{{ author.displayName }}" class="author__picture">
Expand Down Expand Up @@ -38,4 +40,6 @@
</p>
</div>
</div>
{% endif %}
{% if not loop.last %}, {% endif %}{% endfor %}
{% endspaceless %}
3 changes: 2 additions & 1 deletion templates/views/blog/article.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
<div class="col">
<h2 class="article__second_title">{{ article.title }}</h2>
<p class="article__metadata">
Posté par <a href="{{ path('blog', {author: article.author.displayName}) }}" class="article__author">{{ article.author.displayName }}</a> le {{ article.publishedAt |date('d M Y') }} dans<span>
Posté par {% for author in article.author %}<a href="{{ path('blog', {author: author.displayName}) }}" class="article__author">{{ author.displayName }}</a>{% if not loop.last %}, {% endif %}{% endfor %}
le {{ article.publishedAt |date('d M Y') }} dans<span>
{% for tag in article.tags %}<a href="{{ path('blog', {tag: tag.name}) }}" class="article__tag">{{ tag.name }}</a>{% if not loop.last %}, {% endif %}{% endfor %}
</span>
</p>
Expand Down