Passed
Push — master ( 5a2e94...4244be )
by Thomas
04:29 queued 12s
created

CmsInlineFormAction::setSubmitSelector()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace LeKoala\CmsActions;
4
5
use SilverStripe\Admin\ModelAdmin;
6
use SilverStripe\Forms\LiteralField;
7
use LeKoala\CmsActions\DefaultLink;
8
9
/**
10
 * A simple button that links to a given action or url
11
 *
12
 * This is meant to be used inside getCMSFields or getCMSUtils
13
 *
14
 * Action must be implemented on the controller (ModelAdmin for instance)
15
 * The data passed in the content of the form
16
 */
17
class CmsInlineFormAction extends LiteralField
18
{
19
    use DefaultLink;
20
21
    /**
22
     * @var array
23
     */
24
    protected $params = [];
25
26
27
    /**
28
     * @var string
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 action The method to call when the button is clicked
0 ignored issues
show
Bug introduced by
The type LeKoala\CmsActions\The was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
54
     * @param title The label on the button
55
     * @param 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
    public function performReadonlyTransformation()
64
    {
65
        return $this->castedCopy(self::class);
66
    }
67
68
    public function getLink()
69
    {
70
        if (!$this->link) {
71
            $this->link = $this->getControllerLink($this->name, $this->params);
72
        }
73
        return $this->link;
74
    }
75
76
    /**
77
     * Get an icon for this button
78
     *
79
     * @return string
80
     */
81
    public function getButtonIcon()
82
    {
83
        return $this->buttonIcon;
84
    }
85
86
    /**
87
     * Set an icon for this button
88
     *
89
     * Feel free to use SilverStripeIcons constants
90
     *
91
     * @param string $buttonIcon An icon for this button
92
     * @return $this
93
     */
94
    public function setButtonIcon(string $buttonIcon)
95
    {
96
        $this->buttonIcon = $buttonIcon;
97
        return $this;
98
    }
99
100
    public function Type()
101
    {
102
        return 'inline-action';
103
    }
104
105
    public function FieldHolder($properties = array())
106
    {
107
        $classes = $this->extraClass();
108
        if ($this->buttonIcon) {
109
            $classes .= " font-icon";
110
            $classes .= ' font-icon-' . $this->buttonIcon;
111
        }
112
        $link = $this->getLink();
113
        $attrs = '';
114
        if ($this->newWindow) {
115
            $attrs .= ' target="_blank"';
116
        }
117
        if ($this->readonly) {
118
            $attrs .= ' style="display:none"';
119
        }
120
        if (\strlen($this->submitSelector)) {
121
            $attrs .= " data-submit-selector=\"{$this->submitSelector}\"";
122
        }
123
        $title = $this->content;
124
        if ($this->post) {
125
            // This triggers a save action to the new location
126
            $content = '<button data-action="' . $link . '" class="btn ' . $classes . ' no-ajax"' . $attrs . '>';
127
            $content .= $title;
128
            $content .= '</button>';
129
        } else {
130
            $content = '<a href="' . $link . '" class="btn ' . $classes . ' action no-ajax"' . $attrs . '>';
131
            $content .= $title;
132
            $content .= '</a>';
133
        }
134
        $this->content = $content;
135
136
        return parent::FieldHolder($properties);
137
    }
138
139
    /**
140
     * Get the value of params
141
     *
142
     * @return  array
143
     */
144
    public function getParams()
145
    {
146
        return $this->params;
147
    }
148
149
    /**
150
     * Set the value of params
151
     *
152
     * @param  array  $params
153
     *
154
     * @return $this
155
     */
156
    public function setParams(array $params)
157
    {
158
        $this->params = $params;
159
        return $this;
160
    }
161
162
    /**
163
     * Get the value of post
164
     * @return boolean
165
     */
166
    public function getPost()
167
    {
168
        return $this->post;
169
    }
170
171
    /**
172
     * Set the value of post
173
     *
174
     * @param boolean $post
175
     * @return $this
176
     */
177
    public function setPost($post)
178
    {
179
        $this->post = $post;
180
        return $this;
181
    }
182
183
    /**
184
     * Get the value of {@see self::$submitSelector}
185
     *
186
     * @return string
187
     */
188
    public function getSubmitSelector()
189
    {
190
        return $this->submitSelector;
191
    }
192
193
    /**
194
     * Set the value of {@see self::$submitSelector}
195
     * 
196
     * Includes 
197
     *
198
     * @param string $selector
199
     * @return $this
200
     */
201
    public function setSubmitSelector($selector)
202
    {
203
        $this->submitSelector = $selector;
204
        return $this;
205
    }
206
}
207