Completed
Push — master ( 2f61cc...8849ee )
by Andreas
14:04
created

datamanager::display_view()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\extension;
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;
18
use Symfony\Component\Translation\Translator;
19
use Symfony\Component\Translation\Loader\XliffFileLoader;
20
use midcom\datamanager\extension\transformer\multipleTransformer;
21
use Symfony\Component\Form\Extension\Csrf\CsrfExtension;
22
use Symfony\Component\Security\Csrf\CsrfTokenManager;
23
use midcom\datamanager\storage\recreateable;
24
use midcom\datamanager\extension\type\schemaType;
25
use midcom\datamanager\extension\type\toolbarType;
26
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension;
27
use Symfony\Component\Security\Csrf\TokenStorage\SessionTokenStorage;
28
29
/**
30
 * Experimental datamanager class
31
 */
32
class datamanager
33
{
34
    private $schemadb;
35
36
    /**
37
     *
38
     * @var schema
39
     */
40
    private $schema;
41
42
    /**
43
     *
44
     * @var storage\container\container
45
     */
46
    private $storage;
47
48
    /**
49
     *
50
     * @var array
51
     */
52
    private $defaults = [];
53
54
    /**
55
     *
56
     * @var renderer
57
     */
58
    private $renderer;
59
60
    /**
61
     *
62
     * @var FormFactoryInterface
63
     */
64
    private static $factory;
65
66
    /**
67
     *
68
     * @var Form
69
     */
70
    private $form;
71
72 235
    public function __construct(schemadb $schemadb)
73
    {
74 235
        $this->schemadb = $schemadb;
75 235
    }
76
77
    /**
78
     *
79
     * @return \Symfony\Component\Form\FormFactoryInterface
80
     */
81 141
    private static function get_factory()
82
    {
83 141
        if (self::$factory === null) {
84 1
            $fb = new FormFactoryBuilder();
85
86 1
            $lang = midcom::get()->i18n->get_current_language();
87 1
            $translator = new Translator($lang);
88 1
            $translator->addLoader('xlf', new XliffFileLoader);
89
90 1
            $vb = Validation::createValidatorBuilder();
91 1
            self::add_translation_resource($translator, $vb);
92 1
            self::add_translation_resource($translator, $fb);
93
94 1
            $vb->setTranslator($translator);
95
96 1
            $session_storage = new SessionTokenStorage(midcom::get()->session);
97
98 1
            $fb->addExtension(new extension())
99 1
                ->addExtension(new CoreExtension())
100 1
                ->addExtension(new HttpFoundationExtension())
101 1
                ->addExtension(new CsrfExtension(new CsrfTokenManager(null, $session_storage), $translator))
102 1
                ->addExtension(new ValidatorExtension($vb->getValidator()));
103
104 1
            self::$factory = $fb->getFormFactory();
105
        }
106 141
        return self::$factory;
107
    }
108
109 1
    private static function add_translation_resource(Translator $translator, $object)
110
    {
111 1
        $rc = new \ReflectionClass($object);
112 1
        $path = dirname($rc->getFileName());
113 1
        $lang = $translator->getLocale();
114 1
        $translator->addResource('xlf', $path . '/Resources/translations/validators.' . $lang . '.xlf', $lang);
115 1
    }
116
117 79
    public static function from_schemadb($path)
118
    {
119 79
        return new static(schemadb::from_path($path));
120
    }
121
122
    /**
123
     *
124
     * @param array $defaults
125
     * @return \midcom\datamanager\datamanager
126
     */
127 41
    public function set_defaults(array $defaults)
128
    {
129 41
        $this->defaults = $defaults;
130 41
        return $this;
131
    }
132
133
    /**
134
     *
135
     * @param midcom_core_dbaobject $storage
136
     * @param string $schema
137
     * @return \midcom\datamanager\datamanager
138
     */
139 230
    public function set_storage(midcom_core_dbaobject $storage = null, $schemaname = null)
140
    {
141 230
        if (   $schemaname === null
142 230
            && !empty($storage->id)) {
143 71
            $schemaname = $storage->get_parameter('midcom.helper.datamanager2', 'schema_name');
0 ignored issues
show
Bug introduced by
The method get_parameter() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

143
            /** @scrutinizer ignore-call */ 
144
            $schemaname = $storage->get_parameter('midcom.helper.datamanager2', 'schema_name');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
144
        }
145
146 230
        $this->set_schema($schemaname);
147
148 230
        $defaults = array_merge($this->schema->get_defaults(), $this->defaults);
149 230
        if ($storage === null) {
150 12
            $this->storage = new storage\container\nullcontainer($this->schema, $defaults);
151
        } else {
152 218
            $this->storage = new storage\container\dbacontainer($this->schema, $storage, $defaults);
153
        }
154
155 230
        if ($this->form !== null) {
156 1
            if ($this->form->isSubmitted()) {
157
                $this->form = null;
158
            } else {
159 1
                $this->form->setData($this->storage);
160
            }
161
        }
162
163 230
        return $this;
164
    }
165
166 230
    private function set_schema($name)
167
    {
168 230
        if ($name && !$this->schemadb->has($name)) {
169
            debug_add("Given schema name {$name} was not found, reverting to default.", MIDCOM_LOG_INFO);
170
            $name = null;
171
        }
172
173 230
        $schema = ($name) ? $this->schemadb->get($name) : $this->schemadb->get_first();
174 230
        if ($this->schema !== null && $this->schema->get_name() !== $schema->get_name()) {
175
            $this->form = null;
176
        }
177 230
        $this->schema = $schema;
178 230
    }
179
180
    /**
181
     * @param string $name
182
     * @return \midcom\datamanager\schema
183
     */
184 221
    public function get_schema($name = null)
185
    {
186 221
        if ($name) {
187 1
            return $this->schemadb->get($name);
188
        }
189 220
        if ($this->schema === null) {
190 11
            $this->set_schema($name);
191
        }
192 220
        return $this->schema;
193
    }
194
195
    /**
196
     *
197
     * @return storage\container\container
198
     */
199 143
    public function get_storage()
200
    {
201 143
        if (!$this->storage) {
202 11
            $this->set_storage(null);
203
        }
204 143
        return $this->storage;
205
    }
206
207
    /**
208
     *
209
     * @return renderer
210
     */
211 89
    public function get_renderer($template = null, $skip_empty = false)
212
    {
213 89
        if ($this->renderer === null) {
214 89
            $this->renderer = new renderer(new engine);
215 89
            $this->renderer->set_l10n($this->schema->get_l10n());
216
        }
217 89
        if ($template) {
218 89
            if (is_string($template)) {
219 89
                $config = \midcom_baseclasses_components_configuration::get('midcom.datamanager', 'config');
220 89
                $templates = $config->get('templates');
221 89
                if (!array_key_exists($template, $templates)) {
222
                    throw new \midcom_error('Template ' . $template . ' not found in config');
223
                }
224 89
                $template = new $templates[$template]($this->renderer, $skip_empty);
225
            }
226 89
            $view = $this->get_form()->createView();
227 89
            $this->renderer->set_template($view, $template);
228
        }
229 89
        return $this->renderer;
230
    }
231
232
    /**
233
     *
234
     * @param string $name
235
     * @return controller
236
     */
237 103
    public function get_controller($name = null)
238
    {
239 103
        return new controller($this, $name);
240
    }
241
242
    /**
243
     *
244
     * @param string $name
245
     * @param boolean $reset
246
     * @return Form
247
     */
248 141
    public function get_form($name = null, $reset = false)
249
    {
250 141
        if ($reset) {
251
            $this->form = null;
252
        }
253 141
        if ($name == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $name of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
254 140
            $name = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_COMPONENT);
255
            // Replace the dots in the component name with underscores
256 140
            $name = midcom::get()->componentloader->path_to_prefix($name);
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type false; however, parameter $path of midcom_helper__componentloader::path_to_prefix() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

256
            $name = midcom::get()->componentloader->path_to_prefix(/** @scrutinizer ignore-type */ $name);
Loading history...
257
        }
258 141
        if (!$name) {
259
            // Fallback for componentless operation
260 3
            $name = 'midcom_helper_datamanager2';
261
        }
262
263 141
        if (   $this->form === null
264 141
            || $this->form->getName() != $name) {
265
            $config = [
266 141
                'schema' => $this->get_schema()
267
            ];
268 141
            $builder = self::get_factory()->createNamedBuilder($name, schemaType::class, null, $config);
269
270
            $config = [
271 141
                'operations' => $this->schema->get('operations'),
272 141
                'index_method' => 'noindex'
273
            ];
274 141
            $builder->add('form_toolbar', toolbarType::class, $config);
275
276 141
            $this->form = $builder->getForm()
277 141
                ->setData($this->get_storage());
278
        }
279 141
        return $this->form;
280
    }
