initialized = true; $this->config = $config + array( 'new_thread' => true, 'new_message' => 'both', ); } /** * The main entry point of a plugin. */ public function run() { // Attach CSS and JS files of the plugin to chat window. $dispatcher = EventDispatcher::getInstance(); $dispatcher->attachListener('pageAddJS', $this, 'attachJsFiles'); } /** * Event handler for "pageAddJS" event. * * @param array $args */ public function attachJSFiles(&$args) { $need_users_plugin = $this->needUsersPlugin($args['request']); $need_chat_plugin = $this->needChatPlugin($args['request']); if ($need_users_plugin || $need_chat_plugin) { $base_path = $this->getFilesPath(); $args['js'][] = $base_path . '/vendor/jquery-titlealert/jquery.titlealert.js'; if ($need_users_plugin) { $args['js'][] = $base_path . '/js/users_plugin.js'; } else { $args['js'][] = $base_path . '/js/chat_plugin.js'; } } } /** * Specify dependencies of the plugin. * * @return array List of dependencies */ public static function getDependencies() { // This plugin does not depend on others so return an empty array. return array(); } /** * Checks if the JS part for users page should be attached. * * @param Request $request Incoming request * @return boolean */ protected function needUsersPlugin(Request $request) { return $request->attributes->get('_route') == 'users' && $this->config['new_thread']; } /** * Checks if the JS part for chat page should be attached. * * @param Request $request Incoming request * @return boolean */ protected function needChatPlugin(Request $request) { $route = $request->attributes->get('_route'); $new_message = $this->config['new_message']; $notify_operator = ($new_message == 'operator') || ($new_message == 'both'); $notify_client = ($new_message == 'client') || ($new_message == 'both'); return ($route == 'chat_operator' && $notify_operator) || (in_array($route, array('chat_user', 'chat_user_start')) && $notify_client); } }