|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Bulma; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Yiisoft\Html\Html; |
|
9
|
|
|
use Yiisoft\Html\Tag\Button; |
|
10
|
|
|
use Yiisoft\Html\Tag\Div; |
|
11
|
|
|
use Yiisoft\Html\Tag\P; |
|
12
|
|
|
use Yiisoft\Html\Tag\Span; |
|
13
|
|
|
use Yiisoft\Widget\Widget; |
|
14
|
|
|
|
|
15
|
|
|
use function implode; |
|
16
|
|
|
use function in_array; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Message renders Bulma message component. |
|
20
|
|
|
* |
|
21
|
|
|
* For example, |
|
22
|
|
|
* |
|
23
|
|
|
* ```php |
|
24
|
|
|
* <?= Message::widget() |
|
25
|
|
|
* ->headerColor('success') |
|
26
|
|
|
* ->header('System info') |
|
27
|
|
|
* ->body('Say hello...') ?> |
|
28
|
|
|
* ``` |
|
29
|
|
|
* |
|
30
|
|
|
* @link https://bulma.io/documentation/components/message/ |
|
31
|
|
|
*/ |
|
32
|
|
|
final class Message extends Widget |
|
33
|
|
|
{ |
|
34
|
|
|
public const SIZE_SMALL = 'is-small'; |
|
35
|
|
|
public const SIZE_MEDIUM = 'is-medium'; |
|
36
|
|
|
public const SIZE_LARGE = 'is-large'; |
|
37
|
|
|
private const SIZE_ALL = [ |
|
38
|
|
|
self::SIZE_SMALL, |
|
39
|
|
|
self::SIZE_MEDIUM, |
|
40
|
|
|
self::SIZE_LARGE, |
|
41
|
|
|
]; |
|
42
|
|
|
public const COLOR_PRIMARY = 'is-primary'; |
|
43
|
|
|
public const COLOR_LINK = 'is-link'; |
|
44
|
|
|
public const COLOR_INFO = 'is-info'; |
|
45
|
|
|
public const COLOR_SUCCESS = 'is-success'; |
|
46
|
|
|
public const COLOR_WARNING = 'is-warning'; |
|
47
|
|
|
public const COLOR_DANGER = 'is-danger'; |
|
48
|
|
|
public const COLOR_DARK = 'is-dark'; |
|
49
|
|
|
private const COLOR_ALL = [ |
|
50
|
|
|
self::COLOR_PRIMARY, |
|
51
|
|
|
self::COLOR_LINK, |
|
52
|
|
|
self::COLOR_INFO, |
|
53
|
|
|
self::COLOR_SUCCESS, |
|
54
|
|
|
self::COLOR_WARNING, |
|
55
|
|
|
self::COLOR_DANGER, |
|
56
|
|
|
self::COLOR_DARK, |
|
57
|
|
|
]; |
|
58
|
|
|
private array $attributes = []; |
|
59
|
|
|
private string $autoIdPrefix = 'w'; |
|
60
|
|
|
private string $body = ''; |
|
61
|
|
|
private array $bodyAttributes = []; |
|
62
|
|
|
private string $bodyCssClass = 'message-body'; |
|
63
|
|
|
private array $buttonSpanAttributes = []; |
|
64
|
|
|
private string $buttonSpanAriaHidden = 'true'; |
|
65
|
|
|
private string $buttonCssClass = 'delete'; |
|
66
|
|
|
private string $cssClass = 'message'; |
|
67
|
|
|
private bool $closedButton = false; |
|
68
|
|
|
private array $closeButtonAttributes = []; |
|
69
|
|
|
private bool $encode = false; |
|
70
|
|
|
private array $headerAttributes = []; |
|
71
|
|
|
private string $headerColor = self::COLOR_DARK; |
|
72
|
|
|
private string $headerMessage = ''; |
|
73
|
|
|
private string $headerMessageCssClass = 'message-header'; |
|
74
|
|
|
private string $size = ''; |
|
75
|
|
|
private bool $withoutHeader = false; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Returns a new instance with the specified HTML attributes for widget. |
|
79
|
|
|
* |
|
80
|
|
|
* @param array $values Attribute values indexed by attribute names. |
|
81
|
|
|
* |
|
82
|
|
|
* {@see \Yiisoft\Html\Html::renderTagAttributes()} For details on how attributes are being rendered. |
|
83
|
|
|
*/ |
|
84
|
2 |
|
public function attributes(array $values): self |
|
85
|
|
|
{ |
|
86
|
2 |
|
$new = clone $this; |
|
87
|
2 |
|
$new->attributes = $values; |
|
88
|
2 |
|
return $new; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Returns a new instance with the specified prefix to the automatically generated widget IDs. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $value The prefix to the automatically generated widget IDs. |
|
95
|
|
|
*/ |
|
96
|
1 |
|
public function autoIdPrefix(string $value): self |
|
97
|
|
|
{ |
|
98
|
1 |
|
$new = clone $this; |
|
99
|
1 |
|
$new->autoIdPrefix = $value; |
|
100
|
1 |
|
return $new; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Returns a new instance with the specified the body content. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $value The body content. |
|
107
|
|
|
*/ |
|
108
|
12 |
|
public function body(string $value): self |
|
109
|
|
|
{ |
|
110
|
12 |
|
$new = clone $this; |
|
111
|
12 |
|
$new->body = $value; |
|
112
|
12 |
|
return $new; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Returns a new instance with the specified HTML attributes for the widget body tag. |
|
117
|
|
|
* |
|
118
|
|
|
* @param array $values Attribute values indexed by attribute names. |
|
119
|
|
|
* |
|
120
|
|
|
* {@see Html::renderTagAttributes()} for details on how attributes are being rendered. |
|
121
|
|
|
*/ |
|
122
|
2 |
|
public function bodyAttributes(array $values): self |
|
123
|
|
|
{ |
|
124
|
2 |
|
$new = clone $this; |
|
125
|
2 |
|
$new->bodyAttributes = $values; |
|
126
|
2 |
|
return $new; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Returns a new instance with the specified the CSS class for the body container. |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $value The CSS class for the body container. |
|
133
|
|
|
*/ |
|
134
|
1 |
|
public function bodyCssClass(string $value): self |
|
135
|
|
|
{ |
|
136
|
1 |
|
$new = clone $this; |
|
137
|
1 |
|
$new->bodyCssClass = $value; |
|
138
|
1 |
|
return $new; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Returns a new instance with the specified HTML attributes for the close button tag. |
|
143
|
|
|
* |
|
144
|
|
|
* The close button is displayed in the header of the Message. Clicking on the button will hide the message. |
|
145
|
|
|
* |
|
146
|
|
|
* @param array $values Attribute values indexed by attribute names. |
|
147
|
|
|
* |
|
148
|
|
|
* {@see Html::renderTagAttributes()} For details on how attributes are being rendered. |
|
149
|
|
|
*/ |
|
150
|
2 |
|
public function closeButtonAttributes(array $values): self |
|
151
|
|
|
{ |
|
152
|
2 |
|
$new = clone $this; |
|
153
|
2 |
|
$new->closeButtonAttributes = $values; |
|
154
|
2 |
|
return $new; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Returns a new instance with the specified whether the tags for the message are encoded. |
|
159
|
|
|
* |
|
160
|
|
|
* @param bool $value whether to encode the output. |
|
161
|
|
|
*/ |
|
162
|
1 |
|
public function encode(bool $value): self |
|
163
|
|
|
{ |
|
164
|
1 |
|
$new = clone $this; |
|
165
|
1 |
|
$new->encode = $value; |
|
166
|
1 |
|
return $new; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Returns a new instance with the specified HTML attributes for the widget header tag. |
|
171
|
|
|
* |
|
172
|
|
|
* @param array $values Attribute values indexed by attribute names. |
|
173
|
|
|
* |
|
174
|
|
|
* {@see Html::renderTagAttributes()} For details on how attributes are being rendered. |
|
175
|
|
|
*/ |
|
176
|
2 |
|
public function headerAttributes(array $values): self |
|
177
|
|
|
{ |
|
178
|
2 |
|
$new = clone $this; |
|
179
|
2 |
|
$new->headerAttributes = $values; |
|
180
|
2 |
|
return $new; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Returns a new instance with the specified message header color. |
|
185
|
|
|
* |
|
186
|
|
|
* @param string $value The header color. Default is Message::COLOR_DARK. |
|
187
|
|
|
* Possible values: Message::COLOR_PRIMARY, Message::COLOR_LINK, Message::COLOR_INFO, Message::COLOR_SUCCESS, |
|
188
|
|
|
* Message::COLOR_WARNING, Message::COLOR_DANGER, Message::COLOR_DARK. |
|
189
|
|
|
* |
|
190
|
|
|
* @link https://bulma.io/documentation/components/message/#colors |
|
191
|
|
|
*/ |
|
192
|
3 |
|
public function headerColor(string $value): self |
|
193
|
|
|
{ |
|
194
|
3 |
|
if (!in_array($value, self::COLOR_ALL, true)) { |
|
195
|
1 |
|
$values = implode('", "', self::COLOR_ALL); |
|
196
|
1 |
|
throw new InvalidArgumentException("Invalid color. Valid values are: \"$values\"."); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
2 |
|
$new = clone $this; |
|
200
|
2 |
|
$new->headerColor = $value; |
|
201
|
2 |
|
return $new; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Returns a new instance with the specified the header message. |
|
206
|
|
|
* |
|
207
|
|
|
* @param string $value The header message. |
|
208
|
|
|
*/ |
|
209
|
12 |
|
public function headerMessage(string $value): self |
|
210
|
|
|
{ |
|
211
|
12 |
|
$new = clone $this; |
|
212
|
12 |
|
$new->headerMessage = $value; |
|
213
|
12 |
|
return $new; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Returns a new instance with the specified ID of the widget. |
|
218
|
|
|
* |
|
219
|
|
|
* @param string $value The ID of the widget. |
|
220
|
|
|
*/ |
|
221
|
2 |
|
public function id(string $value): self |
|
222
|
|
|
{ |
|
223
|
2 |
|
$new = clone $this; |
|
224
|
2 |
|
$new->attributes['id'] = $value; |
|
225
|
2 |
|
return $new; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Returns a new instance with the specified size for the widget. |
|
230
|
|
|
* |
|
231
|
|
|
* @param string $value size class. By default, not class is added and the size is considered "normal". |
|
232
|
|
|
* Possible values: Message::SIZE_SMALL, Message::SIZE_MEDIUM, Message::SIZE_LARGE. |
|
233
|
|
|
* |
|
234
|
|
|
* @link https://bulma.io/documentation/components/message/#sizes |
|
235
|
|
|
*/ |
|
236
|
3 |
|
public function size(string $value): self |
|
237
|
|
|
{ |
|
238
|
3 |
|
if (!in_array($value, self::SIZE_ALL, true)) { |
|
239
|
1 |
|
$values = implode('", "', self::SIZE_ALL); |
|
240
|
1 |
|
throw new InvalidArgumentException("Invalid size. Valid values are: \"$values\"."); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
2 |
|
$new = clone $this; |
|
244
|
2 |
|
$new->size = $value; |
|
245
|
2 |
|
return $new; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Returns a new instance with the specified allows you to remove the close button. |
|
250
|
|
|
* |
|
251
|
|
|
* @param bool $value Whether to remove the close button. |
|
252
|
|
|
*/ |
|
253
|
2 |
|
public function withoutCloseButton(bool $value): self |
|
254
|
|
|
{ |
|
255
|
2 |
|
$new = clone $this; |
|
256
|
2 |
|
$new->closedButton = $value; |
|
257
|
2 |
|
return $new; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Returns a new instance with the specified allows you to disable header. |
|
262
|
|
|
* |
|
263
|
|
|
* @param bool $value Whether to disable header. |
|
264
|
|
|
* |
|
265
|
|
|
* @link https://bulma.io/documentation/components/message/#message-body-only |
|
266
|
|
|
*/ |
|
267
|
2 |
|
public function withoutHeader(bool $value): self |
|
268
|
|
|
{ |
|
269
|
2 |
|
$new = clone $this; |
|
270
|
2 |
|
$new->withoutHeader = $value; |
|
271
|
2 |
|
return $new; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
11 |
|
public function render(): string |
|
275
|
|
|
{ |
|
276
|
11 |
|
return $this->renderMessage(); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
11 |
|
private function renderCloseButton(): string |
|
280
|
|
|
{ |
|
281
|
11 |
|
$html = ''; |
|
282
|
|
|
|
|
283
|
11 |
|
$buttonSpanAttributes = $this->buttonSpanAttributes; |
|
284
|
11 |
|
$closeButtonAttributes = $this->closeButtonAttributes; |
|
285
|
|
|
|
|
286
|
11 |
|
if ($this->closedButton === true) { |
|
287
|
1 |
|
return $html; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
10 |
|
$buttonSpanAttributes['aria-hidden'] = $this->buttonSpanAriaHidden; |
|
291
|
10 |
|
$closeButtonAttributes['type'] = 'button'; |
|
292
|
|
|
|
|
293
|
10 |
|
Html::addCssClass($closeButtonAttributes, $this->buttonCssClass); |
|
294
|
10 |
|
unset($closeButtonAttributes['label']); |
|
295
|
|
|
|
|
296
|
10 |
|
$label = Span::tag() |
|
297
|
10 |
|
->attributes($buttonSpanAttributes) |
|
298
|
10 |
|
->content('×') |
|
299
|
10 |
|
->encode(false) |
|
300
|
10 |
|
->render(); |
|
301
|
|
|
|
|
302
|
10 |
|
if ($this->size !== '') { |
|
303
|
1 |
|
Html::addCssClass($closeButtonAttributes, $this->size); |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
10 |
|
return Button::tag() |
|
307
|
10 |
|
->attributes($closeButtonAttributes) |
|
308
|
10 |
|
->content($label) |
|
309
|
10 |
|
->encode(false) |
|
310
|
10 |
|
->render() . PHP_EOL; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
11 |
|
private function renderHeader(): string |
|
314
|
|
|
{ |
|
315
|
11 |
|
$html = ''; |
|
316
|
|
|
|
|
317
|
11 |
|
$headerAttributes = $this->headerAttributes; |
|
318
|
11 |
|
$headerMessage = $this->headerMessage; |
|
319
|
|
|
|
|
320
|
11 |
|
Html::addCssClass($headerAttributes, $this->headerMessageCssClass); |
|
321
|
|
|
|
|
322
|
11 |
|
$renderCloseButton = $this->renderCloseButton(); |
|
323
|
|
|
|
|
324
|
11 |
|
if ($this->encode) { |
|
325
|
1 |
|
$headerMessage = Html::encode($headerMessage); |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
11 |
|
if ($renderCloseButton !== '') { |
|
329
|
10 |
|
$headerMessage = PHP_EOL . P::tag()->content($headerMessage) . PHP_EOL . $renderCloseButton; |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
11 |
|
if ($this->withoutHeader === false) { |
|
333
|
10 |
|
$html = Div::tag() |
|
334
|
10 |
|
->attributes($headerAttributes) |
|
335
|
10 |
|
->content($headerMessage) |
|
336
|
10 |
|
->encode(false) |
|
337
|
10 |
|
->render() . PHP_EOL; |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
11 |
|
return $html; |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
11 |
|
private function renderMessage(): string |
|
344
|
|
|
{ |
|
345
|
11 |
|
$attributes = $this->attributes; |
|
346
|
|
|
|
|
347
|
|
|
/** @var string */ |
|
348
|
11 |
|
$id = $attributes['id'] ?? (Html::generateId($this->autoIdPrefix) . '-message'); |
|
349
|
11 |
|
unset($attributes['id']); |
|
350
|
|
|
|
|
351
|
11 |
|
Html::addCssClass($attributes, $this->cssClass); |
|
352
|
11 |
|
Html::addCssClass($attributes, $this->headerColor); |
|
353
|
|
|
|
|
354
|
11 |
|
if ($this->size !== '') { |
|
355
|
1 |
|
Html::addCssClass($attributes, $this->size); |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
11 |
|
return Div::tag() |
|
359
|
11 |
|
->attributes($attributes) |
|
360
|
11 |
|
->content(PHP_EOL . $this->renderHeader() . $this->renderMessageBody()) |
|
361
|
11 |
|
->encode(false) |
|
362
|
11 |
|
->id($id) |
|
363
|
11 |
|
->render(); |
|
364
|
|
|
} |
|
365
|
|
|
|
|
366
|
11 |
|
private function renderMessageBody(): string |
|
367
|
|
|
{ |
|
368
|
11 |
|
$body = $this->body; |
|
369
|
11 |
|
$bodyAttributes = $this->bodyAttributes; |
|
370
|
|
|
|
|
371
|
11 |
|
Html::addCssClass($bodyAttributes, $this->bodyCssClass); |
|
372
|
|
|
|
|
373
|
11 |
|
if ($this->encode) { |
|
374
|
1 |
|
$body = Html::encode($body); |
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
11 |
|
if ($body !== '') { |
|
378
|
11 |
|
$body = PHP_EOL . $body . PHP_EOL; |
|
379
|
|
|
} |
|
380
|
|
|
|
|
381
|
11 |
|
return Div::tag() |
|
382
|
11 |
|
->attributes($bodyAttributes) |
|
383
|
11 |
|
->content($body) |
|
384
|
11 |
|
->encode(false) |
|
385
|
11 |
|
->render() . PHP_EOL; |
|
386
|
|
|
} |
|
387
|
|
|
} |
|
388
|
|
|
|