|
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
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var boolean |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $noAjax = false; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param string $name |
|
26
|
|
|
* @param string $title |
|
27
|
|
|
* @param string|array $link Will default to name of link on current record if not set |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct($name, $title = null, $link = null) |
|
30
|
|
|
{ |
|
31
|
|
|
if ($title === null) { |
|
32
|
|
|
$title = FormField::name_to_label($name); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
parent::__construct($name, ''); |
|
36
|
|
|
|
|
37
|
|
|
// Reset the title later on because we passed '' to parent |
|
38
|
|
|
$this->title = $title; |
|
39
|
|
|
|
|
40
|
|
|
if ($link && is_string($link)) { |
|
41
|
|
|
$this->link = $link; |
|
42
|
|
|
} else { |
|
43
|
|
|
$this->link = $this->getModelLink($name, $link); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function Type() |
|
48
|
|
|
{ |
|
49
|
|
|
return 'custom-link'; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function FieldHolder($properties = array()) |
|
53
|
|
|
{ |
|
54
|
|
|
$link = $this->link; |
|
55
|
|
|
|
|
56
|
|
|
$title = $this->getButtonTitle(); |
|
57
|
|
|
$classes = $this->extraClass(); |
|
58
|
|
|
if ($this->noAjax) { |
|
59
|
|
|
$classes .= ' no-ajax'; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$attrs = ''; |
|
63
|
|
|
|
|
64
|
|
|
// note: links with target are never submitted through ajax |
|
65
|
|
|
if ($this->newWindow) { |
|
66
|
|
|
$attrs .= ' target="_blank"'; |
|
67
|
|
|
} |
|
68
|
|
|
if ($this->confirmation) { |
|
69
|
|
|
$attrs .= ' data-message="' . Convert::raw2htmlatt($this->confirmation) . '"'; |
|
70
|
|
|
$attrs .= ' onclick="return confirm(this.dataset.message);"'; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$content = '<a href="' . $link . '" class="' . $classes . '"' . $attrs . '>' . $title . '</a>'; |
|
74
|
|
|
$this->content = $content; |
|
75
|
|
|
return parent::FieldHolder(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Get the value of noAjax |
|
80
|
|
|
* @return boolean |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getNoAjax() |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->noAjax; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Set the value of noAjax |
|
89
|
|
|
* |
|
90
|
|
|
* @param boolean $noAjax |
|
91
|
|
|
* @return $this |
|
92
|
|
|
*/ |
|
93
|
|
|
public function setNoAjax($noAjax) |
|
94
|
|
|
{ |
|
95
|
|
|
$this->noAjax = $noAjax; |
|
96
|
|
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|