PublicationAdmin::preUpdate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 6
rs 9.4286
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace Fenrizbes\PublicationsBundle\Admin;
4
5
use Fenrizbes\PublicationsBundle\Model\Publication;
6
use Fenrizbes\PublicationsBundle\Model\PublicationQuery;
7
use Fenrizbes\PublicationsBundle\Model\PublicationType;
8
use Fenrizbes\PublicationsBundle\Model\PublicationTypeQuery;
9
use Knp\Menu\MenuItem;
10
use Propel\PropelBundle\Validator\Constraints\UniqueObject;
11
use Sonata\AdminBundle\Admin\Admin;
12
use Sonata\AdminBundle\Datagrid\DatagridMapper;
13
use Sonata\AdminBundle\Datagrid\ListMapper;
14
use Sonata\AdminBundle\Form\FormMapper;
15
use Symfony\Component\Validator\Constraints\Length;
16
use Symfony\Component\Validator\Constraints\Regex;
17
18
class PublicationAdmin extends Admin
19
{
20
    protected $baseRouteName    = 'fenrizbes_publication';
21
    protected $baseRoutePattern = '/fenrizbes/publication/{publication_type_key}';
22
23
    /**
24
     * @var PublicationType
25
     */
26
    protected $publication_type;
27
28
    /**
29
     * @return PublicationType
30
     */
31
    public function getPublicationType()
32
    {
33
        if (is_null($this->publication_type)) {
34
            $this->publication_type = PublicationTypeQuery::create()->findPk(
35
                $this->getRequest()->get('publication_type_key')
36
            );
37
        }
38
39
        return $this->publication_type;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function generateUrl($name, array $parameters = array(), $absolute = false)
46
    {
47
        $parameters = array_merge(array(
48
            'publication_type_key' => $this->getPublicationType()->getKey()
49
        ), $parameters);
50
51
        return parent::generateUrl($name, $parameters, $absolute);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getBreadcrumbs($action)
58
    {
59
        $breadcrumbs = parent::getBreadcrumbs($action);
60
61
        $menu = $this->menuFactory->createItem('root');
62
        $menu = $menu->addChild(
63
            $this->trans($this->getLabelTranslatorStrategy()->getLabel('publication_type_list', 'breadcrumb', 'link')),
64
            array('uri' => $this->routeGenerator->generate('fenrizbes_publication_type_list'))
65
        );
66
67
        array_splice($breadcrumbs, 1, 0, array($menu));
68
69
        /** @var MenuItem $banner_list_menu */
70
        $banner_list_menu = $breadcrumbs[2];
71
        $banner_list_menu->setName(
72
            $this->trans($this->getLabelTranslatorStrategy()->getLabel(
73
                sprintf('%s_list', $this->getClassnameLabel()), 'breadcrumb', 'link'
74
            )) .' "'. $this->getPublicationType()->getTitle() .'"'
75
        );
76
77
        return $breadcrumbs;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function createQuery($context = 'list')
84
    {
85
        /** @var PublicationQuery $query */
86
        $query = parent::createQuery($context);
87
88
        $query->filterByPublicationType($this->getPublicationType());
89
90
        return $query;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function getSubject()
97
    {
98
        $subject = parent::getSubject();
99
100
        if ($subject instanceof Publication) {
101
            $subject->setPublicationType($this->getPublicationType());
102
        }
103
104
        return $subject;
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    protected $datagridValues = array(
111
        '_sort_order' => 'desc',
112
        '_sort_by'    => 'created_at'
113
    );
114
115
    /**
116
     * The format for datetime fields
117
     *
118
     * @var string
119
     */
120
    protected $datetime_format;
121
122
    /**
123
     * FormType's name for the content field
124
     *
125
     * @var string|null
126
     */
127
    protected $content_editor;
128
129
    /**
130
     * Sets the datetime format
131
     *
132
     * @param string $format
133
     */
134
    public function setDateTimeFormat($format)
135
    {
136
        $this->datetime_format = $format;
137
    }
138
139
    /**
140
     * Sets the content field's type
141
     *
142
     * @param string|null $editor
143
     */
144
    public function setContentEditor($editor)
145
    {
146
        $this->content_editor = $editor;
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
153
    {
154
        $datagridMapper
155
            ->add('Title', null, array(
156
                'label' => 'fp.label.admin.publication.title'
157
            ))
158
            ->add('IsPublished', null, array(
159
                'label' => 'fp.label.admin.publication.is_published'
160
            ))
161
        ;
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    protected function configureListFields(ListMapper $listMapper)
168
    {
169
        $listMapper
170
            ->addIdentifier('Title', null, array(
171
                'label' => 'fp.label.admin.publication.title'
172
            ))
173
            ->add('IsPublished', null, array(
174
                'label' => 'fp.label.admin.publication.is_published'
175
            ))
176
            ->add('CreatedAt', null, array(
177
                'label'  => 'fp.label.admin.publication.created_at',
178
                'format' => $this->datetime_format
179
            ))
180
            ->add('UpdatedAt', null, array(
181
                'label'  => 'fp.label.admin.publication.updated_at',
182
                'format' => $this->datetime_format
183
            ))
184
            ->add('_action', 'actions', array(
185
                'label'    => 'fp.label.admin.actions',
186
                'sortable' => false,
187
                'actions'  => array(
188
                    'edit'   => array(),
189
                    'delete' => array()
190
                )
191
            ))
192
        ;
193
    }
194
195
    /**
196
     * {@inheritdoc}
197
     */
198
    protected function configureFormFields(FormMapper $formMapper)
199
    {
200
        $formMapper
201
            ->tab('fp.label.admin.publication.tab_main')
202
                ->with('')
203
                    ->add('Title', null, array(
204
                        'label'       => 'fp.label.admin.publication.title',
205
                        'constraints' => array(
206
                            new Length(array(
207
                                'max'        => 255,
208
                                'minMessage' => 'fp.constraint.longer_than_255'
209
                            ))
210
                        )
211
                    ))
212
                    ->add('Content', $this->content_editor, array(
213
                        'label' => 'fp.label.admin.publication.content'
214
                    ))
215
                    ->add('IsPublished', null, array(
216
                        'label'    => 'fp.label.admin.publication.is_published',
217
                        'required' => false
218
                    ))
219
                ->end()
220
            ->end()
221
222
            ->tab('fp.label.admin.publication.tab_image')
223
                ->with('')
224
                    ->add('CroppableImage', 'croppable', array(
225
                        'translation_domain' => $this->getTranslationDomain(),
226
                        'label'              => 'fp.label.admin.publication.image',
227
                        'width'              => 140,
228
                        'height'             => 95
229
                    ))
230
                ->end()
231
            ->end()
232
233
            ->tab('fp.label.admin.publication.tab_additional')
234
                ->with('')
235
                    ->add('Slug', null, array(
236
                        'label' => 'fp.label.admin.publication.slug',
237
                        'constraints' => array(
238
                            new Regex(array(
239
                                'pattern' => '/^[\w\-]+$/',
240
                                'message' => 'fp.constraint.not_alphanumeric'
241
                            ))
242
                        )
243
                    ))
244
                    ->add('Announcement', null, array(
245
                        'label' => 'fp.label.admin.publication.announcement'
246
                    ))
247
                    ->add('CreatedAt', 'datetime_no_intl_picker', array(
248
                        'label'    => 'fp.label.admin.publication.created_at',
249
                        'format'   => $this->datetime_format,
250
                        'required' => false
251
                    ))
252
                ->end()
253
            ->end()
254
255
            ->setHelps(array(
256
                'Slug'         => 'fp.label.admin.hint.slug',
257
                'Announcement' => 'fp.label.admin.hint.announcement',
258
                'CreatedAt'    => 'fp.label.admin.hint.created_at'
259
            ))
260
        ;
261
    }
262
263
    /**
264
     * @param Publication $object
265
     * @return mixed|void
266
     */
267
    public function preUpdate($object)
268
    {
269
        if (is_null($object->getCreatedAt())) {
270
            $object->setCreatedAt(new \DateTime());
271
        }
272
    }
273
}
274