1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* For licensing terms, see /license.txt */ |
6
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\Repository; |
8
|
|
|
|
9
|
|
|
use Chamilo\CoreBundle\Entity\Language; |
10
|
|
|
use Chamilo\CoreBundle\Settings\SettingsManager; |
11
|
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
12
|
|
|
use Doctrine\Common\Collections\Collection; |
13
|
|
|
use Doctrine\ORM\QueryBuilder; |
14
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
15
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
16
|
|
|
|
17
|
|
|
class LanguageRepository extends ServiceEntityRepository |
18
|
|
|
{ |
19
|
|
|
public function __construct( |
20
|
|
|
ManagerRegistry $registry, |
21
|
|
|
private readonly ParameterBagInterface $parameterBag, |
22
|
|
|
private readonly SettingsManager $settingsManager |
23
|
|
|
) { |
24
|
|
|
parent::__construct($registry, Language::class); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getAllAvailable($excludeDefaultLocale = false): QueryBuilder |
28
|
|
|
{ |
29
|
|
|
$qb = $this->createQueryBuilder('l'); |
30
|
|
|
|
31
|
|
|
$qb->where($qb->expr()->eq('l.available', ':avail')) |
32
|
|
|
->setParameter('avail', true) |
33
|
|
|
->orderBy('l.englishName', 'ASC'); |
34
|
|
|
|
35
|
|
|
if ($excludeDefaultLocale) { |
36
|
|
|
$qb->andWhere($qb->expr()->neq('l.isocode', ':iso_base')) |
37
|
|
|
->setParameter('iso_base', $this->parameterBag->get('locale')); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $qb; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return array<string,string> [isocode => original_name] |
45
|
|
|
*/ |
46
|
|
|
public function getAllAvailableToArray( |
47
|
|
|
bool $onlyActive = true, |
48
|
|
|
bool $includePlatformDefault = false |
49
|
|
|
): array { |
50
|
|
|
$qb = $this->getAllAvailable(); |
51
|
|
|
|
52
|
|
|
if (!$onlyActive) { |
53
|
|
|
$qb->resetDQLPart('where'); |
54
|
|
|
$qb->orderBy('l.englishName', 'ASC'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** @var Language[] $languages */ |
58
|
|
|
$languages = $qb->getQuery()->getResult(); |
59
|
|
|
|
60
|
|
|
$list = []; |
61
|
|
|
|
62
|
|
|
/** @var Language $language */ |
63
|
|
|
foreach ($languages as $language) { |
64
|
|
|
$list[$language->getIsocode()] = $language->getOriginalName(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($includePlatformDefault) { |
68
|
|
|
$defaultIso = $this->getPlatformDefaultIso(); |
69
|
|
|
if ($defaultIso && !isset($list[$defaultIso])) { |
70
|
|
|
$default = $this->findOneBy(['isocode' => $defaultIso]); |
71
|
|
|
if ($default instanceof Language) { |
72
|
|
|
$list[$default->getIsocode()] = $default->getOriginalName(); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $list; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function getPlatformDefaultIso(): ?string |
81
|
|
|
{ |
82
|
|
|
$iso = trim($this->settingsManager->getSetting('language.platform_language', true)); |
83
|
|
|
if ($iso !== '') { |
84
|
|
|
return $iso; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$englishName = trim($this->settingsManager->getSetting('language.platformLanguage')); |
88
|
|
|
if ($englishName === '') { |
89
|
|
|
return null; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$lang = $this->findOneBy(['englishName' => $englishName]); |
93
|
|
|
return $lang?->getIsocode(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Convenience for forms: ordered by englishName, labels = original_name, |
98
|
|
|
* and includes platform default ISO if it is not active. |
99
|
|
|
* |
100
|
|
|
* @return array<string,string> [isocode => original_name] |
101
|
|
|
*/ |
102
|
|
|
public function getAllForSelectIncludePlatformDefault(): array |
103
|
|
|
{ |
104
|
|
|
return $this->getAllAvailableToArray(true, true); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get all the sub languages that are made available by the admin. |
109
|
|
|
* |
110
|
|
|
* @return Collection|Language[] |
111
|
|
|
*/ |
112
|
|
|
public function findAllSubLanguages() |
113
|
|
|
{ |
114
|
|
|
$qb = $this->createQueryBuilder('l'); |
115
|
|
|
$qb->select('l') |
116
|
|
|
->where( |
117
|
|
|
$qb->expr()->eq('l.available', true) |
118
|
|
|
) |
119
|
|
|
->andWhere( |
120
|
|
|
$qb->expr()->isNotNull('l.parent') |
121
|
|
|
) |
122
|
|
|
; |
123
|
|
|
|
124
|
|
|
return $qb->getQuery()->getResult(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function findByIsoCode(string $isoCode): ?Language |
128
|
|
|
{ |
129
|
|
|
$qb = $this->createQueryBuilder('l'); |
130
|
|
|
$qb->where('l.isocode = :isoCode') |
131
|
|
|
->setParameter('isoCode', $isoCode) |
132
|
|
|
->setMaxResults(1) |
133
|
|
|
; |
134
|
|
|
|
135
|
|
|
return $qb->getQuery()->getSingleResult(); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|