|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class SiteTreeListboxField extends ListboxField |
|
|
|
|
|
|
4
|
|
|
{ |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* |
|
8
|
|
|
* @var Int |
|
9
|
|
|
*/ |
|
10
|
|
|
protected $siteTreeParentID = 0; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* |
|
14
|
|
|
* @var Array |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $arrayOfAllowedIDs = array(); |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $classNamesForItems = array("SiteTree"); |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* |
|
26
|
|
|
* @var Array |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $siteTreeParentAllChildren = array(); |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Creates a new dropdown field. |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $name The field name |
|
34
|
|
|
* @param string $title The field title |
|
35
|
|
|
* @param array $source An map of the dropdown items |
|
36
|
|
|
* @param string|array $value You can pass an array of values or a single value like a drop down to be selected |
|
37
|
|
|
* @param int $size Optional size of the select element |
|
|
|
|
|
|
38
|
|
|
* @param form The parent form |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct($name, $title = '', $source = array(), $value = '', $size = null) |
|
41
|
|
|
{ |
|
42
|
|
|
parent::__construct($name, $title, $source, $value, $size, true); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* |
|
47
|
|
|
* @param Int $id |
|
48
|
|
|
*/ |
|
49
|
|
|
public function setSiteTreeParentID($id) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->siteTreeParentID = $id; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* |
|
56
|
|
|
* @param Int $id |
|
57
|
|
|
* @param Int | string $array |
|
58
|
|
|
*/ |
|
59
|
|
|
public function setSiteTreeParentAndChildClassNames($id, $array) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->siteTreeParentID = $id; |
|
62
|
|
|
$this->setClassNamesForItems($array); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* |
|
67
|
|
|
* @param Array | str $array |
|
68
|
|
|
*/ |
|
69
|
|
|
public function setClassNamesForItems($array) |
|
70
|
|
|
{ |
|
71
|
|
|
unset($this->classNamesForItems); |
|
72
|
|
|
if (is_array($array)) { |
|
73
|
|
|
$this->classNamesForItems = $array; |
|
74
|
|
|
} else { |
|
75
|
|
|
$this->classNamesForItems = array($array); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* |
|
81
|
|
|
* @param String $str |
|
82
|
|
|
*/ |
|
83
|
|
|
public function addClassNameForItems($str) |
|
84
|
|
|
{ |
|
85
|
|
|
$this->classNamesForItems[] = $str; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* |
|
90
|
|
|
* @param Array | SS_Map |
|
91
|
|
|
*/ |
|
92
|
|
|
public function setSource($source) |
|
93
|
|
|
{ |
|
94
|
|
|
if ($source instanceof SS_Map) { |
|
95
|
|
|
$source = $source->toArray(); |
|
96
|
|
|
} |
|
97
|
|
|
if ($source) { |
|
98
|
|
|
$hasCommas = array_filter( |
|
99
|
|
|
array_keys($source), |
|
100
|
|
|
create_function('$key', 'return strpos($key, ",") !== FALSE;') |
|
|
|
|
|
|
101
|
|
|
); |
|
102
|
|
|
if ($hasCommas) { |
|
|
|
|
|
|
103
|
|
|
throw new InvalidArgumentException('No commas allowed in $source keys'); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
parent::setSource($source); |
|
107
|
|
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* |
|
112
|
|
|
* @return Array |
|
|
|
|
|
|
113
|
|
|
*/ |
|
114
|
|
|
public function getSource() |
|
115
|
|
|
{ |
|
116
|
|
|
$source = parent::getSource(); |
|
117
|
|
|
//debug::log("original source count ".implode($this->classNamesForItems)." ".$this->siteTreeParentID.": ".count($source)); |
|
|
|
|
|
|
118
|
|
|
if ($this->siteTreeParentID) { |
|
119
|
|
|
$arrayItems = array(); |
|
|
|
|
|
|
120
|
|
|
$allChildrenForSiteTreeParent = $this->getAllChildrenForSiteTreeParent($this->siteTreeParentID); |
|
121
|
|
|
//debug::log("new source count: ".count($allChildrenForSiteTreeParent)); |
|
|
|
|
|
|
122
|
|
|
$finalSource = array(); |
|
123
|
|
|
foreach ($source as $sourceKey => $sourceValue) { |
|
124
|
|
|
if (isset($allChildrenForSiteTreeParent[$sourceKey])) { |
|
125
|
|
|
$finalSource[$sourceKey] = $sourceValue; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} else { |
|
129
|
|
|
$finalSource = $source; |
|
130
|
|
|
} |
|
131
|
|
|
//debug::log("final source count: ".count($finalSource)); |
|
|
|
|
|
|
132
|
|
|
return $finalSource; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param Int |
|
137
|
|
|
* @return Array |
|
138
|
|
|
*/ |
|
139
|
|
View Code Duplication |
private function getAllChildrenForSiteTreeParent($parentID) |
|
|
|
|
|
|
140
|
|
|
{ |
|
141
|
|
|
$children = SiteTree::get()->filter(array("ParentID" => $parentID)); |
|
142
|
|
|
if ($children && $children->count()) { |
|
143
|
|
|
foreach ($children as $child) { |
|
144
|
|
|
foreach ($this->classNamesForItems as $matchingCLassName) { |
|
145
|
|
|
//debug::log("has child"); |
|
|
|
|
|
|
146
|
|
|
if ($child instanceof $matchingCLassName) { |
|
147
|
|
|
//debug::log("we now have ".count($this->siteTreeParentAllChildren)." children"); |
|
|
|
|
|
|
148
|
|
|
$this->siteTreeParentAllChildren[$child->ID] = $child->ID; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
$this->getAllChildrenForSiteTreeParent($child->ID); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
return $this->siteTreeParentAllChildren; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.