CustomAction::actionName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace LeKoala\CmsActions;
4
5
use SilverStripe\Core\Convert;
6
use SilverStripe\Forms\FormAction;
7
use SilverStripe\ORM\FieldType\DBHTMLText;
8
9
/**
10
 * Custom actions to use in getCMSActions
11
 *
12
 * Actions handlers are declared on the DataObject itself
13
 *
14
 * Because it is an action, it will be submitted through ajax
15
 * If you want to create links that open files or show a new page, use CustomLink
16
 */
17
class CustomAction extends FormAction
18
{
19
    use CustomButton;
20
21
    /**
22
     * @var boolean
23
     */
24
    public $useButtonTag = true;
25
26
    /**
27
     * Used in ActionsGridFieldItemRequest::forwardActionToRecord
28
     * @var boolean
29
     */
30
    protected $shouldRefresh = false;
31
32
    /**
33
     * @var string
34
     */
35
    protected $redirectURL = null;
36
37
    public function __construct($name, $title, $form = null)
38
    {
39
        // Actually, an array works just fine!
40
        $name = 'doCustomAction[' . $name . ']';
41
42
        parent::__construct($name, $title, $form);
43
    }
44
45
    public function actionName()
46
    {
47
        return rtrim(str_replace('action_doCustomAction[', '', $this->name), ']');
48
    }
49
50
    public function Type()
51
    {
52
        return 'action';
53
    }
54
55
    /**
56
     * @param array<string,mixed> $properties
57
     * @return DBHTMLText
58
     */
59
    public function Field($properties = [])
60
    {
61
        $icon = $this->buttonIcon;
62
        if (!$icon) {
63
            $icon = $this->icon;
64
        }
65
        if ($icon) {
66
            $this->addExtraClass('font-icon');
67
            $this->addExtraClass('font-icon-' . $icon);
68
            $this->addExtraClass('btn-mobile-collapse'); // we can collapse by default on mobile with an icon
69
        }
70
        // Note: type should stay "action" to properly submit
71
        $this->addExtraClass('custom-action');
72
        if ($this->confirmation) {
73
            $this->setAttribute('data-message', Convert::raw2htmlatt($this->confirmation));
74
            $this->setAttribute('onclick', 'return confirm(this.dataset.message);return false;');
75
        }
76
77
        if ($this->hasLastIcon()) {
78
            $this->addExtraClass('btn-mobile-collapse'); // we can collapse by default on mobile with an icon
79
        }
80
81
        return parent::Field($properties);
82
    }
83
84
    /**
85
     * Get the value of shouldRefresh
86
     * @return mixed
87
     */
88
    public function getShouldRefresh()
89
    {
90
        return $this->shouldRefresh;
91
    }
92
93
    /**
94
     * Set the value of shouldRefresh
95
     *
96
     * @param mixed $shouldRefresh
97
     * @return $this
98
     */
99
    public function setShouldRefresh($shouldRefresh)
100
    {
101
        $this->shouldRefresh = $shouldRefresh;
102
103
        return $this;
104
    }
105
106
    /**
107
     * Get the value of redirectURL
108
     * @return mixed
109
     */
110
    public function getRedirectURL()
111
    {
112
        return $this->redirectURL;
113
    }
114
115
    /**
116
     * Set the value of redirectURL
117
     *
118
     * @param mixed $redirectURL
119
     * @return $this
120
     */
121
    public function setRedirectURL($redirectURL)
122
    {
123
        $this->redirectURL = $redirectURL;
124
125
        return $this;
126
    }
127
}
128