Issues (66)

src/DefaultLink.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace LeKoala\CmsActions;
4
5
use SilverStripe\Control\Controller;
6
7
/**
8
 * Create custom links on ModelAdmin
9
 */
10
trait DefaultLink
11
{
12
    /**
13
     * @var string
14
     */
15
    protected $link;
16
17
    /**
18
     * @var boolean
19
     */
20
    protected $newWindow = false;
21
22
    /**
23
     * Build a url to call an action on current model
24
     *
25
     * Takes into account ModelAdmin current model and set some defaults parameters
26
     * to send along
27
     *
28
     * If you want to call actions on the controller (ModelAdmin), use getControllerLink
29
     *
30
     * @param string $action
31
     * @param array<mixed>|null $params
32
     * @return string
33
     */
34
    public function getModelLink($action, array $params = null)
35
    {
36
        if ($params === null) {
37
            $params = [];
38
        }
39
40
        $params = array_merge(['CustomLink' => $action], $params);
41
42
        $ctrl = Controller::curr();
43
        $request = $ctrl->getRequest();
44
        $url = $request->getURL();
45
        if (!$url) {
46
            return $this->getControllerLink($action, $params);
47
        }
48
49
        $dirParts = explode('/', $url);
50
        // replace the current action if it's not an ID
51
        if (!is_numeric(end($dirParts))) {
52
            array_pop($dirParts);
53
        }
54
55
        $dirParts[] = 'doCustomLink';
56
57
        $action = implode('/', $dirParts);
58
        if (!empty($params)) {
59
            $action .= '?' . http_build_query($params);
60
        }
61
62
        return $action;
63
    }
64
65
    /**
66
     * Build an url for the current controller and pass along some parameters
67
     *
68
     * If you want to call actions on a model, use getModelLink
69
     *
70
     * @param string $action
71
     * @param array|null $params
72
     * @return string
73
     */
74
    public function getControllerLink($action, array $params = null)
75
    {
76
        if ($params === null) {
77
            $params = [];
78
        }
79
        $ctrl = Controller::curr();
80
        $request = $ctrl->getRequest();
81
        $modelClass = $request->param('ModelClass');
82
        if ($modelClass) {
83
            $allParams = $request->allParams();
84
            $action = sprintf('%s/%s', $modelClass, $action);
85
            $params = array_merge($allParams, $params);
86
        }
87
        if (!empty($params)) {
88
            $action .= '?' . http_build_query($params);
89
        }
90
91
        return $ctrl->Link($action);
0 ignored issues
show
Are you sure the usage of $ctrl->Link($action) targeting SilverStripe\Control\RequestHandler::Link() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
92
    }
93
94
    /**
95
     * Get the value of link
96
     * @return string
97
     */
98
    public function getLink()
99
    {
100
        return $this->link;
101
    }
102
103
    /**
104
     * Set the value of link
105
     *
106
     * @param string $link
107
     * @return $this
108
     */
109
    public function setLink($link)
110
    {
111
        $this->link = $link;
112
113
        return $this;
114
    }
115
116
    /**
117
     * Get the value of newWindow
118
     * @return bool
119
     */
120
    public function getNewWindow()
121
    {
122
        return $this->newWindow;
123
    }
124
125
    /**
126
     * Set the value of newWindow
127
     *
128
     * @param bool $newWindow
129
     * @return $this
130
     */
131
    public function setNewWindow($newWindow)
132
    {
133
        $this->newWindow = $newWindow;
134
135
        return $this;
136
    }
137
}
138