1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\CmsActions; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Convert; |
6
|
|
|
use SilverStripe\Forms\FormField; |
7
|
|
|
use SilverStripe\Forms\LiteralField; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Custom links to include in getCMSActions |
11
|
|
|
* |
12
|
|
|
* Link handlers are declared on the DataObject itself |
13
|
|
|
*/ |
14
|
|
|
class CustomLink extends LiteralField |
15
|
|
|
{ |
16
|
|
|
use CustomButton; |
17
|
|
|
use DefaultLink; |
18
|
|
|
use ProgressiveAction; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var boolean |
22
|
|
|
*/ |
23
|
|
|
protected $noAjax = false; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $name |
27
|
|
|
* @param string $title |
28
|
|
|
* @param string|array<mixed> $link Will default to name of link on current record if not set |
29
|
|
|
*/ |
30
|
|
|
public function __construct($name, $title = null, $link = null) |
31
|
|
|
{ |
32
|
|
|
if ($title === null) { |
33
|
|
|
$title = FormField::name_to_label($name); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
parent::__construct($name, ''); |
37
|
|
|
|
38
|
|
|
// Reset the title later on because we passed '' to parent |
39
|
|
|
$this->title = $title; |
40
|
|
|
|
41
|
|
|
if ($link && is_string($link)) { |
42
|
|
|
$this->link = $link; |
43
|
|
|
} else { |
44
|
|
|
$this->link = $this->getModelLink($name, $link); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public function Type() |
52
|
|
|
{ |
53
|
|
|
if ($this->progressive) { |
54
|
|
|
return 'progressive-action'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return 'custom-link'; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array<string,mixed> $properties |
62
|
|
|
* @return FormField|string |
63
|
|
|
*/ |
64
|
|
|
public function FieldHolder($properties = []) |
65
|
|
|
{ |
66
|
|
|
$link = $this->link; |
67
|
|
|
|
68
|
|
|
$title = $this->getButtonTitle(); |
69
|
|
|
$classes = [$this->extraClass()]; |
70
|
|
|
if ($this->noAjax) { |
71
|
|
|
$classes[] = 'no-ajax'; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($this->buttonIcon) { |
75
|
|
|
$classes[] = "font-icon"; |
76
|
|
|
$classes[] = sprintf('font-icon-%s', $this->buttonIcon); |
77
|
|
|
$classes[] = "btn-mobile-collapse"; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$attrs = []; |
81
|
|
|
|
82
|
|
|
// note: links with target are never submitted through ajax |
83
|
|
|
if ($this->newWindow) { |
84
|
|
|
$attrs[] = 'target="_blank"'; |
85
|
|
|
} |
86
|
|
|
if ($this->confirmation) { |
87
|
|
|
/** @var string $att */ |
88
|
|
|
$att = Convert::raw2htmlatt($this->confirmation); |
89
|
|
|
$attrs[] = sprintf('data-message="%s"', $att); |
90
|
|
|
|
91
|
|
|
// With no ajax or progressive, use entwine to show confirm message |
92
|
|
|
if ($this->progressive || $this->getNoAjax()) { |
93
|
|
|
$classes[] = "confirm"; |
94
|
|
|
} else { |
95
|
|
|
$attrs[] = 'onclick="return confirm(this.dataset.message);"'; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$lastIcon = null; |
100
|
|
|
if ($this->hasLastIcon()) { |
101
|
|
|
$lastIcon = $this->renderLastIcon() . ' '; |
102
|
|
|
$classes[] = "btn-mobile-collapse"; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
foreach ($this->attributes as $attributeKey => $attributeValue) { |
106
|
|
|
$attrs[] = sprintf('%s="%s"', $attributeKey, $attributeValue); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$content = sprintf( |
110
|
|
|
'<a href="%s" class="%s" %s>%s<span class="btn__title">%s</span></a>', |
111
|
|
|
$link, |
112
|
|
|
implode(' ', $classes), |
113
|
|
|
implode(' ', $attrs), |
114
|
|
|
$lastIcon, |
115
|
|
|
$title |
116
|
|
|
); |
117
|
|
|
$this->content = $content; |
118
|
|
|
|
119
|
|
|
return parent::FieldHolder(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Hide this action as it needs to exist to be forwarded to the model, |
124
|
|
|
* but you might not want to display it in the action bar |
125
|
|
|
* |
126
|
|
|
* @return $this |
127
|
|
|
*/ |
128
|
|
|
public function setHidden() |
129
|
|
|
{ |
130
|
|
|
$this->addExtraClass("d-none"); |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get the value of noAjax |
137
|
|
|
* @return boolean |
138
|
|
|
*/ |
139
|
|
|
public function getNoAjax() |
140
|
|
|
{ |
141
|
|
|
return $this->noAjax; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Set the value of noAjax |
146
|
|
|
* |
147
|
|
|
* @param boolean $noAjax |
148
|
|
|
* @return $this |
149
|
|
|
*/ |
150
|
|
|
public function setNoAjax($noAjax) |
151
|
|
|
{ |
152
|
|
|
$this->noAjax = $noAjax; |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|