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 1 commit
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" )
chypriote marked this conversation as resolved.
Show resolved Hide resolved
*
*/
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="author")
brano1403 marked this conversation as resolved.
Show resolved Hide resolved
*/
protected $posts;

Expand Down
20 changes: 18 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 All @@ -28,6 +28,8 @@ public function __construct(TranslatorInterface $translator)
$this->translator = $translator;
}


brano1403 marked this conversation as resolved.
Show resolved Hide resolved

chypriote marked this conversation as resolved.
Show resolved Hide resolved
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
Expand All @@ -49,6 +51,20 @@ 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,

brano1403 marked this conversation as resolved.
Show resolved Hide resolved
])
->add('publishedAt', DateType::class, [
'widget' => 'single_text',
'format' => 'yyyy-MM-dd',
Expand Down
57 changes: 57 additions & 0 deletions src/Migrations/Version20201010210915.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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 comment CHANGE author_id author_id INT DEFAULT NULL, CHANGE post_id post_id INT DEFAULT NULL');
chypriote marked this conversation as resolved.
Show resolved Hide resolved
$this->addSql('ALTER TABLE post CHANGE published_at published_at DATETIME DEFAULT NULL, CHANGE deleted_at deleted_at DATETIME DEFAULT NULL, CHANGE picture picture VARCHAR(255) DEFAULT NULL');
$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');
$this->addSql('ALTER TABLE partner CHANGE website website VARCHAR(255) DEFAULT NULL, CHANGE twitter twitter VARCHAR(255) DEFAULT NULL, CHANGE facebook facebook VARCHAR(255) DEFAULT NULL, CHANGE instagram instagram VARCHAR(255) DEFAULT NULL, CHANGE deleted_at deleted_at DATETIME DEFAULT NULL');
chypriote marked this conversation as resolved.
Show resolved Hide resolved
$this->addSql('ALTER TABLE streamer CHANGE picture picture VARCHAR(255) DEFAULT NULL, CHANGE deleted_at deleted_at DATETIME DEFAULT NULL');
$this->addSql('ALTER TABLE member CHANGE role_id role_id INT DEFAULT NULL, CHANGE firstname firstname VARCHAR(255) DEFAULT NULL, CHANGE lastname lastname VARCHAR(255) DEFAULT NULL, CHANGE twitch twitch VARCHAR(255) DEFAULT NULL, CHANGE twitter twitter VARCHAR(255) DEFAULT NULL, CHANGE facebook facebook VARCHAR(255) DEFAULT NULL, CHANGE instagram instagram VARCHAR(255) DEFAULT NULL, CHANGE deleted_at deleted_at DATETIME DEFAULT NULL, CHANGE picture picture VARCHAR(255) DEFAULT NULL, CHANGE team team VARCHAR(255) DEFAULT NULL, CHANGE role role VARCHAR(255) DEFAULT NULL, CHANGE game game VARCHAR(255) DEFAULT NULL');
$this->addSql('ALTER TABLE role CHANGE deleted_at deleted_at DATETIME DEFAULT NULL');
$this->addSql('ALTER TABLE user CHANGE email email VARCHAR(255) DEFAULT NULL, CHANGE discord_id discord_id VARCHAR(255) DEFAULT NULL, CHANGE roles roles JSON NOT NULL, CHANGE deleted_at deleted_at DATETIME DEFAULT NULL, CHANGE twitter twitter VARCHAR(255) DEFAULT NULL, CHANGE facebook facebook VARCHAR(255) DEFAULT NULL, CHANGE youtube youtube VARCHAR(255) DEFAULT NULL, CHANGE twitch twitch VARCHAR(255) DEFAULT NULL, CHANGE discord discord VARCHAR(255) DEFAULT NULL, CHANGE instagram instagram VARCHAR(255) DEFAULT NULL, CHANGE first_name first_name VARCHAR(255) DEFAULT NULL, CHANGE last_name last_name VARCHAR(255) DEFAULT NULL, CHANGE picture picture VARCHAR(255) DEFAULT NULL, CHANGE description description VARCHAR(255) DEFAULT NULL');
}

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 comment CHANGE author_id author_id INT DEFAULT NULL, CHANGE post_id post_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE member CHANGE role_id role_id INT DEFAULT NULL, CHANGE firstname firstname VARCHAR(255) CHARACTER SET utf8mb4 DEFAULT \'NULL\' COLLATE `utf8mb4_unicode_ci`, CHANGE lastname lastname VARCHAR(255) CHARACTER SET utf8mb4 DEFAULT \'NULL\' COLLATE `utf8mb4_unicode_ci`, CHANGE picture picture VARCHAR(255) CHARACTER SET utf8mb4 DEFAULT \'NULL\' COLLATE `utf8mb4_unicode_ci`, CHANGE twitch twitch VARCHAR(255) CHARACTER SET utf8mb4 DEFAULT \'NULL\' COLLATE `utf8mb4_unicode_ci`, CHANGE twitter twitter VARCHAR(255) CHARACTER SET utf8mb4 DEFAULT \'NULL\' COLLATE `utf8mb4_unicode_ci`, CHANGE facebook facebook VARCHAR(255) CHARACTER SET utf8mb4 DEFAULT \'NULL\' COLLATE `utf8mb4_unicode_ci`, CHANGE instagram instagram VARCHAR(255) CHARACTER SET utf8mb4 DEFAULT \'NULL\' COLLATE `utf8mb4_unicode_ci`, CHANGE deleted_at deleted_at DATETIME DEFAULT \'NULL\', CHANGE game game VARCHAR(255) CHARACTER SET utf8mb4 DEFAULT \'NULL\' COLLATE `utf8mb4_unicode_ci`, CHANGE team team VARCHAR(255) CHARACTER SET utf8mb4 DEFAULT \'NULL\' COLLATE `utf8mb4_unicode_ci`, CHANGE role role VARCHAR(255) CHARACTER SET utf8mb4 DEFAULT \'NULL\' COLLATE `utf8mb4_unicode_ci`');
$this->addSql('ALTER TABLE partner CHANGE website website VARCHAR(255) CHARACTER SET utf8mb4 DEFAULT \'NULL\' COLLATE `utf8mb4_unicode_ci`, CHANGE twitter twitter VARCHAR(255) CHARACTER SET utf8mb4 DEFAULT \'NULL\' COLLATE `utf8mb4_unicode_ci`, CHANGE facebook facebook VARCHAR(255) CHARACTER SET utf8mb4 DEFAULT \'NULL\' COLLATE `utf8mb4_unicode_ci`, CHANGE instagram instagram VARCHAR(255) CHARACTER SET utf8mb4 DEFAULT \'NULL\' COLLATE `utf8mb4_unicode_ci`, CHANGE deleted_at deleted_at DATETIME DEFAULT \'NULL\'');
$this->addSql('ALTER TABLE post CHANGE picture picture VARCHAR(255) CHARACTER SET utf8mb4 DEFAULT \'NULL\' COLLATE `utf8mb4_unicode_ci`, CHANGE published_at published_at DATETIME DEFAULT \'NULL\', CHANGE deleted_at deleted_at DATETIME DEFAULT \'NULL\'');
$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');
$this->addSql('ALTER TABLE role CHANGE deleted_at deleted_at DATETIME DEFAULT \'NULL\'');
$this->addSql('ALTER TABLE streamer CHANGE picture picture VARCHAR(255) CHARACTER SET utf8mb4 DEFAULT \'NULL\' COLLATE `utf8mb4_unicode_ci`, CHANGE deleted_at deleted_at DATETIME DEFAULT \'NULL\'');
$this->addSql('ALTER TABLE user CHANGE email email VARCHAR(255) CHARACTER SET utf8mb4 DEFAULT \'NULL\' COLLATE `utf8mb4_unicode_ci`, CHANGE first_name first_name VARCHAR(255) CHARACTER SET utf8mb4 DEFAULT \'NULL\' COLLATE `utf8mb4_unicode_ci`, CHANGE last_name last_name VARCHAR(255) CHARACTER SET utf8mb4 DEFAULT \'NULL\' COLLATE `utf8mb4_unicode_ci`, CHANGE description description TEXT CHARACTER SET utf8mb4 DEFAULT NULL COLLATE `utf8mb4_unicode_ci`, CHANGE discord_id discord_id VARCHAR(255) CHARACTER SET utf8mb4 DEFAULT \'NULL\' COLLATE `utf8mb4_unicode_ci`, CHANGE roles roles LONGTEXT CHARACTER SET utf8mb4 NOT NULL COLLATE `utf8mb4_bin`, CHANGE twitter twitter VARCHAR(255) CHARACTER SET utf8mb4 DEFAULT \'NULL\' COLLATE `utf8mb4_unicode_ci`, CHANGE facebook facebook VARCHAR(255) CHARACTER SET utf8mb4 DEFAULT \'NULL\' COLLATE `utf8mb4_unicode_ci`, CHANGE youtube youtube VARCHAR(255) CHARACTER SET utf8mb4 DEFAULT \'NULL\' COLLATE `utf8mb4_unicode_ci`, CHANGE twitch twitch VARCHAR(255) CHARACTER SET utf8mb4 DEFAULT \'NULL\' COLLATE `utf8mb4_unicode_ci`, CHANGE discord discord VARCHAR(255) CHARACTER SET utf8mb4 DEFAULT \'NULL\' COLLATE `utf8mb4_unicode_ci`, CHANGE instagram instagram VARCHAR(255) CHARACTER SET utf8mb4 DEFAULT \'NULL\' COLLATE `utf8mb4_unicode_ci`, CHANGE picture picture VARCHAR(255) CHARACTER SET utf8mb4 DEFAULT \'NULL\' COLLATE `utf8mb4_unicode_ci`, CHANGE deleted_at deleted_at DATETIME DEFAULT \'NULL\'');
}
}
6 changes: 3 additions & 3 deletions src/Repository/Blog/PostRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(':author MEMBER OF p.author')->setParameter('author', $options['author']);
}

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