PageFormBuilder   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 178
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B addRedirectFieldset() 0 42 1
A getAlias() 0 4 1
A buildForm() 0 11 1
A addMainFieldset() 0 75 1
A addContentFieldset() 0 18 1
A getRedirectRoutes() 0 10 2
1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 *
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 *
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
13
namespace WellCommerce\Bundle\CmsBundle\Form\Admin;
14
15
use WellCommerce\Bundle\CoreBundle\Form\AbstractFormBuilder;
16
use WellCommerce\Component\Form\Conditions\Equals;
17
use WellCommerce\Component\Form\Elements\FormInterface;
18
19
/**
20
 * Class PageFormBuilder
21
 *
22
 * @author Adam Piotrowski <[email protected]>
23
 */
24
class PageFormBuilder extends AbstractFormBuilder
25
{
26
    public function getAlias(): string
27
    {
28
        return 'admin.page';
29
    }
30
    
31
    public function buildForm(FormInterface $form)
32
    {
33
        $this->addMainFieldset($form);
34
        $this->addContentFieldset($form);
35
        $this->addMetadataFieldset($form, $this->get('page.repository'));
36
        $this->addRedirectFieldset($form);
37
        $this->addShopsFieldset($form);
38
        
39
        $form->addFilter($this->getFilter('trim'));
40
        $form->addFilter($this->getFilter('secure'));
41
    }
42
    
43
    /**
44
     * Adds main settings fieldset to form
45
     *
46
     * @param FormInterface $form
47
     */
48
    private function addMainFieldset(FormInterface $form)
49
    {
50
        $mainData = $form->addChild($this->getElement('nested_fieldset', [
51
            'name'  => 'main_data',
52
            'label' => 'common.fieldset.general',
53
        ]));
54
        
55
        $languageData = $mainData->addChild($this->getElement('language_fieldset', [
56
            'name'        => 'translations',
57
            'label'       => 'common.fieldset.translations',
58
            'transformer' => $this->getRepositoryTransformer('translation', $this->get('page.repository')),
59
        ]));
60
        
61
        $name = $languageData->addChild($this->getElement('text_field', [
62
            'name'  => 'name',
63
            'label' => 'common.label.name',
64
            'rules' => [
65
                $this->getRule('required'),
66
            ],
67
        ]));
68
        
69
        $languageData->addChild($this->getElement('slug_field', [
70
            'name'            => 'slug',
71
            'label'           => 'common.label.slug',
72
            'name_field'      => $name,
73
            'generate_route'  => 'route.generate',
74
            'translatable_id' => $this->getRequestHelper()->getAttributesBagParam('id'),
75
            'rules'           => [
76
                $this->getRule('required'),
77
            ],
78
        ]));
79
        
80
        $mainData->addChild($this->getElement('checkbox', [
81
            'name'    => 'publish',
82
            'label'   => 'common.label.publish',
83
            'comment' => 'page.comment.publish',
84
            'default' => 1,
85
        ]));
86
        
87
        $mainData->addChild($this->getElement('text_field', [
88
            'name'  => 'hierarchy',
89
            'label' => 'common.label.hierarchy',
90
            'rules' => [
91
                $this->getRule('required'),
92
            ],
93
        ]));
94
        
95
        $mainData->addChild($this->getElement('text_field', [
96
            'name'  => 'section',
97
            'label' => 'page.label.section',
98
        ]));
99
        
100
        $mainData->addChild($this->getElement('tree', [
101
            'name'        => 'parent',
102
            'label'       => 'page.label.parent',
103
            'choosable'   => true,
104
            'selectable'  => false,
105
            'sortable'    => false,
106
            'clickable'   => false,
107
            'items'       => $this->get('page.dataset.admin')->getResult('flat_tree'),
108
            'restrict'    => $this->getRequestHelper()->getAttributesBagParam('id'),
109
            'transformer' => $this->getRepositoryTransformer('entity', $this->get('page.repository')),
110
        ]));
111
        
112
        $mainData->addChild($this->getElement('tip', [
113
            'tip' => $this->trans('page.tip.client_groups'),
114
        ]));
115
        
116
        $mainData->addChild($this->getElement('multi_select', [
117
            'name'        => 'clientGroups',
118
            'label'       => 'page.label.client_groups',
119
            'options'     => $this->get('client_group.dataset.admin')->getResult('select'),
120
            'transformer' => $this->getRepositoryTransformer('collection', $this->get('client_group.repository')),
121
        ]));
122
    }
123
    
124
    /**
125
     * Adds content editing fieldset to form
126
     *
127
     * @param FormInterface $form
128
     */
129
    private function addContentFieldset(FormInterface $form)
130
    {
131
        $contentData = $form->addChild($this->getElement('nested_fieldset', [
132
            'name'  => 'content_data',
133
            'label' => 'common.fieldset.description',
134
        ]));
135
        
136
        $languageData = $contentData->addChild($this->getElement('language_fieldset', [
137
            'name'        => 'translations',
138
            'label'       => 'common.fieldset.translations',
139
            'transformer' => $this->getRepositoryTransformer('translation', $this->get('page.repository')),
140
        ]));
141
        
142
        $languageData->addChild($this->getElement('rich_text_editor', [
143
            'name'  => 'content',
144
            'label' => 'common.label.description',
145
        ]));
146
    }
147
    
148
    private function addRedirectFieldset(FormInterface $form)
149
    {
150
        $redirectSettings = $form->addChild($this->getElement('nested_fieldset', [
151
            'name'  => 'redirect_settings',
152
            'label' => 'page.fieldset.redirect_settings',
153
        ]));
154
        
155
        $redirectType = $redirectSettings->addChild($this->getElement('select', [
156
            'name'    => 'redirectType',
157
            'label'   => 'page.label.redirect.type',
158
            'options' => [
159
                0 => $this->trans('page.label.redirect.none'),
160
                1 => $this->trans('page.label.redirect.url'),
161
                2 => $this->trans('page.label.redirect.route'),
162
            ],
163
        ]));
164
        
165
        $redirectSettings->addChild($this->getElement('text_field', [
166
            'name'         => 'redirectUrl',
167
            'label'        => 'page.label.redirect.url',
168
            'dependencies' => [
169
                $this->getDependency('show', [
170
                    'form'      => $form,
171
                    'field'     => $redirectType,
172
                    'condition' => new Equals(1),
173
                ]),
174
            ],
175
        ]));
176
        
177
        $redirectSettings->addChild($this->getElement('select', [
178
            'name'         => 'redirectRoute',
179
            'label'        => 'page.label.redirect.route',
180
            'options'      => $this->getRedirectRoutes(),
181
            'dependencies' => [
182
                $this->getDependency('show', [
183
                    'form'      => $form,
184
                    'field'     => $redirectType,
185
                    'condition' => new Equals(2),
186
                ]),
187
            ],
188
        ]));
189
    }
190
    
191
    private function getRedirectRoutes(): array
192
    {
193
        $availableRoutes = [];
194
        
195
        foreach ($this->container->getParameter('route_redirects') as $name => $redirect) {
196
            $availableRoutes[$redirect['route']] = sprintf('%s (%s)', $name, $redirect['route']);
197
        }
198
        
199
        return $availableRoutes;
200
    }
201
}
202