Completed
Push — master ( a30dbc...5978b6 )
by Andreas
17:30
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 18
    public function add_head_elements()
98
    {
99 18
        midcom::get()->head->enable_jquery();
100 18
        midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.services.uimessages/jquery.midcom_services_uimessages.js');
101 18
        midcom::get()->head->add_jsfile(MIDCOM_JQUERY_UI_URL . '/effect.min.js');
102 18
        midcom::get()->head->add_jsfile(MIDCOM_JQUERY_UI_URL . '/effects/effect-pulsate.min.js');
103 18
        midcom::get()->head->add_stylesheet(MIDCOM_STATIC_URL . '/stock-icons/font-awesome-4.7.0/css/font-awesome.min.css');
104 18
        midcom::get()->head->add_stylesheet(MIDCOM_STATIC_URL . '/midcom.services.uimessages/growl.css', 'screen');
105 18
    }
106
107 1
    public function get_class_magic_default_privileges()
108
    {
109
        return [
110 1
            'EVERYONE' => [],
111
            'ANONYMOUS' => [],
112
            'USERS' => []
113
        ];
114
    }
115
116
    /**
117
     * Add a message to be shown to the user.
118
     *
119
     * @param string $title Message title
120
     * @param string $message Message contents, may contain HTML
121
     * @param string $type Type of the message
122
     */
123 27
    public function add($title, $message, $type = 'info')
124
    {
125
        // Make sure the given class is allowed
126 27
        if (!in_array($type, $this->_allowed_types)) {
127
            // Message class not in allowed list
128
            debug_add("Message type {$type} is not allowed");
129
            return false;
130
        }
131
132
        $msg = [
133 27
            'title'   => $title,
134 27
            'message' => $message,
135 27
            'type'    => $type,
136
        ];
137
        // Append to message stack
138 27
        $this->get_message_stack()->add($type, json_encode($msg));
139 27
        return true;
140
    }
141
142 28
    public function get_messages()
143
    {
144 28
        $result = [];
145 28
        if ($this->_message_stack) {
146 28
            foreach ($this->_message_stack->all() as $messages) {
147 5
                foreach ($messages as $message) {
148 5
                    $result[] = $message;
149
                }
150
            }
151
        }
152 28
        return $result;
153
    }
154
155
    /**
156
     * Show the message stack via javascript calls or simple html
157
     *
158
     * @param boolean $show_simple Show simple HTML
159
     */
160 19
    public function show($show_simple = false)
161
    {
162 19
        if (!$this->_message_stack) {
163
            return;
164
        }
165 19
        if (   $show_simple
166 19
            || !midcom::get()->auth->can_user_do('midcom:ajax', null, static::class)) {
167 3
            $this->show_simple();
168 3
            return;
169
        }
170 16
        if ($this->_message_stack && !empty($this->_message_stack->peekAll())) {
171 1
            $this->add_head_elements();
172
        }
173
174 16
        echo "<script type=\"text/javascript\">\n";
175 16
        echo "    // <!--\n";
176 16
        echo "        jQuery(document).ready(function()\n";
177 16
        echo "        {\n";
178 16
        echo "            if (jQuery('#midcom_services_uimessages_wrapper').length == 0)\n";
179 16
        echo "            {\n";
180 16
        echo "                jQuery('<div id=\"midcom_services_uimessages_wrapper\" class=\"uimessages-fancy\"></div>')\n";
181 16
        echo "                    .appendTo('{$this->uimessage_holder}');\n";
182 16
        echo "            }\n";
183
184 16
        foreach ($this->get_messages() as $message) {
185 1
            echo "            jQuery('#midcom_services_uimessages_wrapper').midcom_services_uimessage(" . $message . ")\n";
186
        }
187
188 16
        echo "        })\n";
189 16
        echo "    // -->\n";
190
191 16
        echo "</script>\n";
192 16
    }
193
194
    /**
195
     * Show the message stack via simple html only
196
     */
197 3
    public function show_simple()
198
    {
199 3
        if ($this->_message_stack && !empty($this->_message_stack->peekAll())) {
200 1
            midcom::get()->head->add_stylesheet(MIDCOM_STATIC_URL . '/midcom.services.uimessages/simple.css', 'screen');
201
202 1
            echo "<div id=\"midcom_services_uimessages_wrapper\">\n";
203 1
            foreach ($this->get_messages() as $message) {
204 1
                $this->_render_message($message);
205
            }
206 1
            echo "</div>\n";
207
        }
208 3
    }
209
210
    /**
211
     * Render the message
212
     */
213 1
    private function _render_message($message)
214
    {
215 1
        $message = json_decode($message, true);
216 1
        echo "<div class=\"midcom_services_uimessages_message msu_{$message['type']}\">";
217
218 1
        echo "    <div class=\"midcom_services_uimessages_message_type\">{$message['type']}</div>";
219 1
        echo "    <div class=\"midcom_services_uimessages_message_title\">{$message['title']}</div>";
220 1
        echo "    <div class=\"midcom_services_uimessages_message_msg\">{$message['message']}</div>";
221
222 1
        echo "</div>\n";
223 1
    }
224
}
225