Passed
Push — master ( 848283...5aaadc )
by Thomas
02:19
created

CmsInlineFormAction::getLink()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 6
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
     * Create a new action button.
39
     * @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...
40
     * @param title The label on the button
41
     * @param extraClass A CSS class to apply to the button in addition to 'action'
42
     */
43
    public function __construct($action, $title = "", $extraClass = 'btn-primary')
44
    {
45
        parent::__construct($action, $title);
46
        $this->addExtraClass($extraClass);
47
    }
48
49
    public function performReadonlyTransformation()
50
    {
51
        return $this->castedCopy(self::class);
52
    }
53
54
    public function getLink()
55
    {
56
        if (!$this->link) {
57
            $this->link = $this->getControllerLink($this->name, $this->params);
58
        }
59
        return $this->link;
60
    }
61
62
    /**
63
     * Get an icon for this button
64
     *
65
     * @return string
66
     */
67
    public function getButtonIcon()
68
    {
69
        return $this->buttonIcon;
70
    }
71
72
    /**
73
     * Set an icon for this button
74
     *
75
     * Feel free to use SilverStripeIcons constants
76
     *
77
     * @param string $buttonIcon An icon for this button
78
     * @return $this
79
     */
80
    public function setButtonIcon(string $buttonIcon)
81
    {
82
        $this->buttonIcon = $buttonIcon;
83
        return $this;
84
    }
85
86
    public function Type()
87
    {
88
        return 'inline-action';
89
    }
90
91
    public function FieldHolder($properties = array())
92
    {
93
        $classes = $this->extraClass();
94
        if ($this->buttonIcon) {
95
            $classes .= " font-icon";
96
            $classes .= ' font-icon-' . $this->buttonIcon;
97
        }
98
        $link = $this->getLink();
99
        $attrs = '';
100
        if ($this->newWindow) {
101
            $attrs .= ' target="_blank"';
102
        }
103
        if ($this->readonly) {
104
            $attrs .= ' style="display:none"';
105
        }
106
        $title = $this->content;
107
        if ($this->post) {
108
            // This triggers a save action to the new location
109
            $content = '<button data-action="' . $link . '" class="btn ' . $classes . ' no-ajax"' . $attrs . '>';
110
            $content .= $title;
111
            $content .= '</button>';
112
        } else {
113
            $content = '<a href="' . $link . '" class="btn ' . $classes . ' action no-ajax"' . $attrs . '>';
114
            $content .= $title;
115
            $content .= '</a>';
116
        }
117
        $this->content = $content;
118
119
        return parent::FieldHolder($properties);
120
    }
121
122
    /**
123
     * Get the value of params
124
     *
125
     * @return  array
126
     */
127
    public function getParams()
128
    {
129
        return $this->params;
130
    }
131
132
    /**
133
     * Set the value of params
134
     *
135
     * @param  array  $params
136
     *
137
     * @return $this
138
     */
139
    public function setParams(array $params)
140
    {
141
        $this->params = $params;
142
        return $this;
143
    }
144
145
    /**
146
     * Get the value of post
147
     * @return boolean
148
     */
149
    public function getPost()
150
    {
151
        return $this->post;
152
    }
153
154
    /**
155
     * Set the value of post
156
     *
157
     * @param boolean $post
158
     * @return $this
159
     */
160
    public function setPost($post)
161
    {
162
        $this->post = $post;
163
        return $this;
164
    }
165
}
166