Passed
Push — master ( a7ad57...b00550 )
by Thomas
03:38 queued 14s
created

PureModal::getMoveModalScript()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace LeKoala\PureModal;
4
5
use SilverStripe\Admin\ModelAdmin;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Forms\DatalessField;
8
use SilverStripe\Forms\FormField;
9
use SilverStripe\View\SSViewer;
10
11
/**
12
 * A simple pure css modal for usage in the cms
13
 *
14
 * If your content contains a form, it should be loaded through an iframe
15
 * because you cannot nest forms
16
 *
17
 * @author lekoala
18
 */
19
class PureModal extends DatalessField
20
{
21
    /**
22
     * @var string
23
     */
24
    protected $title;
25
26
    /**
27
     * @var string
28
     */
29
    protected $content;
30
31
    /**
32
     * @var string
33
     */
34
    protected $iframe;
35
36
    /**
37
     * @var bool
38
     */
39
    protected $iframeTop;
40
41
    public function __construct($name, $title, $content)
42
    {
43
        $this->setContent($content);
44
45
        parent::__construct($name, $title);
46
    }
47
48
    public static function getMoveModalScript()
49
    {
50
        if (!self::config()->move_modal_to_body) {
51
            return '';
52
        }
53
        return "document.body.appendChild(this.parentElement.querySelector('.pure-modal'));this.onclick=null;";
54
    }
55
56
    public function getAttributes()
57
    {
58
        $attrs = [];
59
        // Move modal to body to avoid nesting issues
60
        $attrs['onclick'] = self::getMoveModalScript();
61
        // Since the frame is hidden, we need to compute size on click
62
        if ($this->getIframe()) {
63
            $attrs['onclick'] .= "var i=document.querySelector('#" . $this->getIframeID() . "');i.style.height = 0; setTimeout(function() {i.style.height = i.contentWindow.document.body.scrollHeight + 'px';},100);";
64
        }
65
        return $attrs;
66
    }
67
68
    /**
69
     * Render a dialog
70
     *
71
     * @param Controller $controller
72
     * @param array $customFields
73
     * @return string
74
     */
75
    public static function renderDialog($controller, $customFields = null)
76
    {
77
        // Set empty content by default otherwise it will render the full page
78
        if (empty($customFields['Content'])) {
79
            $customFields['Content'] = '';
80
        }
81
82
        $templates = SSViewer::get_templates_by_class(static::class, '_Dialog', __CLASS__);
83
        $templatesWithSuffix = SSViewer::chooseTemplate($templates);
84
85
        return $controller->renderWith($templatesWithSuffix, $customFields);
86
    }
87
88
    /**
89
     * Sets the content of this field to a new value.
90
     *
91
     * @param string|FormField $content
92
     *
93
     * @return $this
94
     */
95
    public function setContent($content)
96
    {
97
        $this->content = $content;
98
99
        return $this;
100
    }
101
102
    /**
103
     * @return string
104
     */
105
    public function getContent()
106
    {
107
        return $this->content;
108
    }
109
110
    /**
111
     * Build an url for the current controller and pass along some parameters
112
     *
113
     * If you want to call actions on a model, use getModelLink
114
     *
115
     * @param string $action
116
     * @param array $params
117
     * @return string
118
     */
119
    public function getControllerLink($action, array $params = null)
120
    {
121
        if ($params === null) {
122
            $params = [];
123
        }
124
        $ctrl = Controller::curr();
125
        if ($ctrl instanceof ModelAdmin) {
126
            $allParams = $ctrl->getRequest()->allParams();
127
            $modelClass = $ctrl->getRequest()->param('ModelClass');
128
            $action = $modelClass . '/' . $action;
129
            $params = array_merge($allParams, $params);
130
        }
131
        if (!empty($params)) {
132
            $action .= '?' . http_build_query($params);
133
        }
134
        return $ctrl->Link($action);
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    public function getTitle()
141
    {
142
        return $this->title;
143
    }
144
145
    /**
146
     * @param string $title
147
     * @return $this
148
     */
149
    public function setTitle($title)
150
    {
151
        $this->title = $title;
152
153
        return $this;
154
    }
155
156
    /**
157
     * @return string
158
     */
159
    public function getIframe()
160
    {
161
        return $this->iframe;
162
    }
163
164
    /**
165
     * Link to iframe src
166
     *
167
     * @param string $iframe
168
     * @return $this
169
     */
170
    public function setIframe($iframe)
171
    {
172
        $this->iframe = $iframe;
173
174
        return $this;
175
    }
176
177
    /**
178
     * Helper for setIframe link using conventions
179
     *
180
     * @param string $action
181
     * @return $this
182
     */
183
    public function setIframeAction($action)
184
    {
185
        return $this->setIframe($this->getControllerLink($action));
186
    }
187
188
    /**
189
     * @return bool
190
     */
191
    public function getIframeTop()
192
    {
193
        return $this->iframeTop;
194
    }
195
196
    /**
197
     * Should the iframe be above the content
198
     *
199
     * @param bool $iframeTop
200
     * @return $this
201
     */
202
    public function setIframeTop($iframeTop)
203
    {
204
        $this->iframeTop = $iframeTop;
205
        return $this;
206
    }
207
208
    /**
209
     *
210
     * @return string
211
     */
212
    public function getModalID()
213
    {
214
        return 'modal_' . $this->name;
215
    }
216
217
    /**
218
     *
219
     * @return string
220
     */
221
    public function getIframeID()
222
    {
223
        return $this->getModalID() . '_iframe';
224
    }
225
}
226