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