Change to user language when sending notification mails

This commit is contained in:
Lucas Bartholemy 2017-02-07 14:40:33 +01:00
parent b0c61f105f
commit 0703cd6515
3 changed files with 25 additions and 21 deletions

View File

@ -13,6 +13,6 @@ It's perfect for individual:
More information: More information:
- [Homepage & Demo](http://www.humhub.org) - [Homepage & Demo](http://www.humhub.org)
- [Documentation & Class Reference](http://www.humhub.org/docs) - [Documentation & Class Reference](http://docs.humhub.org)
- [Licence](http://www.humhub.org/licences) - [Licence](http://www.humhub.org/licences)

View File

@ -23,7 +23,7 @@ class MailNotificationTarget extends NotificationTarget
* @var type * @var type
*/ */
public $defaultSetting = true; public $defaultSetting = true;
/** /**
* @var array Notification mail layout. * @var array Notification mail layout.
*/ */
@ -45,27 +45,31 @@ class MailNotificationTarget extends NotificationTarget
*/ */
public function handle(BaseNotification $notification, User $recipient) public function handle(BaseNotification $notification, User $recipient)
{ {
Yii::$app->i18n->setUserLocale($recipient);
Yii::$app->view->params['showUnsubscribe'] = true; Yii::$app->view->params['showUnsubscribe'] = true;
Yii::$app->view->params['unsubscribeUrl'] = \yii\helpers\Url::to(['/notification/user'], true); Yii::$app->view->params['unsubscribeUrl'] = \yii\helpers\Url::to(['/notification/user'], true);
// Note: the renderer is configured in common.php by default its an instance of MailNotificatoinTarget // Note: the renderer is configured in common.php by default its an instance of MailNotificationTarget
$renderer = $this->getRenderer(); $renderer = $this->getRenderer();
$viewParams = \yii\helpers\ArrayHelper::merge([ $viewParams = \yii\helpers\ArrayHelper::merge([
'headline' => $notification->getHeadline($recipient), 'headline' => $notification->getHeadline($recipient),
'notification' => $notification, 'notification' => $notification,
'space' => $notification->getSpace(), 'space' => $notification->getSpace(),
'content' => $renderer->render($notification), 'content' => $renderer->render($notification),
'content_plaintext' => $renderer->renderText($notification) 'content_plaintext' => $renderer->renderText($notification)
], $notification->getViewParams()); ], $notification->getViewParams());
$from = $notification->originator
? Html::encode($notification->originator->displayName).' ('.Html::encode(Yii::$app->name).')' $from = $notification->originator ? Html::encode($notification->originator->displayName) . ' (' . Html::encode(Yii::$app->name) . ')' : Yii::$app->settings->get('mailer.systemEmailName');
: Yii::$app->settings->get('mailer.systemEmailName');
Yii::$app->mailer->compose($this->view, $viewParams)
return Yii::$app->mailer->compose($this->view, $viewParams) ->setFrom([Yii::$app->settings->get('mailer.systemEmailAddress') => $from])
->setFrom([Yii::$app->settings->get('mailer.systemEmailAddress') => $from]) ->setTo($recipient->email)
->setTo($recipient->email) ->setSubject($notification->getTitle($recipient))->send();
->setSubject($notification->getTitle($recipient))->send();
Yii::$app->i18n->autosetLocale();
} }
} }

View File

@ -153,7 +153,7 @@ abstract class NotificationTarget extends \yii\base\Object
* Used for handling the given $notification for multiple $users. * Used for handling the given $notification for multiple $users.
* *
* @param BaseNotification $notification * @param BaseNotification $notification
* @param type $users * @param User[] $users
*/ */
public function sendBulk(BaseNotification $notification, $users) public function sendBulk(BaseNotification $notification, $users)
{ {