PostAdmin::configureTabMenu()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.8177
c 0
b 0
f 0
cc 6
nc 5
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\NewsBundle\Admin;
15
16
use Knp\Menu\ItemInterface as MenuItemInterface;
17
use Sonata\AdminBundle\Admin\AbstractAdmin;
18
use Sonata\AdminBundle\Admin\AdminInterface;
19
use Sonata\AdminBundle\Datagrid\DatagridMapper;
20
use Sonata\AdminBundle\Datagrid\ListMapper;
21
use Sonata\AdminBundle\Form\FormMapper;
22
use Sonata\AdminBundle\Form\Type\ModelAutocompleteType;
23
use Sonata\AdminBundle\Form\Type\ModelListType;
24
use Sonata\AdminBundle\Show\ShowMapper;
25
use Sonata\DoctrineORMAdminBundle\Filter\CallbackFilter;
26
use Sonata\Form\Type\DateTimePickerType;
27
use Sonata\FormatterBundle\Form\Type\FormatterType;
28
use Sonata\FormatterBundle\Formatter\Pool as FormatterPool;
29
use Sonata\NewsBundle\Form\Type\CommentStatusType;
30
use Sonata\NewsBundle\Model\CommentInterface;
31
use Sonata\NewsBundle\Permalink\PermalinkInterface;
32
use Sonata\UserBundle\Model\UserManagerInterface;
33
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
34
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
35
36
class PostAdmin extends AbstractAdmin
37
{
38
    /**
39
     * @deprecated since sonata-project/news-bundle 3.13, to be removed in 4.0.
40
     *
41
     * @var UserManagerInterface|null
42
     */
43
    protected $userManager;
44
45
    /**
46
     * @var FormatterPool
47
     */
48
    protected $formatterPool;
49
50
    /**
51
     * @var PermalinkInterface
52
     */
53
    protected $permalinkGenerator;
54
55
    /**
56
     * @deprecated since sonata-project/news-bundle 3.13, to be removed in 4.0.
57
     *
58
     * @param UserManagerInterface|null $userManager
59
     */
60
    public function setUserManager($userManager): void
61
    {
62
        $this->userManager = $userManager;
0 ignored issues
show
Deprecated Code introduced by
The property Sonata\NewsBundle\Admin\PostAdmin::$userManager has been deprecated with message: since sonata-project/news-bundle 3.13, to be removed in 4.0.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
63
    }
64
65
    public function setPoolFormatter(FormatterPool $formatterPool): void
66
    {
67
        $this->formatterPool = $formatterPool;
68
    }
69
70
    public function prePersist($post): void
71
    {
72
        $post->setContent($this->formatterPool->transform($post->getContentFormatter(), $post->getRawContent()));
73
    }
74
75
    public function preUpdate($post): void
76
    {
77
        $post->setContent($this->formatterPool->transform($post->getContentFormatter(), $post->getRawContent()));
78
    }
79
80
    public function setPermalinkGenerator(PermalinkInterface $permalinkGenerator): void
81
    {
82
        $this->permalinkGenerator = $permalinkGenerator;
83
    }
84
85
    protected function configureShowFields(ShowMapper $showMapper): void
86
    {
87
        $showMapper
88
            ->add('author')
89
            ->add('enabled')
90
            ->add('title')
91
            ->add('abstract')
92
            ->add('content', null, ['safe' => true])
93
            ->add('tags')
94
        ;
95
    }
96
97
    protected function configureFormFields(FormMapper $formMapper): void
98
    {
99
        $isHorizontal = 'horizontal' === $this->getConfigurationPool()->getOption('form_type');
100
        $formMapper
101
            ->with('group_post', [
102
                    'class' => 'col-md-8',
103
                ])
104
                ->add('author', ModelListType::class)
105
                ->add('title')
106
                ->add('abstract', TextareaType::class, [
107
                    'attr' => ['rows' => 5],
108
                ])
109
                ->add('content', FormatterType::class, [
110
                    'event_dispatcher' => $formMapper->getFormBuilder()->getEventDispatcher(),
111
                    'format_field' => 'contentFormatter',
112
                    'source_field' => 'rawContent',
113
                    'source_field_options' => [
114
                        'horizontal_input_wrapper_class' => $isHorizontal ? 'col-lg-12' : '',
115
                        'attr' => ['class' => $isHorizontal ? 'span10 col-sm-10 col-md-10' : '', 'rows' => 20],
116
                    ],
117
                    'ckeditor_context' => 'news',
118
                    'target_field' => 'content',
119
                    'listener' => true,
120
                ])
121
            ->end()
122
            ->with('group_status', [
123
                    'class' => 'col-md-4',
124
                ])
125
                ->add('enabled', CheckboxType::class, ['required' => false])
126
                ->add('image', ModelListType::class, ['required' => false], [
127
                    'link_parameters' => [
128
                        'context' => 'news',
129
                        'hide_context' => true,
130
                    ],
131
                ])
132
133
                ->add('publicationDateStart', DateTimePickerType::class, [
134
                    'dp_side_by_side' => true,
135
                ])
136
                ->add('commentsCloseAt', DateTimePickerType::class, [
137
                    'dp_side_by_side' => true,
138
                    'required' => false,
139
                ])
140
                ->add('commentsEnabled', CheckboxType::class, [
141
                    'required' => false,
142
                ])
143
                ->add('commentsDefaultStatus', CommentStatusType::class, [
144
                    'expanded' => true,
145
                ])
146
            ->end()
147
148
            ->with('group_classification', [
149
                'class' => 'col-md-4',
150
                ])
151
                ->add('tags', ModelAutocompleteType::class, [
152
                    'property' => 'name',
153
                    'multiple' => 'true',
154
                    'required' => false,
155
                ])
156
                ->add('collection', ModelListType::class, [
157
                    'required' => false,
158
                ])
159
            ->end()
160
        ;
161
    }
162
163
    protected function configureListFields(ListMapper $listMapper): void
164
    {
165
        $listMapper
166
            ->add('custom', 'string', [
167
                'template' => '@SonataNews/Admin/list_post_custom.html.twig',
168
                'label' => 'list.label_post',
169
                'sortable' => 'title',
170
            ])
171
            ->add('commentsEnabled', null, ['editable' => true])
172
            ->add('publicationDateStart')
173
        ;
174
    }
175
176
    protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
177
    {
178
        $that = $this;
179
180
        $datagridMapper
181
            ->add('title')
182
            ->add('enabled')
183
            ->add('tags', null, ['field_options' => ['expanded' => true, 'multiple' => true]])
184
            ->add('author')
185
            ->add('with_open_comments', CallbackFilter::class, [
186
//                'callback'   => array($this, 'getWithOpenCommentFilter'),
187
                'callback' => static function ($queryBuilder, $alias, $field, $data) use ($that): void {
188
                    if (!\is_array($data) || !$data['value']) {
189
                        return;
190
                    }
191
192
                    $queryBuilder->leftJoin(sprintf('%s.comments', $alias), 'c');
193
                    $queryBuilder->andWhere('c.status = :status');
194
                    $queryBuilder->setParameter('status', CommentInterface::STATUS_MODERATE);
195
                },
196
                'field_type' => CheckboxType::class,
197
            ])
198
        ;
199
    }
200
201
    protected function configureTabMenu(MenuItemInterface $menu, $action, ?AdminInterface $childAdmin = null): void
202
    {
203
        if (!$childAdmin && !\in_array($action, ['edit'], true)) {
204
            return;
205
        }
206
207
        $admin = $this->isChild() ? $this->getParent() : $this;
208
209
        $id = $admin->getRequest()->get('id');
210
211
        $menu->addChild(
212
            $this->trans('sidemenu.link_edit_post'),
213
            ['uri' => $admin->generateUrl('edit', ['id' => $id])]
214
        );
215
216
        $menu->addChild(
217
            $this->trans('sidemenu.link_view_comments'),
218
            ['uri' => $admin->generateUrl('sonata.news.admin.comment.list', ['id' => $id])]
219
        );
220
221
        if ($this->hasSubject() && null !== $this->getSubject()->getId()) {
222
            $menu->addChild(
223
                'sidemenu.link_view_post',
224
                ['uri' => $admin->getRouteGenerator()->generate(
225
                    'sonata_news_view',
226
                    ['permalink' => $this->permalinkGenerator->generate($this->getSubject())]
0 ignored issues
show
Documentation introduced by
$this->getSubject() is of type null|object, but the function expects a object<Sonata\NewsBundle\Model\PostInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
227
                )]
228
            );
229
        }
230
    }
231
}
232