Clean up "Chat\User\MailController" logic

This commit is contained in:
Dmitriy Simushev 2014-06-03 08:56:11 +00:00
parent e886e7dc33
commit 44ca9e2a2e
8 changed files with 88 additions and 55 deletions

View File

@ -5,4 +5,4 @@
You may obtain a copy of the License at You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
*/ */
(function(b,d,e){b.Views.SendMailControl=b.Views.Control.extend({template:d.templates.chat_controls_send_mail,events:e.extend({},b.Views.Control.prototype.events,{click:"sendMail"}),sendMail:function(){var a=this.model.get("link"),c=b.Objects.Models.page;if(a){var d=this.model.get("windowParams"),c=c.get("style"),a=a.replace(/\&amp\;/g,"&")+(c?"&style="+c:""),a=window.open(a,"ForwardMail",d);null!==a&&(a.focus(),a.opener=window)}}})})(Mibew,Handlebars,_); (function(b,d,f){b.Views.SendMailControl=b.Views.Control.extend({template:d.templates.chat_controls_send_mail,events:f.extend({},b.Views.Control.prototype.events,{click:"sendMail"}),sendMail:function(){var a=this.model.get("link"),c=b.Objects.Models.page;if(a){var d=this.model.get("windowParams"),c=c.get("style"),e="";c&&(e=(-1===a.indexOf("?")?"?":"&")+"style="+c);a=a.replace(/\&amp\;/g,"&")+e;a=window.open(a,"ForwardMail",d);null!==a&&(a.focus(),a.opener=window)}}})})(Mibew,Handlebars,_);

View File

