Completed
Push — 3.x-dev-kit ( 21be5a )
by
unknown
03:10
created

PostAdmin::configureSideMenu()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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