Skip to content

Commit

Permalink
Merge pull request #648 from lenybernard/feature/article-author
Browse files Browse the repository at this point in the history
Feature/article author
  • Loading branch information
paulandrieux authored Oct 25, 2016
2 parents beb4d84 + 68a8c9e commit 6b01c6b
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 83 deletions.
45 changes: 5 additions & 40 deletions Bundle/BlogBundle/Form/ArticleSettingsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Victoire\Bundle\MediaBundle\Form\Type\MediaType;
use Victoire\Bundle\PageBundle\Entity\PageStatus;

class ArticleSettingsType extends ArticleType
Expand All @@ -31,44 +28,12 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'form.page.type.status.choice.label.scheduled' => PageStatus::SCHEDULED,
],
'choices_as_values' => true,
'attr' => [
'data-refreshOnChange' => 'true',
],
])
->add('image', MediaType::class);

// manage conditional related status in preset data
$builder->get('status')->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$data = $event->getData();
$form = $event->getForm();
self::manageRelatedStatus($data, $form->getParent());
});

// manage conditional related status in pre submit (ajax call to refresh view)
$builder->get('status')->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
$form = $event->getForm();
$data = $event->getData();
self::manageRelatedStatus($data, $form->getParent());
});
}

/**
* Add the related status according to the status field.
**/
public static function manageRelatedStatus($status, $form)
{
switch ($status) {
case PageStatus::SCHEDULED:
$form->add('publishedAt', null, [
'widget' => 'single_text',
'vic_datetimepicker' => true,
'label' => 'form.article.settings.type.publish.label',
]);
break;
default:
$form->remove('publishedAt');
break;
}
->add('publishedAt', null, [
'widget' => 'single_text',
'vic_datetimepicker' => true,
'label' => 'form.article.settings.type.publish.label',
]);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions Bundle/BlogBundle/Form/ArticleType.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'required' => false,
'multiple' => true,
])
->add('author', null, [
'label' => 'form.article.type.author.label',
])
->remove('visibleOnFront');

$builder->get('blog')->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
Expand Down
99 changes: 99 additions & 0 deletions Bundle/BlogBundle/Listener/ArticleSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Victoire\Bundle\BlogBundle\Listener;

use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;
use Doctrine\ORM\UnitOfWork;
use Victoire\Bundle\BlogBundle\Entity\Article;
use Victoire\Bundle\PageBundle\Helper\UserCallableHelper;

class ArticleSubscriber implements EventSubscriber
{
protected $userClass;
protected $userCallableHelper;

/**
* Constructor.
*
* @param UserCallableHelper $userCallableHelper @victoire_page.user_callable
* @param string $userClass %victoire_core.user_class%
*
* @internal param ViewReferenceBuilder $urlBuilder @victoire_view_reference.builder
*/
public function __construct(
UserCallableHelper $userCallableHelper,
$userClass
) {
$this->userClass = $userClass;
$this->userCallableHelper = $userCallableHelper;
}

/**
* bind to LoadClassMetadata method.
*
* @return string[] The subscribed events
*/
public function getSubscribedEvents()
{
return [
'loadClassMetadata',
'onFlush',
];
}

/**
* @param OnFlushEventArgs $eventArgs
*/
public function onFlush(OnFlushEventArgs $eventArgs)
{
/** @var EntityManager $entityManager */
$entityManager = $eventArgs->getEntityManager();
/** @var UnitOfWork $uow */
$uow = $entityManager->getUnitOfWork();

foreach ($uow->getScheduledEntityInsertions() as $entity) {
if ($entity instanceof Article) {
$entity->setAuthor($this->userCallableHelper->getCurrentUser());
}
}
}

/**
* Insert enabled widgets in base widget DiscriminatorMap.
*
* @param LoadClassMetadataEventArgs $eventArgs
*/
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
{
$metadata = $eventArgs->getClassMetadata();

//set a relation between Page and User to define the page author
$metaBuilder = new ClassMetadataBuilder($metadata);

//Add author relation on view
if ($this->userClass && $metadata->name === 'Victoire\Bundle\BlogBundle\Entity\Article') {
$metadata->mapManyToOne([
'fieldName' => 'author',
'targetEntity' => $this->userClass,
'cascade' => ['persist'],
'inversedBy' => 'articles',
'joinColumns' => [
[
'name' => 'author_id',
'referencedColumnName' => 'id',
'onDelete' => 'SET NULL',
],
],
]);
}

// if $article's property exists, add the inversed side on User
if ($metadata->name === $this->userClass && property_exists($this->userClass, 'articles')) {
$metaBuilder->addOneToMany('articles', 'Victoire\Bundle\BlogBundle\Entity\Article', 'author');
}
}
}
9 changes: 9 additions & 0 deletions Bundle/BlogBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ services:
tags:
- { name: form.type }
#ARTICLE

victoire_blog.article.subscriber:
class: Victoire\Bundle\BlogBundle\Listener\ArticleSubscriber
arguments:
- @victoire_page.user_callable
- %victoire_core.user_class%
tags:
- { name: doctrine.event_subscriber, connection: default, priority: -11 }

