Skip to content

Commit 66ddae5

Browse files
author
helpfulrobot
committed
Converted to PSR-2
1 parent afee2ed commit 66ddae5

File tree

1 file changed

+142
-135
lines changed

1 file changed

+142
-135
lines changed

code/NestedCheckboxsetField.php

Lines changed: 142 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,143 @@
11
<?php
2-
class NestedCheckboxSetField extends CheckboxSetField {
3-
private $rootClass;
4-
5-
private $rootTitle;
6-
7-
private $childRelation;
8-
9-
private $childTitle;
10-
11-
/**
12-
* @param $rootClass string Sets the root class (which the relationships should be held within)
13-
* @return NestedCheckboxSetField This object (for chaining)
14-
*/
15-
public function setRootClass($rootClass) {
16-
$this->rootClass = $rootClass;
17-
return $this;
18-
}
19-
20-
public function setRootTitle($rootTitle) {
21-
$this->rootTitle = $rootTitle;
22-
return $this;
23-
}
24-
25-
public function setChildRelation($childRelation) {
26-
$this->childRelation = $childRelation;
27-
return $this;
28-
}
29-
30-
public function setChildTitle($childTitle) {
31-
$this->childTitle = $childTitle;
32-
return $this;
33-
}
34-
35-
public function Field($properties = array()) {
36-
Requirements::css(MODULE_NESTEDCHECKBOXSETFIELD_DIR . '/css/NestedCheckboxSetField.css');
37-
38-
$rootSourceParam = $this->rootClass;
39-
$rootTitleParam = $this->rootTitle;
40-
$childRelationParam = $this->childRelation;
41-
$childTitleParam = $this->childTitle;
42-
$source = $this->source;
43-
$values = $this->value;
44-
$items = array();
45-
46-
// Get values from the join, if available
47-
if(is_object($this->form)) {
48-
$record = $this->form->getRecord();
49-
if(!$values && $record && $record->hasMethod($this->name)) {
50-
$funcName = $this->name;
51-
$join = $record->$funcName();
52-
if($join) {
53-
foreach($join as $joinItem) {
54-
$values[] = $joinItem->ID;
55-
}
56-
}
57-
}
58-
}
59-
60-
// Source is not an array
61-
if(!is_array($source) && !is_a($source, 'SQLMap')) {
62-
if(is_array($values)) {
63-
$items = $values;
64-
} else {
65-
// Source and values are DataObject sets.
66-
if($values && is_a($values, 'SS_List')) {
67-
foreach($values as $object) {
68-
if(is_a($object, 'DataObject')) {
69-
$items[] = $object->ID;
70-
}
71-
}
72-
} elseif($values && is_string($values)) {
73-
$items = explode(',', $values);
74-
$items = str_replace('{comma}', ',', $items);
75-
}
76-
}
77-
} else {
78-
// Sometimes we pass a singluar default value thats ! an array && !SS_List
79-
if($values instanceof SS_List || is_array($values)) {
80-
$items = $values;
81-
} else {
82-
$items = explode(',', $values);
83-
$items = str_replace('{comma}', ',', $items);
84-
}
85-
}
86-
87-
$rootSources = $rootSourceParam::get()->sort("$rootTitleParam ASC");
88-
$rootOptions = array();
89-
$rootOdd = 0;
90-
91-
foreach($rootSources as $source) {
92-
// $source is an instance of $this->rootClass, which we can call $this->childRelation() on
93-
$childSources = $source->$childRelationParam()->sort("$childTitleParam ASC");
94-
$rootTitle = $source->$rootTitleParam;
95-
$childArray = array();
96-
$childOdd = 0;
97-
98-
foreach($childSources as $childSource) {
99-
$title = $childSource->$childTitleParam;
100-
$value = $childSource->ID;
101-
$itemID = $this->ID() . '_' . preg_replace('/[^a-zA-Z0-9]/', '', $value);
102-
$childOdd = ($childOdd + 1) % 2;
103-
$extraClass = $childOdd ? 'odd' : 'even';
104-
$extraClass .= ' val' . preg_replace('/[^a-zA-Z0-9\-\_]/', '_', $value);
105-
106-
$childArray[] = new ArrayData(array(
107-
'ID' => $itemID,
108-
'Class' => $extraClass,
109-
'Name' => "{$this->name}[{$value}]",
110-
'Value' => $value,
111-
'Title' => $title,
112-
'isChecked' => in_array($value, $items) || in_array($value, $this->defaultItems),
113-
'isDisabled' => $this->disabled || in_array($value, $this->disabledItems)
114-
));
115-
}
116-
117-
$rootOdd = ($rootOdd + 1) % 2;
118-
$extraClass = $rootOdd ? 'odd' : 'even';
119-
120-
$rootOptions[] = new ArrayData(array(
121-
'Title' => $rootTitle,
122-
'Class' => $extraClass,
123-
'Options' => new ArrayList($childArray)
124-
));
125-
}
126-
127-
// $rootOptions is now the complete ArrayData of options => sub-options
128-
$properties = array_merge($properties, array('Options' => new ArrayList($rootOptions)));
129-
130-
return $this->customise($properties)->renderWith($this->getTemplates());
131-
}
132-
133-
public function Type() {
134-
return 'optionset checkboxset nestedcheckboxset';
135-
}
136-
}
2+
class NestedCheckboxSetField extends CheckboxSetField
3+
{
4+
private $rootClass;
5+
6+
private $rootTitle;
7+
8+
private $childRelation;
9+
10+
private $childTitle;
11+
12+
/**
13+
* @param $rootClass string Sets the root class (which the relationships should be held within)
14+
* @return NestedCheckboxSetField This object (for chaining)
15+
*/
16+
public function setRootClass($rootClass)
17+
{
18+
$this->rootClass = $rootClass;
19+
return $this;
20+
}
21+
22+
public function setRootTitle($rootTitle)
23+
{
24+
$this->rootTitle = $rootTitle;
25+
return $this;
26+
}
27+
28+
public function setChildRelation($childRelation)
29+
{
30+
$this->childRelation = $childRelation;
31+
return $this;
32+
}
33+
34+
public function setChildTitle($childTitle)
35+
{
36+
$this->childTitle = $childTitle;
37+
return $this;
38+
}
39+
40+
public function Field($properties = array())
41+
{
42+
Requirements::css(MODULE_NESTEDCHECKBOXSETFIELD_DIR . '/css/NestedCheckboxSetField.css');
43+
44+
$rootSourceParam = $this->rootClass;
45+
$rootTitleParam = $this->rootTitle;
46+
$childRelationParam = $this->childRelation;
47+
$childTitleParam = $this->childTitle;
48+
$source = $this->source;
49+
$values = $this->value;
50+
$items = array();
51+
52+
// Get values from the join, if available
53+
if (is_object($this->form)) {
54+
$record = $this->form->getRecord();
55+
if (!$values && $record && $record->hasMethod($this->name)) {
56+
$funcName = $this->name;
57+
$join = $record->$funcName();
58+
if ($join) {
59+
foreach ($join as $joinItem) {
60+
$values[] = $joinItem->ID;
61+
}
62+
}
63+
}
64+
}
65+
66+
// Source is not an array
67+
if (!is_array($source) && !is_a($source, 'SQLMap')) {
68+
if (is_array($values)) {
69+
$items = $values;
70+
} else {
71+
// Source and values are DataObject sets.
72+
if ($values && is_a($values, 'SS_List')) {
73+
foreach ($values as $object) {
74+
if (is_a($object, 'DataObject')) {
75+
$items[] = $object->ID;
76+
}
77+
}
78+
} elseif ($values && is_string($values)) {
79+
$items = explode(',', $values);
80+
$items = str_replace('{comma}', ',', $items);
81+
}
82+
}
83+
} else {
84+
// Sometimes we pass a singluar default value thats ! an array && !SS_List
85+
if ($values instanceof SS_List || is_array($values)) {
86+
$items = $values;
87+
} else {
88+
$items = explode(',', $values);
89+
$items = str_replace('{comma}', ',', $items);
90+
}
91+
}
92+
93+
$rootSources = $rootSourceParam::get()->sort("$rootTitleParam ASC");
94+
$rootOptions = array();
95+
$rootOdd = 0;
96+
97+
foreach ($rootSources as $source) {
98+
// $source is an instance of $this->rootClass, which we can call $this->childRelation() on
99+
$childSources = $source->$childRelationParam()->sort("$childTitleParam ASC");
100+
$rootTitle = $source->$rootTitleParam;
101+
$childArray = array();
102+
$childOdd = 0;
103+
104+
foreach ($childSources as $childSource) {
105+
$title = $childSource->$childTitleParam;
106+
$value = $childSource->ID;
107+
$itemID = $this->ID() . '_' . preg_replace('/[^a-zA-Z0-9]/', '', $value);
108+
$childOdd = ($childOdd + 1) % 2;
109+
$extraClass = $childOdd ? 'odd' : 'even';
110+
$extraClass .= ' val' . preg_replace('/[^a-zA-Z0-9\-\_]/', '_', $value);
111+
112+
$childArray[] = new ArrayData(array(
113+
'ID' => $itemID,
114+
'Class' => $extraClass,
115+
'Name' => "{$this->name}[{$value}]",
116+
'Value' => $value,
117+
'Title' => $title,
118+
'isChecked' => in_array($value, $items) || in_array($value, $this->defaultItems),
119+
'isDisabled' => $this->disabled || in_array($value, $this->disabledItems)
120+
));
121+
}
122+
123+
$rootOdd = ($rootOdd + 1) % 2;
124+
$extraClass = $rootOdd ? 'odd' : 'even';
125+
126+
$rootOptions[] = new ArrayData(array(
127+
'Title' => $rootTitle,
128+
'Class' => $extraClass,
129+
'Options' => new ArrayList($childArray)
130+
));
131+
}
132+
133+
// $rootOptions is now the complete ArrayData of options => sub-options
134+
$properties = array_merge($properties, array('Options' => new ArrayList($rootOptions)));
135+
136+
return $this->customise($properties)->renderWith($this->getTemplates());
137+
}
138+
139+
public function Type()
140+
{
141+
return 'optionset checkboxset nestedcheckboxset';
142+
}
143+
}

0 commit comments

Comments
 (0)