Completed
Pull Request — master (#1783)
by Damian
02:29
created

CMSTreeNode::getLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\CMS\Controllers;
4
5
use SilverStripe\Admin\LeftAndMain_SearchFilter;
6
use SilverStripe\CMS\Model\SiteTree;
7
use SilverStripe\View\SSViewer;
8
use SilverStripe\View\ViewableData;
9
10
/**
11
 * Wrapper around objects being displayed in a tree.
12
 * Caution: Volatile API.
13
 *
14
 * @todo Implement recursive tree node rendering.
15
 */
16
class CMSTreeNode extends ViewableData
17
{
18
19
    /**
20
     * Object represented by this node
21
     *
22
     * @var SiteTree
23
     */
24
    protected $obj;
25
26
    /**
27
     * Edit link to the current record in the CMS
28
     *
29
     * @var string
30
     */
31
    protected $link;
32
33
    /**
34
     * True if this is the currently selected node in the tree
35
     *
36
     * @var bool
37
     */
38
    protected $isCurrent;
39
40
    /**
41
     * Name of method to count the number of children
42
     *
43
     * @var string
44
     */
45
    protected $numChildrenMethod;
46
47
    /**
48
     *
49
     * @var LeftAndMain_SearchFilter
50
     */
51
    protected $filter;
52
53
    /**
54
     * @param Object $obj
55
     * @param string $link
56
     * @param bool $isCurrent
57
     * @param string $numChildrenMethod
58
     * @param LeftAndMain_SearchFilter $filter
59
     */
60
    public function __construct(
61
        $obj,
62
        $link = null,
63
        $isCurrent = false,
64
        $numChildrenMethod = 'numChildren',
65
        $filter = null
66
    ) {
67
        parent::__construct();
68
        $this->obj = $obj;
69
        $this->link = $link;
70
        $this->isCurrent = $isCurrent;
71
        $this->numChildrenMethod = $numChildrenMethod;
72
        $this->filter = $filter;
73
    }
74
75
    /**
76
     * Returns template, for further processing by {@link Hierarchy->getChildrenAsUL()}.
77
     * Does not include closing tag to allow this method to inject its own children.
78
     *
79
     * @todo Remove hardcoded assumptions around returning an <li>, by implementing recursive tree node rendering
80
     *
81
     * @return string
82
     */
83
    public function forTemplate()
84
    {
85
        $obj = $this->obj;
86
87
        return (string)SSViewer::execute_template(
88
            [ 'type' => 'Includes', self::class ],
0 ignored issues
show
Documentation introduced by
array('type' => 'Includes', self::class) is of type array<string|integer,string,{"type":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
89
            $obj,
90
            array(
91
                'Classes' => $this->getClasses(),
92
                'Link' => $this->getLink(),
93
                'Title' => sprintf(
94
                    '(%s: %s) %s',
95
                    trim(_t('LeftAndMain.PAGETYPE', 'Page type'), " :"),
96
                    $obj->i18n_singular_name(),
97
                    $obj->Title
98
                ),
99
            )
100
        );
101
    }
102
103
    /**
104
     * Determine the CSS classes to apply to this node
105
     *
106
     * @return string
107
     */
108
    public function getClasses()
109
    {
110
        // Get classes from object
111
        $classes = $this->obj->CMSTreeClasses($this->numChildrenMethod);
112
        if ($this->isCurrent) {
113
            $classes .= ' current';
114
        }
115
        // Get status flag classes
116
        $flags = $this->obj->hasMethod('getStatusFlags')
117
            ? $this->obj->getStatusFlags()
118
            : false;
119
        if ($flags) {
120
            $statuses = array_keys($flags);
121
            foreach ($statuses as $s) {
122
                $classes .= ' status-' . $s;
123
            }
124
        }
125
        // Get additional filter classes
126
        if ($this->filter && ($filterClasses = $this->filter->getPageClasses($this->obj))) {
127
            if (is_array($filterClasses)) {
128
                $filterClasses = implode(' ', $filterClasses);
129
            }
130
            $classes .= ' ' . $filterClasses;
131
        }
132
        return $classes ?: '';
133
    }
134
135
    /**
136
     * Get page backing this node
137
     *
138
     * @return SiteTree
139
     */
140
    public function getObj()
141
    {
142
        return $this->obj;
143
    }
144
145
    /**
146
     * Set object backing this node
147
     *
148
     * @param SiteTree $obj
149
     * @return $this
150
     */
151
    public function setObj($obj)
152
    {
153
        $this->obj = $obj;
154
        return $this;
155
    }
156
157
    /**
158
     * Get link to this node
159
     *
160
     * @return string
161
     */
162
    public function getLink()
163
    {
164
        return $this->link;
165
    }
166
167
    /**
168
     * Set link to this node
169
     *
170
     * @param string $link
171
     * @return $this
172
     */
173
    public function setLink($link)
174
    {
175
        $this->link = $link;
176
        return $this;
177
    }
178
179
    /**
180
     * Check if this is the currently selected node
181
     *
182
     * @return bool
183
     */
184
    public function getIsCurrent()
185
    {
186
        return $this->isCurrent;
187
    }
188
189
    /**
190
     * Set this node to current, or not current
191
     *
192
     * @param bool $bool
193
     * @return $this
194
     */
195
    public function setIsCurrent($bool)
196
    {
197
        $this->isCurrent = $bool;
198
        return $this;
199
    }
200
}
201