Skip to content

Commit

Permalink
added option to send email notifications when new comments are added
Browse files Browse the repository at this point in the history
  • Loading branch information
robertmarsal committed Nov 27, 2013
1 parent 8d51c21 commit 05083f7
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 3 deletions.
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Zend Framework 2 module for drop-in self-hosted comments.
),
```

***

#### Usage

In your views use the `rbComment` helper to display the count, the list and a form for adding new comments. Invoke it
Expand All @@ -38,6 +40,8 @@ The `$theme` parameter is used to specify the theme of the comments box (if none
Currently, the module is designed to allow only one comment box per page, as it uses
the page uri to identify a thread.

***

#### Themes

The module comes with 2 themes for now. To implement new ones create a new partial using
Expand All @@ -54,6 +58,8 @@ Basic theme with no external dependencies. Contains the minimum styling to make
This theme requires the [UIkit](http://www.getuikit.com/) CSS framework. If you use it in your project this theme
will make your comments box look awesome.

***

#### Configuration
The default configuration of the module can be found in the file `config/module.config.php`.
To override the defaults, add your values under the `rb_comment` key in the `config/autoload/local.php` file
Expand All @@ -69,6 +75,47 @@ visible. If 0 they will not be shown. This is useful for moderation.
This array contains the translations for the strings used in the comments box. To change or translate to another language
override these values with your own.

##### email
This array contains email notifications parameters
```php
'email' => array(
/**
* Send email notifications.
*/
'notify' => false,
/**
* Email addresses where to send the notification.
*/
'to' => array(),
/**
* From header. Usually something like [email protected]
*/
'from' => '',
/**
* Subject of the notification email.
*/
'subject' => 'New Comment',
/**
* Text of the comment link.
*/
'context_link_text' => 'See this comment in context',
),
```
For sending the emails the module uses a service factory called `RbComment\Mailer`. As a default it configures a sendmail transport. This should be changed in production and customized to your needs (probably with smtp). To do this rewrite the service factory in your `config/autoload/global.php` file.
```php
'service_manager' => array(
'factories' => array(
/**
* Placeholder transport config. Do not use this in production.
* Replace with smtp.
*/
'RbComment\Mailer' => function () {
return new Zend\Mail\Transport\Sendmail();
},
),
),
```
***
#### Preview

![Preview](demo/preview.png)
41 changes: 41 additions & 0 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
'RbComment\Controller\Comment' => 'RbComment\Controller\CommentController',
),
),
'controller_plugins' => array(
'invokables' => array(
'rbMailer' => 'RbComment\Mvc\Controller\Plugin\Mailer',
)
),
'view_helpers' => array(
'invokables' => array(
'rbComment' => 'RbComment\View\Helper\Comment',
Expand All @@ -33,7 +38,21 @@
'rbcomment/theme/default' => __DIR__ . '/../view/theme/default.phtml',
),
),
'service_manager' => array(
'factories' => array(
/**
* Placeholder transport config. Do not use this in production.
* Replace with smtp.
*/
'RbComment\Mailer' => function () {
return new Zend\Mail\Transport\Sendmail();
},
),
),
'rb_comment' => array(
/**
* Default visibility of the comments.
*/
'default_visibility' => 1,
'strings' => array(
'author' => 'Author',
Expand All @@ -43,5 +62,27 @@
'comments' => 'Comments',
'required' => 'All fields are required. Contact info will not be published.',
),
'email' => array(
/**
* Send email notifications.
*/
'notify' => false,
/**
* Email addresses where to send the notification.
*/
'to' => array(),
/**
* From header. Usually something like [email protected]
*/
'from' => '',
/**
* Subject of the notification email.
*/
'subject' => 'New Comment',
/**
* Text of the comment link.
*/
'context_link_text' => 'See this comment in context',
),
),
);
8 changes: 7 additions & 1 deletion src/RbComment/Controller/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ public function addAction()
// Set default visibility from config
$comment->visible = $config['rb_comment']['default_visibility'];

$this->getCommentTable()->saveComment($comment);
// We need the id for the mailer
$comment->id = $this->getCommentTable()->saveComment($comment);

// Send email if active
if($config['rb_comment']['email']['notify'] === true) {
$this->rbMailer($comment);
}

return $this->redirect()->toUrl($form->get('uri')->getValue());
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/RbComment/Model/CommentTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,16 @@ public function saveComment(Comment $comment)
$id = (int)$comment->id;
if ($id == 0) {
$this->tableGateway->insert($data);
$id = $this->tableGateway->lastInsertValue;
} else {
if ($this->getComment($id)) {
$this->tableGateway->update($data, array('id' => $id));
} else {
throw new \Exception('Form id does not exist');
}
}

return $id;
}

public function deleteComment($id)
Expand Down
61 changes: 61 additions & 0 deletions src/RbComment/Mvc/Controller/Plugin/Mailer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace RbComment\Mvc\Controller\Plugin;

use Zend\Mail\Message;
use Zend\Mime\Part as MimePart;
use Zend\Mime\Message as MimeMessage;
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\ServiceLocatorAwareInterface;

class Mailer extends AbstractPlugin implements ServiceLocatorAwareInterface
{
private $serviceLocator;

public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;
return $this;
}

public function getServiceLocator()
{
return $this->serviceLocator;
}

public function __invoke($comment)
{
$serviceManager = $this->getServiceLocator()->getServiceLocator();
$viewHelperManager = $serviceManager->get('viewhelpermanager');
$serverUrlHelper = $viewHelperManager->get('serverUrl');

$mailerService = $serviceManager->get('RbComment\Mailer');

$config = $serviceManager->get('Config');
$mailerConfig = $config['rb_comment']['email'];

$htmlContent = $comment->content;
$htmlContent .= '<br><br>';
$htmlContent .= '<a href="' . $serverUrlHelper() . $comment->uri . '#rbcomment-' . $comment->id . '">' .
$mailerConfig['context_link_text'] .
'</a>';

$html = new MimePart($htmlContent);
$html->type = "text/html";

$body = new MimeMessage();
$body->setParts(array($html));

$message = new Message();
$message->addFrom($mailerConfig['from'])
->setSubject($mailerConfig['subject'])
->setBody($body);

foreach($mailerConfig['to'] as $mConfig) {
$message->addTo($mConfig);
}

$mailerService->send($message);
}
}
2 changes: 1 addition & 1 deletion view/theme/default.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

<ul class="rb-comment-list">
<?php foreach ($this->comments as $comment) : ?>
<li class="rb-panel-box">
<li id="rbcomment-<?php echo $comment->id ?>" class="rb-panel-box">
<article class="">
<div class="rb-meta">
<?php echo $comment->author ?> ~ <?php echo $comment->published_on ?>
Expand Down
2 changes: 1 addition & 1 deletion view/theme/uikit.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<ul class="uk-comment-list">
<?php foreach ($this->comments as $comment) : ?>
<li class="uk-panel-box">
<li id="rbcomment-<?php echo $comment->id ?>" class="uk-panel-box">
<article class="uk-comment">
<div class="uk-comment-meta">
<?php echo $comment->author ?> ~ <?php echo $comment->published_on ?>
Expand Down

0 comments on commit 05083f7

Please sign in to comment.