From 7c10592f6bebe065262a7efa185a010bf0784e94 Mon Sep 17 00:00:00 2001 From: coderkun Date: Sun, 25 May 2014 22:38:41 +0200 Subject: [PATCH] implement MailAgent and use it for user and Character registration --- agents/intermediate/MailAgent.inc | 35 ++++++++ controllers/CharactersController.inc | 44 ++++++---- controllers/MailController.inc | 85 +++++++++++++++++++ controllers/UsersController.inc | 40 +++++---- views/htmlmail/mail/characterregistration.tpl | 2 + views/htmlmail/mail/userregistration.tpl | 2 + views/textmail/mail/characterregistration.tpl | 3 + views/textmail/mail/userregistration.tpl | 3 + 8 files changed, 183 insertions(+), 31 deletions(-) create mode 100644 agents/intermediate/MailAgent.inc create mode 100644 controllers/MailController.inc create mode 100644 views/htmlmail/mail/characterregistration.tpl create mode 100644 views/htmlmail/mail/userregistration.tpl create mode 100644 views/textmail/mail/characterregistration.tpl create mode 100644 views/textmail/mail/userregistration.tpl diff --git a/agents/intermediate/MailAgent.inc b/agents/intermediate/MailAgent.inc new file mode 100644 index 00000000..7bb5eef5 --- /dev/null +++ b/agents/intermediate/MailAgent.inc @@ -0,0 +1,35 @@ + + * @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\agents\intermediate; + + + /** + * Agent to generate a mail-message. + * + * @author Oliver Hanraths + */ + class MailAgent extends \nre\agents\IntermediateAgent + { + + + + + /** + * Action: index. + */ + public function index(\nre\core\Request $request, \nre\core\Response $response) + { + } + + } + +?> diff --git a/controllers/CharactersController.inc b/controllers/CharactersController.inc index 6f98b3ca..7d1e20a8 100644 --- a/controllers/CharactersController.inc +++ b/controllers/CharactersController.inc @@ -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()); } } diff --git a/controllers/MailController.inc b/controllers/MailController.inc new file mode 100644 index 00000000..b6519cbe --- /dev/null +++ b/controllers/MailController.inc @@ -0,0 +1,85 @@ + + * @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 + */ + 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); + } + + } + +?> diff --git a/controllers/UsersController.inc b/controllers/UsersController.inc index 5a32ab51..0d80bbd9 100644 --- a/controllers/UsersController.inc +++ b/controllers/UsersController.inc @@ -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 E‑mail 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()); } } diff --git a/views/htmlmail/mail/characterregistration.tpl b/views/htmlmail/mail/characterregistration.tpl new file mode 100644 index 00000000..723931de --- /dev/null +++ b/views/htmlmail/mail/characterregistration.tpl @@ -0,0 +1,2 @@ +: + diff --git a/views/htmlmail/mail/userregistration.tpl b/views/htmlmail/mail/userregistration.tpl new file mode 100644 index 00000000..e5b79d20 --- /dev/null +++ b/views/htmlmail/mail/userregistration.tpl @@ -0,0 +1,2 @@ +: + diff --git a/views/textmail/mail/characterregistration.tpl b/views/textmail/mail/characterregistration.tpl new file mode 100644 index 00000000..22047555 --- /dev/null +++ b/views/textmail/mail/characterregistration.tpl @@ -0,0 +1,3 @@ +: + +link(array('characters',$seminary['url'],$user['url']),0,false,null,false,null,true)?> diff --git a/views/textmail/mail/userregistration.tpl b/views/textmail/mail/userregistration.tpl new file mode 100644 index 00000000..8d77ff03 --- /dev/null +++ b/views/textmail/mail/userregistration.tpl @@ -0,0 +1,3 @@ +: + +link(array('users',$user['url']),0,false,null,false,null,true)?>