implement MailAgent and use it for user and Character registration

This commit is contained in:
coderkun 2014-05-25 22:38:41 +02:00
commit 7c10592f6b
8 changed files with 183 additions and 31 deletions

View file

@ -283,16 +283,17 @@
if($validation === true && $fieldsValidation === true)
{
$characterId = $this->Characters->createCharacter($this->Auth->getUserId(), $types[$typeIndex]['id'], $charactername);
$character = $this->Characters->getCharacterById($characterId);
// Add Seminary fields
foreach($fields as &$field) {
if(!empty($fieldsValues[$field['url']])) {
$this->Seminarycharacterfield->setSeminaryFieldOfCharacter($field['id'], $characterId, $fieldsValues[$field['url']]);
$this->Seminarycharacterfields->setSeminaryFieldOfCharacter($field['id'], $characterId, $fieldsValues[$field['url']]);
}
}
// Send mail
$this->sendRegistrationMail($charactername);
$this->sendRegistrationMail($character);
// Redirect
$this->redirect($this->linker->link(array('seminaries')));
@ -633,23 +634,34 @@
/**
* Send mail for new Character registration.
*
* @param string $charactername Name of newly registered Character
* @param arary $newCharacter Newly registered Character
*/
private function sendRegistrationMail($charactername)
private function sendRegistrationMail($newCharacter)
{
$sender = \nre\configs\AppConfig::$app['mailsender'];
if(empty($sender)) {
return;
}
// Send notification mail to system moderators
$subject = sprintf('new Character registration: %s', $charactername);
$message = sprintf('User “%s” <%s> has registered a new Character “%s” for the Seminary “%s”', self::$user['username'], self::$user['email'], $charactername, self::$seminary['title']);
// Get Seminary moderators
$characters = $this->Characters->getCharactersWithCharacterRole(self::$seminary['id'], 'moderator');
foreach($characters as &$character)
{
$moderator = $this->Users->getUserById($character['user_id']);
\hhu\z\Utils::sendMail($sender, $moderator['email'], $subject, $message);
// Send notification mail
try {
foreach($characters as &$character)
{
$moderator = $this->Users->getUserById($character['user_id']);
\hhu\z\Utils::sendMail(
$moderator['email'],
'characterregistration',
true,
array(
$moderator,
\hhu\z\controllers\SeminaryController::$seminary,
\hhu\z\controllers\IntermediateController::$user,
$newCharacter
),
$this->linker
);
}
}
catch(\hhu\z\exceptions\MailingException $e) {
$this->log($e->getMessage());
}
}

View file

@ -0,0 +1,85 @@
<?php
/**
* The Legend of Z
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
* @copyright 2014 Heinrich-Heine-Universität Düsseldorf
* @license http://www.gnu.org/licenses/gpl.html
* @link https://bitbucket.org/coderkun/the-legend-of-z
*/
namespace hhu\z\controllers;
/**
* Controller of the MailAgent to generate a mail message.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
class MailController extends \nre\core\Controller
{
/**
* Prefilter.
*
* @param Request $request Current request
* @param Response $response Current response
*/
public function preFilter(\nre\core\Request $request, \nre\core\Response $response)
{
parent::preFilter($request, $response);
// Set linker
$this->set('linker', ($request instanceof \hhu\z\requests\MailRequest && !is_null($request->getLinker())) ? $request->getLinker() : null);
}
/**
* Action: userregistration.
*
* Generate a mail message to notify of a new user registration.
*
* @param array $receiver User that the message will be send to
* @param array $neUser Newly registered user
*/
public function userregistration($receiver, $newUser)
{
// Set subject
$this->response->setSubject(_('New user registration'));
// Pass data to view
$this->set('user', $newUser);
}
/**
* Action: characterregistration.
*
* Generate a mail message to notify of a new Character
* registration.
*
* @param array $receiver User that the message will be send to
* @param array $seminary Seminary which the Character was created for
* @param array $newCharacter Newly registered user
*/
public function characterregistration($receiver, $seminary, $user, $newCharacter)
{
// Set subject
$this->response->setSubject(_('New Character registration'));
// Pass data to view
$this->set('seminary', $seminary);
$this->set('user', $user);
$this->set('character', $newCharacter);
}
}
?>

View file

@ -203,14 +203,15 @@
$email,
$this->request->getPostParam('password')
);
$user = $this->Users->getUserById($userId);
// Send mail
$this->sendRegistrationMail($username, $email);
$this->sendRegistrationMail($user);
// Login
$this->Auth->setUserId($userId);
$user = $this->Users->getUserById($userId);
// Redirect to user page
$this->redirect($this->linker->link(array($user['url']), 1));
}
@ -555,20 +556,29 @@
* @param string $username Name of newly registered user
* @param string $email Email address of newly registered user
*/
private function sendRegistrationMail($username, $email)
private function sendRegistrationMail($user)
{
$sender = \nre\configs\AppConfig::$app['mailsender'];
if(empty($sender)) {
return;
}
// Send notification mail to system moderators
$subject = sprintf('new user registration: %s', $username);
$message = sprintf('User “%s” <%s> has registered themself to %s', $username, $email, \nre\configs\AppConfig::$app['name']);
// Get system moderators
$moderators = $this->Users->getUsersWithRole('moderator');
foreach($moderators as &$moderator)
{
\hhu\z\Utils::sendMail($sender, $moderator['email'], $subject, $message);
// Send notification mail
try {
foreach($moderators as &$moderator)
{
\hhu\z\Utils::sendMail(
$moderator['email'],
'userregistration',
true,
array(
$moderator,
$user
),
$this->linker
);
}
}
catch(\hhu\z\exceptions\MailingException $e) {
$this->log($e->getMessage());
}
}