yii2mod / yii2-comments

Comments module for Yii2
MIT License
158 stars 63 forks source link

Sending mail notifications on every comment #30

Closed Gawran closed 7 years ago

Gawran commented 7 years ago

Is it possible to attach event on every comment update to send mail notifications without altering your code?

ihorchepurnyi commented 7 years ago

Hi, Do you want send the email after comment has been created ?

ihorchepurnyi commented 7 years ago

Please use the following code for afterCreate event

'modules' => [
    'comment' => [
        'class' => 'yii2mod\comments\Module',
        // use comment controller event example
        'controllerMap' => [
            'default' => [
                'class' => 'yii2mod\comments\controllers\DefaultController',
                'on afterCreate' => function ($event) {
                    // get userId
                    $event->getUser()->getId();
                    // get identity
                    $event->getUser()->getIdentity();
                    // your custom code
                }
            ]
        ]
    ]
]

https://github.com/yii2mod/yii2-comments#configuration

ihorchepurnyi commented 7 years ago

Resolved by commit 1cbc050

Gawran commented 7 years ago

Yes. I want to send an email every time the comment is created. Sorry for the late response it's different time zone here. Thanks for the solution.

On Sun, Nov 6, 2016 at 12:31 PM, Igor Chepurnoy notifications@github.com wrote:

Closed #30 https://github.com/yii2mod/yii2-comments/issues/30.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/yii2mod/yii2-comments/issues/30#event-849051461, or mute the thread https://github.com/notifications/unsubscribe-auth/AEv4-0b_4g0FKXcd6IzenqryI_sEVLHIks5q7bqIgaJpZM4KqWH_ .

Sa poštovanjem, Miodrag Gavranov

Gawran commented 7 years ago

May I suggest to pass and entity data or only entityId in $event object? I need that connection so I can extract recipients for my MailEvent. Basicly I'll trigger my own afterUpdate event in your afterCreate event.

something like:

public function actionCreate($entity) { /* @var $module Module _/ $module = Yii::$app->getModule(Module::$name); $event = Yii::createObject(['class' => CommentEvent::className(), 'user' => Yii::$app->user]); $commentModelClass = $module->commentModelClass; $decryptEntity = Yii::$app->getSecurity()->decryptByKey(utf8decode($entity), $module::$name); if ($decryptEntity !== false) { $entityData = Json::decode($decryptEntity); / @var $model CommentModel */ $model = new $commentModelClass; $model->setAttributes($entityData); $event->entityData = $entityData; if ($model->load(Yii::$app->request->post()) && $model->save()) { $this->trigger(self::EVENT_AFTER_CREATE, $event); return ['status' => 'success']; } else { return [ 'status' => 'error', 'errors' => ActiveForm::validate($model) ]; } } else { return [ 'status' => 'error', 'message' => Yii::t('yii2mod.comments', 'Oops, something went wrong. Please try again later.') ]; } }

and add public property in CommentEvent named entityData or something similar... or define getter and setter.

/**

On Sun, Nov 6, 2016 at 5:03 PM, Miodrag Gavranov <miodrag.gavranov@gmail.com

wrote:

Yes. I want to send an email every time the comment is created. Sorry for the late response it's different time zone here. Thanks for the solution.

On Sun, Nov 6, 2016 at 12:31 PM, Igor Chepurnoy notifications@github.com wrote:

Closed #30 https://github.com/yii2mod/yii2-comments/issues/30.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/yii2mod/yii2-comments/issues/30#event-849051461, or mute the thread https://github.com/notifications/unsubscribe-auth/AEv4-0b_4g0FKXcd6IzenqryI_sEVLHIks5q7bqIgaJpZM4KqWH_ .

Sa poštovanjem, Miodrag Gavranov

Sa poštovanjem, Miodrag Gavranov

ihorchepurnyi commented 7 years ago

Do you mean that you want get entity object in afterCreate event ?

ihorchepurnyi commented 7 years ago

I think we can pass comment model to event, what do you think?

ihorchepurnyi commented 7 years ago

I was update the CommentEvent class, now you can get the saved comment object as following:

'on afterCreate' => function ($event) {
      $event->getCommentModel();
      // your custom code
}
Gawran commented 7 years ago

Even better! Thanks.

On Sun, Nov 6, 2016 at 7:48 PM, Igor Chepurnoy notifications@github.com wrote:

I was update the CommentEvent class, now you can get the saved comment model object as following:

'on afterCreate' => function ($event) { $event->getCommentModel(); // your custom code}

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/yii2mod/yii2-comments/issues/30#issuecomment-258701251, or mute the thread https://github.com/notifications/unsubscribe-auth/AEv4-0WFqv5Z2UFncMawxZlrs8ZOQW77ks5q7iDugaJpZM4KqWH_ .

Sa poštovanjem, Miodrag Gavranov