Completed
Push — master ( e23571...5adf80 )
by Thomas
23s queued 17s
created

PureModalAction   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 50
c 2
b 0
f 1
dl 0
loc 188
rs 10
wmc 14

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setShouldRefresh() 0 4 1
A setDropUp() 0 4 1
A actionName() 0 3 1
A __construct() 0 5 1
A getButtonTitle() 0 7 2
A getDropUp() 0 3 1
A FieldHolder() 0 39 2
A getButtonIcon() 0 3 1
A setFieldList() 0 4 1
A setButtonIcon() 0 4 1
A getShouldRefresh() 0 3 1
A getFieldList() 0 3 1
1
<?php
2
3
namespace LeKoala\PureModal;
4
5
use SilverStripe\Forms\FieldList;
6
use SilverStripe\View\Requirements;
7
use SilverStripe\Forms\LiteralField;
8
9
/**
10
 * Custom modal action
11
 * Requires cms-actions to work out of the box
12
 */
13
class PureModalAction extends LiteralField
14
{
15
    /**
16
     * @var FieldList
17
     */
18
    protected $fieldList;
19
20
    /**
21
     * An icon for this button
22
     * @var string
23
     */
24
    protected $buttonIcon;
25
26
    /**
27
     * @var boolean
28
     */
29
    protected $shouldRefresh = false;
30
31
    /**
32
     * Whether to place the button in a dot-menu.
33
     * @see https://github.com/lekoala/silverstripe-cms-actions
34
     * @var bool
35
     */
36
    protected $dropUp = false;
37
38
    public function __construct($name, $title)
39
    {
40
        $name = 'doCustomAction[' . $name . ']';
41
        $this->title = $title;
42
        $this->name = $name;
43
    }
44
45
    public function FieldHolder($properties = array())
46
    {
47
        Requirements::javascript('lekoala/silverstripe-pure-modal: client/pure-modal.js');
48
        Requirements::css('lekoala/silverstripe-pure-modal: client/pure-modal.css');
49
50
        $modalContent = '';
51
        foreach ($this->fieldList as $field) {
52
            $modalContent .= $field->FieldHolder();
53
        }
54
55
        $attrs = '';
56
        $modalID = 'modal_' . $this->name;
57
58
        $content = '';
59
        $content .= '<label for="' . $modalID . '" class="btn btn-info"' . $attrs . '>';
60
        $content .= $this->getButtonTitle();
61
        $content .= '</label>';
62
        $content .= '<div class="pure-modal from-top">';
63
        // This is how we show the modal
64
        $content .= '<input id="' . $modalID . '" class="checkbox" type="checkbox">';
65
        $content .= '<div class="pure-modal-overlay">';
66
        // Close in overlay
67
        $content .= '<label for="' . $modalID . '" class="o-close"></label>';
68
        $content .= '<div class="pure-modal-wrap">';
69
        // Close icon
70
        $content .= '<label for="' . $modalID . '" class="close">&#10006;</label>';
71
        $content .= $modalContent;
72
73
        // Add actual button
74
        $content .= '<button type="submit" name="action_' . $this->name . '" class="btn action btn btn-info custom-action">
75
            <span class="btn__title">' . $this->getButtonTitle() . '</span>
76
        </button>';
77
78
        $content .= '</div>';
79
        $content .= '</div>';
80
        $content .= '</div>';
81
        $this->content = $content;
82
83
        return parent::FieldHolder($properties);
84
    }
85
86
    /**
87
     * Get the title with icon if set
88
     *
89
     * @return string
90
     */
91
    protected function getButtonTitle()
92
    {
93
        $title = $this->title;
94
        if ($this->buttonIcon) {
95
            $title = '<span class="font-icon-' . $this->buttonIcon . '"></span> ' . $title;
96
        }
97
        return $title;
98
    }
99
100
101
    /**
102
     * Get an icon for this button
103
     *
104
     * @return string
105
     */
106
    public function getButtonIcon()
107
    {
108
        return $this->buttonIcon;
109
    }
110
111
    /**
112
     * Set an icon for this button
113
     *
114
     * Feel free to use SilverStripeIcons constants
115
     *
116
     * @param string $buttonIcon An icon for this button
117
     * @return $this
118
     */
119
    public function setButtonIcon(string $buttonIcon)
120
    {
121
        $this->buttonIcon = $buttonIcon;
122
        return $this;
123
    }
124
125
126
127
    /**
128
     * Get the value of fieldList
129
     * @return FieldList
130
     */
131
    public function getFieldList()
132
    {
133
        return $this->fieldList;
134
    }
135
136
    /**
137
     * Set the value of fieldList
138
     *
139
     * @param FieldList $fieldList
140
     * @return $this
141
     */
142
    public function setFieldList(FieldList $fieldList)
143
    {
144
        $this->fieldList = $fieldList;
145
        return $this;
146
    }
147
148
    /**
149
     * Get the dropUp value
150
     * Called by ActionsGridFieldItemRequest to determine placement
151
     *
152
     * @see https://github.com/lekoala/silverstripe-cms-actions
153
     * @return bool
154
     */
155
    public function getDropUp()
156
    {
157
        return $this->dropUp;
158
    }
159
160
    /**
161
     * Set the value of dropUp
162
     *
163
     * @see https://github.com/lekoala/silverstripe-cms-actions
164
     * @param bool $is
165
     * @return $this
166
     */
167
    public function setDropUp($is)
168
    {
169
        $this->dropUp = !!$is;
170
        return $this;
171
    }
172
173
    /**
174
     * Required for cms-actions
175
     * @return string
176
     */
177
    public function actionName()
178
    {
179
        return rtrim(str_replace('doCustomAction[', '', $this->name), ']');
180
    }
181
182
    /**
183
     * Get the value of shouldRefresh
184
     * @return mixed
185
     */
186
    public function getShouldRefresh()
187
    {
188
        return $this->shouldRefresh;
189
    }
190
191
    /**
192
     * Set the value of shouldRefresh
193
     *
194
     * @param mixed $shouldRefresh
195
     * @return $this
196
     */
197
    public function setShouldRefresh($shouldRefresh)
198
    {
199
        $this->shouldRefresh = $shouldRefresh;
200
        return $this;
201
    }
202
}
203