victoire_blog.article.form.type:
class: Victoire\Bundle\BlogBundle\Form\ArticleType
arguments:
Expand Down
4 changes: 4 additions & 0 deletions Bundle/BlogBundle/Resources/translations/victoire.en.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@
<source>blog.form.title.label</source>
<target>Category</target>
</trans-unit>
<trans-unit id="19" resname="form.article.type.author.label">
<source>form.article.type.author.label</source>
<target>Author</target>
</trans-unit>
<trans-unit id="20" resname="victoire.blog.category.add.sub.label">
<source> victoire.blog.category.add.sub.label</source>
<target>Add a subcategory</target>
Expand Down
4 changes: 4 additions & 0 deletions Bundle/BlogBundle/Resources/translations/victoire.es.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@
<source>blog.form.title.label</source>
<target>Categoría</target>
</trans-unit>
<trans-unit id="19" resname="form.article.type.author.label">
<source>form.article.type.author.label</source>
<target>Autor</target>
</trans-unit>
<trans-unit id="20" resname="victoire.blog.category.add.sub.label">
<source> victoire.blog.category.add.sub.label</source>
<target>Añadir subcategoría</target>
Expand Down
4 changes: 4 additions & 0 deletions Bundle/BlogBundle/Resources/translations/victoire.fr.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@
<source>blog.form.title.label</source>
<target>Catégorie</target>
</trans-unit>
<trans-unit id="19" resname="form.article.type.author.label">
<source>form.article.type.author.label</source>
<target>Auteur</target>
</trans-unit>
<trans-unit id="20" resname="victoire.blog.category.add.sub.label">
<source> victoire.blog.category.add.sub.label</source>
<target>Ajouter une sous-catégorie</target>
Expand Down
53 changes: 13 additions & 40 deletions Bundle/BlogBundle/Resources/views/Article/_form.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,28 @@

<form method="POST" action="{{ action }}">
<div class="vic-row">
{% set date = form.publishedAt is defined ? true : false %}
{{ _self.global(form, date) }}
</div>
<br>
<div class="vic-row">
{{ form_widget(form.translations) }}
</div>

<div class="vic-row">
<div class="vic-col-md-12">
<h1>{{ 'form.article.settings.type.article.title'|trans }}</h1>
<div class="col-md-7">
{{ form_widget(form.translations) }}
</div>
</div>
<br>
<div class="vic-row">
<div class="col-md-6">
<div class="col-md-5">
{% if form.status is defined %}
{{ form_row(form.status) }}
<div class="publication-date-container{% if form.vars.value.status != constant('Victoire\\Bundle\\PageBundle\\Entity\\PageStatus::SCHEDULED')%} vic-hidden{% endif %}">
{{ form_row(form.publishedAt) }}
</div>
{% endif %}
{{ form_row(form.category) }}
</div>
<div class="vic-col-md-6">
{{ form_row(form.tags) }}
</div>
</div>

<div class="vic-row">
<div class="vic-col-md-12">
{{ form_row(form.author) }}
{{ form_rest(form) }}
</div>
</div>


</form>

<script type="text/javascript">
$vic('#victoire_page_settings_type_status').on('change', function(){
$vic('#article_settings_status').on('change', function(){
if ($vic(this).val() == '{{ constant('Victoire\\Bundle\\PageBundle\\Entity\\PageStatus::SCHEDULED') }}') {
$vic('.publication-date-container').removeClass('vic-hidden');
} else {
Expand All @@ -48,20 +38,3 @@
%}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}

{% macro global(form, datePicker) %}
{% set col = 3 %}
<div class="vic-col-md-{{ datePicker ? 12 - col * 2 : 12 - col }}">
<h1>{{ 'form.article.settings.type.global.title'|trans }}</h1>
</div>
{% if form.status is defined %}
<div class="vic-col-md-{{ col }}">
{{ form_row(form.status) }}
</div>
{% if datePicker %}
<div class="vic-col-md-{{ col }}">
{{ form_row(form.publishedAt) }}
</div>
{% endif %}
{% endif %}
{% endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<div>
<div class="vic-panel vic-shadow-plan1 -vicBlog">
<div class="vic-panelBlog-image" {% if article.image %}style="background-image: url({{ article.image.url }});"{% endif %}>
<a class="vic-panelBlog-title" href="{{vic_business_link(article)}}">
<a class="vic-panelBlog-title" href="{{ vic_business_link(article) }}">
<span class="vic-panelBlog-badge -{{ article.status }}">
<span class="vic-panelBlog-badge-popover">
{% set transKey = 'modal.blog.list.articlesList.status.' ~ article.status %}
Expand All @@ -25,6 +25,7 @@
<a class="vic-panelBlog-title" href="{{vic_business_link(article)}}">
{{ article.name }}
</a>
<em>{{ article.author.firstname }} {{ article.author.lastname }}</em>

<time datetime="{{ article.publishedAt|date("Y-m-d") }}" class="vic-panelBlog-date">{{ article.publishedAt|localizeddate("medium", "none", null, null, "cccc d LLLL yyyy") }}</time>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
&.hasmedia {
.preview {
height: 250px;
background-size: cover
background-size: cover;
background-position: center;
}
}

Expand Down
11 changes: 10 additions & 1 deletion Tests/Features/Blog/create.feature
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,14 @@ Background:
When I fill in "article_translations_fr_description" with "This is a great description."
When I select "First blog template" from "Modèle à utiliser"
And I follow "Créer"
And I wait 10 seconds
And I wait 5 seconds
Then I should be on "/fr/the-jedi-network/i-m-your-father"

@alice(Blog) @alice(Article) @alice(BlogTemplate)
Scenario: I can view the Article list in the blog management window
Given I open the hamburger menu
Then I should see "Blog"
When I follow "Blog"
Then I should see "Listes des articles"
And I should see "I'm your father."
And I should see "Anakin Skywalker"

0 comments on commit 6b01c6b

Please sign in to comment.