Passed
Push — main ( d1a6b2...a01d04 )
by Axel
04:37
created

CategoriesInitializer::localize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\CategoriesBundle\Bundle\Initializer;
15
16
use Doctrine\ORM\EntityManagerInterface;
17
use Doctrine\ORM\Mapping\ClassMetadata;
18
use Doctrine\ORM\Mapping\ClassMetadataInfo;
19
use Symfony\Contracts\Translation\TranslatorInterface;
20
use Zikula\CategoriesBundle\Entity\Category;
21
use Zikula\CoreBundle\Api\ApiInterface\LocaleApiInterface;
22
use Zikula\CoreBundle\Bundle\Initializer\BundleInitializerInterface;
23
use Zikula\UsersBundle\Entity\User;
24
use Zikula\UsersBundle\Repository\UserRepositoryInterface;
25
use Zikula\UsersBundle\UsersConstant;
26
27
class CategoriesInitializer implements BundleInitializerInterface
28
{
29
    public function __construct(
30
        private readonly TranslatorInterface $translator,
31
        private readonly EntityManagerInterface $entityManager,
32
        private readonly UserRepositoryInterface $userRepository,
33
        private readonly LocaleApiInterface $localeApi
34
    ) {
35
    }
36
37
    public function init(): void
38
    {
39
        /**
40
         * explicitly set admin as user to be set as `updatedBy` and `createdBy` fields. Normally this would be taken care of
41
         * by the BlameListener but during installation from the CLI this listener is not available
42
         */
43
        /** @var User $adminUser */
44
        $adminUser = $this->userRepository->find(UsersConstant::USER_ID_ADMIN);
45
46
        $this->insertDefaultData($adminUser);
47
48
        // Set autonumber to 10000 (for DB's that support autonumber fields)
49
        $category = (new Category())
50
            ->setId(9999)
51
            ->setUpdatedBy($adminUser)
52
            ->setCreatedBy($adminUser);
53
        $this->entityManager->persist($category);
54
        $this->entityManager->flush();
55
        $this->entityManager->remove($category);
56
        $this->entityManager->flush();
57
    }
58
59
    private function insertDefaultData(User $adminUser): void
60
    {
61
        $categoryData = $this->getDefaultCategoryData();
62
        $categoryObjectMap = [];
63
        /** @var ClassMetadata */
64
        $categoryMetaData = $this->entityManager->getClassMetaData(Category::class);
65
        // disable auto-generation of keys to allow manual setting from this data set.
66
        $categoryMetaData->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_NONE);
67
68
        foreach ($categoryData as $data) {
69
            $data['parent'] = 0 < $data['parent_id'] && isset($categoryObjectMap[$data['parent_id']]) ? $categoryObjectMap[$data['parent_id']] : null;
70
            unset($data['parent_id']);
71
            $attributes = $data['attributes'] ?? [];
72
            unset($data['attributes']);
73
74
            $category = (new Category())
75
                ->setId($data['id'])
76
                ->setParent($data['parent'])
77
                ->setLocked($data['locked'])
78
                ->setLeaf($data['leaf'])
79
                ->setName($data['name'])
80
                ->setValue($data['value'])
81
                ->setDisplayName($data['displayName'])
82
                ->setDisplayDesc($data['displayDesc'])
83
                ->setStatus($data['status'])
84
                ->setCreatedBy($adminUser)
85
                ->setUpdatedBy($adminUser);
86
            $this->entityManager->persist($category);
87
88
            $categoryObjectMap[$data['id']] = $category;
89
90
            if (isset($attributes)) {
91
                foreach ($attributes as $key => $value) {
92
                    $category->setAttribute($key, $value);
93
                }
94
            }
95
        }
96
97
        $this->entityManager->flush();
98
        $categoryMetaData->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
99
    }
100
101
    private function getDefaultCategoryData(): array
