Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.91% covered (success)
90.91%
40 / 44
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Flashmessages
90.91% covered (success)
90.91%
40 / 44
66.67% covered (warning)
66.67%
2 / 3
15.17
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getClassForNamespace
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __invoke
90.48% covered (success)
90.48%
38 / 42
0.00% covered (danger)
0.00%
0 / 1
13.15
1<?php
2
3/**
4 * Flash message view helper
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  View_Helpers
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development Wiki
28 */
29
30namespace VuFind\View\Helper\Root;
31
32use Laminas\Mvc\Plugin\FlashMessenger\FlashMessenger;
33use Laminas\View\Helper\AbstractHelper;
34
35use function is_array;
36
37/**
38 * Flash message view helper
39 *
40 * @category VuFind
41 * @package  View_Helpers
42 * @author   Demian Katz <demian.katz@villanova.edu>
43 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
44 * @link     https://vufind.org/wiki/development Wiki
45 */
46class Flashmessages extends AbstractHelper
47{
48    /**
49     * Flash messenger controller helper
50     *
51     * @var FlashMessenger
52     */
53    protected $fm;
54
55    /**
56     * Flash messenger namespaces
57     *
58     * @var string[]
59     */
60    protected $namespaces = ['error', 'warning', 'info', 'success', 'default'];
61
62    /**
63     * Constructor
64     *
65     * @param FlashMessenger $fm Flash messenger controller helper
66     */
67    public function __construct(FlashMessenger $fm)
68    {
69        $this->fm = $fm;
70    }
71
72    /**
73     * Get the CSS class to correspond with a messenger namespace
74     *
75     * @param string $ns Namespace
76     *
77     * @return string
78     */
79    protected function getClassForNamespace($ns)
80    {
81        return $ns;
82    }
83
84    /**
85     * Generate flash message <div>'s with appropriate classes based on message type.
86     *
87     * @return string $html
88     */
89    public function __invoke()
90    {
91        if (!empty($this->getView()->layout()->lightboxChild)) {
92            return '';
93        }
94        $html = '';
95        foreach ($this->namespaces as $ns) {
96            $messages = array_merge(
97                $this->fm->getMessages($ns),
98                $this->fm->getCurrentMessages($ns)
99            );
100            foreach (array_unique($messages, SORT_REGULAR) as $msg) {
101                $html .= '<div role="alert" class="'
102                    . $this->getClassForNamespace($ns) . '"';
103                if (isset($msg['dataset'])) {
104                    foreach ($msg['dataset'] as $attr => $value) {
105                        $html .= ' data-' . $attr . '="'
106                            . htmlspecialchars($value) . '"';
107                    }
108                }
109                $html .= '>';
110                // Advanced form:
111                if (is_array($msg)) {
112                    $msgHtml = $msg['html'] ?? false;
113                    $message = $msg['msg'];
114                    $escapeHtml = $this->getView()->plugin('escapeHtml');
115                    // Process tokens and translate the message unless requested not
116                    // to:
117                    if ($msg['translate'] ?? true) {
118                        $translate = $this->getView()->plugin('translate');
119                        $tokens = $msg['tokens'] ?? [];
120                        if ($tokens) {
121                            if ($msg['translateTokens'] ?? false) {
122                                $tokens = array_map(
123                                    $translate,
124                                    $tokens
125                                );
126                            }
127                            // Escape tokens if the main message is HTML, unless
128                            // requested not to by setting tokensHtml to true:
129                            if ($msgHtml && !($msg['tokensHtml'] ?? false)) {
130                                $tokens = array_map($escapeHtml, $tokens);
131                            }
132                        }
133                        $default = $msg['default'] ?? null;
134
135                        // Translate the message:
136                        $message = $translate($message, $tokens, $default);
137                    }
138                    // Escape the message unless requested not to:
139                    if (!$msgHtml) {
140                        $message = $escapeHtml($message);
141                    }
142
143                    $html .= $message;
144                } else {
145                    // Basic default string:
146                    $transEsc = $this->getView()->plugin('transEsc');
147                    $html .= $transEsc($msg);
148                }
149                $html .= '</div>';
150            }
151            $this->fm->clearMessages($ns);
152            $this->fm->clearCurrentMessages($ns);
153        }
154        return $html;
155    }
156}