|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Amarkal\UI; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Implements a Composite UI component. |
|
7
|
|
|
*/ |
|
8
|
|
|
class Component_composite |
|
9
|
|
|
extends AbstractComponent |
|
|
|
|
|
|
10
|
|
|
implements ValueComponentInterface, |
|
|
|
|
|
|
11
|
|
|
DisableableComponentInterface, |
|
|
|
|
|
|
12
|
|
|
FilterableComponentInterface, |
|
|
|
|
|
|
13
|
|
|
ValidatableComponentInterface |
|
|
|
|
|
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* The list of child components. |
|
17
|
|
|
* |
|
18
|
|
|
* @var UI\AbstractComponent[] |
|
19
|
|
|
*/ |
|
20
|
|
|
private $components; |
|
21
|
|
|
|
|
22
|
|
|
public $component_type = 'composite'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The __set magic method is overridden here to apply value & name changes to |
|
26
|
|
|
* child components. |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __set( $name, $value ) |
|
29
|
|
|
{ |
|
30
|
|
|
parent::__set($name, $value); |
|
31
|
|
|
|
|
32
|
|
|
if( 'value' === $name ) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->set_value($value); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
if( 'name' === $name ) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->set_name($value); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Apply the value to each of the child components. |
|
45
|
|
|
* |
|
46
|
|
|
* @param array $value |
|
47
|
|
|
* @return void |
|
48
|
|
|
*/ |
|
49
|
|
|
public function set_value( array $value ) |
|
50
|
|
|
{ |
|
51
|
|
|
foreach($value as $n => $v) |
|
52
|
|
|
{ |
|
53
|
|
|
$component = $this->get_component($n); |
|
54
|
|
|
$component->value = $v; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Apply the new name to each of the child components. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $name |
|
62
|
|
|
* @return void |
|
63
|
|
|
*/ |
|
64
|
|
|
public function set_name( $name ) |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
|
|
foreach($this->components as $c) |
|
67
|
|
|
{ |
|
68
|
|
|
$c->name_template = str_replace('{{parent_name}}', $this->get_name(), $c->composite_name_template); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
|
|
public function default_model() |
|
76
|
|
|
{ |
|
77
|
|
|
return array( |
|
78
|
|
|
'name' => '', |
|
79
|
|
|
'parent_name' => '', |
|
80
|
|
|
'id' => '', |
|
81
|
|
|
'disabled' => false, |
|
82
|
|
|
'template' => null, |
|
83
|
|
|
'components' => array(), |
|
84
|
|
|
'default' => array(), |
|
85
|
|
|
'filter' => array($this, 'filter'), |
|
86
|
|
|
'validation' => array($this, 'validation'), |
|
87
|
|
|
'show' => null |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritdoc} |
|
93
|
|
|
*/ |
|
94
|
|
|
public function required_arguments() |
|
95
|
|
|
{ |
|
96
|
|
|
return array('name','components','template'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritdoc} |
|
101
|
|
|
*/ |
|
102
|
|
|
public function get_template_path() |
|
103
|
|
|
{ |
|
104
|
|
|
return __DIR__.'/template.phtml'; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Parse the template by converting the name tokens into UI components. |
|
109
|
|
|
* |
|
110
|
|
|
* @return string The parsed template |
|
111
|
|
|
*/ |
|
112
|
|
|
public function parse_template() |
|
113
|
|
|
{ |
|
114
|
|
|
$self = $this; |
|
115
|
|
|
return preg_replace_callback('/\{\{([a-zA-Z\d-_]+)\}\}/', function($a) use($self) { |
|
116
|
|
|
$component = $self->get_component($a[1]); |
|
117
|
|
|
return $component->render(); |
|
118
|
|
|
}, $this->model['template']); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* If this is the root composite component, this will return the component's |
|
123
|
|
|
* name. If this is a child composite component, this will return the |
|
124
|
|
|
* component's name as a key in the parent's array, i.e 'parent_name[child_name]' |
|
125
|
|
|
* |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
|
|
public function get_name() |
|
129
|
|
|
{ |
|
130
|
|
|
if('' !== $this->parent_name) |
|
|
|
|
|
|
131
|
|
|
{ |
|
132
|
|
|
return "{$this->parent_name}[{$this->name}]"; |
|
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
return $this->name; |
|
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function filter($v) |
|
138
|
|
|
{ |
|
139
|
|
View Code Duplication |
foreach($this->components as $component) |
|
|
|
|
|
|
140
|
|
|
{ |
|
141
|
|
|
if($component instanceof FilterableComponentInterface && |
|
142
|
|
|
\is_callable($component->filter)) |
|
143
|
|
|
{ |
|
144
|
|
|
$n = $component->name; |
|
145
|
|
|
$v[$n] = $component->filter($v[$n]); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $v; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function validation($v,&$e) |
|
153
|
|
|
{ |
|
154
|
|
View Code Duplication |
foreach($this->components as $component) |
|
|
|
|
|
|
155
|
|
|
{ |
|
156
|
|
|
if($component instanceof ValidatableComponentInterface && |
|
157
|
|
|
\is_callable($component->validation)) |
|
158
|
|
|
{ |
|
159
|
|
|
$n = $component->name; |
|
160
|
|
|
if(!$component->validation($v[$n],$e)) |
|
161
|
|
|
{ |
|
162
|
|
|
return false; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
return true; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Get a child component by name. |
|
171
|
|
|
* |
|
172
|
|
|
* @param string $name |
|
173
|
|
|
* @return UI\AbstractComponent |
|
174
|
|
|
* @throws \RuntimeException If there's no child component corresponding to the given name |
|
175
|
|
|
*/ |
|
176
|
|
|
public function get_component( $name ) |
|
177
|
|
|
{ |
|
178
|
|
|
if(!array_key_exists($name, $this->components)) |
|
179
|
|
|
{ |
|
180
|
|
|
throw new \RuntimeException("Composite sub-component not found with name $name"); |
|
181
|
|
|
} |
|
182
|
|
|
return $this->components[$name]; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Instantiate child UI components when created. |
|
187
|
|
|
*/ |
|
188
|
|
|
protected function on_created() |
|
189
|
|
|
{ |
|
190
|
|
|
foreach( $this->model['components'] as $args ) |
|
191
|
|
|
{ |
|
192
|
|
|
$this->components[$args['name']] = $this->create_component($args); |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* |
|
198
|
|
|
* @param type $args |
|
199
|
|
|
* @return type |
|
200
|
|
|
*/ |
|
201
|
|
|
private function create_component( $args ) |
|
202
|
|
|
{ |
|
203
|
|
|
$type = $args['type']; |
|
204
|
|
|
|
|
205
|
|
|
if('composite' === $type) |
|
206
|
|
|
{ |
|
207
|
|
|
$args['parent_name'] = $this->get_name(); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
$c = \Amarkal\UI\ComponentFactory::create( $type, $args ); |
|
211
|
|
|
|
|
212
|
|
|
// Apply the composite name template |
|
213
|
|
|
$c->name_template = str_replace('{{parent_name}}', $this->get_name(), $c->composite_name_template); |
|
214
|
|
|
|
|
215
|
|
|
return $c; |
|
216
|
|
|
} |
|
217
|
|
|
} |