Completed
Push — 3.x ( 733455...aa9981 )
by Grégoire
02:00
created

PostAdmin::configureFormFields()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 65
rs 8.7636
c 1
b 0
f 0
cc 3
nc 1
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
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 Sonata\NewsBundle\Admin;
13
14
use Knp\Menu\ItemInterface as MenuItemInterface;
15
use Sonata\AdminBundle\Admin\AbstractAdmin;
16
use Sonata\AdminBundle\Admin\AdminInterface;
17
use Sonata\AdminBundle\Datagrid\DatagridMapper;
18
use Sonata\AdminBundle\Datagrid\ListMapper;
19
use Sonata\AdminBundle\Form\FormMapper;
20
use Sonata\AdminBundle\Form\Type\ModelAutocompleteType;
21
use Sonata\AdminBundle\Form\Type\ModelListType;
22
use Sonata\AdminBundle\Show\ShowMapper;
23
use Sonata\CoreBundle\Form\Type\DateTimePickerType;
24
use Sonata\DoctrineORMAdminBundle\Filter\CallbackFilter;
25
use Sonata\FormatterBundle\Form\Type\FormatterType;
26
use Sonata\FormatterBundle\Formatter\Pool as FormatterPool;
27
use Sonata\NewsBundle\Form\Type\CommentStatusType;
28
use Sonata\NewsBundle\Model\CommentInterface;
29
use Sonata\NewsBundle\Permalink\PermalinkInterface;
30
use Sonata\UserBundle\Model\UserManagerInterface;
31
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
32
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
33
34
class PostAdmin extends AbstractAdmin
35
{
36
    /**
37
     * @var UserManagerInterface
38
     */
39
    protected $userManager;
40
41
    /**
42
     * @var FormatterPool
43
     */
44
    protected $formatterPool;
45
46
    /**
47
     * @var PermalinkInterface
48
     */
49
    protected $permalinkGenerator;
50
51
    /**
52
     * @param UserManagerInterface $userManager
53
     */
54
    public function setUserManager($userManager)
55
    {
56
        $this->userManager = $userManager;
57
    }
58
59
    /**
60
     * @param FormatterPool $formatterPool
61
     */
62
    public function setPoolFormatter(FormatterPool $formatterPool)
63
    {
64
        $this->formatterPool = $formatterPool;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function prePersist($post)
71
    {
72
        $post->setContent($this->formatterPool->transform($post->getContentFormatter(), $post->getRawContent()));
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function preUpdate($post)
79
    {
80
        $post->setContent($this->formatterPool->transform($post->getContentFormatter(), $post->getRawContent()));
81
    }
82
83
    /**
84
     * @param PermalinkInterface $permalinkGenerator
85
     */
86
    public function setPermalinkGenerator(PermalinkInterface $permalinkGenerator)
87
    {
88
        $this->permalinkGenerator = $permalinkGenerator;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    protected function configureShowFields(ShowMapper $showMapper)
95
    {
96
        $showMapper
97
            ->add('author')
98
            ->add('enabled')
99
            ->add('title')
100
            ->add('abstract')
101
            ->add('content', null, ['safe' => true])
102
            ->add('tags')
103
        ;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    protected function configureFormFields(FormMapper $formMapper)
110
    {
111
        $isHorizontal = 'horizontal' == $this->getConfigurationPool()->getOption('form_type');
112
        $formMapper
113
            ->with('group_post', [
114
                    'class' => 'col-md-8',
115
                ])
116
                ->add('author', ModelListType::class)
117
                ->add('title')
118
                ->add('abstract', TextareaType::class, [
119
                    'attr' => ['rows' => 5],
120
                ])
121
                ->add('content', FormatterType::class, [
122
                    'event_dispatcher' => $formMapper->getFormBuilder()->getEventDispatcher(),
123
                    'format_field' => 'contentFormatter',
124
                    'source_field' => 'rawContent',
125
                    'source_field_options' => [
126
                        'horizontal_input_wrapper_class' => $isHorizontal ? 'col-lg-12' : '',
127
                        'attr' => ['class' => $isHorizontal ? 'span10 col-sm-10 col-md-10' : '', 'rows' => 20],
128
                    ],
129
                    'ckeditor_context' => 'news',
130
                    'target_field' => 'content',
131
                    'listener' => true,
132
                ])
133
            ->end()
134
            ->with('group_status', [
135
                    'class' => 'col-md-4',
136
                ])
137
                ->add('enabled', CheckboxType::class, ['required' => false])
138
                ->add('image', ModelListType::class, ['required' => false], [
139
                    'link_parameters' => [
140
                        'context' => 'news',
141
                        'hide_context' => true,
142
                    ],
143
                ])
144
145
                ->add('publicationDateStart', DateTimePickerType::class, [
146
                    'dp_side_by_side' => true,
147
                ])
148
                ->add('commentsCloseAt', DateTimePickerType::class, [
149
                    'dp_side_by_side' => true,
150
                    'required' => false,
151
                ])
152
                ->add('commentsEnabled', CheckboxType::class, [
153
                    'required' => false,
154
                ])
155
                ->add('commentsDefaultStatus', CommentStatusType::class, [
156
                    'expanded' => true,
157
                ])
158
            ->end()
159
160
            ->with('group_classification', [
161
                'class' => 'col-md-4',
162
                ])
163
                ->add('tags', ModelAutocompleteType::class, [
164
                    'property' => 'name',
165
                    'multiple' => 'true',
166
                    'required' => false,
167
                ])
168
                ->add('collection', ModelListType::class, [
169
                    'required' => false,
170
                ])
171
            ->end()
172
        ;
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178
    protected function configureListFields(ListMapper $listMapper)
179
    {
180
        $listMapper
181
            ->add('custom', 'string', [
182
                'template' => '@SonataNews/Admin/list_post_custom.html.twig',
183
                'label' => 'list.label_post',
184
                'sortable' => 'title',
185
            ])
186
            ->add('commentsEnabled', null, ['editable' => true])
187
            ->add('publicationDateStart')
188
        ;
189
    }
190
191
    /**
192
     * {@inheritdoc}
193
     */
194
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
195
    {
196
        $that = $this;
197
198
        $datagridMapper
199
            ->add('title')
200
            ->add('enabled')
201
            ->add('tags', null, ['field_options' => ['expanded' => true, 'multiple' => true]])
202
            ->add('author')
203
            ->add('with_open_comments', CallbackFilter::class, [
204
//                'callback'   => array($this, 'getWithOpenCommentFilter'),
205
                'callback' => function ($queryBuilder, $alias, $field, $data) use ($that) {
206
                    if (!is_array($data) || !$data['value']) {
207
                        return;
208
                    }
209
210
                    $queryBuilder->leftJoin(sprintf('%s.comments', $alias), 'c');
211
                    $queryBuilder->andWhere('c.status = :status');
212
                    $queryBuilder->setParameter('status', CommentInterface::STATUS_MODERATE);
213
                },
214
                'field_type' => CheckboxType::class,
215
            ])
216
        ;
217
    }
218
219
    /**
220
     * {@inheritdoc}
221
     */
222
    protected function configureTabMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null)
223
    {
224
        if (!$childAdmin && !in_array($action, ['edit'])) {
225
            return;
226
        }
227
228
        $admin = $this->isChild() ? $this->getParent() : $this;
229
230
        $id = $admin->getRequest()->get('id');
231
232
        $menu->addChild(
233
            $this->trans('sidemenu.link_edit_post'),
234
            ['uri' => $admin->generateUrl('edit', ['id' => $id])]
235
        );
236
237
        $menu->addChild(
238
            $this->trans('sidemenu.link_view_comments'),
239
            ['uri' => $admin->generateUrl('sonata.news.admin.comment.list', ['id' => $id])]
240
        );
241
242
        if ($this->hasSubject() && null !== $this->getSubject()->getId()) {
243
            $menu->addChild(
244
                'sidemenu.link_view_post',
245
                ['uri' => $admin->getRouteGenerator()->generate(
246
                    'sonata_news_view',
247
                    ['permalink' => $this->permalinkGenerator->generate($this->getSubject())]
248
                )]
249
            );
250
        }
251
    }
252
}
253