Passed
Pull Request — master (#7182)
by
unknown
10:12
created

LegalController::getExtraFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Controller;
8
9
use Chamilo\CoreBundle\Entity\Legal;
10
use Chamilo\CoreBundle\Repository\LegalRepository;
11
use Doctrine\ORM\EntityManagerInterface;
12
use ExtraField;
13
use ExtraFieldValue;
14
use LegalManager;
15
use Symfony\Component\HttpFoundation\JsonResponse;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
use Symfony\Component\Routing\Attribute\Route;
19
20
#[Route('/legal')]
21
class LegalController
22
{
23
    #[Route('/save', name: 'chamilo_core_legal_save', methods: ['POST'])]
24
    public function saveLegal(
25
        Request $request,
26
        EntityManagerInterface $entityManager,
27
        LegalRepository $legalRepository
28
    ): Response {
29
        $data = json_decode($request->getContent(), true);
30
31
        $languageId = (int) ($data['lang'] ?? 0);
32
        if ($languageId <= 0) {
33
            return new JsonResponse(['message' => 'Invalid language id'], Response::HTTP_BAD_REQUEST);
34
        }
35
36
        $changes = (string) ($data['changes'] ?? '');
37
        $sections = $data['sections'] ?? null;
38
39
        if (!is_array($sections)) {
40
            return new JsonResponse(['message' => 'Missing sections payload'], Response::HTTP_BAD_REQUEST);
41
        }
42
43
        $lastVersion = $legalRepository->findLatestVersionByLanguage($languageId);
44
        $newVersion = $lastVersion + 1;
45
        $timestamp = time();
46
47
        for ($type = 0; $type <= 15; $type++) {
48
            $key = (string) $type;
49
            $content = $sections[$key] ?? ($sections[$type] ?? '');
50
            $content = is_string($content) ? $content : '';
51
52
            $legal = new Legal();
53
            $legal->setLanguageId($languageId);
54
            $legal->setVersion($newVersion);
55
            $legal->setType($type);
56
            $legal->setDate($timestamp);
57
            $legal->setChanges($changes);
58
            $legal->setContent($content === '' ? null : $content);
59
60
            $entityManager->persist($legal);
61
        }
62
63
        $entityManager->flush();
64
65
        return new JsonResponse([
66
            'message' => 'Terms saved successfully',
67
            'version' => $newVersion,
68
        ], Response::HTTP_OK);
69
    }
70
71
    #[Route('/extra-fields', name: 'chamilo_core_get_extra_fields')]
72
    public function getExtraFields(Request $request): JsonResponse
73
    {
74
        return new JsonResponse([
75
            ['type' => 0, 'title' => 'Terms and Conditions', 'subtitle' => ''],
76
            ['type' => 1, 'title' => 'Personal data collection', 'subtitle' => 'Why do we collect this data?'],
77
            // ...
78
            ['type' => 15, 'title' => 'Personal data profiling', 'subtitle' => 'For what purpose do we process personal data?'],
79
        ]);
80
    }
81
82
    /**
83
     * Checks if the extra field values have changed.
84
     *
85
     * This function compares the new values for extra fields against the old ones to determine
86
     * if there have been any changes. It is useful for triggering events or updates only when
87
     * actual changes to data occur.
88
     */
89
    private function hasExtraFieldsChanged(ExtraFieldValue $extraFieldValue, int $legalId, array $newValues): bool
0 ignored issues
show
Unused Code introduced by
The method hasExtraFieldsChanged() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
90
    {
91
        $oldValues = $extraFieldValue->getAllValuesByItem($legalId);
92
        $oldValues = array_column($oldValues, 'value', 'variable');
93
94
        foreach ($newValues as $key => $newValue) {
95
            if (isset($oldValues[$key]) && $newValue != $oldValues[$key]) {
96
                return true;
97
            }
98
            if (!isset($oldValues[$key])) {
99
                return true;
100
            }
101
        }
102
103
        return false;
104
    }
105
106
    /**
107
     * Updates the extra fields with new values for a specific item.
108
     */
109
    private function updateExtraFields(ExtraFieldValue $extraFieldValue, int $legalId, array $values): void
0 ignored issues
show
Unused Code introduced by
The method updateExtraFields() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
110
    {
111
        $values['item_id'] = $legalId;
112
        $extraFieldValue->saveFieldValues($values);
113
    }
114
115
    /**
116
     * Maps an integer representing a field type to its corresponding string value.
117
     */
118
    private function mapFieldType(int $type): string
0 ignored issues
show
Unused Code introduced by
The method mapFieldType() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
119
    {
120
        switch ($type) {
121
            case ExtraField::FIELD_TYPE_TEXT:
122
                return 'text';
123
124
            case ExtraField::FIELD_TYPE_TEXTAREA:
125
                return 'editor';
126
127
            case ExtraField::FIELD_TYPE_SELECT_MULTIPLE:
128
            case ExtraField::FIELD_TYPE_DATE:
129
            case ExtraField::FIELD_TYPE_DATETIME:
130
            case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
131
            case ExtraField::FIELD_TYPE_RADIO:
132
                // Manage as needed
133
                break;
134
135
            case ExtraField::FIELD_TYPE_SELECT:
136
                return 'select';
137
        }
138
139
        return 'text';
140
    }
141
}
142