Passed
Pull Request — master (#22)
by Mr.
02:05
created

Modal::renderCloseButton()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Bootstrap5;
6
7
use JsonException;
8
use Yiisoft\Arrays\ArrayHelper;
9
use Yiisoft\Html\Html;
10
11
use function array_merge;
12
13
/**
14
 * Modal renders a modal window that can be toggled by clicking on a button.
15
 *
16
 * The following example will show the content enclosed between the {@see begin()} and {@see end()} calls within the
17
 * modal window:
18
 *
19
 * ```php
20
 * Modal::widget()
21
 *     ->title('<h2>Hello world</h2>')
22
 *     ->toggleButton(['label' => 'click me'])
23
 *     ->begin();
24
 *
25
 * echo 'Say hello...';
26
 *
27
 * echo Modal::end();
28
 * ```
29
 */
30
class Modal extends Widget
31
{
32
    /**
33
     * The additional css class of large modal
34
     */
35
    public const SIZE_LARGE = 'modal-lg';
36
37
    /**
38
     * The additional css class of small modal
39
     */
40
    public const SIZE_SMALL = 'modal-sm';
41
42
    /**
43
     * The additional css class of default modal
44
     */
45
    public const SIZE_DEFAULT = '';
46
47
    private ?string $title = null;
48
    private array $titleOptions = [];
49
    private array $headerOptions = [];
50
    private array $bodyOptions = [];
51
    private ?string $footer = null;
52
    private array $footerOptions = [];
53
    private ?string $size = null;
54
    private array $closeButton = [];
55
    private bool $closeButtonEnabled = true;
56
    private array $toggleButton = [];
57
    private bool $toggleButtonEnabled = true;
58
    private array $options = [];
59
60 3
    public function begin(): ?string
61
    {
62 3
        parent::begin();
63
64 3
        if (!isset($this->options['id'])) {
65 3
            $this->options['id'] = "{$this->getId()}-modal";
66
        }
67
68 3
        $this->initOptions();
69
70
        return
71 3
            $this->renderToggleButton() . "\n" .
72 3
            Html::beginTag('div', $this->options) . "\n" .
73 3
            Html::beginTag('div', ['class' => 'modal-dialog ' . $this->size]) . "\n" .
74 3
            Html::beginTag('div', ['class' => 'modal-content']) . "\n" .
75 3
            $this->renderHeader() . "\n" .
76 3
            $this->renderBodyBegin() . "\n";
77
    }
78
79 3
    protected function run(): string
80
    {
81 3
        $this->registerPlugin('modal', $this->options);
82
83
        return
84 3
            "\n" . $this->renderBodyEnd() .
85 3
            "\n" . $this->renderFooter() .
86 3
            "\n" . Html::endTag('div') . // modal-content
87 3
            "\n" . Html::endTag('div') . // modal-dialog
88 3
            "\n" . Html::endTag('div');
89
    }
90
91
    /**
92
     * Renders the header HTML markup of the modal.
93
     *
94
     * @throws JsonException
95
     *
96
     * @return string the rendering result
97
     */
98 3
    protected function renderHeader(): string
99
    {
100 3
        $button = $this->renderCloseButton();
101
102 3
        if ($this->title !== null) {
103 2
            Html::addCssClass($this->titleOptions, ['widget' => 'modal-title']);
104
        }
105
106 3
        $header = ($this->title === null) ? '' : Html::tag('h5', $this->title, $this->titleOptions);
107
108 3
        if ($button !== null) {
109 2
            $header .= "\n" . $button;
110 1
        } elseif ($header === '') {
111 1
            return '';
112
        }
113
114 2
        Html::addCssClass($this->headerOptions, ['widget' => 'modal-header']);
115
116 2
        return Html::div($header, $this->headerOptions);
117
    }
118
119
    /**
120
     * Renders the opening tag of the modal body.
121
     *
122
     * @throws JsonException
123
     *
124
     * @return string the rendering result
125
     */
126 3
    protected function renderBodyBegin(): string
127
    {
128 3
        Html::addCssClass($this->bodyOptions, ['widget' => 'modal-body']);
129
130 3
        return Html::beginTag('div', $this->bodyOptions);
131
    }
132
133
    /**
134
     * Renders the closing tag of the modal body.
135
     *
136
     * @return string the rendering result
137
     */
138 3
    protected function renderBodyEnd(): string
139
    {
140 3
        return Html::endTag('div');
141
    }
142
143
    /**
144
     * Renders the HTML markup for the footer of the modal.
145
     *
146
     * @throws JsonException
147
     *
148
     * @return string the rendering result
149
     */
150 3
    protected function renderFooter(): ?string
151
    {
152 3
        if ($this->footer === null) {
153 1
            return null;
154
        }
155
156 2
        Html::addCssClass($this->footerOptions, ['widget' => 'modal-footer']);
157 2
        return Html::div($this->footer, $this->footerOptions);
158
    }
159
160
    /**
161
     * Renders the toggle button.
162
     *
163
     * @throws JsonException
164
     *
165
     * @return string the rendering result
166
     */
167 3
    protected function renderToggleButton(): ?string
168
    {
169 3
        if ($this->toggleButtonEnabled === false) {
170 2
            return null;
171
        }
172
173 1
        $tag = ArrayHelper::remove($this->toggleButton, 'tag', 'button');
174 1
        $label = ArrayHelper::remove($this->toggleButton, 'label', 'Show');
175
176 1
        return Html::tag($tag, $label, $this->toggleButton);
177
    }
178
179
    /**
180
     * Renders the close button.
181
     *
182
     * @throws JsonException
183
     *
184
     * @return string the rendering result
185
     */
186 3
    protected function renderCloseButton(): ?string
187
    {
188 3
        if ($this->closeButtonEnabled === false) {
189 1
            return null;
190
        }
191
192 2
        $tag = ArrayHelper::remove($this->closeButton, 'tag', 'button');
193 2
        $label = ArrayHelper::remove($this->closeButton, 'label', Html::span('&times;', [
194 2
            'aria-hidden' => 'true',
195
        ]));
196
197 2
        return Html::tag($tag, $label, $this->closeButton);
198
    }
199
200
    /**
201
     * Initializes the widget options.
202
     *
203
     * This method sets the default values for various options.
204
     */
205 3
    protected function initOptions(): void
206
    {
207 3
        $this->options = array_merge([
208 3
            'class' => 'fade',
209
            'role' => 'dialog',
210
            'tabindex' => -1,
211
            'aria-hidden' => 'true',
212 3
        ], $this->options);
213
214 3
        Html::addCssClass($this->options, ['widget' => 'modal']);
215
216 3
        if ($this->getEnableClientOptions() !== false) {
217
            $this->clientOptions(array_merge(['show' => false], $this->getClientOptions()));
218
        }
219
220 3
        $this->titleOptions = array_merge([
221 3
            'id' => $this->options['id'] . '-label',
222 3
        ], $this->titleOptions);
223
224 3
        if (!isset($this->options['aria-label'], $this->options['aria-labelledby']) && $this->title !== null) {
225 2
            $this->options['aria-labelledby'] = $this->titleOptions['id'];
226
        }
227
228 3
        if ($this->closeButtonEnabled !== false) {
229 2
            $this->closeButton = array_merge([
230 2
                'data-dismiss' => 'modal',
231
                'class' => 'close',
232
                'type' => 'button',
233 2
            ], $this->closeButton);
234
        }
235
236 3
        if ($this->toggleButton !== []) {
237 1
            $this->toggleButton = array_merge([
238 1
                'data-toggle' => 'modal',
239
                'type' => 'button',
240 1
            ], $this->toggleButton);
241 1
            if (!isset($this->toggleButton['data-target']) && !isset($this->toggleButton['href'])) {
242 1
                $this->toggleButton['data-target'] = '#' . $this->options['id'];
243
            }
244
        }
245 3
    }
246
247
    /**
248
     * Body options.
249
     *
250
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
251
     *
252
     * @param array $value
253
     *
254
     * @return $this
255
     */
256 1
    public function bodyOptions(array $value): self
257
    {
258 1
        $this->bodyOptions = $value;
259
260 1
        return $this;
261
    }
262
263
    /**
264
     * The options for rendering the close button tag.
265
     *
266
     * The close button is displayed in the header of the modal window. Clicking on the button will hide the modal
267
     * window. If {@see closeButtonEnabled} is false, no close button will be rendered.
268
     *
269
     * The following special options are supported:
270
     *
271
     * - tag: string, the tag name of the button. Defaults to 'button'.
272
     * - label: string, the label of the button. Defaults to '&times;'.
273
     *
274
     * The rest of the options will be rendered as the HTML attributes of the button tag. Please refer to the
275
     * [Modal plugin help](http://getbootstrap.com/javascript/#modals) for the supported HTML attributes.
276
     *
277
     * @param array $value
278
     *
279
     * @return $this
280
     */
281
    public function closeButton(array $value): self
282
    {
283
        $this->closeButton = $value;
284
285
        return $this;
286
    }
287
288
    /**
289
     * Enable/Disable close button.
290
     *
291
     * @param bool $value
292
     *
293
     * @return $this
294
     */
295 1
    public function closeButtonEnabled(bool $value): self
296
    {
297 1
        $this->closeButtonEnabled = $value;
298
299 1
        return $this;
300
    }
301
302
    /**
303
     * The footer content in the modal window.
304
     *
305
     * @param string|null $value
306
     *
307
     * @return $this
308
     */
309 2
    public function footer(?string $value): self
310
    {
311 2
        $this->footer = $value;
312
313 2
        return $this;
314
    }
315
316
    /**
317
     * Additional footer options.
318
     *
319
     * @param array $value
320
     *
321
     * @return $this
322
     *
323
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
324
     */
325
    public function footerOptions(array $value): self
326
    {
327
        $this->footerOptions = $value;
328
329
        return $this;
330
    }
331
332
    /**
333
     * Additional header options.
334
     *
335
     * @param array $value
336
     *
337
     * @return $this
338
     *
339
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
340
     */
341
    public function headerOptions(array $value): self
342
    {
343
        $this->headerOptions = $value;
344
345
        return $this;
346
    }
347
348
    /**
349
     * @param array $value the HTML attributes for the widget container tag. The following special options are
350
     * recognized.
351
     *
352
     * @return $this
353
     *
354
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
355
     */
356
    public function options(array $value): self
357
    {
358
        $this->options = $value;
359
360
        return $this;
361
    }
362
363
    /**
364
     * The title content in the modal window.
365
     *
366
     * @param string|null $value
367
     *
368
     * @return $this
369
     */
370 2
    public function title(?string $value): self
371
    {
372 2
        $this->title = $value;
373
374 2
        return $this;
375
    }
376
377
    /**
378
     * Additional title options.
379
     *
380
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
381
     *
382
     * @param array $value
383
     *
384
     * @return $this
385
     */
386
    public function titleOptions(array $value): self
387
    {
388
        $this->titleOptions = $value;
389
390
        return $this;
391
    }
392
393
    /**
394
     * The options for rendering the toggle button tag.
395
     *
396
     * The toggle button is used to toggle the visibility of the modal window. If {@see toggleButtonEnabled} is false,
397
     * no toggle button will be rendered.
398
     *
399
     * The following special options are supported:
400
     *
401
     * - tag: string, the tag name of the button. Defaults to 'button'.
402
     * - label: string, the label of the button. Defaults to 'Show'.
403
     *
404
     * The rest of the options will be rendered as the HTML attributes of the button tag. Please refer to the
405
     * [Modal plugin help](http://getbootstrap.com/javascript/#modals) for the supported HTML attributes.
406
     *
407
     * @param array $value
408
     *
409
     * @return $this
410
     */
411 1
    public function toggleButton(array $value): self
412
    {
413 1
        $this->toggleButton = $value;
414
415 1
        return $this;
416
    }
417
418
    /**
419
     * Enable/Disable toggle button.
420
     *
421
     * @param bool $value
422
     *
423
     * @return $this
424
     */
425 2
    public function toggleButtonEnabled(bool $value): self
426
    {
427 2
        $this->toggleButtonEnabled = $value;
428
429 2
        return $this;
430
    }
431
432
    /**
433
     * The modal size. Can be {@see SIZE_LARGE} or {@see SIZE_SMALL}, or null for default.
434
     *
435
     * @param string|null $value
436
     *
437
     * @return $this
438
     */
439
    public function size(?string $value): self
440
    {
441
        $this->size = $value;
442
443
        return $this;
444
    }
445
}
446