Passed
Push — master ( 3420f6...8740f6 )
by Andreas
10:59
created

midcom_services_uimessages::initialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package midcom.services
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
11
12
/**
13
 * User interface messaging service
14
 *
15
 * This service is used for passing messages from applications to the MidCOM
16
 * user.
17
 *
18
 * <b>Displaying UI messages on site:</b>
19
 *
20
 * If you want the UI messages to be shown in your site, you must place
21
 * the following call inside the HTML BODY tags of your style:
22
 *
23
 * <code>
24
 * midcom::get()->uimessages->show();
25
 * </code>
26
 *
27
 * <b>Adding UI messages to show:</b>
28
 *
29
 * Any MidCOM component can add its own UI messages to be displayed. The
30
 * messages also carry across a relocate() call so you can tell a document
31
 * has been saved before relocating user into its view.
32
 *
33
 * UI messages can be specified into the following types: <i>info</i>,
34
 * <i>ok</i>, <i>warning</i> and <i>error</i>.
35
 *
36
 * To add a UI message, call the following:
37
 *
38
 * <code>
39
 * midcom::get()->uimessages->add($title, $message, $type);
40
 * </code>
41
 *
42
 * For example:
43
 *
44
 * <code>
45
 * midcom::get()->uimessages->add($this->_l10n->get('net.nemein.wiki'), sprintf($this->_l10n->get('page "%s" added'), $this->_wikiword), 'ok');
46
 * </code>
47
 *
48
 * <b>Configuration:</b>
49
 *
50
 * @see midcom_config for configuration options.
51
 * @package midcom.services
52
 */
53
class midcom_services_uimessages
54
{
55
    private ?FlashBagInterface $_message_stack = null;
56
57
    private array $_allowed_types = ['info', 'ok', 'warning', 'error', 'debug'];
58
59
    /**
60
     * DOM path of the UI message holder object
61
     */
62
    public string $uimessage_holder = 'body';
63
64 27
    private function get_message_stack() : FlashBagInterface
65
    {
66 27
        return $this->_message_stack ??= midcom::get()->session->getFlashBag();
67
    }
68
69
    /**
70
     * Initialize the message stack on service start-up. Reads older unshown
71
     * messages from user session.
72
     */
73
    public function initialize(Request $request)
74
    {
75
        if ($request->hasPreviousSession()) {
76
            $this->get_message_stack();
77
        }
78
    }
79
80 14
    public function add_head_elements()
81
    {
82 14
        midcom::get()->head->enable_jquery();
83 14
        midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.services.uimessages/jquery.midcom_services_uimessages.js');
84 14
        midcom::get()->head->add_stylesheet(MIDCOM_STATIC_URL . '/stock-icons/font-awesome-4.7.0/css/font-awesome.min.css');
85 14
        midcom::get()->head->prepend_stylesheet(MIDCOM_STATIC_URL . '/midcom.services.uimessages/growl.css', 'screen');
86
    }
87
88 1
    public function get_class_magic_default_privileges() : array
89
    {
90 1
        return [
91 1
            'EVERYONE' => [],
92 1
            'ANONYMOUS' => [],
93 1
            'USERS' => []
94 1
        ];
95
    }
96
97
    /**
98
     * Add a message to be shown to the user.
99
     *
100
     * @param string $message Message contents, may contain HTML
101
     */
102 27
    public function add(string $title, string $message, string $type = 'info') : bool
103
    {
104
        // Make sure the given class is allowed
105 27
        if (!in_array($type, $this->_allowed_types)) {
106
            // Message class not in allowed list
107
            debug_add("Message type {$type} is not allowed");
108
            return false;
109
        }
110
111 27
        $msg = [
112 27
            'title'   => $title,
113 27
            'message' => $message,
114 27
            'type'    => $type,
115 27
        ];
116
        // Append to message stack
117 27
        $this->get_message_stack()->add($type, json_encode($msg));
118 27
        return true;
119
    }
120
121 26
    public function get_messages() : array
122
    {
123 26
        if ($this->_message_stack) {
124 26
            if ($messages = $this->_message_stack->all()) {
125 5
                return array_merge(...array_values($messages));
126
            }
127
        }
128 22
        return [];
129
    }
130
131
    /**
132
     * Show the message stack via javascript calls or simple html
133
     */
134 16
    public function show(bool $show_simple = false)
135
    {
136 16
        if (   $show_simple
137 16
            || !midcom::get()->auth->can_user_do('midcom:ajax', null, static::class)) {
138 3
            $this->show_simple();
139 3
            return;
140
        }
141 13
        if ($this->has_messages()) {
142
            $this->add_head_elements();
143
        }
144
145 13
        echo "<script type=\"text/javascript\">\n";
146 13
        echo "    // <!--\n";
147 13
        echo "        jQuery(document).ready(function()\n";
148 13
        echo "        {\n";
149 13
        echo "            if (jQuery('#midcom_services_uimessages_wrapper').length == 0)\n";
150 13
        echo "            {\n";
151 13
        echo "                jQuery('<div id=\"midcom_services_uimessages_wrapper\" class=\"uimessages-fancy\"></div>')\n";
152 13
        echo "                    .appendTo('{$this->uimessage_holder}');\n";
153 13
        echo "            }\n";
154
155 13
        foreach ($this->get_messages() as $message) {
156
            echo "            jQuery('#midcom_services_uimessages_wrapper').midcom_services_uimessage(" . $message . ")\n";
157
        }
158
159 13
        echo "        })\n";
160 13
        echo "    // -->\n";
161
162 13
        echo "</script>\n";
163
    }
164
165
    /**
166
     * Show the message stack via simple html only
167
     */
168 3
    public function show_simple()
169
    {
170 3
        if ($this->has_messages()) {
171 1
            midcom::get()->head->prepend_stylesheet(MIDCOM_STATIC_URL . '/midcom.services.uimessages/simple.css', 'screen');
172
173 1
            echo "<div id=\"midcom_services_uimessages_wrapper\">\n";
174 1
            foreach ($this->get_messages() as $message) {
175 1
                $this->_render_message($message);
176
            }
177 1
            echo "</div>\n";
178
        }
179
    }
180
181 16
    private function has_messages() : bool
182
    {
183 16
        return $this->_message_stack && !empty($this->_message_stack->peekAll());
184
    }
185
186
    /**
187
     * Render the message
188
     */
189 1
    private function _render_message(string $message)
190
    {
191 1
        $message = json_decode($message, true);
192 1
        echo "<div class=\"midcom_services_uimessages_message msu_{$message['type']}\">";
193
194 1
        echo "    <div class=\"midcom_services_uimessages_message_type\">{$message['type']}</div>";
195 1
        echo "    <div class=\"midcom_services_uimessages_message_title\">{$message['title']}</div>";
196 1
        echo "    <div class=\"midcom_services_uimessages_message_msg\">{$message['message']}</div>";
197
198 1
        echo "</div>\n";
199
    }
200
}
201