Passed
Push — master ( 606222...364e96 )
by Thomas
02:59 queued 35s
created

CustomLink::setHidden()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
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 0
dl 0
loc 4
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
    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 $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);
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

44
            $this->link = $this->getModelLink($name, /** @scrutinizer ignore-type */ $link);
Loading history...
45
        }
46
    }
47
48
    public function Type()
49
    {
50
        if ($this->progressive) {
51
            return 'progressive-action';
52
        }
53
        return 'custom-link';
54
    }
55
56
    public function FieldHolder($properties = array())
57
    {
58
        $link = $this->link;
59
60
        $title = $this->getButtonTitle();
61
        $classes = $this->extraClass();
62
        if ($this->noAjax) {
63
            $classes .= ' no-ajax';
64
        }
65
66
        $attrs = '';
67
68
        // note: links with target are never submitted through ajax
69
        if ($this->newWindow) {
70
            $attrs .= ' target="_blank"';
71
        }
72
        if ($this->confirmation) {
73
            $attrs .= ' data-message="' . Convert::raw2htmlatt($this->confirmation) . '"';
74
            if ($this->progressive) {
75
                $classes .= " confirm";
76
            } else {
77
                $attrs .= ' onclick="return confirm(this.dataset.message);"';
78
            }
79
        }
80
        foreach ($this->attributes as $attributeKey => $attributeValue) {
81
            $attrs .= ' ' . $attributeKey . '="' . $attributeValue . '"';
82
        }
83
84
        $content = '<a href="' . $link . '" class="' . $classes . '"' . $attrs . '>' . $title . '</a>';
85
        $this->content = $content;
86
        return parent::FieldHolder();
87
    }
88
89
    /**
90
     * Hide this action as it needs to exist to be forwarded to the model
91
     * but you might not want to display it in the action bar
92
     *
93
     * @return $this
94
     */
95
    public function setHidden()
96
    {
97
        $this->addExtraClass("d-none");
98
        return $this;
99
    }
100
101
    /**
102
     * Get the value of noAjax
103
     * @return boolean
104
     */
105
    public function getNoAjax()
106
    {
107
        return $this->noAjax;
108
    }
109
110
    /**
111
     * Set the value of noAjax
112
     *
113
     * @param boolean $noAjax
114
     * @return $this
115
     */
116
    public function setNoAjax($noAjax)
117
    {
118
        $this->noAjax = $noAjax;
119
        return $this;
120
    }
121
}
122