Passed
Push — master ( ae0137...9d50f2 )
by Andreas
37:09
created

get_class_magic_default_privileges()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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