102
    {
103
        $categoryData = [];
104
        $categoryData[] = [
105
            'id' => 1,
106
            'parent_id' => 0,
107
            'locked' => true,
108
            'leaf' => false,
109
            'value' => '',
110
            'name' => '__SYSTEM__',
111
            'displayName' => $this->localize($this->translator->trans('Category root')),
112
            'displayDesc' => $this->localize(),
113
            'status' => 'A'
114
        ];
115
        $categoryData[] = [
116
            'id' => 2,
117
            'parent_id' => 1,
118
            'locked' => false,
119
            'leaf' => false,
120
            'value' => '',
121
            'name' => 'Bundles',
122
            'displayName' => $this->localize($this->translator->trans('Bundles')),
123
            'displayDesc' => $this->localize(),
124
            'status' => 'A'
125
        ];
126
        $categoryData[] = [
127
            'id' => 3,
128
            'parent_id' => 1,
129
            'locked' => false,
130
            'leaf' => false,
131
            'value' => '',
132
            'name' => 'General',
133
            'displayName' => $this->localize($this->translator->trans('General')),
134
            'displayDesc' => $this->localize(),
135
            'status' => 'A'
136
        ];
137
        $categoryData[] = [
138
            'id' => 10,
139
            'parent_id' => 3,
140
            'locked' => false,
141
            'leaf' => false,
142
            'value' => '',
143
            'name' => 'Publication Status (extended)',
144
            'displayName' => $this->localize($this->translator->trans('Publication status (extended)')),
145
            'displayDesc' => $this->localize(),
146
            'status' => 'A'
147
        ];
148
        $categoryData[] = [
149
            'id' => 11,
150
            'parent_id' => 10,
151
            'locked' => false,
152
            'leaf' => true,
153
            'value' => 'P',
154
            'name' => 'Pending',
155
            'displayName' => $this->localize($this->translator->trans('Pending')),
156
            'displayDesc' => $this->localize(),
157
            'status' => 'A',
158
            'attributes' => ['code' => 'P']
159
        ];
160
        $categoryData[] = [
161
            'id' => 12,
162
            'parent_id' => 10,
163
            'locked' => false,
164
            'leaf' => true,
165
            'value' => 'C',
166
            'name' => 'Checked',
167
            'displayName' => $this->localize($this->translator->trans('Checked')),
168
            'displayDesc' => $this->localize(),
169
            'status' => 'A',
170
            'attributes' => ['code' => 'C']
171
        ];
172
        $categoryData[] = [
173
            'id' => 13,
174
            'parent_id' => 10,
175
            'locked' => false,
176
            'leaf' => true,
177
            'value' => 'A',
178
            'name' => 'Approved',
179
            'displayName' => $this->localize($this->translator->trans('Approved')),
180
            'displayDesc' => $this->localize(),
181
            'status' => 'A',
182
            'attributes' => ['code' => 'A']
183
        ];
184
        $categoryData[] = [
185
            'id' => 14,
186
            'parent_id' => 10,
187
            'locked' => false,
188
            'leaf' => true,
189
            'value' => 'O',
190
            'name' => 'On-line',
191
            'displayName' => $this->localize($this->translator->trans('On-line')),
192
            'displayDesc' => $this->localize(),
193
            'status' => 'A',
194
            'attributes' => ['code' => 'O']
195
        ];
196
        $categoryData[] = [
197
            'id' => 15,
198
            'parent_id' => 10,
199
            'locked' => false,
200
            'leaf' => true,
201
            'value' => 'R',
202
            'name' => 'Rejected',
203
            'displayName' => $this->localize($this->translator->trans('Rejected')),
204
            'displayDesc' => $this->localize(),
205
            'status' => 'A',
206
            'attributes' => ['code' => 'R']
207
        ];
208
        $categoryData[] = [
209
            'id' => 25,
210
            'parent_id' => 3,
211
            'locked' => false,
212
            'leaf' => false,
213
            'value' => '',
214
            'name' => 'ActiveStatus',
215
            'displayName' => $this->localize($this->translator->trans('Activity status')),
216
            'displayDesc' => $this->localize(),
217
            'status' => 'A'
218
        ];
219
        $categoryData[] = [
220
            'id' => 26,
221
            'parent_id' => 25,
222
            'locked' => false,
223
            'leaf' => true,
224
            'value' => 'A',
225
            'name' => 'Active',
226
            'displayName' => $this->localize($this->translator->trans('Active')),
227
            'displayDesc' => $this->localize(),
228
            'status' => 'A',
229
            'attributes' => ['code' => 'A']
230
        ];
231
        $categoryData[] = [
232
            'id' => 27,
233
            'parent_id' => 25,
234
            'locked' => false,
235
            'leaf' => true,
236
            'value' => 'I',
237
            'name' => 'Inactive',
238
            'displayName' => $this->localize($this->translator->trans('Inactive')),
239
            'displayDesc' => $this->localize(),
240
            'status' => 'A',
241
            'attributes' => ['code' => 'I']
242
        ];
243
        $categoryData[] = [
244
            'id' => 28,
245
            'parent_id' => 3,
246
            'locked' => false,
247
            'leaf' => false,
248
            'value' => '',
249
            'name' => 'Publication status (basic)',
250
            'displayName' => $this->localize($this->translator->trans('Publication status (basic)')),
251
            'displayDesc' => $this->localize(),
252
            'status' => 'A'
253
        ];
254
        $categoryData[] = [
255
            'id' => 29,
256
            'parent_id' => 28,
257
            'locked' => false,
258
            'leaf' => true,
259
            'value' => 'P',
260
            'name' => 'Pending',
261
            'displayName' => $this->localize($this->translator->trans('Pending')),
262
            'displayDesc' => $this->localize(),
263
            'status' => 'A',
264
            'attributes' => ['code' => 'P']
265
        ];
266
        $categoryData[] = [
267
            'id' => 30,
268
            'parent_id' => 28,
269
            'locked' => false,
270
            'leaf' => true,
271
            'value' => 'A',
272
            'name' => 'Approved',
273
            'displayName' => $this->localize($this->translator->trans('Approved')),
274
            'displayDesc' => $this->localize(),
275
            'status' => 'A',
276
            'attributes' => ['code' => 'A']
277
        ];
278
        $categoryData[] = [
279
            'id' => 32,
280
            'parent_id' => 2,
281
            'locked' => false,
282
            'leaf' => false,
283
            'value' => '',
284
            'name' => 'Global',
285
            'displayName' => $this->localize($this->translator->trans('Global')),
286
            'displayDesc' => $this->localize(),
287
            'status' => 'A'
288
        ];
289
        $categoryData[] = [
290
            'id' => 33,
291
            'parent_id' => 32,
292
            'locked' => false,
293
            'leaf' => true,
294
            'value' => '',
295
            'name' => 'Blogging',
296
            'displayName' => $this->localize($this->translator->trans('Blogging')),
297
            'displayDesc' => $this->localize(),
298
            'status' => 'A'
299
        ];
300
        $categoryData[] = [
301
            'id' => 34,
302
            'parent_id' => 32,
303
            'locked' => false,
304
            'leaf' => true,
305
            'value' => '',
306
            'name' => 'Music and audio',
307
            'displayName' => $this->localize($this->translator->trans('Music and audio')),
308
            'displayDesc' => $this->localize(),
309
            'status' => 'A'
310
        ];
311
        $categoryData[] = [
312
            'id' => 35,
313
            'parent_id' => 32,
314
            'locked' => false,
315
            'leaf' => true,
316
            'value' => '',
317
            'name' => 'Art and photography',
318
            'displayName' => $this->localize($this->translator->trans('Art and photography')),
319
            'displayDesc' => $this->localize(),
320
            'status' => 'A'
321
        ];
322
        $categoryData[] = [
323
            'id' => 36,
324
            'parent_id' => 32,
325
            'locked' => false,
326
            'leaf' => true,
327
            'value' => '',
328
            'name' => 'Writing and thinking',
329
            'displayName' => $this->localize($this->translator->trans('Writing and thinking')),
330
            'displayDesc' => $this->localize(),
331
            'status' => 'A'
332
        ];
333
        $categoryData[] = [
334
            'id' => 37,
335
            'parent_id' => 32,
336
            'locked' => false,
337
            'leaf' => true,
338
            'value' => '',
339
            'name' => 'Communications and media',
340
            'displayName' => $this->localize($this->translator->trans('Communications and media')),
341
            'displayDesc' => $this->localize(),
342
            'status' => 'A'
343
        ];
344
        $categoryData[] = [
345
            'id' => 38,
346
            'parent_id' => 32,
347
            'locked' => false,
348
            'leaf' => true,
349
            'value' => '',
350
            'name' => 'Travel and culture',
351
            'displayName' => $this->localize($this->translator->trans('Travel and culture')),
352
            'displayDesc' => $this->localize(),
353
            'status' => 'A'
354
        ];
355
        $categoryData[] = [
356
            'id' => 39,
357
            'parent_id' => 32,
358
            'locked' => false,
359
            'leaf' => true,
360
            'value' => '',
361
            'name' => 'Science and technology',
362
            'displayName' => $this->localize($this->translator->trans('Science and technology')),
363
            'displayDesc' => $this->localize(),
364
            'status' => 'A'
365
        ];
366
        $categoryData[] = [
367
            'id' => 40,
368
            'parent_id' => 32,
369
            'locked' => false,
370
            'leaf' => true,
371
            'value' => '',
372
            'name' => 'Sport and activities',
373
            'displayName' => $this->localize($this->translator->trans('Sport and activities')),
374
            'displayDesc' => $this->localize(),
375
            'status' => 'A'
376
        ];
377
        $categoryData[] = [
378
            'id' => 41,
379
            'parent_id' => 32,
380
            'locked' => false,
381
            'leaf' => true,
382
            'value' => '',
383
            'name' => 'Business and work',
384
            'displayName' => $this->localize($this->translator->trans('Business and work')),
385
            'displayDesc' => $this->localize(),
386
            'status' => 'A'
387
        ];
388
389
        return $categoryData;
390
    }
391
392
    private function localize(string $value = ''): array
393
    {
394
        $values = [];
395
        foreach ($this->localeApi->getSupportedLocales() as $code) {
396
            $values[$code] = $this->translator->trans($value, [], 'zikula', $code);
397
        }
398
399
        return $values;
400
    }
401
}
402