Passed
Push — master ( 746a0b...e23bd4 )
by Thomas
02:13
created

CustomLink::getNoAjax()   A

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\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);
0 ignored issues
show
Bug introduced by
It seems like $link can also be of type string; however, parameter $params of LeKoala\CmsActions\CustomLink::getModelLink() does only seem to accept array|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
            $this->link = $this->getModelLink($name, /** @scrutinizer ignore-type */ $link);
Loading history...
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