281
282 9
    public function get_content_raw()
283
    {
284 9
        $ret = [];
285
286 9
        foreach ($this->storage as $field => $value)
287
        {
288 9
            $ret[$field] = $value->get_value();
289 9
            $config = $this->schema->get_field($field);
290 9
            if (!empty($config['type_config']['allow_multiple'])) {
291 1
                $transformer = new multipleTransformer($config);
292 9
                $ret[$field] = $transformer->transform($ret[$field]);
293
            }
294
        }
295
296 9
        return $ret;
297
    }
298
299 2
    public function get_content_csv()
300
    {
301 2
        $ret = [];
302
303 2
        $renderer = $this->get_renderer('csv');
304 2
        foreach ($renderer->get_view()->children as $name => $value) {
305 2
            if ($name == 'form_toolbar') {
306 2
                continue;
307
            }
308 2
            $ret[$name] = $renderer->widget($value);
309
        }
310
311 2
        return $ret;
312
    }
313
314 34
    public function get_content_html()
315
    {
316 34
        $ret = [];
317
318 34
        $renderer = $this->get_renderer('view');
319 34
        foreach ($renderer->get_view()->children as $name => $value) {
320 34
            if ($name == 'form_toolbar') {
321 34
                continue;
322
            }
323 34
            $ret[$name] = $renderer->widget($value);
324
        }
325 34
        return $ret;
326
    }
327
328 7
    public function display_view($skip_empty = false)
329
    {
330 7
        $renderer = $this->get_renderer('view', $skip_empty);
331 7
        echo $renderer->block($renderer->get_view(), 'form');
332 7
    }
333
334
    public function recreate()
335
    {
336
        $ret = true;
337
        foreach ($this->storage as $field) {
338
            if (   $field instanceof recreateable
339
                && !$field->recreate()) {
340
                $ret = false;
341
            }
342
        }
343
        return $ret;
344
    }
345
}
346