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