1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class SiteTreeCheckboxSetField extends CheckboxSetField |
|
|
|
|
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='', $form=null, $emptyString=null) |
41
|
|
|
{ |
42
|
|
|
Requirements::css('sitetreeformfields/css/SiteTreeCheckboxSetField.css'); |
43
|
|
|
parent::__construct($name, $title, $source, $value, $form, $emptyString); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* |
48
|
|
|
* @param Int $id |
49
|
|
|
*/ |
50
|
|
|
public function setSiteTreeParentID($id) |
51
|
|
|
{ |
52
|
|
|
$this->siteTreeParentID = $id; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* |
57
|
|
|
* @param Int $id |
58
|
|
|
* @param Int | string $array |
59
|
|
|
*/ |
60
|
|
|
public function setSiteTreeParentAndChildClassNames($id, $array) |
61
|
|
|
{ |
62
|
|
|
$this->siteTreeParentID = $id; |
63
|
|
|
$this->setClassNamesForItems($array); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* |
68
|
|
|
* @param Array | str $array |
69
|
|
|
*/ |
70
|
|
|
public function setClassNamesForItems($array) |
71
|
|
|
{ |
72
|
|
|
unset($this->classNamesForItems); |
73
|
|
|
if (is_array($array)) { |
74
|
|
|
$this->classNamesForItems = $array; |
75
|
|
|
} else { |
76
|
|
|
$this->classNamesForItems = array($array); |
77
|
|
|
} |
78
|
|
|
$this->source = $this->getSource(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* |
83
|
|
|
* @param String $str |
84
|
|
|
*/ |
85
|
|
|
public function addClassNameForItems($str) |
86
|
|
|
{ |
87
|
|
|
$this->classNamesForItems[] = $str; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* |
93
|
|
|
* @return Array |
|
|
|
|
94
|
|
|
*/ |
95
|
|
|
public function getSource() |
96
|
|
|
{ |
97
|
|
|
$source = parent::getSource(); |
98
|
|
|
//debug::log("original source count ".implode($this->classNamesForItems)." ".$this->siteTreeParentID.": ".count($source)); |
|
|
|
|
99
|
|
|
if ($this->siteTreeParentID) { |
100
|
|
|
$arrayItems = array(); |
|
|
|
|
101
|
|
|
$source = $this->getAllChildrenForSiteTreeParent($this->siteTreeParentID); |
102
|
|
|
} |
103
|
|
|
return $source; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param Int |
108
|
|
|
* @return Array |
109
|
|
|
*/ |
110
|
|
View Code Duplication |
private function getAllChildrenForSiteTreeParent($parentID) |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
$children = SiteTree::get()->filter(array("ParentID" => $parentID)); |
113
|
|
|
if ($children && $children->count()) { |
114
|
|
|
foreach ($children as $child) { |
115
|
|
|
foreach ($this->classNamesForItems as $matchingCLassName) { |
116
|
|
|
//debug::log("has child"); |
|
|
|
|
117
|
|
|
if ($child instanceof $matchingCLassName) { |
118
|
|
|
//debug::log("we now have ".count($this->siteTreeParentAllChildren)." children"); |
|
|
|
|
119
|
|
|
$this->siteTreeParentAllChildren[$child->ID] = $child->MenuTitle; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
$this->getAllChildrenForSiteTreeParent($child->ID); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
return $this->siteTreeParentAllChildren; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
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.