|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class JsonField combines form inputs into a key-value pair |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
class JsonField extends CompositeField |
|
|
|
|
|
|
7
|
|
|
{ |
|
8
|
|
|
public function __construct($name, $children = null) |
|
9
|
|
|
{ |
|
10
|
|
|
$this->setName($name); |
|
11
|
|
|
|
|
12
|
|
|
if ($children instanceof FieldList) { |
|
13
|
|
|
foreach ($children as $child) { |
|
14
|
|
|
$child->setName($this->getName() . "[" . $child->getName() . "]"); |
|
15
|
|
|
} |
|
16
|
|
|
} elseif (is_array($children)) { |
|
17
|
|
|
foreach ($children as $child) { |
|
18
|
|
|
$child->setName($this->getName() . "[" . $child->getName() . "]"); |
|
19
|
|
|
} |
|
20
|
|
|
} else { |
|
21
|
|
|
$children = is_array(func_get_args()) ? func_get_args() : array(); |
|
22
|
|
|
if (!empty($children)) { |
|
23
|
|
|
array_shift($children); |
|
24
|
|
|
} |
|
25
|
|
|
foreach ($children as $child) { |
|
26
|
|
|
$child->setName($this->getName() . "[" . $child->getName() . "]"); |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
parent::__construct($children); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function hasData() |
|
33
|
|
|
{ |
|
34
|
|
|
return true; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
//have to has it to overwritten CompositeField behavior |
|
38
|
|
|
public function collateDataFields(&$list, $saveableOnly = false) |
|
39
|
|
|
{ |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
function array_filter_empty_recursive($haystack) |
|
|
|
|
|
|
43
|
|
|
{ |
|
44
|
|
|
foreach ($haystack as $key => $value) { |
|
45
|
|
|
if (is_array($value)) { |
|
46
|
|
|
$haystack[$key] = $this->array_filter_empty_recursive($haystack[$key]); |
|
47
|
|
|
} |
|
48
|
|
|
if (empty($haystack[$key])) { |
|
49
|
|
|
unset($haystack[$key]); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $haystack; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function dataValue() |
|
57
|
|
|
{ |
|
58
|
|
|
if (is_array($this->value)) { |
|
59
|
|
|
$this->value = $this->array_filter_empty_recursive($this->value); |
|
60
|
|
|
if (!empty($this->value)) { |
|
61
|
|
|
return Convert::array2json($this->value); |
|
62
|
|
|
} else { |
|
63
|
|
|
return null; |
|
64
|
|
|
} |
|
65
|
|
|
} else { |
|
66
|
|
|
return parent::dataValue(); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Load a value into this CheckboxSetField |
|
72
|
|
|
*/ |
|
73
|
|
|
public function setValue($value) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->value = $value; |
|
76
|
|
|
if (is_string($value) && !empty($value)) { |
|
77
|
|
|
$value = Convert::json2array($value); |
|
78
|
|
|
} // @codeCoverageIgnoreStart |
|
79
|
|
|
elseif (!is_array($value)) { |
|
80
|
|
|
$value = array($value); |
|
81
|
|
|
} |
|
82
|
|
|
// @codeCoverageIgnoreEnd |
|
83
|
|
|
$patten = "/^" . $this->getName() . "\[(.*)\]$/"; |
|
84
|
|
|
foreach ($this->children as $c) { |
|
85
|
|
|
$title = $c->getName(); |
|
86
|
|
|
preg_match($patten, $title, $matches); |
|
87
|
|
|
if (!empty($matches[1]) && isset($value[$matches[1]])) { |
|
88
|
|
|
$c->setValue($value[$matches[1]]); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
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.