diff --git a/src/mibew/libs/classes/Mibew/Controller/Chat/AbstractController.php b/src/mibew/libs/classes/Mibew/Controller/Chat/AbstractController.php index 7f303518..f5e8e1fa 100644 --- a/src/mibew/libs/classes/Mibew/Controller/Chat/AbstractController.php +++ b/src/mibew/libs/classes/Mibew/Controller/Chat/AbstractController.php @@ -68,4 +68,66 @@ abstract class AbstractController extends BaseAbstractController return $this->redirect($path); } + + /** + * Generates JavaScript code that starts client side application. + * + * @param Request $request Incomming request. + * @param array $options Client side application options. At the moment the + * method accepts the following options: + * - "company": array, a set of company info. See {@link setup_logo()} + * for details. + * - "mibewHost": string, a URL which is used as a Mibew host. See + * {@link setup_logo()} for details. + * - "page.title": string, a value which will be used as a page title. + * - "startFrom": string, indicates what module should be invoked first. + * - "chatOptions": array, (optional) list of chat module options. + * - "surveyOptions": array, (optional) list of pre-chat survey module + * options. + * - "leaveMessageOptions": array, (optional) list of leave message module + * options. + * - "invitationOptions": array, (optional) list of invitation module + * options. + * @return string JavaScript code that starts "users" client side + * application. + * @todo The way options passed here should be reviewed. The method must get + * finite number of well-structured arguments. + */ + protected function startJsApplication(Request $request, $options) + { + $app_settings = array( + 'server' => array( + 'url' => $this->generateUrl('chat_thread_update'), + 'requestsFrequency' => Settings::get('updatefrequency_chat'), + ), + 'page' => array( + 'style' => $this->getStyle()->getName(), + 'mibewBasePath' => $request->getBasePath(), + 'mibewBaseUrl' => $request->getBaseUrl(), + 'stylePath' => $request->getBasePath() . '/' . $this->getStyle()->getFilesPath(), + 'company' => $options['company'], + 'mibewHost' => $options['mibewHost'], + 'title' => $options['page.title'], + ), + 'startFrom' => $options['startFrom'], + ); + + // Add module specific options + $module_options_list = array( + 'chatOptions', + 'surveyOptions', + 'leaveMessageOptions', + 'invitationOptions', + ); + foreach ($module_options_list as $key) { + if (isset($options[$key])) { + $app_settings[$key] = $options[$key]; + } + } + + return sprintf( + 'jQuery(document).ready(function() {Mibew.Application.start(%s);});', + json_encode($app_settings) + ); + } } diff --git a/src/mibew/libs/classes/Mibew/Controller/Chat/OperatorChatController.php b/src/mibew/libs/classes/Mibew/Controller/Chat/OperatorChatController.php index 622992f8..f836484d 100644 --- a/src/mibew/libs/classes/Mibew/Controller/Chat/OperatorChatController.php +++ b/src/mibew/libs/classes/Mibew/Controller/Chat/OperatorChatController.php @@ -19,6 +19,7 @@ namespace Mibew\Controller\Chat; +use Mibew\Asset\AssetManagerInterface; use Mibew\Http\Exception\NotFoundException; use Mibew\Thread; use Symfony\Component\HttpFoundation\Request; @@ -68,13 +69,15 @@ class OperatorChatController extends AbstractController ); // Build js application options - $page['chatOptions'] = json_encode($page['chat']); - - $page['mibewBasePath'] = $request->getBasePath(); - $page['mibewBaseUrl'] = $request->getBaseUrl(); + $page['chatOptions'] = $page['chat']; // Initialize client side application $this->getAssetManager()->attachJs('js/compiled/chat_app.js'); + $this->getAssetManager()->attachJs( + $this->startJsApplication($request, $page), + AssetManagerInterface::INLINE, + 1000 + ); // Render the page with chat. return $this->render('chat', $page); diff --git a/src/mibew/libs/classes/Mibew/Controller/Chat/UserChatController.php b/src/mibew/libs/classes/Mibew/Controller/Chat/UserChatController.php index 82d34fb9..8b68a98f 100644 --- a/src/mibew/libs/classes/Mibew/Controller/Chat/UserChatController.php +++ b/src/mibew/libs/classes/Mibew/Controller/Chat/UserChatController.php @@ -19,6 +19,7 @@ namespace Mibew\Controller\Chat; +use Mibew\Asset\AssetManagerInterface; use Mibew\Http\Exception\NotFoundException; use Mibew\Settings; use Mibew\Thread; @@ -54,13 +55,15 @@ class UserChatController extends AbstractController ); // Build js application options - $page['chatOptions'] = json_encode($page['chat']); - - $page['mibewBasePath'] = $request->getBasePath(); - $page['mibewBaseUrl'] = $request->getBaseUrl(); + $page['chatOptions'] = $page['chat']; // Initialize client side application $this->getAssetManager()->attachJs('js/compiled/chat_app.js'); + $this->getAssetManager()->attachJs( + $this->startJsApplication($request, $page), + AssetManagerInterface::INLINE, + 1000 + ); // Expand page return $this->render('chat', $page); @@ -150,13 +153,15 @@ class UserChatController extends AbstractController $group_id, $info, $referrer - ), - array( - 'mibewBasePath' => $request->getBasePath(), - 'mibewBaseUrl' => $request->getBaseUrl(), ) ); - $page['leaveMessageOptions'] = json_encode($page['leaveMessage']); + $page['leaveMessageOptions'] = $page['leaveMessage']; + + $this->getAssetManager()->attachJs( + $this->startJsApplication($request, $page), + AssetManagerInterface::INLINE, + 1000 + ); return $this->render('chat', $page); } @@ -186,13 +191,15 @@ class UserChatController extends AbstractController $group_id, $info, $referrer - ), - array( - 'mibewBasePath' => $request->getBasePath(), - 'mibewBaseUrl' => $request->getBaseUrl(), ) ); - $page['surveyOptions'] = json_encode($page['survey']); + $page['surveyOptions'] = $page['survey']; + + $this->getAssetManager()->attachJs( + $this->startJsApplication($request, $page), + AssetManagerInterface::INLINE, + 1000 + ); return $this->render('chat', $page); } @@ -254,13 +261,15 @@ class UserChatController extends AbstractController $page = setup_invitation_view($thread); // Build js application options - $page['invitationOptions'] = json_encode($page['invitation']); - - $page['mibewBasePath'] = $request->getBasePath(); - $page['mibewBaseUrl'] = $request->getBaseUrl(); + $page['invitationOptions'] = $page['invitation']; // Initialize client side application $this->getAssetManager()->attachJs('js/compiled/chat_app.js'); + $this->getAssetManager()->attachJs( + $this->startJsApplication($request, $page), + AssetManagerInterface::INLINE, + 1000 + ); // Expand page return $this->render('chat', $page); diff --git a/src/mibew/styles/dialogs/default/templates_src/server_side/chat.handlebars b/src/mibew/styles/dialogs/default/templates_src/server_side/chat.handlebars index df50a40e..74e4a38b 100644 --- a/src/mibew/styles/dialogs/default/templates_src/server_side/chat.handlebars +++ b/src/mibew/styles/dialogs/default/templates_src/server_side/chat.handlebars @@ -6,43 +6,6 @@ - - - {{/override}} {{#override "page"}}