Completed
Push — 1.x ( e997bd )
by Sullivan
19:38
created

DatagridBuilder::fixFieldDescription()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 26
rs 8.439
cc 5
eloc 12
nc 7
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sonata 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\DoctrinePHPCRAdminBundle\Builder;
13
14
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
15
use Sonata\AdminBundle\Datagrid\PagerInterface;
16
use Sonata\AdminBundle\Filter\FilterInterface;
17
use Sonata\AdminBundle\Admin\AdminInterface;
18
use Sonata\AdminBundle\Datagrid\DatagridInterface;
19
use Sonata\AdminBundle\Datagrid\Datagrid;
20
use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
21
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
22
use Sonata\AdminBundle\Filter\FilterFactoryInterface;
23
24
use Sonata\DoctrinePHPCRAdminBundle\Datagrid\SimplePager;
25
26
use Symfony\Component\Form\FormFactory;
27
28
class DatagridBuilder implements DatagridBuilderInterface
29
{
30
    /**
31
     * @var FilterFactoryInterface
32
     */
33
    protected $filterFactory;
34
35
    /**
36
     * @var FormFactory
37
     */
38
    protected $formFactory;
39
40
    /**
41
     * @var TypeGuesserInterface
42
     */
43
    protected $guesser;
44
45
    /**
46
     * Indicates that csrf protection enabled
47
     *
48
     * @var bool
49
     */
50
    protected $csrfTokenEnabled;
51
52
    /**
53
     * @var PagerInterface
54
     */
55
    protected $pager;
56
57
    /**
58
     * @param FormFactory $formFactory
59
     * @param FilterFactoryInterface $filterFactory
60
     * @param TypeGuesserInterface $guesser
61
     * @param bool $csrfTokenEnabled
62
     */
63
    public function __construct(FormFactory $formFactory, FilterFactoryInterface $filterFactory, TypeGuesserInterface $guesser, $csrfTokenEnabled = true)
64
    {
65
        $this->formFactory   = $formFactory;
66
        $this->filterFactory = $filterFactory;
67
        $this->guesser       = $guesser;
68
        $this->csrfTokenEnabled = $csrfTokenEnabled;
69
    }
70
71
    /**
72
     * @param PagerInterface $pager
73
     */
74
    public function setPager(PagerInterface $pager)
75
    {
76
        $this->pager = $pager;
77
    }
78
79
    /**
80
     * @return PagerInterface
81
     */
82
    public function getPager()
83
    {
84
        if (null === $this->pager) {
85
            $this->pager = new SimplePager();
86
        }
87
88
        return $this->pager;
89
    }
90
91
    /**
92
     * {@inheritDoc}
93
     */
94
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
95
    {
96
        // set default values
97
        $fieldDescription->setAdmin($admin);
98
99
        if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
100
            $metadata = $admin->getModelManager()->getMetadata($admin->getClass());
101
102
            // set the default field mapping
103
            if (isset($metadata->mappings[$fieldDescription->getName()])) {
104
                $fieldDescription->setFieldMapping($metadata->mappings[$fieldDescription->getName()]);
105
106
                if ($metadata->mappings[$fieldDescription->getName()]['type'] == 'string') {
107
                    $fieldDescription->setOption('global_search', $fieldDescription->getOption('global_search', true)); // always search on string field only
108
                }
109
            }
110
111
            // set the default association mapping
112
            if (isset($metadata->associationMappings[$fieldDescription->getName()])) {
113
                $fieldDescription->setAssociationMapping($metadata->associationMappings[$fieldDescription->getName()]);
114
            }
115
        }
116
117
        $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
118
        $fieldDescription->setOption('name', $fieldDescription->getOption('name', $fieldDescription->getName()));
119
    }
120
121
    /**
122
     * {@inheritDoc}
123
     *
124
     * @return FilterInterface
125
     */
126
    public function addFilter(DatagridInterface $datagrid, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
127
    {
128
        if ($type == null) {
129
            $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
130
            $type = $guessType->getType();
131
            $fieldDescription->setType($type);
132
            $options = $guessType->getOptions();
133
134
            foreach ($options as $name => $value) {
135
                if (is_array($value)) {
136
                    $fieldDescription->setOption($name, array_merge($value, $fieldDescription->getOption($name, array())));
137
                } else {
138
                    $fieldDescription->setOption($name, $fieldDescription->getOption($name, $value));
139
                }
140
            }
141
        } else {
142
            $fieldDescription->setType($type);
143
        }
144
145
        $this->fixFieldDescription($admin, $fieldDescription);
146
        $admin->addFilterFieldDescription($fieldDescription->getName(), $fieldDescription);
147
148
        $fieldDescription->mergeOption('field_options', array('required' => false));
149
        $filter = $this->filterFactory->create($fieldDescription->getName(), $type, $fieldDescription->getOptions());
150
151
        if (!$filter->getLabel()) {
152
            $filter->setLabel($admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'filter', 'label'));
153
        }
154
155
        return $datagrid->addFilter($filter);
156
    }
157
158
    /**
159
     * {@inheritDoc}
160
     */
161
    public function getBaseDatagrid(AdminInterface $admin, array $values = array())
162
    {
163
        $defaultOptions = array();
164
        if ($this->csrfTokenEnabled) {
165
            $defaultOptions['csrf_protection'] = false;
166
        }
167
168
        $formBuilder = $this->formFactory->createNamedBuilder('filter', 'form', array(), $defaultOptions);
169
170
        return new Datagrid($admin->createQuery(), $admin->getList(), $this->getPager(), $formBuilder, $values);
0 ignored issues
show
Compatibility introduced by
$formBuilder of type object<Symfony\Component...m\FormBuilderInterface> is not a sub-type of object<Symfony\Component\Form\FormBuilder>. It seems like you assume a concrete implementation of the interface Symfony\Component\Form\FormBuilderInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
171
    }
172
}
173