Completed
Push — master ( 9410bd...c3a706 )
by Andreas
36:52 queued 12:30
created

datamanager::get_controller()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
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\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 midcom\datamanager\storage\container\dbacontainer;
19
use Symfony\Component\Form\FormBuilderInterface;
20
21
/**
22
 * Experimental datamanager class
23
 */
24
class datamanager
25
{
26
    private $schemadb;
27
28
    /**
29
     * @var schema
30
     */
31
    private $schema;
32
33
    /**
34
     * @var storage\container\container
35
     */
36
    private $storage;
37
38
    /**
39
     * @var array
40
     */
41
    private $defaults = [];
42
43
    /**
44
     * @var renderer
45
     */
46
    private $renderer;
47
48
    /**
49
     * @var Form
50
     */
51
    private $form;
52
53 238
    public function __construct(schemadb $schemadb)
54
    {
55 238
        $this->schemadb = $schemadb;
56 238
    }
57
58 144
    private static function get_factory() : FormFactoryInterface
59
    {
60 144
        return midcom::get()->getContainer()->get('form.factory');
61
    }
62
63 79
    public static function from_schemadb($path) : self
64
    {
65 79
        return new static(schemadb::from_path($path));
66
    }
67
68 40
    public function set_defaults(array $defaults) : self
69
    {
70 40
        $this->defaults = $defaults;
71 40
        return $this;
72
    }
73
74
    /**
75
     * @param midcom_core_dbaobject $storage
76
     * @param string $schema
77
     */
78 233
    public function set_storage(midcom_core_dbaobject $storage = null, $schemaname = null) : self
79
    {
80 233
        if (   $schemaname === null
81 233
            && !empty($storage->id)) {
82 72
            $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

82
            /** @scrutinizer ignore-call */ 
83
            $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...
83
        }
84
85 233
        $this->set_schema($schemaname);
86
87 233
        $defaults = array_merge($this->schema->get_defaults(), $this->defaults);
88 233
        if ($storage === null) {
89 13
            $this->storage = new storage\container\nullcontainer($this->schema, $defaults);
90
        } else {
91 220
            $this->storage = new storage\container\dbacontainer($this->schema, $storage, $defaults);
92
        }
93
94 233
        if ($this->form !== null) {
95 3
            if ($this->form->isSubmitted()) {
96 2
                $this->form = null;
97
            } else {
98 1
                $this->form->setData($this->storage);
99
            }
100
        }
101
102 233
        return $this;
103
    }
104
105 233
    private function set_schema($name)
106
    {
107 233
        if ($name && !$this->schemadb->has($name)) {
108
            debug_add("Given schema name {$name} was not found, reverting to default.", MIDCOM_LOG_INFO);
109
            $name = null;
110
        }
111
112 233
        $schema = ($name) ? $this->schemadb->get($name) : $this->schemadb->get_first();
113 233
        if ($this->schema !== null && $this->schema->get_name() !== $schema->get_name()) {
114
            $this->form = null;
115
        }
116 233
        $this->schema = $schema;
117 233
    }
118
119
    /**
120
     * @param string $name
121
     */
122 225
    public function get_schema($name = null) : schema
123
    {
124 225
        if ($name) {
125 1
            return $this->schemadb->get($name);
126
        }
127 225
        if ($this->schema === null) {
128 15
            $this->set_schema($name);
129
        }
130 225
        return $this->schema;
131
    }
132
133 146
    public function get_storage() : container
134
    {
135 146
        if (!$this->storage) {
136 12
            $this->set_storage(null);
137
        }
138 146
        return $this->storage;
139
    }
140
141 140
    public function get_renderer($template = null, $skip_empty = false) : renderer
142
    {
143 140
        if ($this->renderer === null) {
144 140
            $this->renderer = new renderer(new engine);
145 140
            $this->renderer->set_l10n($this->schema->get_l10n());
146
        }
147 140
        if ($template) {
148 140
            if (is_string($template)) {
149 140
                $config = \midcom_baseclasses_components_configuration::get('midcom.datamanager', 'config');
150 140
                $templates = $config->get('templates');
151 140
                if (!array_key_exists($template, $templates)) {
152
                    throw new \midcom_error('Template ' . $template . ' not found in config');
153
                }
154 140
                $template = new $templates[$template]($this->renderer, $skip_empty);
155
            }
156 140
            $view = $this->get_form()->createView();
157 140
            $this->renderer->set_template($view, $template);
158
        }
159 140
        return $this->renderer;
160
    }
161
162
    /**
163
     * @param string $name
164
     */
165 104
    public function get_controller($name = null) : controller
166
    {
167 104
        return new controller($this, $name);
168
    }
169
170
    /**
171
     * @param string $name
172
     * @param boolean $reset
173
     */
174 144
    public function get_form($name = null, $reset = false) : Form
175
    {
176 144
        if ($reset) {
177
            $this->form = null;
178
        }
179 144
        $name = $this->get_name($name);
180
181 144
        if (   $this->form === null
182 144
            || $this->form->getName() != $name) {
183 141
            $this->build_form($this->get_builder($name));
184
        }
185 144
        return $this->form;
186
    }
187
188 144
    public function get_builder(string $name = null) : FormBuilderInterface
189
    {
190
        $config = [
191 144
            'schema' => $this->get_schema()
192
        ];
193 144
        return self::get_factory()->createNamedBuilder($this->get_name($name), schemaType::class, null, $config);
194
    }
195
196 144
    public function build_form(FormBuilderInterface $builder) : self
197
    {
198 144
        $storage = $this->get_storage();
199
200
        $config = [
201 144
            'operations' => $this->schema->get('operations'),
202 144
            'index_method' => 'noindex',
203 144
            'is_create' => $storage instanceof dbacontainer && empty($storage->get_value()->id)
204
        ];
205 144
        $builder->add('form_toolbar', toolbarType::class, $config);
206
207 144
        $this->form = $builder->getForm()
208 144
            ->setData($storage);
209
210 144
        return $this;
211
    }
212
213 144
    private function get_name(?string $name) : string
214
    {
215 144
        if (!$name) {
216 142
            $name = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_COMPONENT);
217
            // Replace the dots in the component name with underscores
218 142
            $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

218
            $name = midcom::get()->componentloader->path_to_prefix(/** @scrutinizer ignore-type */ $name);
Loading history...
219
        }
220 144
        return $name ?: 'midcom_helper_datamanager2';
221
    }
