From 9898fb0cbe784b91a73ebb5b28f6f1172ac2a7b4 Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Thu, 9 Oct 2014 13:51:37 +0000 Subject: [PATCH] Create "additionalCss" HBS helper --- .../libs/classes/Mibew/Asset/AssetManager.php | 13 +- .../Mibew/Controller/AbstractController.php | 8 ++ .../Chat/OperatorChatController.php | 13 +- .../Controller/Chat/UserChatController.php | 18 +-- .../Mibew/Controller/UsersController.php | 2 - .../Handlebars/Helper/AdditionalCssHelper.php | 131 ++++++++++++++++++ src/mibew/libs/common/response.php | 52 ------- .../templates_src/server_side/chat.handlebars | 2 +- .../server_side/users.handlebars | 2 +- 9 files changed, 162 insertions(+), 79 deletions(-) create mode 100644 src/mibew/libs/classes/Mibew/Handlebars/Helper/AdditionalCssHelper.php diff --git a/src/mibew/libs/classes/Mibew/Asset/AssetManager.php b/src/mibew/libs/classes/Mibew/Asset/AssetManager.php index 1de8c23b..3643e4b0 100644 --- a/src/mibew/libs/classes/Mibew/Asset/AssetManager.php +++ b/src/mibew/libs/classes/Mibew/Asset/AssetManager.php @@ -203,8 +203,17 @@ class AssetManager implements AssetManagerInterface } /** - * Triggers "pageAddCSS" event and prepares CSS assets which are returned by - * plugins. + * Gets additional CSS assets by triggering some events. + * + * Triggers "pageAddCSS" and passes to the listeners an associative array + * with the following keys: + * - "request": {@link \Symfony\Component\HttpFoundation\Request}, a + * request instance. CSS files will be attached to the requested page. + * - "css": array of assets. Each asset can be either a string with + * absolute URL of a CSS file or an array with "content" and "type" + * items. See {@link \Mibew\Asset\AssetManagerInterface::getCssAssets()} + * for details of their meaning. Modify this array to add or remove + * additional CSS files. * * @return array Assets list. */ diff --git a/src/mibew/libs/classes/Mibew/Controller/AbstractController.php b/src/mibew/libs/classes/Mibew/Controller/AbstractController.php index e20c9635..8f51314a 100644 --- a/src/mibew/libs/classes/Mibew/Controller/AbstractController.php +++ b/src/mibew/libs/classes/Mibew/Controller/AbstractController.php @@ -26,6 +26,7 @@ use Mibew\Authentication\AuthenticationManagerAwareInterface; use Mibew\Authentication\AuthenticationManagerInterface; use Mibew\Cache\CacheAwareInterface; use Mibew\Handlebars\HandlebarsAwareInterface; +use Mibew\Handlebars\Helper\AdditionalCssHelper; use Mibew\Handlebars\Helper\AdditionalJsHelper; use Mibew\Handlebars\Helper\AssetHelper; use Mibew\Handlebars\Helper\CsrfProtectedRouteHelper; @@ -131,6 +132,9 @@ abstract class AbstractController implements if ($handlebars->hasHelper('additionalJs')) { $handlebars->getHelper('additionalJs')->setAssetManager($manager); } + if ($handlebars->hasHelper('additionalCss')) { + $handlebars->getHelper('additionalCss')->setAssetManager($manager); + } } } @@ -291,6 +295,10 @@ abstract class AbstractController implements 'additionalJs', new AdditionalJsHelper($this->getAssetManager()) ); + $style->getHandlebars()->addHelper( + 'additionalCss', + new AdditionalCssHelper($this->getAssetManager()) + ); } return $style; diff --git a/src/mibew/libs/classes/Mibew/Controller/Chat/OperatorChatController.php b/src/mibew/libs/classes/Mibew/Controller/Chat/OperatorChatController.php index a2712731..b3ba47b0 100644 --- a/src/mibew/libs/classes/Mibew/Controller/Chat/OperatorChatController.php +++ b/src/mibew/libs/classes/Mibew/Controller/Chat/OperatorChatController.php @@ -60,14 +60,11 @@ class OperatorChatController extends AbstractController return $this->showErrors(array('Cannot view threads')); } - $page = array_merge_recursive( - setup_chatview_for_operator( - $this->getRouter(), - $request, - $thread, - $operator - ), - get_plugins_data($request) + $page = setup_chatview_for_operator( + $this->getRouter(), + $request, + $thread, + $operator ); // Build js application options diff --git a/src/mibew/libs/classes/Mibew/Controller/Chat/UserChatController.php b/src/mibew/libs/classes/Mibew/Controller/Chat/UserChatController.php index 16232749..c713011c 100644 --- a/src/mibew/libs/classes/Mibew/Controller/Chat/UserChatController.php +++ b/src/mibew/libs/classes/Mibew/Controller/Chat/UserChatController.php @@ -47,13 +47,10 @@ class UserChatController extends AbstractController throw new NotFoundException('The thread is not found.'); } - $page = array_merge_recursive( - setup_chatview_for_user( - $this->getRouter(), - $request, - $thread - ), - get_plugins_data($request) + $page = setup_chatview_for_user( + $this->getRouter(), + $request, + $thread ); // Build js application options @@ -148,7 +145,6 @@ class UserChatController extends AbstractController $info, $referrer ), - get_plugins_data($request), array( 'mibewBasePath' => $request->getBasePath(), 'mibewBaseUrl' => $request->getBaseUrl(), @@ -185,7 +181,6 @@ class UserChatController extends AbstractController $info, $referrer ), - get_plugins_data($request), array( 'mibewBasePath' => $request->getBasePath(), 'mibewBaseUrl' => $request->getBaseUrl(), @@ -250,10 +245,7 @@ class UserChatController extends AbstractController $thread = Thread::load($invitation_state['threadid']); // Prepare page - $page = array_merge_recursive( - setup_invitation_view($thread), - get_plugins_data($request) - ); + $page = setup_invitation_view($thread); // Build js application options $page['invitationOptions'] = json_encode($page['invitation']); diff --git a/src/mibew/libs/classes/Mibew/Controller/UsersController.php b/src/mibew/libs/classes/Mibew/Controller/UsersController.php index 28b77468..e7e48b9b 100644 --- a/src/mibew/libs/classes/Mibew/Controller/UsersController.php +++ b/src/mibew/libs/classes/Mibew/Controller/UsersController.php @@ -80,8 +80,6 @@ class UsersController extends AbstractController $page['title'] = getlocal("List of visitors waiting"); $page['menuid'] = "users"; - // Get additional plugins data - $page = array_merge($page, get_plugins_data($request)); $page = array_merge($page, prepare_menu($operator)); return $this->render('users', $page); diff --git a/src/mibew/libs/classes/Mibew/Handlebars/Helper/AdditionalCssHelper.php b/src/mibew/libs/classes/Mibew/Handlebars/Helper/AdditionalCssHelper.php new file mode 100644 index 00000000..c6e63f90 --- /dev/null +++ b/src/mibew/libs/classes/Mibew/Handlebars/Helper/AdditionalCssHelper.php @@ -0,0 +1,131 @@ + + * {{additionalCss}} + * + */ +class AdditionalCssHelper implements HelperInterface, AssetManagerAwareInterface +{ + /** + * @var AssetManagerInterface|null + */ + protected $manager = null; + + /** + * Class constructor. + * + * @param AssetUrlGeneratorInterface $manager An instance of Asset Manager. + */ + public function __construct(AssetManagerInterface $manager) + { + $this->manager = $manager; + } + + /** + * {@inheritdoc} + */ + public function getAssetManager() + { + return $this->manager; + } + + /** + * {@inheritdoc} + */ + public function setAssetManager(AssetManagerInterface $manager) + { + $this->manager = $manager; + } + + /** + * {@inheritdoc} + */ + public function execute(Template $template, Context $context, $args, $source) + { + $generator = $this->getAssetManager()->getUrlGenerator(); + $buffer = array(); + + foreach ($this->getAssetManager()->getCssAssets() as $asset) { + switch ($asset['type']) { + case AssetManagerInterface::ABSOLUTE_URL: + $buffer[] = $this->renderUrl($asset['content']); + break; + + case AssetManagerInterface::RELATIVE_URL: + $buffer[] = $this->renderUrl($generator->generate($asset['content'])); + break; + + case AssetManagerInterface::INLINE: + $buffer[] = $this->renderContent($asset['content']); + break; + + default: + throw new \RuntimeException(sprintf( + 'Unknown asset type "%s"', + $asset['type'] + )); + } + } + + return new SafeString(implode("\n", $buffer)); + } + + /** + * Renders URL of an asset. + * + * @param string $url URL of an asset. + * @return string HTML markup. + */ + protected function renderUrl($url) + { + return sprintf( + '', + safe_htmlspecialchars($url) + ); + } + + /** + * Renders content of an asset. + * + * @param string $content Content of an asset. + * @return string HTML markup. + */ + protected function renderContent($content) + { + return sprintf( + '', + $content + ); + } +} diff --git a/src/mibew/libs/common/response.php b/src/mibew/libs/common/response.php index e3530b76..e2a96dcd 100644 --- a/src/mibew/libs/common/response.php +++ b/src/mibew/libs/common/response.php @@ -17,9 +17,6 @@ * limitations under the License. */ -use Mibew\EventDispatcher; -use Symfony\Component\HttpFoundation\Request; - function get_popup($href, $js_href, $message, $title, $wnd_name, $options) { if (!$js_href) { @@ -33,55 +30,6 @@ function get_popup($href, $js_href, $message, $title, $wnd_name, $options) . "this.newWindow.focus();this.newWindow.opener=window;return false;\">$message"; } -/** - * Load additional CSS files, required by plugins, and build HTML code to - * include them - * - * Triggers 'pageAddCSS' and pass listeners associative array with - * following keys: - * - 'request': {@link \Symfony\Component\HttpFoundation\Request}, a request - * instance. CSS files will be attached to the requested page. - * - 'css': array, with CSS files paths. Modify this array to add or remove - * additional CSS files. - * - * @param Request $request A Request instance. - * @return string HTML block of 'link' tags - */ -function get_additional_css(Request $request) -{ - // Prepare event arguments array - $args = array( - 'request' => $request, - 'css' => array(), - ); - - // Trigger event - $dispatcher = EventDispatcher::getInstance(); - $dispatcher->triggerEvent('pageAddCSS', $args); - - // Build resulting css list - $result = array(); - foreach ($args['css'] as $css) { - $result[] = ''; - } - - return implode("\n", $result); -} - -/** - * Get additional plugins data for specified page - * - * @param Request $request A Request instance. - * @return array Associative array of plugins data. It contains following keys: - * - 'additional_css': contains results of the 'get_additional_css function - */ -function get_plugins_data(Request $request) -{ - return array( - 'additional_css' => get_additional_css($request), - ); -} - function no_field($key) { return getlocal('Please fill "{0}".', array(getlocal($key))); 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 97fd415d..fd7a7323 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 @@ -4,7 +4,7 @@ - {{{additional_css}}} + {{additionalCss}} diff --git a/src/mibew/styles/pages/default/templates_src/server_side/users.handlebars b/src/mibew/styles/pages/default/templates_src/server_side/users.handlebars index 8988a911..cc668007 100644 --- a/src/mibew/styles/pages/default/templates_src/server_side/users.handlebars +++ b/src/mibew/styles/pages/default/templates_src/server_side/users.handlebars @@ -5,7 +5,7 @@ {{#override "head"}} - {{{additional_css}}} + {{additionalCss}}