1<?php
2/**
3 * Bootstrap Wrapper Plugin: Alert
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Giuseppe Di Terlizzi <giuseppe.diterlizzi@gmail.com>
7 * @author     Jos Roossien <mail@jroossien.com>
8 * @copyright  (C) 2015-2020, Giuseppe Di Terlizzi
9 */
10
11class syntax_plugin_bootswrapper_modal extends syntax_plugin_bootswrapper_bootstrap
12{
13
14    public $p_type         = 'normal';
15    public $pattern_start  = '<modal.*?>(?=.*?</modal>)';
16    public $pattern_end    = '</modal>';
17    public $tag_name       = 'modal';
18    public $tag_attributes = array(
19
20        'id'       => array(
21            'type'     => 'string',
22            'values'   => null,
23            'required' => true,
24            'default'  => null),
25
26        'size'     => array(
27            'type'     => 'string',
28            'values'   => array('lg', 'sm'),
29            'required' => false,
30            'default'  => null),
31
32        'title'    => array(
33            'type'     => 'string',
34            'values'   => null,
35            'required' => false,
36            'default'  => null),
37
38        'keyboard' => array(
39            'type'     => 'boolean',
40            'values'   => array(0, 1),
41            'required' => false,
42            'default'  => null),
43
44        'dismiss'  => array(
45            'type'     => 'boolean',
46            'values'   => array(0, 1),
47            'required' => false,
48            'default'  => true),
49
50        'show'     => array(
51            'type'     => 'boolean',
52            'values'   => array(0, 1),
53            'required' => false,
54            'default'  => false),
55
56        'fade'     => array(
57            'type'     => 'boolean',
58            'values'   => array(0, 1),
59            'required' => false,
60            'default'  => true),
61
62        'backdrop' => array(
63            'type'     => 'string',
64            'values'   => array('true', 'false', 'static'),
65            'required' => false,
66            'default'  => null),
67
68        'remote'   => array(
69            'type'     => 'string',
70            'values'   => null,
71            'required' => false,
72            'default'  => null),
73
74    );
75
76    public function render($mode, Doku_Renderer $renderer, $data)
77    {
78
79        if (empty($data)) {
80            return false;
81        }
82
83        if ($mode !== 'xhtml') {
84            return false;
85        }
86
87        /** @var Doku_Renderer_xhtml $renderer */
88        list($state, $match, $pos, $attributes) = $data;
89
90        if ($state == DOKU_LEXER_ENTER) {
91            $id       = $attributes['id'];
92            $size     = $attributes['size'];
93            $title    = $attributes['title'];
94            $keyboard = $attributes['keyboard'];
95            $dismiss  = $attributes['dismiss'];
96            $show     = $attributes['show'];
97            $fade     = $attributes['fade'] === true ? 'fade' : '';
98            $backdrop = $attributes['backdrop'];
99            $remote   = $attributes['remote'];
100
101            $html5_attributes = array();
102
103            if ($remote) {
104                $html5_attributes['data-remote'] = wl($remote, array('do' => 'export_xhtmlbody'), true);
105            }
106
107            if ($title) {
108                $html5_attributes['data-labelledby'] = $title;
109            }
110
111            if ($show) {
112                $html5_attributes['data-show'] = $show;
113            }
114
115            if ($backdrop) {
116                $html5_attributes['data-backdrop'] = $backdrop;
117            }
118
119            if ($keyboard) {
120                $html5_attributes['data-keyboard'] = $keyboard;
121            }
122
123            //Modal
124            $markup = '<div class="bs-wrap bs-wrap-modal modal ' . $fade . '" id="' . $id . '" role="dialog" tabindex="-1" ' . $this->buildAttributes($html5_attributes) . '>';
125
126            $markup .= '<div class="bs-wrap modal-dialog modal-' . $size . '" role="document"><div class="bs-wrap modal-content">';
127
128            //Header/Title
129            if ($title) {
130                $markup .= '<div class="bs-wrap modal-header">';
131
132                if ($dismiss === true) {
133                    $markup .= '<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
134                }
135
136                $markup .= '<h4 class="bs-wrap modal-title">' . $title . '</h4>';
137                $markup .= '</div>';
138            }
139
140            //Body
141            $markup .= '<div class="bs-wrap modal-body">';
142            if ($dismiss === true && !$title) {
143                //Show dismiss button in body when there is no header.
144                $markup .= '<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
145            }
146
147            $renderer->doc .= $markup;
148            return true;
149        }
150
151        if ($state == DOKU_LEXER_EXIT) {
152            $renderer->doc .= '</div></div></div></div>';
153            return true;
154        }
155
156        return true;
157    }
158}
159