Completed
Push — master ( bbb282...43d0b8 )
by Daniel
25s
created

LeftAndMain_TreeNode   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 145
rs 10
c 1
b 1
f 0
wmc 16
lcom 1
cbo 2

9 Methods

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