|
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\Block; |
|
15
|
|
|
|
|
16
|
|
|
use Sonata\AdminBundle\Admin\Pool; |
|
17
|
|
|
use Sonata\AdminBundle\Form\FormMapper; |
|
18
|
|
|
use Sonata\BlockBundle\Block\BlockContextInterface; |
|
19
|
|
|
use Sonata\BlockBundle\Block\Service\AbstractAdminBlockService; |
|
20
|
|
|
use Sonata\BlockBundle\Model\BlockInterface; |
|
21
|
|
|
use Sonata\CoreBundle\Form\Type\ImmutableArrayType; |
|
22
|
|
|
use Sonata\CoreBundle\Model\ManagerInterface; |
|
23
|
|
|
use Sonata\CoreBundle\Model\Metadata; |
|
24
|
|
|
use Sonata\NewsBundle\Model\PostManagerInterface; |
|
25
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
|
26
|
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
|
27
|
|
|
use Symfony\Component\Form\Extension\Core\Type\IntegerType; |
|
28
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
|
29
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
30
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @author Thomas Rabaix <[email protected]> |
|
34
|
|
|
*/ |
|
35
|
|
|
class RecentPostsBlockService extends AbstractAdminBlockService |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* @var PostManagerInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $manager; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var Pool |
|
44
|
|
|
*/ |
|
45
|
|
|
private $adminPool; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $name |
|
49
|
|
|
* @param EngineInterface $templating |
|
50
|
|
|
* @param ManagerInterface $postManager |
|
51
|
|
|
* @param Pool $adminPool |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct($name, EngineInterface $templating, ManagerInterface $postManager, Pool $adminPool = null) |
|
54
|
|
|
{ |
|
55
|
|
|
if (!$postManager instanceof PostManagerInterface) { |
|
56
|
|
|
@trigger_error( |
|
|
|
|
|
|
57
|
|
|
'Calling the '.__METHOD__.' method with a Sonata\CoreBundle\Model\ManagerInterface is deprecated' |
|
58
|
|
|
.' since version 2.4 and will be removed in 3.0.' |
|
59
|
|
|
.' Use the new signature with a Sonata\NewsBundle\Model\PostManagerInterface instead.', |
|
60
|
|
|
E_USER_DEPRECATED |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$this->manager = $postManager; |
|
|
|
|
|
|
65
|
|
|
$this->adminPool = $adminPool; |
|
66
|
|
|
|
|
67
|
|
|
parent::__construct($name, $templating); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
|
|
public function execute(BlockContextInterface $blockContext, Response $response = null) |
|
74
|
|
|
{ |
|
75
|
|
|
$criteria = [ |
|
76
|
|
|
'mode' => $blockContext->getSetting('mode'), |
|
77
|
|
|
]; |
|
78
|
|
|
|
|
79
|
|
|
$parameters = [ |
|
80
|
|
|
'context' => $blockContext, |
|
81
|
|
|
'settings' => $blockContext->getSettings(), |
|
82
|
|
|
'block' => $blockContext->getBlock(), |
|
83
|
|
|
'pager' => $this->manager->getPager($criteria, 1, $blockContext->getSetting('number')), |
|
84
|
|
|
'admin_pool' => $this->adminPool, |
|
85
|
|
|
]; |
|
86
|
|
|
|
|
87
|
|
|
if ('admin' === $blockContext->getSetting('mode')) { |
|
88
|
|
|
return $this->renderPrivateResponse($blockContext->getTemplate(), $parameters, $response); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $this->renderResponse($blockContext->getTemplate(), $parameters, $response); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* {@inheritdoc} |
|
96
|
|
|
*/ |
|
97
|
|
|
public function buildEditForm(FormMapper $formMapper, BlockInterface $block): void |
|
98
|
|
|
{ |
|
99
|
|
|
$formMapper->add('settings', ImmutableArrayType::class, [ |
|
100
|
|
|
'keys' => [ |
|
101
|
|
|
['number', IntegerType::class, [ |
|
102
|
|
|
'required' => true, |
|
103
|
|
|
'label' => 'form.label_number', |
|
104
|
|
|
]], |
|
105
|
|
|
['title', TextType::class, [ |
|
106
|
|
|
'label' => 'form.label_title', |
|
107
|
|
|
'required' => false, |
|
108
|
|
|
]], |
|
109
|
|
|
['translation_domain', TextType::class, [ |
|
110
|
|
|
'label' => 'form.label_translation_domain', |
|
111
|
|
|
'required' => false, |
|
112
|
|
|
]], |
|
113
|
|
|
['icon', TextType::class, [ |
|
114
|
|
|
'label' => 'form.label_icon', |
|
115
|
|
|
'required' => false, |
|
116
|
|
|
]], |
|
117
|
|
|
['class', TextType::class, [ |
|
118
|
|
|
'label' => 'form.label_class', |
|
119
|
|
|
'required' => false, |
|
120
|
|
|
]], |
|
121
|
|
|
['mode', ChoiceType::class, [ |
|
122
|
|
|
'choices' => [ |
|
123
|
|
|
'public' => 'form.label_mode_public', |
|
124
|
|
|
'admin' => 'form.label_mode_admin', |
|
125
|
|
|
], |
|
126
|
|
|
'label' => 'form.label_mode', |
|
127
|
|
|
]], |
|
128
|
|
|
], |
|
129
|
|
|
'translation_domain' => 'SonataNewsBundle', |
|
130
|
|
|
]); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* {@inheritdoc} |
|
135
|
|
|
*/ |
|
136
|
|
|
public function configureSettings(OptionsResolver $resolver): void |
|
137
|
|
|
{ |
|
138
|
|
|
$resolver->setDefaults([ |
|
139
|
|
|
'number' => 5, |
|
140
|
|
|
'mode' => 'public', |
|
141
|
|
|
'title' => null, |
|
142
|
|
|
'translation_domain' => null, |
|
143
|
|
|
'icon' => 'fa fa-pencil', |
|
144
|
|
|
'class' => null, |
|
145
|
|
|
'template' => '@SonataNews/Block/recent_posts.html.twig', |
|
146
|
|
|
]); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* {@inheritdoc} |
|
151
|
|
|
*/ |
|
152
|
|
|
public function getBlockMetadata($code = null) |
|
153
|
|
|
{ |
|
154
|
|
|
return new Metadata($this->getName(), (null !== $code ? $code : $this->getName()), false, 'SonataNewsBundle', [ |
|
155
|
|
|
'class' => 'fa fa-pencil', |
|
156
|
|
|
]); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: