Passed
Push — master ( 542bba...4c863a )
by Dev
13:56
created

PageAdminFormFieldsTrait   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 228
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 127
dl 0
loc 228
rs 10
c 1
b 0
f 0
wmc 26

18 Methods

Rating   Name   Duplication   Size   Complexity  
A configureFormFieldMainContent() 0 12 1
A configureFormFieldMainImage() 0 7 1
A configureFormFieldH1() 0 6 1
A getSlugHelp() 0 14 4
A configureFormFieldMetaRobots() 0 8 1
A configureFormFieldLocale() 0 6 1
A configureFormFieldMainContentIsMarkdown() 0 7 1
A configureFormFieldExcrept() 0 7 1
A configureFormFieldCreatedAt() 0 12 1
A configureFormFieldOtherProperties() 0 11 2
A configureFormFieldParentPage() 0 6 1
A configureFormFieldTranslations() 0 15 2
A configureFormFieldName() 0 7 1
A getHosts() 0 3 1
A configureFormFieldImages() 0 23 1
A configureFormFieldSlug() 0 10 3
A configureFormFieldHost() 0 11 2
A configureFormFieldTitle() 0 8 1
1
<?php
2
3
namespace PiedWeb\CMSBundle\Admin;
4
5
use Sonata\AdminBundle\Form\FormMapper;
6
use Sonata\AdminBundle\Form\Type\ModelAutocompleteType;
7
use Sonata\Form\Type\CollectionType;
8
use Sonata\Form\Type\DateTimePickerType;
9
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
10
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
11
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
12
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
13
use Symfony\Component\Form\Extension\Core\Type\TextType;
14
15
trait PageAdminFormFieldsTrait
16
{
17
    protected function configureFormFieldParentPage(FormMapper $formMapper): FormMapper
18
    {
19
        return $formMapper->add('parentPage', EntityType::class, [
20
                'class' => $this->pageClass,
21
                'label' => 'admin.page.parentPage.label',
22
                'required' => false,
23
            ]);
24
    }
25
26
    protected function configureFormFieldCreatedAt(FormMapper $formMapper): FormMapper
27
    {
28
        return $formMapper->add('createdAt', DateTimePickerType::class, [
29
            'format' => DateTimeType::HTML5_FORMAT,
30
            'dp_side_by_side' => true,
31
            'dp_use_current' => true,
32
            'dp_use_seconds' => false,
33
            'dp_collapse' => true,
34
            'dp_calendar_weeks' => false,
35
            'dp_view_mode' => 'days',
36
            'dp_min_view_mode' => 'days',
37
            'label' => 'admin.page.createdAt.label',
38
        ]);
39
    }
40
41
    protected function configureFormFieldMetaRobots(FormMapper $formMapper): FormMapper
42
    {
43
        return $formMapper->add('metaRobots', ChoiceType::class, [
44
                'choices' => [
45
                    'admin.page.metaRobots.choice.noIndex' => 'noindex',
46
                ],
47
                'label' => 'admin.page.metaRobots.label',
48
                'required' => false,
49
            ]);
50
    }
51
52
    protected function configureFormFieldName(FormMapper $formMapper): FormMapper
53
    {
54
        return $formMapper->add('name', TextType::class, [
55
                'label' => 'admin.page.name.label',
56
                'required' => false,
57
                'help_html' => true,
58
                'help' => 'admin.page.name.help',
59
            ]);
60
    }
61
62
    protected function configureFormFieldExcrept(FormMapper $formMapper): FormMapper
63
    {
64
        return $formMapper->add('excrept', TextareaType::class, [
65
                'required' => false,
66
                'label' => 'admin.page.excrept.label',
67
                'help_html' => true,
68
                'help' => 'admin.page.excrept.help',
69
            ]);
70
    }
71
72
    protected function configureFormFieldMainContent(FormMapper $formMapper): FormMapper
73
    {
74
        return $formMapper->add('mainContent', TextareaType::class, [
75
            'attr' => [
76
                'style' => 'min-height: 50vh;font-size:125%; max-width:900px',
77
                'data-editor' => 'markdown',
78
                'data-gutter' => 0,
79
            ],
80
            'required' => false,
81
            'label' => ' ',
82
            'help_html' => true,
83
            'help' => 'admin.page.mainContent.help',
84
        ]);
85
    }
86
87
    protected function configureFormFieldMainContentIsMarkdown(FormMapper $formMapper): FormMapper
88
    {
89
        return $formMapper->add('mainContentIsMarkdown', null, [
90
            'required' => false,
91
            'label' => 'admin.page.markdown.label',
92
            'help_html' => true,
93
            'help' => 'admin.page.markdown.help',
94
        ]);
95
    }
96
97
    protected function configureFormFieldOtherProperties(FormMapper $formMapper): FormMapper
98
    {
99
        return !$this->exists('otherProperties') ? $formMapper : $formMapper->add('otherProperties', null, [
0 ignored issues
show
Bug introduced by
It seems like exists() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
        return !$this->/** @scrutinizer ignore-call */ exists('otherProperties') ? $formMapper : $formMapper->add('otherProperties', null, [
Loading history...
100
            'required' => false,
101
            'attr' => [
102
                'style' => 'min-height:15vh',
103
                'data-editor' => 'yaml',
104
            ],
105
            'label' => 'admin.page.otherProperties.label',
106
            'help_html' => true,
107
            'help' => 'admin.page.otherProperties.help',
108
        ]);
109
    }
110
111
    protected function getHosts()
112
    {
113
        return array_keys($this->apps);
114
    }
115
116
    protected function configureFormFieldHost(FormMapper $formMapper): FormMapper
117
    {
118
        if (null === $this->getSubject()->getHost()) {
0 ignored issues
show
Bug introduced by
It seems like getSubject() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

118
        if (null === $this->/** @scrutinizer ignore-call */ getSubject()->getHost()) {
Loading history...
119
            $this->getSubject()->setHost($this->getHosts()[0]);
120
        }
121
122
        return $formMapper->add('host', ChoiceType::class, [
123
            'choices' => array_combine($this->getHosts(), $this->getHosts()),
124
            'required' => false,
125
            'label' => 'admin.page.host.label',
126
            'empty_data' => $this->getHosts()[0],
127
        ]);
128
    }
129
130
    protected function configureFormFieldTranslations(FormMapper $formMapper): FormMapper
131
    {
132
        return $formMapper->add('translations', ModelAutocompleteType::class, [
133
            'required' => false,
134
            'multiple' => true,
135
            'class' => $this->pageClass,
136
            'property' => 'slug',
137
            'label' => 'admin.page.translations.label',
138
            'help_html' => true,
139
            'help' => 'admin.page.translations.help',
140
            'btn_add' => false,
141
            'to_string_callback' => function ($entity) {
142
                return $entity->getLocale()
143
                    ? $entity->getLocale().' ('.$entity->getSlug().')'
144
                    : $entity->getSlug(); // switch for getLocale
145
                // todo : remove it in next release and leave only get locale
146
                // todo : add a clickable link to the other admin
147
            },
148
        ]);
149
    }
150
151
    protected function configureFormFieldTitle(FormMapper $formMapper): FormMapper
152
    {
153
        return $formMapper->add('title', TextType::class, [
154
            'label' => 'admin.page.title.label',
155
            'required' => false,
156
            'help_html' => true,
157
            'help' => 'admin.page.title.help',
158
            'attr' => ['class' => 'titleToMeasure'],
159
        ]);
160
    }
161
162
    protected function configureFormFieldH1(FormMapper $formMapper): FormMapper
163
    {
164
        return $formMapper->add('h1', TextType::class, [
165
                'required' => false,
166
                'attr' => ['class' => 'input-lg', 'placeholder' => 'admin.page.title.label'],
167
                'label' => ' ',
168
169
            ]);
170
    }
171
172
    protected function configureFormFieldMainImage(FormMapper $formMapper): FormMapper
173
    {
174
        return $formMapper->add('mainImage', \Sonata\AdminBundle\Form\Type\ModelListType::class, [
175
                'required' => false,
176
                'class' => $this->mediaClass,
177
                'label' => 'admin.page.mainImage.label',
178
                'btn_edit' => false,
179
            ]);
180
    }
181
182
    protected function getSlugHelp()
183
    {
184
        $url = !$this->getSubject() ? null : $this->pageCanonicalService->generatePathForPage($this->getSubject()->getSlug());
185
186
        return $this->getSubject() && $this->getSubject()->getSlug()
187
                ? '<span class="btn btn-link" onclick="toggleDisabled()" id="disabledLinkSlug">
188
                    <i class="fa fa-unlock"></i></span>
189
                    <script>function toggleDisabled() {
190
                        $(".slug_disabled").first().removeAttr("disabled");
191
                        $(".slug_disabled").first().focus();
192
                        $("#disabledLinkSlug").first().remove();
193
                    }</script><small>Changer le slug change l\'URL et peut créer des erreurs.</small>'
194
                    .'<br><small>URL actuelle&nbsp: <a href="'.$url.'">'.$url.'</a></small>'
195
                : 'admin.page.slug.help';
196
    }
197
    protected function configureFormFieldSlug(FormMapper $formMapper): FormMapper
198
    {
199
        return $formMapper->add('slug', TextType::class, [
200
            'required' => false,
201
            'label' => 'admin.page.slug.label',
202
            'help_html' => true,
203
            'help' => $this->getSlugHelp(),
204
            'attr' => [
205
                'class' => 'slug_disabled',
206
                ($this->getSubject() ? ($this->getSubject()->getSlug() ? 'disabled' : 't') : 't') => '',
207
            ],
208
        ]);
209
    }
210
211
    protected function configureFormFieldLocale(FormMapper $formMapper): FormMapper
212
    {
213
        return $formMapper->add('locale', TextType::class, [
214
                'label' => 'admin.page.locale.label',
215
                'help_html' => true,
216
                'help' => 'admin.page.locale.help',
217
            ]);
218
    }
219
220
    protected function configureFormFieldImages(FormMapper $formMapper): FormMapper
221
    {
222
        return $formMapper->add(
223
                'pageHasMedias',
224
                CollectionType::class,
225
                [
226
                    'by_reference' => false,
227
                    'required' => false,
228
                    'label' => ' ',
229
                    'type_options' => [
230
                        'delete' => true,
231
                    ],
232
                ],
233
                [
234
                    'allow_add' => false,
235
                    'allow_delete' => true,
236
                    'btn_add' => false,
237
                    'btn_catalogue' => false,
238
                    'edit' => 'inline',
239
                    'inline' => 'table',
240
                    'sortable' => 'position',
241
                    //'link_parameters' => ['context' => $context],
242
                    'admin_code' => 'piedweb.admin.pagehasmedia',
243
                ]
244
            );
245
    }
246
}
247