|
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 |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.