Issues (806)

src/midcom/datamanager/datamanager.php (4 issues)

Labels
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\FormFactoryInterface;
9
use Symfony\Component\Form\Form;
10
use midcom_core_dbaobject;
11
use midcom_core_context;
12
use midcom;
13
use midcom\datamanager\extension\transformer\multipleTransformer;
14
use midcom\datamanager\storage\recreateable;
15
use midcom\datamanager\extension\type\schemaType;
16
use midcom\datamanager\extension\type\toolbarType;
17
use midcom\datamanager\storage\container\container;
18
use Symfony\Component\Form\FormBuilderInterface;
19
20
/**
21
 * Experimental datamanager class
22
 */
23
class datamanager
24
{
25
    private schemadb $schemadb;
26
27
    private ?schema $schema = null;
28
29
    private ?container $storage = null;
30
31
    private array $defaults = [];
32
33
    private ?renderer $renderer = null;
34
35
    private ?Form $form = null;
36
37 175
    public function __construct(schemadb $schemadb)
38
    {
39 175
        $this->schemadb = $schemadb;
40
    }
41
42 152
    private function get_factory() : FormFactoryInterface
43
    {
44 152
        return midcom::get()->getContainer()->get('form.factory');
45
    }
46
47 81
    public static function from_schemadb(string $path) : self
48
    {
49 81
        return new static(schemadb::from_path($path));
50
    }
51
52 42
    public function set_defaults(array $defaults) : self
53
    {
54 42
        $this->defaults = $defaults;
55 42
        return $this;
56
    }
57
58 170
    public function set_storage(?midcom_core_dbaobject $storage = null, ?string $schemaname = null) : self
59
    {
60 170
        if (   $schemaname === null
61 170
            && !empty($storage->id)) {
62 74
            $schemaname = $storage->get_parameter('midcom.helper.datamanager2', 'schema_name');
0 ignored issues
show
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

62
            /** @scrutinizer ignore-call */ 
63
            $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...
63
        }
64
65 170
        $this->set_schema($schemaname);
66
67 170
        $defaults = array_merge($this->schema->get_defaults(), $this->defaults);
68 170
        if ($storage === null) {
69 13
            $this->storage = new storage\container\nullcontainer($this->schema, $defaults);
70
        } else {
71 157
            $this->storage = new storage\container\dbacontainer($this->schema, $storage, $defaults);
72
        }
73
74 170
        if ($this->form !== null) {
75 16
            if ($this->form->isSubmitted()) {
76 13
                $this->form = null;
77
            } else {
78 3
                $this->form->setData($this->storage);
79
            }
80
        }
81
82 170
        return $this;
83
    }
84
85 170
    private function set_schema(?string $name)
86
    {
87 170
        if ($name && !$this->schemadb->has($name)) {
88
            debug_add("Given schema name {$name} was not found, reverting to default.", MIDCOM_LOG_INFO);
89
            $name = null;
90
        }
91
92 170
        $schema = ($name) ? $this->schemadb->get($name) : $this->schemadb->get_first();
93 170
        if ($this->schema !== null && $this->schema->get_name() !== $schema->get_name()) {
94
            $this->form = null;
95
        }
96 170
        $this->schema = $schema;
97
    }
98
99 158
    public function get_schema(?string $name = null) : schema
100
    {
101 158
        if ($name) {
102 1
            return $this->schemadb->get($name);
103
        }
104 158
        if ($this->schema === null) {
105 15
            $this->set_schema($name);
106
        }
107 158
        return $this->schema;
108
    }
109
110 156
    public function get_storage() : container
111
    {
112 156
        if (!$this->storage) {
113 12
            $this->set_storage(null);
114
        }
115 156
        return $this->storage;
116
    }
117
118 148
    public function get_renderer($template = null, bool $skip_empty = false) : renderer
119
    {
120 148
        if ($this->renderer === null) {
121 148
            $this->renderer = new renderer(new engine);
122 148
            $this->renderer->set_l10n($this->get_schema()->get_l10n());
0 ignored issues
show
The method set_l10n() 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

122
            $this->renderer->/** @scrutinizer ignore-call */ 
123
                             set_l10n($this->get_schema()->get_l10n());

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...
123
        }
124 148
        if ($template) {
125 148
            if (is_string($template)) {
126 148
                $templates = $this->get_schema()->get('templates');
127 148
                if (!array_key_exists($template, $templates)) {
128
                    throw new \midcom_error('Template ' . $template . ' not found in config');
129
                }
130 148
                $template = new $templates[$template]($this->renderer, $skip_empty);
131
            }
132 148
            $view = $this->get_form()->createView();
133 148
            $this->renderer->set_template($view, $template);
134
        }
135 148
        return $this->renderer;
136
    }
137
138 106
    public function get_controller(?string $name = null) : controller
139
    {
140 106
        return new controller($this, $name);
141
    }
142
143 152
    public function get_form(?string $name = null, bool $reset = false) : Form
144
    {
145 152
        if ($reset) {
146
            $this->form = null;
147
        }
148
149 152
        if (   $this->form === null
150 152
            || ($name && $this->form->getName() != $name)) {
151 150
            $this->build_form($this->get_builder($name));
152
        }
153 152
        return $this->form;
154
    }
155
156
    /**
157
     * Get FormBuilder for manipulating the form before it gets locked by setData().
158
     * The builder instance needs to be passed back to build_form() for the modifications
159
     * to have a consistent effect.
160
     */
161 152
    public function get_builder(?string $name = null) : FormBuilderInterface
162
    {
163 152
        $config = [
164 152
            'schema' => $this->get_schema()
165 152
        ];
166 152
        $builder = $this->get_factory()->createNamedBuilder($this->get_name($name), schemaType::class, options: $config);
167 152
        $builder->add('form_toolbar', toolbarType::class, [
168 152
            'operations' => $this->schema->get('operations'),
0 ignored issues
show
The method get() 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

168
            'operations' => $this->schema->/** @scrutinizer ignore-call */ get('operations'),

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...
169 152
            'index_method' => 'noindex'
170 152
        ]);
171 152
        return $builder;
172
    }
173
174 152
    public function build_form(FormBuilderInterface $builder) : self
175
    {
176 152
        $this->form = $builder->getForm()
177 152
            ->setData($this->get_storage());
178
179 152
        return $this;
180
    }
181
182 152
    private function get_name(?string $name) : string
183
    {
184 152
        if (!$name && $name = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_COMPONENT)) {
185
            // Replace the dots in the component name with underscores
186 145
            $name = midcom::get()->componentloader->path_to_prefix($name);
187
        }
188 152
        return $name ?: 'midcom_helper_datamanager2';
189
    }
