Passed
Pull Request — master (#204)
by
unknown
23:41
created

formExtension::buildView()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7.0099

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 17
nc 9
nop 3
dl 0
loc 21
ccs 16
cts 17
cp 0.9412
crap 7.0099
rs 8.8333
c 1
b 0
f 0
1
<?php
2
/**
3
 * @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de
4
 */
5
6
namespace midcom\datamanager\extension;
7
8
use Symfony\Component\Form\AbstractTypeExtension;
9
use Symfony\Component\Form\FormView;
10
use Symfony\Component\Form\FormInterface;
11
use Symfony\Component\OptionsResolver\OptionsResolver;
12
use Symfony\Component\Form\Extension\Core\Type\FormType;
13
use Symfony\Component\OptionsResolver\Options;
14
use midcom;
15
use midcom\datamanager\storage\container\dbacontainer;
16
17
/**
18
 * Experimental extension class
19
 */
20
class formExtension extends AbstractTypeExtension
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25 143
    public function configureOptions(OptionsResolver $resolver)
26
    {
27 1
        $resolver->setDefaults([
28 1
            'widget_config' => [],
29
            'type_config' => [],
30
            'dm2_type' => null,
31
            'storage' => null,
32 1
            'index_method' => 'auto',
33
            'index_merge_with_content' => true,
34
            'start_fieldset' => null,
35
            'end_fieldset' => null,
36
            'helptext' => null,
37
            'hidden' => false,
38
            'readonly' => false,
39
            'write_privilege' => null,
40
            'disabled' => function(Options $options) {
41 143
                return !empty($options['hidden']);
42 1
            }
43
        ]);
44 1
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 140
    public function buildView(FormView $view, FormInterface $form, array $options)
50
    {
51 140
        $view->vars['start_fieldset'] = $options['start_fieldset'];
52 140
        $view->vars['end_fieldset'] = $options['end_fieldset'];
53 140
        $view->vars['index_method'] = $options['index_method'];
54 140
        $view->vars['index_merge_with_content'] = $options['index_merge_with_content'];
55 140
        $view->vars['hidden'] = $options['hidden'];
56 140
        $view->vars['readonly'] = $options['readonly'];
57
58 140
        if ($options['write_privilege'] !== null) {
59 34
            if (   array_key_exists('group', $options['write_privilege'])
60 34
                && !midcom::get()->auth->is_group_member($options['write_privilege']['group'])) {
61
                $view->vars['readonly'] = true;
62
            }
63 34
            if (array_key_exists('privilege', $options['write_privilege'])) {
64 34
                $storage = $form->getParent()->getData();
65 34
                if ($storage instanceof dbacontainer) {
66 34
                    if ($storage->get_value()->id) {
67 26
                        $view->vars['readonly'] = !$storage->get_value()->can_do($options['write_privilege']['privilege']);
68
                    } else {
69 9
                        $view->vars['readonly'] = !$storage->get_value()->can_user_do($options['write_privilege']['privilege']);
70
                    }
71
                }
72
            }
73
        }
74 140
    }
75
76 1
    public static function getExtendedTypes() : iterable
77
    {
78 1
        return [FormType::class];
79
    }
80
}
81