Completed
Push — master ( 14bd4d...eb12ea )
by Thomas
22:31
created

GalleryListBlockService::execute()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 25
rs 8.8571
cc 1
eloc 17
nc 1
nop 2
1
<?php
2
3
namespace Sonata\MediaBundle\Block;
4
5
use Sonata\AdminBundle\Form\FormMapper;
6
use Sonata\BlockBundle\Block\BaseBlockService;
7
use Sonata\BlockBundle\Block\BlockContextInterface;
8
use Sonata\BlockBundle\Model\BlockInterface;
9
use Sonata\CoreBundle\Model\Metadata;
10
use Sonata\MediaBundle\Model\GalleryManagerInterface;
11
use Sonata\MediaBundle\Provider\Pool;
12
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\OptionsResolver\OptionsResolver;
15
16
class GalleryListBlockService extends BaseBlockService
17
{
18
    /**
19
     * @var GalleryManagerInterface
20
     */
21
    protected $galleryManager;
22
23
    /**
24
     * @var Pool
25
     */
26
    protected $pool;
27
28
    /**
29
     * @param string                  $name
30
     * @param EngineInterface         $templating
31
     * @param GalleryManagerInterface $galleryManager
32
     * @param Pool                    $pool
33
     */
34
    public function __construct($name, EngineInterface $templating, GalleryManagerInterface $galleryManager, Pool $pool)
35
    {
36
        parent::__construct($name, $templating);
37
38
        $this->galleryManager = $galleryManager;
39
        $this->pool = $pool;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
46
    {
47
        $contextChoices = array();
48
49
        foreach ($this->pool->getContexts() as $name => $context) {
50
            $contextChoices[$name] = $name;
51
        }
52
53
        $formMapper->add('settings', 'sonata_type_immutable_array', array(
54
            'keys' => array(
55
                array('title', 'text', array(
56
                    'label'    => 'form.label_title',
57
                    'required' => false,
58
                )),
59
                array('number', 'integer', array(
60
                    'label'    => 'form.label_number',
61
                    'required' => true,
62
                )),
63
                array('context', 'choice', array(
64
                    'required' => true,
65
                    'label'    => 'form.label_context',
66
                    'choices'  => $contextChoices,
67
                )),
68
                array('mode', 'choice', array(
69
                    'label'   => 'form.label_mode',
70
                    'choices' => array(
71
                        'public' => 'form.label_mode_public',
72
                        'admin'  => 'form.label_mode_admin',
73
                    ),
74
                )),
75
                array('order', 'choice',  array(
76
                    'label'   => 'form.label_order',
77
                    'choices' => array(
78
                        'name'        => 'form.label_order_name',
79
                        'createdAt'   => 'form.label_order_created_at',
80
                        'updatedAt'   => 'form.label_order_updated_at',
81
                    ),
82
                )),
83
                array('sort', 'choice', array(
84
                    'label'   => 'form.label_sort',
85
                    'choices' => array(
86
                        'desc' => 'form.label_sort_desc',
87
                        'asc'  => 'form.label_sort_asc',
88
                    ),
89
                )),
90
            ),
91
            'translation_domain' => 'SonataMediaBundle',
92
        ));
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function execute(BlockContextInterface $blockContext, Response $response = null)
99
    {
100
        $context = $blockContext->getBlock()->getSetting('context');
101
102
        $criteria = array(
103
            'mode'       => $blockContext->getSetting('mode'),
104
            'context'    => $context,
105
        );
106
107
        $order = array(
108
            $blockContext->getSetting('order') => $blockContext->getSetting('sort'),
109
        );
110
111
        return $this->renderResponse($blockContext->getTemplate(), array(
112
            'context'    => $blockContext,
113
            'settings'   => $blockContext->getSettings(),
114
            'block'      => $blockContext->getBlock(),
115
            'pager'      => $this->galleryManager->getPager(
116
                $criteria,
117
                1,
118
                $blockContext->getSetting('number'),
119
                $order
120
            ),
121
        ), $response);
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function configureSettings(OptionsResolver $resolver)
128
    {
129
        $resolver->setDefaults(array(
130
            'number'   => 15,
131
            'mode'     => 'public',
132
            'order'    => 'createdAt',
133
            'sort'     => 'desc',
134
            'context'  => false,
135
            'title'    => 'Gallery List',
136
            'template' => 'SonataMediaBundle:Block:block_gallery_list.html.twig',
137
        ));
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function getBlockMetadata($code = null)
144
    {
145
        return new Metadata($this->getName(), (!is_null($code) ? $code : $this->getName()), false, 'SonataMediaBundle', array(
146
            'class' => 'fa fa-picture-o',
147
        ));
148
    }
149
}
150