Completed
Push — refonte ( 758c88...ee8395 )
by Arnaud
02:27
created

FieldConfigurationHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace LAG\AdminBundle\Field\Helper;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use LAG\AdminBundle\Bridge\Doctrine\ORM\Helper\MetadataTrait;
7
use LAG\AdminBundle\Configuration\ApplicationConfiguration;
8
use LAG\AdminBundle\LAGAdminBundle;
9
use LAG\AdminBundle\Routing\RoutingLoader;
10
11
class FieldConfigurationHelper
12
{
13
    use MetadataTrait;
14
15
    /**
16
     * @var ApplicationConfiguration
17
     */
18
    private $applicationConfiguration;
19
20
    public function __construct(
21
        EntityManagerInterface $entityManager,
22
        ApplicationConfiguration $applicationConfiguration
23
    ) {
24
        $this->entityManager = $entityManager;
25
        $this->applicationConfiguration = $applicationConfiguration;
26
    }
27
28
    public function addDefaultFields(array &$configuration, $entityClass, $adminName)
29
    {
30
        $mapping = [
31
            'string' => [
32
                'type' => 'string',
33
                'options' => [
34
                    'length' => 100,
35
                ],
36
            ],
37
            'boolean' => [
38
                'type' => 'boolean',
39
                'options' => [],
40
            ],
41
            'datetime' => [
42
                'type' => 'date',
43
                'options' => [],
44
            ],
45
        ];
46
47
        foreach ($configuration['actions'] as $actionName => $action) {
48
            if (null === $action) {
49
                $action = [];
50
            }
51
            $metadata = $this->findMetadata($entityClass);
52
53
            if (null === $metadata) {
54
                continue;
55
            }
56
57
            // If fields are already defined, nothing to do
58
            if (key_exists('fields', $action) && is_array($action['fields']) && count($action['fields'])) {
59
                $fields = $action['fields'];
60
            } else {
61
                $fields = [];
62
63
                // Get fields names from the metadata if no configuration is defined
64
                foreach ($metadata->getFieldNames() as $fieldName) {
65
                    $fields[$fieldName] = null;
66
                }
67
            }
68
            $actionField = $this->getActionField($metadata->getFieldNames());
69
70
            foreach ($fields as $fieldName => $fieldConfiguration) {
71
                $fieldType = $metadata->getTypeOfField($fieldName);
72
73
                if (
74
                    'list' === $actionName &&
75
                    $fieldName === $actionField &&
76
                    key_exists('edit', $configuration['actions'])
77
                ) {
78
                    $fieldConfiguration = [
79
                        'type' => 'action',
80
                        'options' => [
81
                            'admin' => $adminName,
82
                            'action' => 'edit',
83
                            'parameters' => [
84
                                'id' => null,
85
                            ],
86
                        ]
87
                    ];
88
89
                } else if (
90
                    '_delete' === $fieldName &&
91
                    !$metadata->hasField('_delete') &&
92
                    null === $fieldConfiguration &&
93
                    key_exists('delete', $configuration['actions'])
94
                ) {
95
                    // If a "delete" field is declared, and if it is not configured in the metadata, and if no
96
                    // configuration is declared for this field, and if the "delete" action is allowed, we add a default
97
                    // "button" configuration
98
                    $fieldConfiguration = [
99
                        'type' => 'link',
100
                        'options' => [
101
                            'admin' => $adminName,
102
                            'action' => 'delete',
103
                            'parameters' => [
104
                                'id' => null,
105
                            ],
106
                            'text' => 'Delete',
107
                            'class' => 'btn btn-sm btn-danger',
108
                            'icon' => 'remove',
109
                        ],
110
                    ];
111
112
                } else if (key_exists($fieldType, $mapping)) {
113
                    $fieldConfiguration = $mapping[$metadata->getTypeOfField($fieldName)];
114
                }
115
                $configuration['actions'][$actionName]['fields'][$fieldName] = $fieldConfiguration;
116
            }
117
        }
118
    }
119
120
    public function addDefaultStrategy(array &$configuration)
121
    {
122
        $mapping = [
123
            'list' => LAGAdminBundle::LOAD_STRATEGY_MULTIPLE,
124
            'create' => LAGAdminBundle::LOAD_STRATEGY_NONE,
125
            'delete' => LAGAdminBundle::LOAD_STRATEGY_UNIQUE,
126
            'edit' => LAGAdminBundle::LOAD_STRATEGY_UNIQUE,
127
        ];
128
129
        foreach ($configuration['actions'] as $name => $action) {
130
            if (null === $action) {
131
                continue;
132
            }
133
134
            if (key_exists('load_strategy', $action)) {
135
                continue;
136
            }
137
138
            if (!key_exists($name, $mapping)) {
139
                continue;
140
            }
141
            $configuration['actions'][$name]['load_strategy'] = $mapping[$name];
142
        }
143
    }
144
145
    public function addDefaultRouteParameters(array &$configuration)
146
    {
147
        $mapping = [
148
            'edit' => [
149
                'id' => '\d+',
150
            ],
151
            'delete' => [
152
                'id' => '\d+',
153
            ],
154
        ];
155
156
        foreach ($configuration['actions'] as $name => $actionConfiguration) {
157
            if (key_exists($name, $mapping) && !key_exists('route_requirements', $actionConfiguration)) {
158
                $configuration['actions'][$name]['route_requirements'] = [
159
                    'id' => '\d+',
160
                ];
161
            }
162
        }
163
    }
164
165
    public function addDefaultFormUse(array &$configuration)
166
    {
167
        $mapping = [
168
            'edit',
169
            'create',
170
            'delete',
171
        ];
172
173
        foreach ($configuration['actions'] as $name => $action) {
174
            if (!in_array($name, $mapping) && !isset($action['use_form'])) {
175
                continue;
176
            }
177
            $configuration['actions'][$name]['use_form'] = true;
178
        }
179
    }
180
181
    public function provideActionsFieldConfiguration(array &$configuration, string $adminName)
182
    {
183
        foreach ($configuration['actions'] as $actionName => $actionConfiguration) {
184
185
            if (!key_exists('fields', $actionConfiguration)) {
186
                continue;
187
            }
188
189
            if (!key_exists('_actions', $actionConfiguration['fields'])) {
190
                continue;
191
            }
192
193
            if (null !== $actionConfiguration['fields']['_actions']) {
194
                continue;
195
            }
196
197
            if (!key_exists('edit', $configuration['actions']) && !key_exists('delete', $configuration['actions'])) {
198
                continue;
199
            }
200
            $configuration['actions'][$actionName]['fields']['_actions'] = [
201
                'type' => 'action_collection',
202
                'options' => [],
203
            ];
204
            $pattern = $this->applicationConfiguration->get('routing_name_pattern');
205
206
            if (key_exists('edit', $configuration['actions'])) {
207
                $configuration['actions'][$actionName]['fields']['_actions']['options']['fields']['edit'] = [
208
                    'type' => 'action',
209
                    'options' => [
210
                        'title' => 'test',
211
                        'route' => RoutingLoader::generateRouteName(
212
                            $adminName,
213
                            'edit',
214
                            $pattern
215
                        ),
216
                        'parameters' => [
217
                            'id' => null,
218
                        ],
219
                        'icon' => 'pencil',
220
                    ],
221
                ];
222
            }
223
224
            if (key_exists('delete', $configuration['actions'])) {
225
                $configuration['actions'][$actionName]['fields']['_actions']['options']['fields']['delete'] = [
226
                    'type' => 'action',
227
                    'options' => [
228
                        'title' => 'test',
229
                        'route' => RoutingLoader::generateRouteName(
230
                            $adminName,
231
                            'delete',
232
                            $pattern
233
                        ),
234
                        'parameters' => [
235
                            'id' => null,
236
                        ],
237
                        'icon' => 'remove',
238
                    ],
239
                ];
240
            }
241
        }
242
    }
243
244
    /**
245
     * Return the default action field if found.
246
     *
247
     * @param array $fields
248
     *
249
     * @return string|null
250
     */
251
    private function getActionField(array $fields): ?string
252
    {
253
        $mapping = [
254
            'title',
255
            'name',
256
            'id',
257
        ];
258
259
        foreach ($mapping as $name) {
260
            if (in_array($name, $fields)) {
261
                return $name;
262
            }
263
        }
264
265
        return null;
266
    }
267
}
268