@ -230,7 +230,7 @@ d="";c&&(d=(-1===a.indexOf("?")?"?":"&")+"style="+c);window.location.href=a.repl
You may obtain a copy of the License at You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
*/ */
(function(b,d,e){b.Views.SendMailControl=b.Views.Control.extend({template:d.templates.chat_controls_send_mail,events:e.extend({},b.Views.Control.prototype.events,{click:"sendMail"}),sendMail:function(){var a=this.model.get("link"),c=b.Objects.Models.page;if(a){var d=this.model.get("windowParams"),c=c.get("style"),a=a.replace(/\&amp\;/g,"&")+(c?"&style="+c:""),a=window.open(a,"ForwardMail",d);null!==a&&(a.focus(),a.opener=window)}}})})(Mibew,Handlebars,_); (function(b,d,f){b.Views.SendMailControl=b.Views.Control.extend({template:d.templates.chat_controls_send_mail,events:f.extend({},b.Views.Control.prototype.events,{click:"sendMail"}),sendMail:function(){var a=this.model.get("link"),c=b.Objects.Models.page;if(a){var d=this.model.get("windowParams"),c=c.get("style"),e="";c&&(e=(-1===a.indexOf("?")?"?":"&")+"style="+c);a=a.replace(/\&amp\;/g,"&")+e;a=window.open(a,"ForwardMail",d);null!==a&&(a.focus(),a.opener=window)}}})})(Mibew,Handlebars,_);
/* /*
Copyright 2005-2014 the original author or authors. Copyright 2005-2014 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License"). Licensed under the Apache License, Version 2.0 (the "License").

View File

@ -44,10 +44,14 @@
var winParams = this.model.get('windowParams'); var winParams = this.model.get('windowParams');
var style = page.get('style'); var style = page.get('style');
var styleArg = '';
if (style) {
styleArg = ((link.indexOf('?') === -1) ? '?' : '&')
+ 'style=' + style;
}
// TODO: Kill & at the server side // TODO: Kill & at the server side
link = link.replace(/\&amp\;/g, '&') link = link.replace(/\&amp\;/g, '&') + styleArg;
+ (style ? ('&style=' + style) : '');
var newWindow = window.open(link, 'ForwardMail', winParams); var newWindow = window.open(link, 'ForwardMail', winParams);
if (newWindow !== null) { if (newWindow !== null) {

View File

@ -526,9 +526,8 @@ function setup_chatview_for_user(Thread $thread)
$params = "thread=" . $thread->id . "&token=" . $thread->lastToken; $params = "thread=" . $thread->id . "&token=" . $thread->lastToken;
// Set link to send mail page // Set link to send mail page
$data['chat']['links']['mail'] = MIBEW_WEB_ROOT . "/chat?" $data['chat']['links']['mail'] = MIBEW_WEB_ROOT . "/chat"
. $params . '/' . $thread->id . '/' . $thread->lastToken . '/mail';
. "&act=mailthread";
// Set SSL link // Set SSL link
if (Settings::get('enablessl') == "1" && !is_secure_request()) { if (Settings::get('enablessl') == "1" && !is_secure_request()) {

View File

@ -56,7 +56,7 @@ class ChatController extends AbstractController
} }
$action = $request->query->get('act'); $action = $request->query->get('act');
if (!in_array($action, array('invitation', 'mailthread'))) { if ($action != 'invitation') {
$action = 'default'; $action = 'default';
} }
@ -101,16 +101,12 @@ class ChatController extends AbstractController
$page = setup_chatview_for_user($thread); $page = setup_chatview_for_user($thread);
if ($action == 'mailthread') {
return $this->render('mail', $page);
} else {
// Build js application options // Build js application options
$page['chatOptions'] = json_encode($page['chat']); $page['chatOptions'] = json_encode($page['chat']);
// Expand page // Expand page
return $this->render('chat', $page); return $this->render('chat', $page);
} }
}
protected function startChat(Request $request) protected function startChat(Request $request)
{ {

View File

@ -18,7 +18,7 @@
namespace Mibew\Controller\Chat\User; namespace Mibew\Controller\Chat\User;
use Mibew\Controller\Chat\AbstractController; use Mibew\Controller\Chat\AbstractController;
use Mibew\Http\Exception\BadRequestException; use Mibew\Http\Exception\NotFoundException;
use Mibew\Settings; use Mibew\Settings;
use Mibew\Thread; use Mibew\Thread;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@ -29,47 +29,33 @@ use Symfony\Component\HttpFoundation\Request;
class MailController extends AbstractController class MailController extends AbstractController
{ {
/** /**
* Process sending chat history to an email. * Renders the mail form.
* *
* @param Request $request Incoming request. * @param Request $request Incoming request.
* @return string|\Symfony\Component\HttpFoundation\RedirectResponse Rendered * @return string Rendered page content.
* page content or a redirect response. * @throws NotFoundException If the thread with specified ID and token is
* @throws BadRequestException If the thread cannot be loaded by some * not found.
* reasons.
*/ */
public function indexAction(Request $request) public function showFormAction(Request $request)
{ {
$page = array( $page = array(
'errors' => array(), // Use errors list stored in the request. We need to do so to have
// an ability to pass the request from the "submitForm" action.
'errors' => $request->attributes->get('errors', array()),
); );
// Get and validate thread id $thread_id = $request->attributes->get('thread_id');
$thread_id = $request->request->get('thread'); $token = $request->attributes->get('token');
if (!preg_match("/^\d{1,10}$/", $thread_id)) {
throw new BadRequestException('Wrong value of "thread" argument.');
}
// Get token and verify it
$token = $request->request->get('token');
if (!preg_match("/^\d{1,10}$/", $token)) {
throw new BadRequestException('Wrong value of "token" argument.');
}
// Try to load the thread
$thread = Thread::load($thread_id, $token); $thread = Thread::load($thread_id, $token);
if (!$thread) { if (!$thread) {
throw new BadRequestException('Wrong thread.'); throw new NotFoundException('The thread is not found.');
} }
$email = $request->request->get('email'); $email = $request->request->get('email', '');
$page['email'] = $email;
$group = is_null($thread->groupId) ? null : group_by_id($thread->groupId); $group = is_null($thread->groupId) ? null : group_by_id($thread->groupId);
if (!$email) {
$page['errors'][] = no_field('form.field.email');
} elseif (!is_valid_email($email)) {
$page['errors'][] = wrong_field('form.field.email');
}
if (count($page['errors']) > 0) {
$page['formemail'] = $email; $page['formemail'] = $email;
$page['chat.thread.id'] = $thread->id; $page['chat.thread.id'] = $thread->id;
$page['chat.thread.token'] = $thread->lastToken; $page['chat.thread.token'] = $thread->lastToken;
@ -82,6 +68,42 @@ class MailController extends AbstractController
return $this->render('mail', $page); return $this->render('mail', $page);
} }
/**
* Process submitting of the mail form.
*
* @param Request $request Incoming request.
* @return string Rendered page content.
* @throws NotFoundException If the thread with specified ID and token is
* not found.
*/
public function submitFormAction(Request $request)
{
$errors = array();
$thread_id = $request->attributes->get('thread_id');
$token = $request->attributes->get('token');
// Try to load the thread
$thread = Thread::load($thread_id, $token);
if (!$thread) {
throw new NotFoundException('The thread is not found.');
}
$email = $request->request->get('email');
$group = is_null($thread->groupId) ? null : group_by_id($thread->groupId);
if (!$email) {
$errors[] = no_field('form.field.email');
} elseif (!is_valid_email($email)) {
$errors[] = wrong_field('form.field.email');
}
if (count($errors) > 0) {
$request->attributes->set('errors', $errors);
// Render the mail form again
return $this->showFormAction($request);
}
$history = ''; $history = '';
$last_id = -1; $last_id = -1;
$messages = $thread->getMessages(true, $last_id); $messages = $thread->getMessages(true, $last_id);
@ -102,7 +124,8 @@ class MailController extends AbstractController
mibew_mail($email, MIBEW_MAILBOX, $subject, $body); mibew_mail($email, MIBEW_MAILBOX, $subject, $body);
$page = array_merge_recursive($page, setup_logo($group)); $page = setup_logo($group);
$page['email'] = $email;
return $this->render('mailsent', $page); return $this->render('mailsent', $page);
} }

