From 051179ce509d53007a03d6d0b0e35afff3100529 Mon Sep 17 00:00:00 2001 From: Aspectis <tobias@aspectis.net> Date: Thu, 7 Jan 2021 16:03:22 +0100 Subject: [PATCH] refs #18868 [fid_bbi] add icons to alerts --- .../View/Helper/Root/Flashmessages.php | 113 ++++++++++++++++++ themes/fid_bbi/theme.config.php | 3 + 2 files changed, 116 insertions(+) create mode 100644 module/fid_bbi/src/fid_bbi/View/Helper/Root/Flashmessages.php diff --git a/module/fid_bbi/src/fid_bbi/View/Helper/Root/Flashmessages.php b/module/fid_bbi/src/fid_bbi/View/Helper/Root/Flashmessages.php new file mode 100644 index 00000000000..295d26a3b25 --- /dev/null +++ b/module/fid_bbi/src/fid_bbi/View/Helper/Root/Flashmessages.php @@ -0,0 +1,113 @@ +<?php +/** + * Flash message view helper + * + * PHP version 7 + * + * Copyright (C) Leipzig University Library 2020. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * @category VuFind + * @package View_Helpers + * @author Heike Reinken <reinken@ub.uni-leipzig.de> + * @author Tobias Schäfer <ts@aspectis.net> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org/wiki/development Wiki + */ +namespace fid_bbi\View\Helper\Root; + +/** + * Flash message view helper + * + * @category VuFind + * @package View_Helpers + * @author Heike Reinken <reinken@ub.uni-leipzig.de> + * @author Tobias Schäfer <ts@aspectis.net> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org/wiki/development Wiki + */ +class Flashmessages extends \VuFind\View\Helper\Bootstrap3\Flashmessages +{ + /** + * Generate flash message <p>'s with appropriate classes based on message type. + * This is basically a copy of the finc method but adds BBI-specific icons + * + * @return string $html + */ + public function __invoke() + { + $html = ''; + $namespaces = ['error', 'info', 'success']; + $icons = [ + 'error' => 'error', + 'info' => 'info', + 'success' => 'checkmark', + ]; + + foreach ($namespaces as $ns) { + $messages = array_merge( + $this->fm->getMessages($ns), + $this->fm->getCurrentMessages($ns) + ); + + foreach (array_unique($messages, SORT_REGULAR) as $msg) { + $html .= '<p class="' . $this->getClassForNamespace($ns) . '"'; + if (isset($msg['dataset'])) { + foreach ($msg['dataset'] as $attr => $value) { + $html .= ' data-' . $attr . '="' . htmlspecialchars($value) . '"'; + } + } + $html .= '>'; + + // BBI-specific + // Add icon based on namespace + $iconHelper = $this->getView()->plugin('icon'); + $html .= $iconHelper("small/{$icons[$ns]}"); + + // Advanced form + if (is_array($msg)) { + // Use a different translate helper depending on whether or not we're in HTML mode. + if (!isset($msg['translate']) || $msg['translate']) { + $helper = (isset($msg['html']) && $msg['html']) + ? 'translate' + : 'transEsc'; + } else { + $helper = (isset($msg['html']) && $msg['html']) + ? false + : 'escapeHtml'; + } + $helper = $helper + ? $this->getView()->plugin($helper) + : false; + $tokens = $msg['tokens'] ?? []; + $default = $msg['default'] ?? null; + $html .= $helper + ? $helper($msg['msg'], $tokens, $default) + : $msg['msg']; + } else { + // Basic default string + $transEsc = $this->getView()->plugin('transEsc'); + $html .= $transEsc($msg); + } + + $html .= '</p>'; + } + + $this->fm->clearMessages($ns); + $this->fm->clearCurrentMessages($ns); + } + return $html; + } +} diff --git a/themes/fid_bbi/theme.config.php b/themes/fid_bbi/theme.config.php index c809e8ff2cc..c16b0eef8ec 100644 --- a/themes/fid_bbi/theme.config.php +++ b/themes/fid_bbi/theme.config.php @@ -12,10 +12,13 @@ return [ ], 'helpers' => array( 'aliases' => [ + 'flashmessages' => 'fid_bbi\View\Helper\Root\Flashmessages', 'icon' => 'fid_bbi\View\Helper\Root\Icon', 'record' => 'fid_bbi\View\Helper\Root\Record', ], 'factories' => [ + 'fid_bbi\View\Helper\Root\Flashmessages' => + 'VuFind\View\Helper\Root\FlashmessagesFactory', 'fid_bbi\View\Helper\Root\Icon' => 'fid_bbi\View\Helper\Root\Icon', 'fid_bbi\View\Helper\Root\Record' => -- GitLab