Passed
Push — master ( 0bbff1...dc2b13 )
by Thomas
02:09
created

CustomButton::setNoChangeTrack()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace LeKoala\CmsActions;
4
5
/**
6
 * Common button functionnality that is shared between CustomAction and CustomLink
7
 */
8
trait CustomButton
9
{
10
    /**
11
     * Default classes applied in constructor
12
     * @config
13
     * @var array
14
     */
15
    private static $default_classes = [
16
        'btn', 'btn-info'
17
    ];
18
19
    /**
20
     * An icon for this button
21
     * @var string
22
     */
23
    protected $buttonIcon;
24
25
    /**
26
     * The confirmation message
27
     * @var string
28
     */
29
    protected $confirmation;
30
31
    /**
32
     * Get the title of the link
33
     * Called by ActionsGridFieldItemRequest to build default message
34
     *
35
     * @return string
36
     */
37
    public function getTitle()
38
    {
39
        return $this->title;
40
    }
41
42
    /**
43
     * Set the value of title
44
     *
45
     * @return $this
46
     */
47
    public function setTitle($title)
48
    {
49
        $this->title = $title;
0 ignored issues
show
Bug Best Practice introduced by
The property title does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
50
        return $this;
51
    }
52
53
    /**
54
     * @param string $type
55
     *
56
     * @return void
57
     */
58
    public function setButtonType($type)
59
    {
60
        if ($this->extraClasses) {
61
            foreach ($this->extraClasses as $k => $v) {
62
                if (strpos($k, 'btn-') !== false) {
63
                    unset($this->extraClasses[$k]);
64
                }
65
            }
66
        }
67
68
        $btn = "btn-$type";
69
        $this->extraClasses[$btn] = $btn;
0 ignored issues
show
Bug Best Practice introduced by
The property extraClasses does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
70
    }
71
72
    /**
73
     * Get the title with icon if set
74
     *
75
     * @return string
76
     */
77
    protected function getButtonTitle()
78
    {
79
        $title = $this->title;
80
        if ($this->buttonIcon) {
81
            $title = '<span class="font-icon-' . $this->buttonIcon . '"></span> ' . $title;
82
        }
83
        return $title;
84
    }
85
86
    /**
87
     * Get an icon for this button
88
     *
89
     * @return string
90
     */
91
    public function getButtonIcon()
92
    {
93
        return $this->buttonIcon;
94
    }
95
96
    /**
97
     * Set an icon for this button
98
     *
99
     * Feel free to use SilverStripeIcons constants
100
     *
101
     * @param string $buttonIcon An icon for this button
102
     * @return $this
103
     */
104
    public function setButtonIcon(string $buttonIcon)
105
    {
106
        $this->buttonIcon = $buttonIcon;
107
        return $this;
108
    }
109
110
    /**
111
     * Get the value of confirmation
112
     */
113
    public function getConfirmation()
114
    {
115
        return $this->confirmation;
116
    }
117
118
    /**
119
     * Set the value of confirmation
120
     *
121
     * @param string|bool A confirm message or true for a generic message
122
     * @return $this
123
     */
124
    public function setConfirmation($confirmation)
125
    {
126
        if ($confirmation === true) {
127
            $confirmation = _t('CustomButton.CONFIRM_MESSAGE', 'Are you sure?');
128
        }
129
        $this->confirmation = $confirmation;
130
        return $this;
131
    }
132
}
133