222
223 10
    public function get_content_raw() : array
224
    {
225 10
        $ret = [];
226
227 10
        foreach ($this->storage as $field => $value)
228
        {
229 10
            $ret[$field] = $value->get_value();
230 10
            $config = $this->schema->get_field($field);
231 10
            if (!empty($config['type_config']['allow_multiple'])) {
232 1
                $transformer = new multipleTransformer($config);
233 1
                $ret[$field] = $transformer->transform($ret[$field]);
234
            }
235
        }
236
237 10
        return $ret;
238
    }
239
240 2
    public function get_content_csv() : array
241
    {
242 2
        $ret = [];
243
244 2
        $renderer = $this->get_renderer('csv');
245 2
        foreach ($renderer->get_view()->children as $name => $value) {
246 2
            if ($name == 'form_toolbar') {
247 2
                continue;
248
            }
249 2
            $ret[$name] = $renderer->widget($value);
250
        }
251
252 2
        return $ret;
253
    }
254
255 34
    public function get_content_html() : array
256
    {
257 34
        $ret = [];
258
259 34
        $renderer = $this->get_renderer('view');
260 34
        foreach ($renderer->get_view()->children as $name => $value) {
261 34
            if ($name == 'form_toolbar') {
262 34
                continue;
263
            }
264 34
            $ret[$name] = $renderer->widget($value);
265
        }
266 34
        return $ret;
267
    }
268
269 11
    public function display_view($skip_empty = false)
270
    {
271 11
        $renderer = $this->get_renderer('view', $skip_empty);
272 11
        echo $renderer->block($renderer->get_view(), 'form');
273 11
    }
274
275
    public function recreate() : bool
276
    {
277
        $ret = true;
278
        foreach ($this->storage as $field) {
279
            if (   $field instanceof recreateable
280
                && !$field->recreate()) {
281
                $ret = false;
282
            }
283
        }
284
        return $ret;
285
    }
286
}
287