1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class Flash |
5
|
|
|
* @method static void info(string $message, bool $closable = null, bool $fadeOut = null) |
6
|
|
|
* @method static void success(string $message, bool $closable = null, bool $fadeOut = null) |
7
|
|
|
* @method static void warning(string $message, bool $closable = null, bool $fadeOut = null) |
8
|
|
|
* @method static void danger(string $message, bool $closable = null, bool $fadeOut = null) |
9
|
|
|
* @method static void alert(string $message, bool $closable = null, bool $fadeOut = null) |
10
|
|
|
* @method static void modal(string $message, bool $closable = null, bool $fadeOut = null) |
11
|
|
|
*/ |
12
|
|
|
class Flash extends ViewableData implements TemplateGlobalProvider |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @config |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
private static $defaults = [ |
|
|
|
|
19
|
|
|
'Type' => 'success', |
20
|
|
|
'IsModal' => false, |
21
|
|
|
'Closable' => true, |
22
|
|
|
'FadeOut' => false |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @config |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private static $supported_methods = [ |
|
|
|
|
30
|
|
|
'info', |
31
|
|
|
'success', |
32
|
|
|
'warning', |
33
|
|
|
'danger', |
34
|
|
|
'alert', |
35
|
|
|
'modal' |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @config |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private static $template = 'FlashMessage'; |
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @config |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private static $session_name = 'FlashMessage'; |
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @config |
52
|
|
|
* @var bool |
53
|
|
|
*/ |
54
|
|
|
private static $load_javascript = true; |
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* The Flash Message data |
58
|
|
|
* |
59
|
|
|
* @var array |
60
|
|
|
*/ |
61
|
|
|
protected $data = []; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param array $data |
65
|
|
|
*/ |
66
|
|
|
public function __construct($data) |
67
|
|
|
{ |
68
|
|
|
$this->data = (array)$data + (array)self::config()->defaults; |
69
|
|
|
|
70
|
|
|
parent::__construct(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
public static function get_template_global_variables() |
77
|
|
|
{ |
78
|
|
|
return [ |
79
|
|
|
'FlashMessage' |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return Flash |
85
|
|
|
*/ |
86
|
|
|
public static function FlashMessage() |
87
|
|
|
{ |
88
|
|
|
return Flash::get(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param $message |
93
|
|
|
* @param string $type |
94
|
|
|
* @param null $closable |
95
|
|
|
* @param null $fadeOut |
96
|
|
|
*/ |
97
|
|
|
public static function set($message, $type = 'success', $closable = null, $fadeOut = null) |
98
|
|
|
{ |
99
|
|
|
$data = [ |
100
|
|
|
'Message' => $message, |
101
|
|
|
'Type' => $type |
102
|
|
|
]; |
103
|
|
|
|
104
|
|
|
if (null !== $closable) { |
105
|
|
|
$data['Closable'] = $closable; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (null !== $fadeOut) { |
109
|
|
|
$data['FadeOut'] = $fadeOut; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if('modal' === $type) { |
113
|
|
|
$data['IsModal'] = true; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
Session::set(Flash::config()->session_name, $data); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return Flash |
121
|
|
|
*/ |
122
|
|
|
public static function get() |
123
|
|
|
{ |
124
|
|
|
$key = Flash::config()->session_name; |
125
|
|
|
$data = Session::get($key); |
126
|
|
|
Session::clear($key); |
127
|
|
|
|
128
|
|
|
return (new Flash($data)); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return string |
|
|
|
|
133
|
|
|
*/ |
134
|
|
|
public function forTemplate() |
135
|
|
|
{ |
136
|
|
|
if (self::config()->load_javascript) { |
137
|
|
|
Requirements::javascript('flashmessage/javascript/flashmessage.js'); |
138
|
|
|
} |
139
|
|
|
return $this->customise($this->data)->renderWith(self::config()->template); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param $method |
144
|
|
|
* @param $args |
145
|
|
|
* @throws BadMethodCallException |
146
|
|
|
*/ |
147
|
|
|
public static function __callStatic($method, $args) |
148
|
|
|
{ |
149
|
|
|
if (in_array($method, self::config()->supported_methods)) { |
150
|
|
|
self::set($args[0], $method, isset($args[1]) ? $args[1] : null, isset($args[2]) ? $args[2] : null); |
151
|
|
|
} else { |
152
|
|
|
throw new BadMethodCallException("Method '$method' does not exist on " . __CLASS__); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.