190
191 10
    public function get_content_raw() : array
192
    {
193 10
        $ret = [];
194
195 10
        foreach ($this->storage as $field => $value)
196
        {
197 10
            $ret[$field] = $value->get_value();
198 10
            $config = $this->schema->get_field($field);
0 ignored issues
show
It seems like $field can also be of type null; however, parameter $name of midcom\datamanager\schema::get_field() 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

198
            $config = $this->schema->get_field(/** @scrutinizer ignore-type */ $field);
Loading history...
199 10
            if (!empty($config['type_config']['allow_multiple'])) {
200 1
                $transformer = new multipleTransformer($config);
201 1
                $ret[$field] = $transformer->transform($ret[$field]);
202
            }
203
        }
204
205 10
        return $ret;
206
    }
207
208 4
    public function get_content_csv() : array
209
    {
210 4
        return $this->render('csv');
211
    }
212
213 40
    public function get_content_html() : array
214
    {
215 40
        return $this->render('view');
216
    }
217
218 46
    public function render(string $type) : array
219
    {
220 46
        $ret = [];
221
222 46
        $renderer = $this->get_renderer($type);
223 46
        foreach ($renderer->get_view()->children as $name => $value) {
224 46
            if ($name == 'form_toolbar') {
225 46
                continue;
226
            }
227 46
            $ret[$name] = $renderer->widget($value);
228
        }
229 46
        return $ret;
230
    }
231
232 13
    public function display_view(bool $skip_empty = false)
233
    {
234 13
        $renderer = $this->get_renderer('view', $skip_empty);
235 13
        echo $renderer->block($renderer->get_view(), 'form');
236
    }
237
238
    public function recreate() : bool
239
    {
240
        $ret = true;
241
        foreach ($this->storage as $field) {
242
            if (   $field instanceof recreateable
243
                && !$field->recreate()) {
244
                $ret = false;
245
            }
246
        }
247
        return $ret;
248
    }
249
}
250