Skip to content

Commit cd1f3b5

Browse files
committed
Notify post author about new comments
1 parent 21f82cc commit cd1f3b5

File tree

7 files changed

+52
-17
lines changed

7 files changed

+52
-17
lines changed

app/AppKernel.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ public function registerBundles()
2222
new CodeExplorerBundle\CodeExplorerBundle(),
2323
new AppBundle\AppBundle(),
2424
new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(), // used for initial population of non-SQLite databases in production envs
25-
// uncomment the following line if your application sends emails
26-
// new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
25+
new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
2726
];
2827

2928
// Some bundles are only used while developing the application or during

app/Resources/translations/messages.en.xlf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,14 @@
276276
<source>post.deleted_successfully</source>
277277
<target>Post deleted successfully!</target>
278278
</trans-unit>
279+
<trans-unit id="post.recieved_comment">
280+
<source>post.recieved_comment</source>
281+
<target>Your post recieved a comment!</target>
282+
</trans-unit>
283+
<trans-unit id="post.recieved_comment_message">
284+
<source>post.recieved_comment_message</source>
285+
<target><![CDATA[Your post "%title%" has recieved a new comment. You can read the comment by following this <a href="%link%">link</a>!]]></target>
286+
</trans-unit>
279287

280288
<trans-unit id="help.app_description">
281289
<source>help.app_description</source>

app/Resources/views/blog/post_show.html.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
{% for comment in post.comments %}
3232
<div class="row post-comment">
33+
<a name="comment_{{ comment.id }}"></a>
3334
<h4 class="col-sm-3">
3435
<strong>{{ comment.authorEmail }}</strong> {{ 'post.commented_on'|trans }}
3536
{# it's not mandatory to set the timezone in localizeddate(). This is done to

app/config/config.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ doctrine:
8383
# stores options that change the application behavior and parameters.yml
8484
# stores options that change from one server to another
8585
#
86-
# swiftmailer:
87-
# transport: "%mailer_transport%"
88-
# host: "%mailer_host%"
89-
# username: "%mailer_user%"
90-
# password: "%mailer_password%"
91-
# spool: { type: memory }
86+
swiftmailer:
87+
transport: "%mailer_transport%"
88+
host: "%mailer_host%"
89+
username: "%mailer_user%"
90+
password: "%mailer_password%"
91+
spool: { type: memory }

app/config/config_dev.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ monolog:
2929
# type: chromephp
3030
# level: info
3131

32-
#swiftmailer:
33-
# delivery_address: me@example.com
32+
swiftmailer:
33+
disable_delivery: true

app/config/parameters.yml.dist

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,9 @@ parameters:
3131
# $ php bin/console doctrine:schema:create
3232
# $ php bin/console doctrine:fixtures:load
3333

34-
# Uncomment these parameters if your application sends emails:
35-
#
36-
# mailer_transport: smtp
37-
# mailer_host: 127.0.0.1
38-
# mailer_user: ~
39-
# mailer_password: ~
40-
#
4134
# If you don't use a real mail server, you can send emails via your Gmail account.
4235
# see http://symfony.com/doc/current/cookbook/email/gmail.html
36+
mailer_transport: smtp
37+
mailer_host: 127.0.0.1
38+
mailer_user: ~
39+
mailer_password: ~

src/AppBundle/Controller/BlogController.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
2323
use Symfony\Component\HttpFoundation\Request;
2424
use Symfony\Component\HttpFoundation\Response;
25+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
2526

2627
/**
2728
* Controller used to manage blog contents in the public part of the site.
@@ -96,6 +97,8 @@ public function commentNewAction(Request $request, Post $post)
9697
$entityManager->persist($comment);
9798
$entityManager->flush();
9899

100+
$this->notifyAuthorAboutNewComment($comment);
101+
99102
return $this->redirectToRoute('blog_post', ['slug' => $post->getSlug()]);
100103
}
101104

@@ -105,6 +108,33 @@ public function commentNewAction(Request $request, Post $post)
105108
]);
106109
}
107110

111+
private function notifyAuthorAboutNewComment(Comment $comment)
112+
{
113+
$post = $comment->getPost();
114+
115+
$linkToPost = $this->generateUrl('blog_post', ['slug' => $post->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL);
116+
117+
$translator = $this->get('translator');
118+
119+
$subject = $translator->trans('post.recieved_comment');
120+
$body = $translator->trans('post.recieved_comment_message', [
121+
'%title%' => $post->getTitle(),
122+
'%link%' => $linkToPost.'#comment_'.$comment->getId(),
123+
]);
124+
125+
// See http://symfony.com/doc/current/email.html#sending-emails
126+
$message = \Swift_Message::newInstance()
127+
->setSubject($subject)
128+
->setTo($post->getAuthorEmail())
129+
->setFrom('symfony-demo@localhost')
130+
->setBody($body, 'text/html')
131+
;
132+
133+
// You can view a sent email in the debug toolbar
134+
// See http://symfony.com/doc/current/email/dev_environment.html#viewing-from-the-web-debug-toolbar
135+
$this->get('mailer')->send($message);
136+
}
137+
108138
/**
109139
* This controller is called directly via the render() function in the
110140
* blog/post_show.html.twig template. That's why it's not needed to define

0 commit comments

Comments
 (0)