View File

@ -43,9 +43,22 @@ chat_user:
_controller: Mibew\Controller\Chat\User\ChatController::indexAction _controller: Mibew\Controller\Chat\User\ChatController::indexAction
chat_user_mail: chat_user_mail:
path: /chat/mail path: /chat/{thread_id}/{token}/mail
defaults: defaults:
_controller: Mibew\Controller\Chat\User\MailController::indexAction _controller: Mibew\Controller\Chat\User\MailController::showFormAction
requirements:
thread_id: \d{1,10}
token: \d{1,10}
methods: [GET]
chat_user_mail_send:
path: /chat/{thread_id}/{token}/mail
defaults:
_controller: Mibew\Controller\Chat\User\MailController::submitFormAction
requirements:
thread_id: \d{1,10}
token: \d{1,10}
methods: [POST]
# Pages that are available for all users # Pages that are available for all users
button: button:

View File

@ -8,10 +8,8 @@
{{/override}} {{/override}}
{{#override "message"}} {{#override "message"}}
<form name="mailThreadForm" method="post" action="{{mibewRoot}}/chat/mail"> <form name="mailThreadForm" method="post" action="{{mibewRoot}}/chat/{{this.[chat.thread.id]}}/{{this.[chat.thread.token]}}/mail">
<input type="hidden" name="style" value="{{styleName}}"/> <input type="hidden" name="style" value="{{styleName}}"/>
<input type="hidden" name="thread" value="{{chat.thread.id}}"/>
<input type="hidden" name="token" value="{{chat.thread.token}}"/>
<input type="hidden" name="level" value="{{level}}"/> <input type="hidden" name="level" value="{{level}}"/>
<strong>{{l10n "mailthread.enter_email"}}</strong> <strong>{{l10n "mailthread.enter_email"}}</strong>