Completed
Push — master ( 55311e...acc5b2 )
by Craig
06:34
created

SessionExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zikula\Bundle\CoreBundle\Twig\Extension;
13
14
use Symfony\Component\HttpFoundation\Session\SessionInterface;
15
use Zikula\Bundle\CoreBundle\Twig;
16
17
class SessionExtension extends \Twig_Extension
18
{
19
    /**
20
     * @var SessionInterface
21
     */
22
    private $session;
23
24
    /**
25
     * CoreExtension constructor.
26
     * @param SessionInterface $session
27
     */
28
    public function __construct(SessionInterface $session)
29
    {
30
        $this->session = $session;
31
    }
32
33
    /**
34
     * Returns a list of functions to add to the existing list.
35
     *
36
     * @return array An array of functions
37
     */
38
    public function getFunctions()
39
    {
40
        return [
41
            new \Twig_SimpleFunction('showflashes', [$this, 'showFlashes'], ['is_safe' => ['html']]),
42
        ];
43
    }
44
45
    /**
46
     * Display flash messages in twig template. Defaults to bootstrap alert classes.
47
     *
48
     * <pre>
49
     *  {{ showflashes() }}
50
     *  {{ showflashes({'class': 'custom-class', 'tag': 'span'}) }}
51
     * </pre>
52
     *
53
     * @param array $params
54
     * @return string
55
     */
56
    public function showFlashes(array $params = [])
57
    {
58
        $result = '';
59
        $total_messages = [];
60
        $messageTypeMap = [
61
            'error' => 'danger',
62
            'warning' => 'warning',
63
            'status' => 'success',
64
            'danger' => 'danger',
65
            'success' => 'success',
66
            'info' => 'info'
67
        ];
68
69
        foreach ($messageTypeMap as $messageType => $bootstrapClass) {
70
            $messages = $this->session->getFlashBag()->get($messageType);
71
            if (count($messages) > 0) {
72
                // Set class for the messages.
73
                $class = (!empty($params['class'])) ? $params['class'] : "alert alert-$bootstrapClass";
74
                $total_messages = $total_messages + $messages;
75
                // Build output of the messages.
76
                if (empty($params['tag']) || ($params['tag'] != 'span')) {
77
                    $params['tag'] = 'div';
78
                }
79
                $result .= '<' . $params['tag'] . ' class="' . $class . '"';
80
                if (!empty($params['style'])) {
81
                    $result .= ' style="' . $params['style'] . '"';
82
                }
83
                $result .= '>';
84
                $result .= implode('<hr />', $messages);
85
                $result .= '</' . $params['tag'] . '>';
86
            }
87
        }
88
89
        if (empty($total_messages)) {
90
            return "";
91
        }
92
93
        return $result;
94
    }
95
}
96