1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace midcom\datamanager; |
7
|
|
|
|
8
|
|
|
use Symfony\Component\Form\FormFactoryBuilder; |
9
|
|
|
use Symfony\Component\Form\Extension\Core\CoreExtension; |
10
|
|
|
use midcom\datamanager\extension\schemaextension; |
11
|
|
|
use Symfony\Component\Form\Extension\Validator\ValidatorExtension; |
12
|
|
|
use Symfony\Component\Validator\Validation; |
13
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
14
|
|
|
use Symfony\Component\Form\Form; |
15
|
|
|
use midcom_core_dbaobject; |
16
|
|
|
use midcom_core_context; |
17
|
|
|
use midcom_helper_misc; |
18
|
|
|
use midcom; |
19
|
|
|
use Symfony\Component\Translation\Translator; |
20
|
|
|
use Symfony\Component\Translation\Loader\XliffFileLoader; |
21
|
|
|
use midcom\datamanager\extension\compat; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Experimental datamanager class |
25
|
|
|
*/ |
26
|
|
|
class datamanager |
27
|
|
|
{ |
28
|
|
|
private $schemadb; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
* @var schema |
33
|
|
|
*/ |
34
|
|
|
private $schema; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
* @var storage\container\container |
39
|
|
|
*/ |
40
|
|
|
private $storage; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
private $defaults = array(); |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* |
50
|
|
|
* @var renderer |
51
|
|
|
*/ |
52
|
|
|
private $renderer; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* |
56
|
|
|
* @var FormFactoryInterface |
57
|
|
|
*/ |
58
|
|
|
private static $factory; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* |
62
|
|
|
* @var Form |
63
|
|
|
*/ |
64
|
|
|
private $form; |
65
|
|
|
|
66
|
1 |
|
public function __construct(schemadb $schemadb) |
67
|
|
|
{ |
68
|
1 |
|
$this->schemadb = $schemadb; |
69
|
1 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* |
73
|
|
|
* @return \Symfony\Component\Form\FormFactoryInterface |
74
|
|
|
*/ |
75
|
1 |
|
private static function get_factory() |
76
|
|
|
{ |
77
|
1 |
|
if (static::$factory === null) { |
|
|
|
|
78
|
1 |
|
$fb = new FormFactoryBuilder(); |
79
|
1 |
|
$fb->addExtension(new schemaextension()); |
80
|
1 |
|
$fb->addExtension(new CoreExtension()); |
81
|
|
|
|
82
|
1 |
|
$vb = Validation::createValidatorBuilder(); |
83
|
1 |
|
$lang = midcom::get()->i18n->get_current_language(); |
84
|
1 |
|
$translator = new Translator($lang); |
85
|
1 |
|
$translator->addLoader('xlf', new XliffFileLoader); |
86
|
1 |
|
$rc = new \ReflectionClass($vb); |
87
|
1 |
|
$path = dirname($rc->getFileName()); |
88
|
1 |
|
$translator->addResource('xlf', $path . '/Resources/translations/validators.' . $lang . '.xlf', $lang); |
89
|
1 |
|
$vb->setTranslator($translator); |
90
|
1 |
|
$fb->addExtension(new ValidatorExtension($vb->getValidator())); |
91
|
|
|
|
92
|
1 |
|
self::$factory = $fb->getFormFactory(); |
93
|
|
|
} |
94
|
1 |
|
return self::$factory; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public static function from_schemadb($path) |
98
|
|
|
{ |
99
|
|
|
$data = midcom_helper_misc::get_snippet_content($path); |
100
|
|
|
$data = midcom_helper_misc::parse_config($data); |
101
|
|
|
$schemadb = new schemadb; |
102
|
|
|
foreach ($data as $name => $config) { |
103
|
|
|
$schemadb->add($name, new schema($config)); |
104
|
|
|
} |
105
|
|
|
return new static($schemadb); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* |
110
|
|
|
* @param array $defaults |
111
|
|
|
* @return \midcom\datamanager\datamanager |
112
|
|
|
*/ |
113
|
|
|
public function set_defaults(array $defaults) |
114
|
|
|
{ |
115
|
|
|
$this->defaults = $defaults; |
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* |
121
|
|
|
* @param midcom_core_dbaobject $storage |
122
|
|
|
* @param string $schema |
123
|
|
|
* @return \midcom\datamanager\datamanager |
124
|
|
|
*/ |
125
|
1 |
|
public function set_storage(midcom_core_dbaobject $storage = null, $schema = null) |
126
|
|
|
{ |
127
|
1 |
|
if ( $schema === null |
128
|
1 |
|
&& !empty($storage->id)) { |
129
|
|
|
$schema = $storage->get_parameter('midcom.helper.datamanager2', 'schema_name'); |
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
$this->schema = ($schema) ? $this->schemadb->get($schema) : $this->schemadb->get_first(); |
133
|
|
|
|
134
|
1 |
|
if ($storage === null) { |
135
|
1 |
|
$this->storage = new storage\container\nullcontainer($this->schema, $this->defaults); |
136
|
|
|
} else { |
137
|
|
|
$this->storage = new storage\container\dbacontainer($this->schema, $storage, $this->defaults); |
138
|
|
|
} |
139
|
1 |
|
$this->form = null; |
140
|
|
|
|
141
|
1 |
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* |
146
|
|
|
* @return storage\container\container |
147
|
|
|
*/ |
148
|
1 |
|
public function get_storage() |
149
|
|
|
{ |
150
|
1 |
|
if (!$this->storage) { |
151
|
1 |
|
$this->set_storage(null); |
152
|
|
|
} |
153
|
1 |
|
return $this->storage; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* |
158
|
|
|
* @return renderer |
159
|
|
|
*/ |
160
|
1 |
|
public function get_renderer() |
161
|
|
|
{ |
162
|
1 |
|
if ($this->renderer === null) { |
163
|
1 |
|
$this->renderer = new renderer(new engine); |
164
|
1 |
|
$this->renderer->set_l10n($this->schema->get_l10n()); |
165
|
|
|
} |
166
|
1 |
|
return $this->renderer; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* |
171
|
|
|
* @param string $name |
172
|
|
|
* @return controller |
173
|
|
|
*/ |
174
|
1 |
|
public function get_controller($name = null) |
175
|
|
|
{ |
176
|
1 |
|
return new controller($this->get_form($name), $this->get_storage(), $this->get_renderer()); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* |
181
|
|
|
* @param string $name |
182
|
|
|
* @return Form |
183
|
|
|
*/ |
184
|
1 |
|
public function get_form($name = null) |
185
|
|
|
{ |
186
|
1 |
|
if ($name == null) { |
|
|
|
|
187
|
|
|
$name = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_COMPONENT); |
188
|
|
|
// Replace the dots in the component name with underscores |
189
|
|
|
$name = midcom::get()->componentloader->path_to_prefix($name); |
190
|
|
|
} |
191
|
1 |
|
if (! $name) { |
192
|
|
|
// Fallback for componentless operation |
193
|
|
|
$name = 'midcom_helper_datamanager2'; |
194
|
|
|
} |
195
|
|
|
|
196
|
1 |
|
if ( $this->form === null |
197
|
1 |
|
|| $this->form->getName() != $name) { |
198
|
1 |
|
$this->get_storage(); |
199
|
1 |
|
$builder = self::get_factory()->createNamedBuilder($name, compat::get_type_name('form'), $this->storage); |
200
|
1 |
|
$this->form = $this->schema->build_form($builder); |
201
|
|
|
} |
202
|
1 |
|
return $this->form; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function get_content_html() |
206
|
|
|
{ |
207
|
|
|
$ret = array(); |
208
|
|
|
|
209
|
|
|
$view = $this->get_form()->createView(); |
210
|
|
|
|
211
|
|
|
$renderer = $this->get_renderer(); |
212
|
|
|
$renderer->set_template($view, new template\view($renderer)); |
213
|
|
|
|
214
|
|
|
foreach ($view->children as $name => $value) { |
215
|
|
|
if ($name == 'form_toolbar') { |
216
|
|
|
continue; |
217
|
|
|
} |
218
|
|
|
$ret[$name] = $renderer->widget($value); |
219
|
|
|
} |
220
|
|
|
return $ret; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function get_schemadb() |
224
|
|
|
{ |
225
|
|
|
return $this->schemadb; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: