Completed
Push — master ( efb5c5...a5da08 )
by Daniel
09:31
created

LeftAndMain_TreeNode::getClasses()   D

Complexity

Conditions 9
Paths 48

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 16
nc 48
nop 0
dl 0
loc 26
rs 4.909
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Admin;
4
5
use SilverStripe\View\SSViewer;
6
use SilverStripe\View\ViewableData;
7
8
/**
9
 * Wrapper around objects being displayed in a tree.
10
 * Caution: Volatile API.
11
 *
12
 * @todo Implement recursive tree node rendering.
13
 */
14
class LeftAndMain_TreeNode extends ViewableData
15
{
16
17
    /**
18
     * Object represented by this node
19
     *
20
     * @var Object
21
     */
22
    protected $obj;
23
24
    /**
25
     * Edit link to the current record in the CMS
26
     *
27
     * @var string
28
     */
29
    protected $link;
30
31
    /**
32
     * True if this is the currently selected node in the tree
33
     *
34
     * @var bool
35
     */
36
    protected $isCurrent;
37
38
    /**
39
     * Name of method to count the number of children
40
     *
41
     * @var string
42
     */
43
    protected $numChildrenMethod;
44
45
46
    /**
47
     *
48
     * @var LeftAndMain_SearchFilter
49
     */
50
    protected $filter;
51
52
    /**
53
     * @param Object $obj
54
     * @param string $link
55
     * @param bool $isCurrent
56
     * @param string $numChildrenMethod
57
     * @param LeftAndMain_SearchFilter $filter
58
     */
59
    public function __construct(
60
        $obj,
61
        $link = null,
62
        $isCurrent = false,
63
        $numChildrenMethod = 'numChildren',
64
        $filter = null
65
    ) {
66
        parent::__construct();
67
        $this->obj = $obj;
68
        $this->link = $link;
69
        $this->isCurrent = $isCurrent;
70
        $this->numChildrenMethod = $numChildrenMethod;
71
        $this->filter = $filter;
72
    }
73
74
    /**
75
     * Returns template, for further processing by {@link Hierarchy->getChildrenAsUL()}.
76
     * Does not include closing tag to allow this method to inject its own children.
77
     *
78
     * @todo Remove hardcoded assumptions around returning an <li>, by implementing recursive tree node rendering
79
     *
80
     * @return string
81
     */
82
    public function forTemplate()
83
    {
84
        $obj = $this->obj;
85
86
        return (string)SSViewer::execute_template(
87
            'SilverStripe\\Admin\\Includes\\LeftAndMain_TreeNode',
88
            $obj,
89
            array(
90
                'Classes' => $this->getClasses(),
91
                'Link' => $this->getLink(),
92
                'Title' => sprintf(
93
                    '(%s: %s) %s',
94
                    trim(_t('LeftAndMain.PAGETYPE', 'Page type'), " :"),
95
                    $obj->i18n_singular_name(),
96
                    $obj->Title
97
                ),
98
            )
99
        );
100
    }
101
102
    /**
103
     * Determine the CSS classes to apply to this node
104
     *
105
     * @return string
106
     */
107
    public function getClasses()
108
    {
109
        // Get classes from object
110
        $classes = $this->obj->CMSTreeClasses($this->numChildrenMethod);
111
        if ($this->isCurrent) {
112
            $classes .= ' current';
113
        }
114
        // Get status flag classes
115
        $flags = $this->obj->hasMethod('getStatusFlags')
116
            ? $this->obj->getStatusFlags()
117
            : false;
118
        if ($flags) {
119
            $statuses = array_keys($flags);
120
            foreach ($statuses as $s) {
121
                $classes .= ' status-' . $s;
122
            }
123
        }
124
        // Get additional filter classes
125
        if ($this->filter && ($filterClasses = $this->filter->getPageClasses($this->obj))) {
126
            if (is_array($filterClasses)) {
127
                $filterClasses = implode(' ' . $filterClasses);
128
            }
129
            $classes .= ' ' . $filterClasses;
130
        }
131
        return $classes ?: '';
132
    }
133
134
    public function getObj()
135
    {
136
        return $this->obj;
137
    }
138
139
    public function setObj($obj)
140
    {
141
        $this->obj = $obj;
142
        return $this;
143
    }
144
145
    public function getLink()
146
    {
147
        return $this->link;
148
    }
149
150
    public function setLink($link)
151
    {
152
        $this->link = $link;
153
        return $this;
154
    }
155
156
    public function getIsCurrent()
157
    {
158
        return $this->isCurrent;
159
    }
160
161
    public function setIsCurrent($bool)
162
    {
163
        $this->isCurrent = $bool;
164
        return $this;
165
    }
166
}
167