1 | <?php |
||
28 | class DatagridBuilder implements DatagridBuilderInterface |
||
29 | { |
||
30 | /** |
||
31 | * @var FilterFactoryInterface |
||
32 | */ |
||
33 | protected $filterFactory; |
||
34 | |||
35 | /** |
||
36 | * @var FormFactoryInterface |
||
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 | * @param bool $csrfTokenEnabled |
||
54 | */ |
||
55 | public function __construct(FormFactoryInterface $formFactory, FilterFactoryInterface $filterFactory, TypeGuesserInterface $guesser, $csrfTokenEnabled = true) |
||
56 | { |
||
57 | $this->formFactory = $formFactory; |
||
58 | $this->filterFactory = $filterFactory; |
||
59 | $this->guesser = $guesser; |
||
60 | $this->csrfTokenEnabled = $csrfTokenEnabled; |
||
61 | } |
||
62 | |||
63 | public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription): void |
||
64 | { |
||
65 | // set default values |
||
66 | $fieldDescription->setAdmin($admin); |
||
67 | |||
68 | if ($admin->getModelManager()->hasMetadata($admin->getClass())) { |
||
69 | [$metadata, $lastPropertyName, $parentAssociationMappings] = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName()); |
||
70 | |||
71 | // set the default field mapping |
||
72 | if (isset($metadata->fieldMappings[$lastPropertyName])) { |
||
73 | $fieldDescription->setOption('field_mapping', $fieldDescription->getOption('field_mapping', $metadata->fieldMappings[$lastPropertyName])); |
||
74 | |||
75 | if ('string' === $metadata->fieldMappings[$lastPropertyName]['type']) { |
||
76 | $fieldDescription->setOption('global_search', $fieldDescription->getOption('global_search', true)); // always search on string field only |
||
77 | } |
||
78 | } |
||
79 | |||
80 | // set the default association mapping |
||
81 | if (isset($metadata->associationMappings[$lastPropertyName])) { |
||
82 | $fieldDescription->setOption('association_mapping', $fieldDescription->getOption('association_mapping', $metadata->associationMappings[$lastPropertyName])); |
||
83 | } |
||
84 | |||
85 | $fieldDescription->setOption('parent_association_mappings', $fieldDescription->getOption('parent_association_mappings', $parentAssociationMappings)); |
||
86 | } |
||
87 | |||
88 | $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName())); |
||
89 | $fieldDescription->setOption('name', $fieldDescription->getOption('name', $fieldDescription->getName())); |
||
90 | |||
91 | if (\in_array($fieldDescription->getMappingType(), [ClassMetadata::ONE, ClassMetadata::MANY], true)) { |
||
92 | $admin->attachAdminClass($fieldDescription); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | public function addFilter(DatagridInterface $datagrid, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin): void |
||
97 | { |
||
98 | if (null === $type) { |
||
99 | $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager()); |
||
100 | |||
101 | $type = $guessType->getType(); |
||
102 | |||
103 | $fieldDescription->setType($type); |
||
104 | |||
105 | $options = $guessType->getOptions(); |
||
106 | |||
107 | foreach ($options as $name => $value) { |
||
108 | if (\is_array($value)) { |
||
109 | $fieldDescription->setOption($name, array_merge($value, $fieldDescription->getOption($name, []))); |
||
110 | } else { |
||
111 | $fieldDescription->setOption($name, $fieldDescription->getOption($name, $value)); |
||
112 | } |
||
113 | } |
||
114 | } else { |
||
115 | $fieldDescription->setType($type); |
||
116 | } |
||
117 | |||
118 | $this->fixFieldDescription($admin, $fieldDescription); |
||
119 | $admin->addFilterFieldDescription($fieldDescription->getName(), $fieldDescription); |
||
120 | |||
121 | $fieldDescription->mergeOption('field_options', ['required' => false]); |
||
122 | $filter = $this->filterFactory->create($fieldDescription->getName(), $type, $fieldDescription->getOptions()); |
||
123 | |||
124 | if (false !== $filter->getLabel() && !$filter->getLabel()) { |
||
125 | $filter->setLabel($admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'filter', 'label')); |
||
126 | } |
||
127 | |||
128 | $datagrid->addFilter($filter); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @return \Sonata\AdminBundle\Datagrid\DatagridInterface |
||
133 | */ |
||
134 | public function getBaseDatagrid(AdminInterface $admin, array $values = []) |
||
148 | } |
||
149 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.