Completed
Push — master ( 2f08ff...fd91a1 )
by Thomas
25s queued 12s
created

CustomLink::FieldHolder()   B

Complexity

Conditions 7
Paths 48

Size

Total Lines 36
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 1
Metric Value
cc 7
eloc 22
c 6
b 0
f 1
nc 48
nop 1
dl 0
loc 36
rs 8.6346
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
        if($this->buttonIcon) {
67
            $classes .= " font-icon";
68
            $classes .= ' font-icon-'.$this->buttonIcon;
69
        }
70
71
        $attrs = '';
72
73
        // note: links with target are never submitted through ajax
74
        if ($this->newWindow) {
75
            $attrs .= ' target="_blank"';
76
        }
77
        if ($this->confirmation) {
78
            $attrs .= ' data-message="' . Convert::raw2htmlatt($this->confirmation) . '"';
79
            if ($this->progressive) {
80
                $classes .= " confirm";
81
            } else {
82
                $attrs .= ' onclick="return confirm(this.dataset.message);"';
83
            }
84
        }
85
        foreach ($this->attributes as $attributeKey => $attributeValue) {
86
            $attrs .= ' ' . $attributeKey . '="' . $attributeValue . '"';
87
        }
88
89
        $content = '<a href="' . $link . '" class="' . $classes . '"' . $attrs . '>' . $title . '</a>';
90
        $this->content = $content;
91
        return parent::FieldHolder();
92
    }
93
94
    /**
95
     * Hide this action as it needs to exist to be forwarded to the model
96
     * but you might not want to display it in the action bar
97
     *
98
     * @return $this
99
     */
100
    public function setHidden()
101
    {
102
        $this->addExtraClass("d-none");
103
        return $this;
104
    }
105
106
    /**
107
     * Get the value of noAjax
108
     * @return boolean
109
     */
110
    public function getNoAjax()
111
    {
112
        return $this->noAjax;
113
    }
114
115
    /**
116
     * Set the value of noAjax
117
     *
118
     * @param boolean $noAjax
119
     * @return $this
120
     */
121
    public function setNoAjax($noAjax)
122
    {
123
        $this->noAjax = $noAjax;
124
        return $this;
125
    }
126
}
127