Completed
Push — master ( d7c1bf...e848e1 )
by
unknown
03:15
created

PostAdmin::configureSideMenu()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 26
rs 8.439
cc 6
eloc 14
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
        $isHorizontal = $this->getConfigurationPool()->getOption('form_type') == 'horizontal';
104
        $formMapper
105
            ->with('group_post', array(
106
                    'class' => 'col-md-8',
107
                ))
108
                ->add('author', 'sonata_type_model_list')
109
                ->add('title')
110
                ->add('abstract', null, array('attr' => array('rows' => 5)))
111
                ->add('content', 'sonata_formatter_type', array(
112
                    'event_dispatcher' => $formMapper->getFormBuilder()->getEventDispatcher(),
113
                    'format_field' => 'contentFormatter',
114
                    'source_field' => 'rawContent',
115
                    'source_field_options' => array(
116
                        'horizontal_input_wrapper_class' => $isHorizontal ? 'col-lg-12' : '',
117
                        'attr' => array('class' => $isHorizontal ? 'span10 col-sm-10 col-md-10' : '', 'rows' => 20),
118
                    ),
119
                    'ckeditor_context' => 'news',
120
                    'target_field' => 'content',
121
                    'listener' => true,
122
                ))
123
            ->end()
124
            ->with('group_status', array(
125
                    'class' => 'col-md-4',
126
                ))
127
                ->add('enabled', null, array('required' => false))
128
                ->add('image', 'sonata_type_model_list', array('required' => false), array(
129
                    'link_parameters' => array(
130
                        'context' => 'news',
131
                        'hide_context' => true,
132
                    ),
133
                ))
134
135
                ->add('publicationDateStart', 'sonata_type_datetime_picker', array('dp_side_by_side' => true))
136
                ->add('commentsCloseAt', 'sonata_type_datetime_picker', array(
137
                    'dp_side_by_side' => true,
138
                    'required' => false,
139
                ))
140
                ->add('commentsEnabled', null, array('required' => false))
141
                ->add('commentsDefaultStatus', 'sonata_news_comment_status', array('expanded' => true))
142
            ->end()
143
144
            ->with('group_classification', array(
145
                'class' => 'col-md-4',
146
                ))
147
                ->add('tags', 'sonata_type_model_autocomplete', array(
148
                    'property' => 'name',
149
                    'multiple' => 'true',
150
                    'required' => false,
151
                ))
152
                ->add('collection', 'sonata_type_model_list', array('required' => false))
153
            ->end()
154
        ;
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    protected function configureListFields(ListMapper $listMapper)
161
    {
162
        $listMapper
163
            ->add('custom', 'string', array(
164
                'template' => 'SonataNewsBundle:Admin:list_post_custom.html.twig',
165
                'label' => 'Post',
166
                'sortable' => 'title',
167
            ))
168
            ->add('commentsEnabled', null, array('editable' => true))
169
            ->add('publicationDateStart')
170
        ;
171
    }
172
173
    /**
174
     * {@inheritdoc}
175
     */
176
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
177
    {
178
        $that = $this;
179
180
        $datagridMapper
181
            ->add('title')
182
            ->add('enabled')
183
            ->add('tags', null, array('field_options' => array('expanded' => true, 'multiple' => true)))
184
            ->add('author')
185
            ->add('with_open_comments', 'doctrine_orm_callback', array(
186
//                'callback'   => array($this, 'getWithOpenCommentFilter'),
187
                'callback' => function ($queryBuilder, $alias, $field, $data) use ($that) {
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' => 'checkbox',
197
            ))
198
        ;
199
    }
200
201
    /**
202
     * {@inheritdoc}
203
     */
204
    protected function configureTabMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null)
205
    {
206
        if (!$childAdmin && !in_array($action, array('edit'))) {
207
            return;
208
        }
209
210
        $admin = $this->isChild() ? $this->getParent() : $this;
211
212
        $id = $admin->getRequest()->get('id');
213
214
        $menu->addChild(
215
            $this->trans('sidemenu.link_edit_post'),
216
            array('uri' => $admin->generateUrl('edit', array('id' => $id)))
217
        );
218
219
        $menu->addChild(
220
            $this->trans('sidemenu.link_view_comments'),
221
            array('uri' => $admin->generateUrl('sonata.news.admin.comment.list', array('id' => $id)))
222
        );
223
224
        if ($this->hasSubject() && $this->getSubject()->getId() !== null) {
225
            $menu->addChild(
226
                'sidemenu.link_view_post',
227
                array('uri' => $admin->getRouteGenerator()->generate(
228
                    'sonata_news_view',
229
                    array('permalink' => $this->permalinkGenerator->generate($this->getSubject()))
230
                ))
231
            );
232
        }
233
    }
234
}
235