Completed
Push — master ( f1576d...9f3efa )
by Andreas
18:20
created

formExtension::configureOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 1
dl 0
loc 17
ccs 5
cts 5
cp 1
crap 1
rs 9.7666
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\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\datamanager\storage\container\dbacontainer;
15
16
/**
17
 * Experimental extension class
18
 */
19
class formExtension extends AbstractTypeExtension
20
{
21
    /**
22
     * @var \midcom_services_auth
23
     */
24
    private $auth;
25
26 1
    public function __construct(\midcom_services_auth $auth)
27
    {
28 1
        $this->auth = $auth;
29 1
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 144
    public function configureOptions(OptionsResolver $resolver)
35
    {
36 1
        $resolver->setDefaults([
37 1
            'widget_config' => [],
38
            'type_config' => [],
39
            'dm2_type' => null,
40
            'storage' => null,
41 1
            'index_method' => 'auto',
42
            'index_merge_with_content' => true,
43
            'start_fieldset' => null,
44
            'end_fieldset' => null,
45
            'helptext' => null,
46
            'hidden' => false,
47
            'readonly' => false,
48
            'write_privilege' => null,
49
            'disabled' => function(Options $options) {
50 144
                return !empty($options['hidden']);
51 1
            }
52
        ]);
53 1
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 140
    public function buildView(FormView $view, FormInterface $form, array $options)
59
    {
60 140
        $view->vars['start_fieldset'] = $options['start_fieldset'];
61 140
        $view->vars['end_fieldset'] = $options['end_fieldset'];
62 140
        $view->vars['index_method'] = $options['index_method'];
63 140
        $view->vars['index_merge_with_content'] = $options['index_merge_with_content'];
64 140
        $view->vars['hidden'] = $options['hidden'];
65 140
        $view->vars['readonly'] = $options['readonly'];
66
67 140
        if ($options['write_privilege'] !== null) {
68 34
            if (   array_key_exists('group', $options['write_privilege'])
69 34
                && !$this->auth->is_group_member($options['write_privilege']['group'])) {
70
                $view->vars['readonly'] = true;
71
            }
72 34
            if (array_key_exists('privilege', $options['write_privilege'])) {
73 34
                $storage = $form->getParent()->getData();
74 34
                if ($storage instanceof dbacontainer) {
75 34
                    if ($storage->get_value()->id) {
76 26
                        $view->vars['readonly'] = !$storage->get_value()->can_do($options['write_privilege']['privilege']);
77
                    } else {
78 9
                        $view->vars['readonly'] = !$storage->get_value()->can_user_do($options['write_privilege']['privilege']);
79
                    }
80
                }
81
            }
82
        }
83 140
    }
84
85 1
    public static function getExtendedTypes() : iterable
86
    {
87 1
        return [FormType::class];
88
    }
89
}
90