Passed
Push — master ( d17d6b...1ee2b4 )
by Thomas
11:00
created

CustomAction::setRedirectURL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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