Completed
Push — master ( 131e79...afdc39 )
by Andreas
34:52 queued 12:29
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 2
CRAP Score 2.1481

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 2
cts 3
cp 0.6667
crap 2.1481
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
    /**
56
     * The current message stack
57
     *
58
     * @var Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface
59
     */
60
    private $_message_stack;
61
62
    /**
63
     * List of allowed message types
64
     *
65
     * @var Array
66
     */
67
    private $_allowed_types = ['info', 'ok', 'warning', 'error', 'debug'];
68
69
    /**
70
     * DOM path of the UI message holder object
71
     *
72
     * @var String
73
     */
74
    public $uimessage_holder = 'body';
75
76 27
    private function get_message_stack() : FlashBagInterface
77
    {
78 27
        if (!$this->_message_stack) {
79 1
            $this->_message_stack = midcom::get()->session->getFlashBag();
80
        }
81 27
        return $this->_message_stack;
82
    }
83
84
    /**
85
     * Initialize the message stack on service start-up. Reads older unshown
86
     * messages from user session.
87
     */
88 1
    public function initialize(Request $request)
89
    {
90 1
        if ($request->hasPreviousSession()) {
91
            $this->get_message_stack();
92
        }
93 1
    }
94
95 15
    public function add_head_elements()
96
    {
97 15
        midcom::get()->head->enable_jquery();
98 15
        midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.services.uimessages/jquery.midcom_services_uimessages.js');
99 15
        midcom::get()->head->add_stylesheet(MIDCOM_STATIC_URL . '/stock-icons/font-awesome-4.7.0/css/font-awesome.min.css');
100 15
        midcom::get()->head->prepend_stylesheet(MIDCOM_STATIC_URL . '/midcom.services.uimessages/growl.css', 'screen');
101 15
    }
102
103 1
    public function get_class_magic_default_privileges()
104
    {
105
        return [
106 1
            'EVERYONE' => [],
107
            'ANONYMOUS' => [],
108
            'USERS' => []
109
        ];
110
    }
111
112
    /**
113
     * Add a message to be shown to the user.
114
     *
115
     * @param string $title Message title
116
     * @param string $message Message contents, may contain HTML
117
     * @param string $type Type of the message
118
     */
119 27
    public function add($title, $message, $type = 'info') : bool
120
    {
121
        // Make sure the given class is allowed
122 27
        if (!in_array($type, $this->_allowed_types)) {
123
            // Message class not in allowed list
124
            debug_add("Message type {$type} is not allowed");
125
            return false;
126
        }
127
128
        $msg = [
129 27
            'title'   => $title,
130 27
            'message' => $message,
131 27
            'type'    => $type,
132
        ];
133
        // Append to message stack
134 27
        $this->get_message_stack()->add($type, json_encode($msg));
135 27
        return true;
136
    }
137
138 28
    public function get_messages() : array
139
    {
140 28
        if ($this->_message_stack) {
141 28
            if ($messages = $this->_message_stack->all()) {
142 5
                return call_user_func_array('array_merge', $messages);
143
            }
144
        }
145 24
        return [];
146
    }
147
148
    /**
149
     * Show the message stack via javascript calls or simple html
150
     *
151
     * @param boolean $show_simple Show simple HTML
152
     */
153 18
    public function show($show_simple = false)
154
    {
155 18
        if (!$this->_message_stack) {
156
            return;
157
        }
158 18
        if (   $show_simple
159 18
            || !midcom::get()->auth->can_user_do('midcom:ajax', null, static::class)) {
160 3
            $this->show_simple();
161 3
            return;
162
        }
163 15
        if ($this->_message_stack && !empty($this->_message_stack->peekAll())) {
164 1
            $this->add_head_elements();
165
        }
166
167 15
        echo "<script type=\"text/javascript\">\n";
168 15
        echo "    // <!--\n";
169 15
        echo "        jQuery(document).ready(function()\n";
170 15
        echo "        {\n";
171 15
        echo "            if (jQuery('#midcom_services_uimessages_wrapper').length == 0)\n";
172 15
        echo "            {\n";
173 15
        echo "                jQuery('<div id=\"midcom_services_uimessages_wrapper\" class=\"uimessages-fancy\"></div>')\n";
174 15
        echo "                    .appendTo('{$this->uimessage_holder}');\n";
175 15
        echo "            }\n";
176
177 15
        foreach ($this->get_messages() as $message) {
178 1
            echo "            jQuery('#midcom_services_uimessages_wrapper').midcom_services_uimessage(" . $message . ")\n";
179
        }
180
181 15
        echo "        })\n";
182 15
        echo "    // -->\n";
183
184 15
        echo "</script>\n";
185 15
    }
186
187
    /**
188
     * Show the message stack via simple html only
189
     */
190 3
    public function show_simple()
191
    {
192 3
        if ($this->_message_stack && !empty($this->_message_stack->peekAll())) {
193 1
            midcom::get()->head->prepend_stylesheet(MIDCOM_STATIC_URL . '/midcom.services.uimessages/simple.css', 'screen');
194
195 1
            echo "<div id=\"midcom_services_uimessages_wrapper\">\n";
196 1
            foreach ($this->get_messages() as $message) {
197 1
                $this->_render_message($message);
198
            }
199 1
            echo "</div>\n";
200
        }
201 3
    }
202
203
    /**
204
     * Render the message
205
     */
206 1
    private function _render_message(string $message)
207
    {
208 1
        $message = json_decode($message, true);
209 1
        echo "<div class=\"midcom_services_uimessages_message msu_{$message['type']}\">";
210
211 1
        echo "    <div class=\"midcom_services_uimessages_message_type\">{$message['type']}</div>";
212 1
        echo "    <div class=\"midcom_services_uimessages_message_title\">{$message['title']}</div>";
213 1
        echo "    <div class=\"midcom_services_uimessages_message_msg\">{$message['message']}</div>";
214
215 1
        echo "</div>\n";
216 1
    }
217
}
218