Don't use global environment in "setup_chatview_for_user" func

This commit is contained in:
Dmitriy Simushev 2014-08-28 10:22:17 +00:00
parent 896e0d924b
commit f61ef23e3f
4 changed files with 82 additions and 10 deletions

View File

@ -488,11 +488,16 @@ function setup_chatview(Thread $thread)
/** /**
* Prepare some data for chat for user * Prepare some data for chat for user
* *
* @param UrlGeneratorInterface $url_generator A URL generator object.
* @param Request $request The current request.
* @param Thread $thread thread object that will be used * @param Thread $thread thread object that will be used
* @return array Array of chat view data * @return array Array of chat view data
*/ */
function setup_chatview_for_user(Thread $thread) function setup_chatview_for_user(
{ UrlGeneratorInterface $url_generator,
Request $request,
Thread $thread
) {
$data = setup_chatview($thread); $data = setup_chatview($thread);
// Load JavaScript plugins and JavaScripts, CSS files required by them // Load JavaScript plugins and JavaScripts, CSS files required by them
@ -508,13 +513,24 @@ function setup_chatview_for_user(Thread $thread)
); );
// Set link to send mail page // Set link to send mail page
$data['chat']['links']['mail'] = MIBEW_WEB_ROOT . "/chat" $data['chat']['links']['mail'] = $url_generator->generate(
. '/' . $thread->id . '/' . $thread->lastToken . '/mail'; 'chat_user_mail',
array(
'thread_id' => $thread->id,
'token' => $thread->lastToken,
)
);
// Set SSL link // Set SSL link
if (Settings::get('enablessl') == "1" && !is_secure_request()) { if (Settings::get('enablessl') == "1" && $request->isSecure()) {
$data['chat']['links']['ssl'] = get_app_location(true, true) $data['chat']['links']['ssl'] = $url_generator->generateSecure(
. '/chat/' . $thread->id . '/' . $thread->lastToken; 'chat_user',
array(
'thread_id' => $thread->id,
'token' => $thread->lastToken,
),
UrlGeneratorInterface::ABSOLUTE_URL
);
} }
// Set default operator's avatar // Set default operator's avatar

View File

@ -37,6 +37,7 @@ class ThreadController extends AbstractController
public function updateAction(Request $request) public function updateAction(Request $request)
{ {
$processor = ThreadProcessor::getInstance(); $processor = ThreadProcessor::getInstance();
$processor->setRouter($this->getRouter());
return $processor->handleRequest($request); return $processor->handleRequest($request);
} }

View File

@ -47,7 +47,11 @@ class UserChatController extends AbstractController
throw new NotFoundException('The thread is not found.'); throw new NotFoundException('The thread is not found.');
} }
$page = setup_chatview_for_user($thread); $page = setup_chatview_for_user(
$this->getRouter(),
$request,
$thread
);
// Build js application options // Build js application options
$page['chatOptions'] = json_encode($page['chat']); $page['chatOptions'] = json_encode($page['chat']);

View File

@ -24,6 +24,9 @@ use Mibew\Settings;
use Mibew\Thread; use Mibew\Thread;
use Mibew\API\API as MibewAPI; use Mibew\API\API as MibewAPI;
use Mibew\RequestProcessor\Exception\ThreadProcessorException; use Mibew\RequestProcessor\Exception\ThreadProcessorException;
use Mibew\Routing\RouterAwareInterface;
use Mibew\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\Request;
/** /**
* Incapsulates thread api and thread processing functions. * Incapsulates thread api and thread processing functions.
@ -38,9 +41,25 @@ use Mibew\RequestProcessor\Exception\ThreadProcessorException;
* WARNING: * WARNING:
* threadResponseReceived registered but never called because of asynchronous * threadResponseReceived registered but never called because of asynchronous
* nature of Core-to-Window interaction * nature of Core-to-Window interaction
*
* @todo Move all API functions to another place.
*/ */
class ThreadProcessor extends ClientSideProcessor class ThreadProcessor extends ClientSideProcessor implements RouterAwareInterface
{ {
/**
* The request which is hadled now.
*
* @var Request|null
*/
protected $currentRequest = null;
/**
* A Router instance.
*
* @var RouterInterface|null
*/
protected $router = null;
/** /**
* Loads thread by id and token and checks if thread loaded * Loads thread by id and token and checks if thread loaded
* *
@ -108,6 +127,34 @@ class ThreadProcessor extends ClientSideProcessor
return $operator; return $operator;
} }
/**
* {@inheritdoc}
*/
public function handleRequest($request)
{
$this->currentRequest = $request;
$response = parent::handleRequest($request);
$this->currentRequest = null;
return $response;
}
/**
* {@inheritdoc}
*/
public function getRouter()
{
return $this->router;
}
/**
* {@inheritdoc}
*/
public function setRouter(RouterInterface $router)
{
$this->router = $router;
}
/** /**
* Class constructor * Class constructor
*/ */
@ -520,7 +567,11 @@ class ThreadProcessor extends ClientSideProcessor
} }
// Prepare chat options // Prepare chat options
$client_data = setup_chatview_for_user($thread); $client_data = setup_chatview_for_user(
$this->getRouter(),
$this->currentRequest,
$thread
);
$options = $client_data['chat']; $options = $client_data['chat'];
$options['page'] += setup_logo($group); $options['page'] += setup_logo($group);