CmsInlineFormAction   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 213
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
eloc 57
c 5
b 0
f 1
dl 0
loc 213
rs 10
wmc 20

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSubmitSelector() 0 3 1
A performReadonlyTransformation() 0 3 1
A Type() 0 3 1
A getButtonIcon() 0 3 1
A getPost() 0 3 1
A setPost() 0 5 1
A setSubmitSelector() 0 5 1
B FieldHolder() 0 42 7
A setParams() 0 5 1
A setButtonIcon() 0 4 1
A getLink() 0 7 2
A getParams() 0 3 1
1
<?php
2
3
namespace LeKoala\CmsActions;
4
5
use SilverStripe\Forms\FormField;
6
use SilverStripe\Forms\LiteralField;
7
8
/**
9
 * A simple button that links to a given action or url
10
 *
11
 * This is meant to be used inside getCMSFields or getCMSUtils
12
 *
13
 * Action must be implemented on the controller (ModelAdmin for instance)
14
 * The data passed in the content of the form
15
 */
16
class CmsInlineFormAction extends LiteralField
17
{
18
    use DefaultLink;
19
    use ProgressiveAction;
20
21
    /**
22
     * @var array<mixed>
23
     */
24
    protected $params = [];
25
26
27
    /**
28
     * @var string|null
29
     */
30
    protected $buttonIcon = null;
31
32
    /**
33
     * @var boolean
34
     */
35
    protected $post = false;
36
37
    /**
38
     * This will be the selector that's click event gets called by {@see self}'s entwine event.
39
     *
40
     * This is a temporary hack since form.sumbit() doesn't seem to be working.
41
     * For example setting this up to work on CMSEditPage:
42
     * ```
43
     * CmsInlineFormAction::create('myAction', 'My Action')->setSubmitSelector('Form_ItemEditForm_action_save');
44
     * ```
45
     * You can also use this to hackishly publish on post.
46
     *
47
     * @var string
48
     */
49
    protected $submitSelector = '#Form_ItemEditForm_action_doSave';
50
51
    /**
52
     * Create a new action button.
53
     * @param string $action The method to call when the button is clicked
54
     * @param string $title The label on the button
55
     * @param string $extraClass A CSS class to apply to the button in addition to 'action'
56
     */
57
    public function __construct($action, $title = "", $extraClass = 'btn-primary')
58
    {
59
        parent::__construct($action, $title);
60
        $this->addExtraClass($extraClass);
61
    }
62
63
    /**
64
     * @return FormField
65
     */
66
    public function performReadonlyTransformation()
67
    {
68
        return $this->castedCopy(self::class);
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getLink()
75
    {
76
        if (!$this->link) {
77
            $this->link = $this->getControllerLink($this->name, $this->params);
78
        }
79
80
        return $this->link;
81
    }
82
83
    /**
84
     * Get an icon for this button
85
     *
86
     * @return string|null
87
     */
88
    public function getButtonIcon()
89
    {
90
        return $this->buttonIcon;
91
    }
92
93
    /**
94
     * Set an icon for this button
95
     *
96
     * Feel free to use SilverStripeIcons constants
97
     *
98
     * @param string|null $buttonIcon An icon for this button
99
     * @return $this
100
     */
101
    public function setButtonIcon(string $buttonIcon = null)
102
    {
103
        $this->buttonIcon = $buttonIcon;
104
        return $this;
105
    }
106
107
    /**
108
     * @return string
109
     */
110
    public function Type()
111
    {
112
        return 'inline-action';
113
    }
114
115
    /**
116
     * @param array<string,mixed> $properties
117
     * @return FormField|string
118
     */
119
    public function FieldHolder($properties = [])
120
    {
121
        $classes = [$this->extraClass()];
122
        if ($this->buttonIcon) {
123
            $classes[] = "font-icon";
124
            $classes[] = sprintf('font-icon-%s', $this->buttonIcon);
125
        }
126
        if ($this->progressive) {
127
            $classes[] = "progressive-action";
128
        }
129
        $link = $this->getLink();
130
        $attrs = [];
131
        if ($this->newWindow) {
132
            $attrs[] = 'target="_blank"';
133
        }
134
        if ($this->readonly) {
135
            $attrs[] = 'style="display:none"';
136
        }
137
        if (strlen($this->submitSelector)) {
138
            $attrs[] = sprintf('data-submit-selector="%s"', $this->submitSelector);
139
        }
140
        $title = $this->content;
141
        if ($this->post) {
142
            $content = sprintf(
143
                '<button data-action="%s" class="btn no-ajax %s" %s>%s</button>',
144
                $link,
145
                implode(' ', $classes),
146
                implode('', $attrs),
147
                $title
148
            );
149
        } else {
150
            $content = sprintf(
151
                '<a href="%s" class="btn action no-ajax %s" %s>%s</a>',
152
                $link,
153
                implode(' ', $classes),
154
                implode('', $attrs),
155
                $title
156
            );
157
        }
158
        $this->content = $content;
159
160
        return parent::FieldHolder($properties);
161
    }
162
163
    /**
164
     * Get the value of params
165
     *
166
     * @return array<mixed>
167
     */
168
    public function getParams()
169
    {
170
        return $this->params;
171
    }
172
173
    /**
174
     * Set the value of params
175
     *
176
     * @param array<mixed> $params
177
     * @return $this
178
     */
179
    public function setParams(array $params)
180
    {
181
        $this->params = $params;
182
183
        return $this;
184
    }
185
186
    /**
187
     * Get the value of post
188
     * @return boolean
189
     */
190
    public function getPost()
191
    {
192
        return $this->post;
193
    }
194
195
    /**
196
     * Set the value of post
197
     *
198
     * @param boolean $post
199
     * @return $this
200
     */
201
    public function setPost($post)
202
    {
203
        $this->post = $post;
204
205
        return $this;
206
    }
207
208
    /**
209
     * Get the value of {@see self::$submitSelector}
210
     *
211
     * @return string
212
     */
213
    public function getSubmitSelector()
214
    {
215
        return $this->submitSelector;
216
    }
217
218
    /**
219
     * Set the value of {@see self::$submitSelector}
220
     *
221
     * @param string $selector
222
     * @return $this
223
     */
224
    public function setSubmitSelector($selector)
225
    {
226
        $this->submitSelector = $selector;
227
228
        return $this;
229
    }
230
}
231