Passed
Push — master ( cd338b...b49bd2 )
by Vladimir
02:02
created

TranslationsListener::getFieldsOptions()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file is part of A2lix projects.
5
 *
6
 * (c) David ALLIX
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Koff\Bundle\I18nFormBundle\Form\EventListener;
13
14
use Koff\Bundle\I18nFormBundle\Form\Manipulator\FormManipulatorInterface;
15
use Koff\Bundle\I18nFormBundle\Form\Type\AutoFormType;
16
use Symfony\Component\Form\FormEvent;
17
use Symfony\Component\Form\FormEvents;
18
use Symfony\Component\Form\FormInterface;
19
20
class TranslationsListener extends KoffI18nListener
21
{
22
    /** @var FormManipulatorInterface */
23
    private $formManipulator;
24
25
    /**
26
     * @param FormManipulatorInterface $formManipulator
27
     */
28
    public function __construct(FormManipulatorInterface $formManipulator)
29
    {
30
        $this->formManipulator = $formManipulator;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public static function getSubscribedEvents()
37
    {
38
        return [
39
            FormEvents::PRE_SET_DATA => 'preSetData',
40
            FormEvents::SUBMIT => 'submit',
41
        ];
42
    }
43
44
    /**
45
     * @param FormEvent $event
46
     */
47
    public function preSetData(FormEvent $event)
48
    {
49
        $form = $event->getForm();
50
        $formOptions = $form->getConfig()->getOptions();
51
52
        $fieldsOptions = $this->getFieldsOptions($form, $formOptions);
53
        $translationClass = $this->getTranslationClass($form->getParent());
54
55
        foreach ($formOptions['locales'] as $locale) {
56
            if (isset($fieldsOptions[$locale])) {
57
                $form->add($locale, AutoFormType::class, [
58
                    'data_class' => $translationClass,
59
                    'required' => in_array($locale, $formOptions['required_locales'], true),
60
                    'block_name' => ('field' === $formOptions['theming_granularity']) ? 'locale' : null,
61
                    'fields' => $fieldsOptions[$locale],
62
                    'excluded_fields' => $formOptions['excluded_fields'],
63
                ]);
64
            }
65
        }
66
    }
67
68
    /**
69
     * @param FormInterface $form
70
     *
71
     * @return string
72
     */
73
    private function getTranslationClass(FormInterface $form)
74
    {
75
        do {
76
            $translatableClass = $form->getConfig()->getDataClass();
77
        } while ((null === $translatableClass) && $form->getConfig()->getVirtual() && ($form = $form->getParent()));
78
79
        // Knp
80
        if (method_exists($translatableClass, 'getTranslationEntityClass')) {
81
            return $translatableClass::getTranslationEntityClass();
82
        }
83
84
        // Gedmo
85
        if (method_exists($translatableClass, 'getTranslationClass')) {
86
            return $translatableClass::getTranslationClass();
87
        }
88
89
        return $translatableClass.'Translation';
90
    }
91
92
    /**
93
     * @param FormInterface $form
94
     * @param array         $formOptions
95
     *
96
     * @return array
97
     */
98
    public function getFieldsOptions(FormInterface $form, array $formOptions)
99
    {
100
        $fieldsOptions = array_fill_keys($formOptions['locales'], $this->formManipulator->getFieldsConfig($form));
101
102
        foreach ($fieldsOptions as $locale => &$field) {
103
            array_walk($field, function (&$v, $k, $l) {
104
                if (array_key_exists('locale_options', $v) && array_key_exists($l, $v['locale_options'])) {
105
                    $lo = $v['locale_options'];
106
                    unset($v['locale_options']);
107
108
                    $v = $lo[$l] + $v;
109
                }
110
            }, $locale);
111
        }
112
113
        return $fieldsOptions;
114
    }
115
}
116