Completed
Push — master ( 6b7261...8d0e91 )
by Artem
10:22 queued 04:32
created

SponsorAdmin::preUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Stfalcon\Bundle\SponsorBundle\Admin;
4
5
use A2lix\TranslationFormBundle\Util\GedmoTranslatable;
6
use Sonata\AdminBundle\Admin\Admin;
7
use Sonata\AdminBundle\Form\FormMapper;
8
use Sonata\AdminBundle\Datagrid\ListMapper;
9
use Stfalcon\Bundle\SponsorBundle\Entity\Sponsor;
10
11
/**
12
 * SponsorAdmin Class.
13
 */
14
class SponsorAdmin extends Admin
15
{
16
    /**
17
     * @return array|void
18
     */
19
    public function getBatchActions()
20
    {
21
        $actions = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $actions is dead and can be removed.
Loading history...
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function preUpdate($object)
28
    {
29
        $this->removeNullTranslate($object);
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function prePersist($object)
36
    {
37
        $this->removeNullTranslate($object);
38
    }
39
40
    /**
41
     * @param \Sonata\AdminBundle\Datagrid\ListMapper $listMapper
42
     */
43
    protected function configureListFields(ListMapper $listMapper)
44
    {
45
        $listMapper
46
            ->addIdentifier('name', null, ['label' => 'Название'])
47
            ->add('site', null, ['label' => 'Сайт'])
48
            ->add('about', null, ['label' => 'Описание'])
49
            ->add('sortOrder', null, ['label' => 'Номер сортировки'])
50
            ->add('_action', 'actions', [
51
                'label' => 'Действие',
52
                'actions' => [
53
                    'edit' => [],
54
                    'delete' => [],
55
                ],
56
            ]);
57
    }
58
59
    /**
60
     * @param \Sonata\AdminBundle\Form\FormMapper $formMapper
61
     */
62
    protected function configureFormFields(FormMapper $formMapper)
63
    {
64
        /** @var Sponsor $subject */
65
        $subject = $this->getSubject();
66
        $localsRequiredService = $this->getConfigurationPool()->getContainer()->get('application_default.sonata.locales.required');
67
        $localOptionsAllFalse = $localsRequiredService->getLocalsRequiredArray(false);
68
        $formMapper
69
            ->with('Переводы')
70
            ->add('translations', 'a2lix_translations_gedmo', [
71
                'label' => 'Переводы',
72
                'translatable_class' => $this->getClass(),
73
                'fields' => [
74
75
                    'about' => [
76
                        'label' => 'Описание',
77
                        'locale_options' => $localOptionsAllFalse,
78
                    ],
79
                ],
80
            ])
81
            ->end()
82
            ->with('Общие')
83
                ->add('name')
84
                ->add('site', null, ['label' => 'Сайт'])
85
                ->add(
86
                    'file',
87
                    'file',
88
                    [
89
                        'label' => 'Логотип',
90
                        'required' => false,
91
                        'data_class' => 'Symfony\Component\HttpFoundation\File\File',
92
                        'property_path' => 'file',
93
                        'help' => $subject->getLogo() ? $subject->getLogo() : '',
94
                    ]
95
                )
96
                ->add('sortOrder', null, ['attr' => ['min' => 1], 'label' => 'Номер сортировки'])
97
            ->end()
98
            ->with('События')
99
                ->add(
100
                    'sponsorEvents',
101
                    'sonata_type_collection',
102
                    [
103
                        'label' => 'Спонсируемые события',
104
                        'by_reference' => false,
105
                    ],
106
                    [
107
                        'edit' => 'inline',
108
                        'inline' => 'table',
109
                    ]
110
                )
111
            ->end();
112
    }
113
114
    /**
115
     * @param GedmoTranslatable $object
116
     */
117
    private function removeNullTranslate($object)
118
    {
119
        foreach ($object->getTranslations() as $key => $translation) {
120
            if (!$translation->getContent()) {
121
                $object->getTranslations()->removeElement($translation);
122
            }
123
        }
124
    }
125
}
126