From 06808417731562be267213f5f551745c840233f1 Mon Sep 17 00:00:00 2001 From: Oleg Voronkovich Date: Sun, 30 Aug 2015 01:23:31 +0300 Subject: [PATCH 01/10] Show flash messages after creating, updating and deleting posts --- app/Resources/views/admin/blog/edit.html.twig | 4 ++++ .../views/admin/blog/index.html.twig | 4 ++++ app/Resources/views/admin/layout.html.twig | 3 +++ app/Resources/views/macro/messages.html.twig | 23 +++++++++++++++++++ .../Controller/Admin/BlogController.php | 9 ++++++++ 5 files changed, 43 insertions(+) create mode 100644 app/Resources/views/macro/messages.html.twig diff --git a/app/Resources/views/admin/blog/edit.html.twig b/app/Resources/views/admin/blog/edit.html.twig index 5fee71964..eacb39018 100644 --- a/app/Resources/views/admin/blog/edit.html.twig +++ b/app/Resources/views/admin/blog/edit.html.twig @@ -5,6 +5,10 @@ {% block main %}

{{ 'title.edit_post'|trans({'%id%': post.id}) }}

+
+ {{ messages.flashes([ 'post.updated_successfully' ], 'success') }} +
+ {{ include('admin/blog/_form.html.twig', { form: edit_form, button_label: 'action.save'|trans, diff --git a/app/Resources/views/admin/blog/index.html.twig b/app/Resources/views/admin/blog/index.html.twig index 7a0442a50..b6fb87980 100644 --- a/app/Resources/views/admin/blog/index.html.twig +++ b/app/Resources/views/admin/blog/index.html.twig @@ -5,6 +5,10 @@ {% block main %}

{{ 'title.post_list'|trans }}

+
+ {{ messages.flashes([ 'post.created_successfully', 'post.deleted_successfully' ], 'success') }} +
+ diff --git a/app/Resources/views/admin/layout.html.twig b/app/Resources/views/admin/layout.html.twig index 53a51a9b4..33d8ed135 100644 --- a/app/Resources/views/admin/layout.html.twig +++ b/app/Resources/views/admin/layout.html.twig @@ -7,6 +7,9 @@ #} {% extends 'base.html.twig' %} +{# Import macros needed for displaying flash messages. See http://twig.sensiolabs.org/doc/tags/import.html #} +{% import 'macro/messages.html.twig' as messages %} + {% block header_navigation_links %}
  • diff --git a/app/Resources/views/macro/messages.html.twig b/app/Resources/views/macro/messages.html.twig new file mode 100644 index 000000000..f0b612524 --- /dev/null +++ b/app/Resources/views/macro/messages.html.twig @@ -0,0 +1,23 @@ +{# + Macros are comparable with functions in regular programming languages. + They are useful to put often used HTML idioms into reusable elements to not repeat yourself. + See http://twig.sensiolabs.org/doc/tags/macro.html +#} +{% macro flashes(flash_keys, alert_class = 'info') %} +{% from _self import alert %} + {% for flash_key in flash_keys %} + {% for flash_message in app.session.flashBag.get(flash_key) %} + {{ alert(flash_message|trans, alert_class) }} + {% endfor %} + {% endfor %} +{% endmacro %} + +{% macro alert(text, class = 'info') %} +{% spaceless %} +{# Bootstrap alert, see http://getbootstrap.com/components/#alerts #} + +{% endspaceless %} +{% endmacro %} diff --git a/src/AppBundle/Controller/Admin/BlogController.php b/src/AppBundle/Controller/Admin/BlogController.php index b229c7a3e..b606ea6f3 100644 --- a/src/AppBundle/Controller/Admin/BlogController.php +++ b/src/AppBundle/Controller/Admin/BlogController.php @@ -87,6 +87,11 @@ public function newAction(Request $request) $em->persist($post); $em->flush(); + // Add a flash message to user + // $this->addFlash is equivalent to $this->get('session')->getFlashBag()->add + // See http://symfony.com/doc/current/book/controller.html#flash-messages + $this->addFlash('post.created_successfully', 'post.created_successfully'); + return $this->redirectToRoute('admin_post_index'); } @@ -142,6 +147,8 @@ public function editAction(Post $post, Request $request) $post->setSlug($this->get('slugger')->slugify($post->getTitle())); $em->flush(); + $this->addFlash('post.updated_successfully', 'post.updated_successfully'); + return $this->redirectToRoute('admin_post_edit', array('id' => $post->getId())); } @@ -173,6 +180,8 @@ public function deleteAction(Request $request, Post $post) $em->remove($post); $em->flush(); + + $this->addFlash('post.deleted_successfully', 'post.deleted_successfully'); } return $this->redirectToRoute('admin_post_index'); From 12bd670a13957046dedda85c21d2af233b2bd221 Mon Sep 17 00:00:00 2001 From: Oleg Voronkovich Date: Sun, 30 Aug 2015 01:28:17 +0300 Subject: [PATCH 02/10] Update Russian and English translations --- app/Resources/translations/messages.en.xliff | 11 +++++++++++ app/Resources/translations/messages.ru.xliff | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/app/Resources/translations/messages.en.xliff b/app/Resources/translations/messages.en.xliff index ed4fe6838..3b43952a9 100644 --- a/app/Resources/translations/messages.en.xliff +++ b/app/Resources/translations/messages.en.xliff @@ -198,6 +198,17 @@ post.no_posts_found No posts found. + + post.created_successfully + Post created successfully! + + + post.updated_successfully + Post updated successfully! + + + post.deleted_successfully + Post deleted successfully! diff --git a/app/Resources/translations/messages.ru.xliff b/app/Resources/translations/messages.ru.xliff index f5b3f1e17..4036ce477 100644 --- a/app/Resources/translations/messages.ru.xliff +++ b/app/Resources/translations/messages.ru.xliff @@ -198,6 +198,17 @@ post.no_posts_found Ни одной записи не найдено. + + post.created_successfully + Запись успешно создана! + + + post.updated_successfully + Запись успешно обновлена! + + + post.deleted_successfully + Запись успешно удалена! From a46c9485aa1042e9d9d386224f0cd73671d95bc3 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 31 Aug 2015 09:07:30 +0200 Subject: [PATCH 03/10] Use 'success' as the name of the messages related to success messages --- src/AppBundle/Controller/Admin/BlogController.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/AppBundle/Controller/Admin/BlogController.php b/src/AppBundle/Controller/Admin/BlogController.php index b606ea6f3..d571602e7 100644 --- a/src/AppBundle/Controller/Admin/BlogController.php +++ b/src/AppBundle/Controller/Admin/BlogController.php @@ -87,10 +87,10 @@ public function newAction(Request $request) $em->persist($post); $em->flush(); - // Add a flash message to user - // $this->addFlash is equivalent to $this->get('session')->getFlashBag()->add + // Flash messages are used to notify the user about the result of the + // actions. They only last as long as the next request. // See http://symfony.com/doc/current/book/controller.html#flash-messages - $this->addFlash('post.created_successfully', 'post.created_successfully'); + $this->addFlash('success', 'post.created_successfully'); return $this->redirectToRoute('admin_post_index'); } @@ -147,7 +147,7 @@ public function editAction(Post $post, Request $request) $post->setSlug($this->get('slugger')->slugify($post->getTitle())); $em->flush(); - $this->addFlash('post.updated_successfully', 'post.updated_successfully'); + $this->addFlash('success', 'post.updated_successfully'); return $this->redirectToRoute('admin_post_edit', array('id' => $post->getId())); } @@ -181,7 +181,7 @@ public function deleteAction(Request $request, Post $post) $em->remove($post); $em->flush(); - $this->addFlash('post.deleted_successfully', 'post.deleted_successfully'); + $this->addFlash('success', 'post.deleted_successfully'); } return $this->redirectToRoute('admin_post_index'); From 7507a50bbbb12b1eb23f0d6e9e9cac1e0a06d78b Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 31 Aug 2015 09:15:27 +0200 Subject: [PATCH 04/10] Simplified the template code used to render flash messages --- app/Resources/views/admin/blog/edit.html.twig | 4 +-- .../views/admin/blog/index.html.twig | 4 +-- app/Resources/views/admin/layout.html.twig | 7 ++-- app/Resources/views/macro/messages.html.twig | 32 ++++++++++-------- app/data/blog.sqlite | Bin 59392 -> 61440 bytes 5 files changed, 24 insertions(+), 23 deletions(-) diff --git a/app/Resources/views/admin/blog/edit.html.twig b/app/Resources/views/admin/blog/edit.html.twig index eacb39018..2b02a8929 100644 --- a/app/Resources/views/admin/blog/edit.html.twig +++ b/app/Resources/views/admin/blog/edit.html.twig @@ -5,9 +5,7 @@ {% block main %}

    {{ 'title.edit_post'|trans({'%id%': post.id}) }}

    -
    - {{ messages.flashes([ 'post.updated_successfully' ], 'success') }} -
    + {{ helper.render_flash_messages() }} {{ include('admin/blog/_form.html.twig', { form: edit_form, diff --git a/app/Resources/views/admin/blog/index.html.twig b/app/Resources/views/admin/blog/index.html.twig index b6fb87980..e0829a247 100644 --- a/app/Resources/views/admin/blog/index.html.twig +++ b/app/Resources/views/admin/blog/index.html.twig @@ -5,9 +5,7 @@ {% block main %}

    {{ 'title.post_list'|trans }}

    -
    - {{ messages.flashes([ 'post.created_successfully', 'post.deleted_successfully' ], 'success') }} -
    + {{ helper.render_flash_messages() }}
  • diff --git a/app/Resources/views/admin/layout.html.twig b/app/Resources/views/admin/layout.html.twig index 33d8ed135..955b225fb 100644 --- a/app/Resources/views/admin/layout.html.twig +++ b/app/Resources/views/admin/layout.html.twig @@ -7,8 +7,11 @@ #} {% extends 'base.html.twig' %} -{# Import macros needed for displaying flash messages. See http://twig.sensiolabs.org/doc/tags/import.html #} -{% import 'macro/messages.html.twig' as messages %} +{# + Import macros used for displaying flash messages. + See http://twig.sensiolabs.org/doc/tags/import.html +#} +{% import 'macro/messages.html.twig' as helper %} {% block header_navigation_links %}
  • diff --git a/app/Resources/views/macro/messages.html.twig b/app/Resources/views/macro/messages.html.twig index f0b612524..81b2e7a25 100644 --- a/app/Resources/views/macro/messages.html.twig +++ b/app/Resources/views/macro/messages.html.twig @@ -1,23 +1,25 @@ {# Macros are comparable with functions in regular programming languages. - They are useful to put often used HTML idioms into reusable elements to not repeat yourself. + They are useful to put often used HTML idioms into reusable elements + to not repeat yourself. See http://twig.sensiolabs.org/doc/tags/macro.html #} -{% macro flashes(flash_keys, alert_class = 'info') %} -{% from _self import alert %} - {% for flash_key in flash_keys %} - {% for flash_message in app.session.flashBag.get(flash_key) %} - {{ alert(flash_message|trans, alert_class) }} +{% macro render_flash_messages() %} + {% from _self import alert %} + +
    + {% for flash_message in app.session.flashBag.get('success') %} + {{ alert(flash_message|trans) }} {% endfor %} - {% endfor %} +
    {% endmacro %} -{% macro alert(text, class = 'info') %} -{% spaceless %} -{# Bootstrap alert, see http://getbootstrap.com/components/#alerts #} - -{% endspaceless %} +{% macro alert(text, class = 'success') %} + {# Bootstrap alert, see http://getbootstrap.com/components/#alerts #} + {% endmacro %} diff --git a/app/data/blog.sqlite b/app/data/blog.sqlite index 0141e70e34d0f9627b3234e93c2e0b4f3444987c..8b0044a758fc0df9c354f47a4c000a5e7adf507d 100644 GIT binary patch delta 664 zcmZp;z})bFd4e=ACj$e64G>!cG1Ej1b4JdM35%7b85BxObQH=`^HTE?OEQZU5|b5j zQj<$ci}iRX>uUHg@=mVQ2oUAv$}7#uNmMAyEJ;jNNG&eWU(t zshWa7rDd7{K=PQTFOU@0@&l5ITB6J#rR7?IK;8*0kbxrFAekg>4Q7^ delta 91 zcmZp8z}#?wd4e=AI|BoQ6%d2LL=AIB_KgXPl^MAw|Mha1tgKP7d5?}2JC~7yp@o%^ rft8`@CLJ~jj$I7Q!i=XFc1 Date: Mon, 31 Aug 2015 09:18:05 +0200 Subject: [PATCH 05/10] Restored the content database --- app/data/blog.sqlite | Bin 61440 -> 59392 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/app/data/blog.sqlite b/app/data/blog.sqlite index 8b0044a758fc0df9c354f47a4c000a5e7adf507d..0141e70e34d0f9627b3234e93c2e0b4f3444987c 100644 GIT binary patch delta 91 zcmZp8z}#?wd4e=AI|BoQ6%d2LL=AIB_KgXPl^MAw|Mha1tgKP7d5?}2JC~7yp@o%^ rft8`@CLJ~jj$I7Q!i=XFc1!cG1Ej1b4JdM35%7b85BxObQH=`^HTE?OEQZU5|b5j zQj<$ci}iRX>uUHg@=mVQ2oUAv$}7#uNmMAyEJ;jNNG&eWU(t zshWa7rDd7{K=PQTFOU@0@&l5ITB6J#rR7?IK;8*0kbxrFAekg>4Q7^ From da04216df9181f9439188a31631557c8bccffa67 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 31 Aug 2015 10:47:04 +0200 Subject: [PATCH 06/10] Fixed an issue with the translation file after the rebase --- app/Resources/translations/messages.en.xliff | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Resources/translations/messages.en.xliff b/app/Resources/translations/messages.en.xliff index 3b43952a9..02a1ec5ea 100644 --- a/app/Resources/translations/messages.en.xliff +++ b/app/Resources/translations/messages.en.xliff @@ -198,6 +198,7 @@ post.no_posts_found No posts found. + post.created_successfully Post created successfully! From f481eef846a85d9c73b8576f20218d37c208c163 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 31 Aug 2015 11:49:01 +0200 Subject: [PATCH 07/10] Refactored the template code related to macros --- app/Resources/views/admin/blog/edit.html.twig | 6 ++++++ app/Resources/views/admin/blog/index.html.twig | 6 ++++++ app/Resources/views/admin/layout.html.twig | 6 ------ app/Resources/views/{macro => helper}/messages.html.twig | 0 4 files changed, 12 insertions(+), 6 deletions(-) rename app/Resources/views/{macro => helper}/messages.html.twig (100%) diff --git a/app/Resources/views/admin/blog/edit.html.twig b/app/Resources/views/admin/blog/edit.html.twig index 2b02a8929..b5e5521d4 100644 --- a/app/Resources/views/admin/blog/edit.html.twig +++ b/app/Resources/views/admin/blog/edit.html.twig @@ -1,5 +1,11 @@ {% extends 'admin/layout.html.twig' %} +{# + Import macros used for displaying flash messages. + See http://twig.sensiolabs.org/doc/tags/import.html +#} +{% import 'helper/messages.html.twig' as helper %} + {% block body_id 'admin_post_edit' %} {% block main %} diff --git a/app/Resources/views/admin/blog/index.html.twig b/app/Resources/views/admin/blog/index.html.twig index e0829a247..4e9e76fd9 100644 --- a/app/Resources/views/admin/blog/index.html.twig +++ b/app/Resources/views/admin/blog/index.html.twig @@ -1,5 +1,11 @@ {% extends 'admin/layout.html.twig' %} +{# + Import macros used for displaying flash messages. + See http://twig.sensiolabs.org/doc/tags/import.html +#} +{% import 'helper/messages.html.twig' as helper %} + {% block body_id 'admin_post_index' %} {% block main %} diff --git a/app/Resources/views/admin/layout.html.twig b/app/Resources/views/admin/layout.html.twig index 955b225fb..53a51a9b4 100644 --- a/app/Resources/views/admin/layout.html.twig +++ b/app/Resources/views/admin/layout.html.twig @@ -7,12 +7,6 @@ #} {% extends 'base.html.twig' %} -{# - Import macros used for displaying flash messages. - See http://twig.sensiolabs.org/doc/tags/import.html -#} -{% import 'macro/messages.html.twig' as helper %} - {% block header_navigation_links %}
  • diff --git a/app/Resources/views/macro/messages.html.twig b/app/Resources/views/helper/messages.html.twig similarity index 100% rename from app/Resources/views/macro/messages.html.twig rename to app/Resources/views/helper/messages.html.twig From d01311b53f2065b7971e49b9dfdd13e806be6b62 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 31 Aug 2015 11:53:33 +0200 Subject: [PATCH 08/10] Transformed the macro() of the Flash messages into a regular include() template --- app/Resources/views/admin/blog/edit.html.twig | 8 +----- .../views/admin/blog/index.html.twig | 8 +----- .../views/default/_flash_messages.html.twig | 12 +++++++++ app/Resources/views/helper/messages.html.twig | 25 ------------------- 4 files changed, 14 insertions(+), 39 deletions(-) create mode 100644 app/Resources/views/default/_flash_messages.html.twig delete mode 100644 app/Resources/views/helper/messages.html.twig diff --git a/app/Resources/views/admin/blog/edit.html.twig b/app/Resources/views/admin/blog/edit.html.twig index b5e5521d4..20d3a63ad 100644 --- a/app/Resources/views/admin/blog/edit.html.twig +++ b/app/Resources/views/admin/blog/edit.html.twig @@ -1,17 +1,11 @@ {% extends 'admin/layout.html.twig' %} -{# - Import macros used for displaying flash messages. - See http://twig.sensiolabs.org/doc/tags/import.html -#} -{% import 'helper/messages.html.twig' as helper %} - {% block body_id 'admin_post_edit' %} {% block main %}

    {{ 'title.edit_post'|trans({'%id%': post.id}) }}

    - {{ helper.render_flash_messages() }} + {{ include('default/_flash_messages.html.twig') }} {{ include('admin/blog/_form.html.twig', { form: edit_form, diff --git a/app/Resources/views/admin/blog/index.html.twig b/app/Resources/views/admin/blog/index.html.twig index 4e9e76fd9..29348e01c 100644 --- a/app/Resources/views/admin/blog/index.html.twig +++ b/app/Resources/views/admin/blog/index.html.twig @@ -1,17 +1,11 @@ {% extends 'admin/layout.html.twig' %} -{# - Import macros used for displaying flash messages. - See http://twig.sensiolabs.org/doc/tags/import.html -#} -{% import 'helper/messages.html.twig' as helper %} - {% block body_id 'admin_post_index' %} {% block main %}

    {{ 'title.post_list'|trans }}

    - {{ helper.render_flash_messages() }} + {{ include('default/_flash_messages.html.twig') }}
  • diff --git a/app/Resources/views/default/_flash_messages.html.twig b/app/Resources/views/default/_flash_messages.html.twig new file mode 100644 index 000000000..882adf542 --- /dev/null +++ b/app/Resources/views/default/_flash_messages.html.twig @@ -0,0 +1,12 @@ +
    + {% for message in app.session.flashBag.get('success') %} + {# Bootstrap alert, see http://getbootstrap.com/components/#alerts #} + + {% endfor %} +
    diff --git a/app/Resources/views/helper/messages.html.twig b/app/Resources/views/helper/messages.html.twig deleted file mode 100644 index 81b2e7a25..000000000 --- a/app/Resources/views/helper/messages.html.twig +++ /dev/null @@ -1,25 +0,0 @@ -{# - Macros are comparable with functions in regular programming languages. - They are useful to put often used HTML idioms into reusable elements - to not repeat yourself. - See http://twig.sensiolabs.org/doc/tags/macro.html -#} -{% macro render_flash_messages() %} - {% from _self import alert %} - -
    - {% for flash_message in app.session.flashBag.get('success') %} - {{ alert(flash_message|trans) }} - {% endfor %} -
    -{% endmacro %} - -{% macro alert(text, class = 'success') %} - {# Bootstrap alert, see http://getbootstrap.com/components/#alerts #} - -{% endmacro %} From 9a8d43d6a3b1e9cffcfcd041e3b84f6c156d6dbd Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 31 Aug 2015 13:31:25 +0200 Subject: [PATCH 09/10] Check if the session exists before reading flash messages --- .../views/default/_flash_messages.html.twig | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/app/Resources/views/default/_flash_messages.html.twig b/app/Resources/views/default/_flash_messages.html.twig index 882adf542..e667c37e3 100644 --- a/app/Resources/views/default/_flash_messages.html.twig +++ b/app/Resources/views/default/_flash_messages.html.twig @@ -1,12 +1,14 @@ -
    - {% for message in app.session.flashBag.get('success') %} - {# Bootstrap alert, see http://getbootstrap.com/components/#alerts #} - + {% endfor %} +
    +{% endif %} From 3eefa769a4850940977a247aedaef9efc7e5bc9a Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 3 Sep 2015 08:40:02 +0200 Subject: [PATCH 10/10] Reworded the help note about flash messages --- src/AppBundle/Controller/Admin/BlogController.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/AppBundle/Controller/Admin/BlogController.php b/src/AppBundle/Controller/Admin/BlogController.php index d571602e7..f39e80269 100644 --- a/src/AppBundle/Controller/Admin/BlogController.php +++ b/src/AppBundle/Controller/Admin/BlogController.php @@ -88,7 +88,8 @@ public function newAction(Request $request) $em->flush(); // Flash messages are used to notify the user about the result of the - // actions. They only last as long as the next request. + // actions. They are deleted automatically from the session as soon + // as they are accessed. // See http://symfony.com/doc/current/book/controller.html#flash-messages $this->addFlash('success', 'post.created_successfully');