aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/MessageBox.class.php
blob: 450851f41daa4e95ad8ac0412028483b34b7ff58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
# Lifter010: TODO
/**
 * MessageBox.class.php
 *
 * html boxes for different kinds of messages
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * @author      Michael Riehemann <michael.riehemann@uni-oldenburg.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL Licence 2
 * @category    Stud.IP
 * @package     layout
 * @since       1.10
 *
 */

/**
 * class MessageBox
 *
 * usage:
 *
 * echo MessageBox::error('Nachricht', ['optional details']);
 *
 * use the optional parameter $close_details for displaying the message box with
 * closed details
 *
 * echo MessageBox::success('Nachricht', ['optional details'], true);
 *
 */
class MessageBox implements LayoutMessage
{
    /**
     * type and contents of the message box
     */
    public $class;
    public $message;
    public $details;
    public $close_details;
    protected $hide_close = false;

    /**
     * This function returns an exception message box. Use it only for system errors
     * or security related problems.
     *
     * @param string $message
     * @param array $details
     * @param boolean $close_details
     * @return object MessageBox object
     */
    public static function exception($message, $details = [], $close_details = false)
    {
        return new MessageBox('exception', $message, $details, $close_details);
    }

    /**
     * This function returns an error message box. Use it for validation errors,
     * problems and other wrong user input.
     *
     * @param string $message
     * @param array $details (optional)
     * @param boolean $close_details (optional)
     * @return object MessageBox object
     */
    public static function error($message, $details = [], $close_details = false)
    {
        return new MessageBox('error', $message, $details, $close_details);
    }

    /**
     * This function returns a success message box. Use it for confirmation of user
     * interaction.
     *
     * @param string $message
     * @param array $details (optional)
     * @param boolean $close_details (optional)
     * @return object MessageBox object
     */
    public static function success($message, $details = [], $close_details = false)
    {
        return new MessageBox('success', $message, $details, $close_details);
    }

    /**
     * This function returns an info message box. Use it to report neutral
     * informations.
     *
     * @param string $message
     * @param array $details (optional)
     * @param boolean $close_details (optional)
     * @return object MessageBox object
     */
    public static function info($message, $details = [], $close_details = false)
    {
        return new MessageBox('info', $message, $details, $close_details);
    }

    /**
     * This function returns a warning message box. Use it to report potentially
     * wrong behaviour.
     *
     * @param string $message
     * @param array $details (optional)
     * @param boolean $close_details (optional)
     * @return object MessageBox object
     */
    public static function warning($message, $details = [], $close_details = false)
    {
        return new MessageBox('warning', $message, $details, $close_details);
    }

    /**
     * Initializes a new MessageBox object of the given class.
     *
     * @param string $class the type of this message
     * @param string $message
     * @param array $details (optional)
     * @param boolean $close_details (optional)
     */
    protected function __construct($class, $message, $details = [], $close_details = false)
    {
        $this->class         = $class;
        $this->message       = $message;
        $this->details       = $details;
        $this->close_details = $close_details;
    }

    /**
     * Sets the state whether the close button should be hidden or not.
     *
     * @param  boolean $state Whether the close button should be hidden or not
     * @return MessageBox instance to allow chaining
     */
    public function hideClose($state = true)
    {
        $this->hide_close = (bool) $state;
        return $this;
    }

    /**
     * This method renders a MessageBox object to a string.
     *
     * @return string   html output of the message box
     */
    public function __toString()
    {
        return $GLOBALS['template_factory']->render('shared/message_box', [
            'class'         => $this->class,
            'message'       => $this->message,
            'details'       => is_array($this->details) ? $this->details : [],
            'close_details' => $this->close_details,
            'hide_close'    => $this->hide_close,
        ]);
    }
}