diff --git a/Bundle/BlogBundle/Form/ArticleSettingsType.php b/Bundle/BlogBundle/Form/ArticleSettingsType.php index 96302c8a7..b5ef6b465 100644 --- a/Bundle/BlogBundle/Form/ArticleSettingsType.php +++ b/Bundle/BlogBundle/Form/ArticleSettingsType.php @@ -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 @@ -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', + ]); } /** diff --git a/Bundle/BlogBundle/Form/ArticleType.php b/Bundle/BlogBundle/Form/ArticleType.php index 4d9d5a4dc..d210dfab5 100644 --- a/Bundle/BlogBundle/Form/ArticleType.php +++ b/Bundle/BlogBundle/Form/ArticleType.php @@ -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) { diff --git a/Bundle/BlogBundle/Listener/ArticleSubscriber.php b/Bundle/BlogBundle/Listener/ArticleSubscriber.php new file mode 100644 index 000000000..bdc880c56 --- /dev/null +++ b/Bundle/BlogBundle/Listener/ArticleSubscriber.php @@ -0,0 +1,99 @@ +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'); + } + } +} diff --git a/Bundle/BlogBundle/Resources/config/services.yml b/Bundle/BlogBundle/Resources/config/services.yml index 66df750c8..9c9d54e2d 100644 --- a/Bundle/BlogBundle/Resources/config/services.yml +++ b/Bundle/BlogBundle/Resources/config/services.yml @@ -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: diff --git a/Bundle/BlogBundle/Resources/translations/victoire.en.xliff b/Bundle/BlogBundle/Resources/translations/victoire.en.xliff index 723d2e5c1..68ab4d825 100644 --- a/Bundle/BlogBundle/Resources/translations/victoire.en.xliff +++ b/Bundle/BlogBundle/Resources/translations/victoire.en.xliff @@ -110,6 +110,10 @@ blog.form.title.label Category + + form.article.type.author.label + Author + victoire.blog.category.add.sub.label Add a subcategory diff --git a/Bundle/BlogBundle/Resources/translations/victoire.es.xliff b/Bundle/BlogBundle/Resources/translations/victoire.es.xliff index 8205a9463..e60146af0 100644 --- a/Bundle/BlogBundle/Resources/translations/victoire.es.xliff +++ b/Bundle/BlogBundle/Resources/translations/victoire.es.xliff @@ -110,6 +110,10 @@ blog.form.title.label Categoría + + form.article.type.author.label + Autor + victoire.blog.category.add.sub.label Añadir subcategoría diff --git a/Bundle/BlogBundle/Resources/translations/victoire.fr.xliff b/Bundle/BlogBundle/Resources/translations/victoire.fr.xliff index cbe28e0ef..6b2fcc80c 100644 --- a/Bundle/BlogBundle/Resources/translations/victoire.fr.xliff +++ b/Bundle/BlogBundle/Resources/translations/victoire.fr.xliff @@ -110,6 +110,10 @@ blog.form.title.label Catégorie + + form.article.type.author.label + Auteur + victoire.blog.category.add.sub.label Ajouter une sous-catégorie diff --git a/Bundle/BlogBundle/Resources/views/Article/_form.html.twig b/Bundle/BlogBundle/Resources/views/Article/_form.html.twig index b8347e3fa..c65ab7827 100644 --- a/Bundle/BlogBundle/Resources/views/Article/_form.html.twig +++ b/Bundle/BlogBundle/Resources/views/Article/_form.html.twig @@ -3,38 +3,28 @@
- {% set date = form.publishedAt is defined ? true : false %} - {{ _self.global(form, date) }} -
-
-
- {{ form_widget(form.translations) }} -
- -
-
-

{{ 'form.article.settings.type.article.title'|trans }}

+
+ {{ form_widget(form.translations) }}
-
-
-
-
+
+ {% if form.status is defined %} + {{ form_row(form.status) }} +
+ {{ form_row(form.publishedAt) }} +
+ {% endif %} {{ form_row(form.category) }} -
-
{{ form_row(form.tags) }} -
-
- -
-
+ {{ form_row(form.author) }} {{ form_rest(form) }}
+ + {% endjavascripts %} - -{% macro global(form, datePicker) %} - {% set col = 3 %} -
-

{{ 'form.article.settings.type.global.title'|trans }}

-
- {% if form.status is defined %} -
- {{ form_row(form.status) }} -
- {% if datePicker %} -
- {{ form_row(form.publishedAt) }} -
- {% endif %} - {% endif %} -{% endmacro %} diff --git a/Bundle/BlogBundle/Resources/views/Blog/Tabs/_articles.html.twig b/Bundle/BlogBundle/Resources/views/Blog/Tabs/_articles.html.twig index 8585fddba..66b6da3ac 100644 --- a/Bundle/BlogBundle/Resources/views/Blog/Tabs/_articles.html.twig +++ b/Bundle/BlogBundle/Resources/views/Blog/Tabs/_articles.html.twig @@ -11,7 +11,7 @@
- + {% set transKey = 'modal.blog.list.articlesList.status.' ~ article.status %} @@ -25,6 +25,7 @@ {{ article.name }} + {{ article.author.firstname }} {{ article.author.lastname }} diff --git a/Bundle/CoreBundle/Resources/style/less/bricks/input-media.less b/Bundle/CoreBundle/Resources/style/less/bricks/input-media.less index 7a32c89e9..33c15a12e 100644 --- a/Bundle/CoreBundle/Resources/style/less/bricks/input-media.less +++ b/Bundle/CoreBundle/Resources/style/less/bricks/input-media.less @@ -14,7 +14,8 @@ &.hasmedia { .preview { height: 250px; - background-size: cover + background-size: cover; + background-position: center; } } diff --git a/Tests/Features/Blog/create.feature b/Tests/Features/Blog/create.feature index 22e34e71e..826d342da 100644 --- a/Tests/Features/Blog/create.feature +++ b/Tests/Features/Blog/create.feature @@ -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"