|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace L10nBundle\Manager\MySql; |
|
4
|
|
|
|
|
5
|
|
|
use L10nBundle\Entity\L10nResource; |
|
6
|
|
|
use L10nBundle\Manager\L10nManagerInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Manager plugged on a MySQL database |
|
10
|
|
|
* |
|
11
|
|
|
* @author Cyril Otal |
|
12
|
|
|
*/ |
|
13
|
|
|
class L10nMySqlManager implements L10nManagerInterface |
|
14
|
|
|
{ |
|
15
|
|
|
const ID_RESOURCE_KEY = 'id_resource'; |
|
16
|
|
|
const ID_LOCALISATION_KEY = 'id_localization'; |
|
17
|
|
|
const LOCALE_KEY = 'locale'; |
|
18
|
|
|
const VALUE_KEY = 'value'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var \Doctrine\DBAL\Connection |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $connection; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $table; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param string $host |
|
32
|
|
|
* @param string $port |
|
33
|
|
|
* @param string $username |
|
34
|
|
|
* @param string $password |
|
35
|
|
|
* @param string $database |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct($host, $port, $username, $password, $database, $table) |
|
38
|
|
|
{ |
|
39
|
|
|
$config = new \Doctrine\DBAL\Configuration(); |
|
40
|
|
|
|
|
41
|
|
|
$connectionParams = array( |
|
42
|
|
|
'dbname' => $database, |
|
43
|
|
|
'user' => $username, |
|
44
|
|
|
'password' => $password, |
|
45
|
|
|
'host' => $host, |
|
46
|
|
|
'port' => $port, |
|
47
|
|
|
'driver' => 'pdo_mysql', |
|
48
|
|
|
); |
|
49
|
|
|
$this->connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config); |
|
50
|
|
|
|
|
51
|
|
|
$this->table = $table; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Return a L10nResource |
|
56
|
|
|
* |
|
57
|
|
|
* @param $idResource |
|
58
|
|
|
* @param $idLocalization |
|
59
|
|
|
* |
|
60
|
|
|
* @return L10nResource $l10nResource |
|
61
|
|
|
*/ |
|
62
|
2 |
|
public function getL10nResource($idResource, $idLocalization) |
|
63
|
|
|
{ |
|
64
|
|
|
$sql = 'SELECT `' |
|
65
|
|
|
. self::ID_RESOURCE_KEY |
|
66
|
2 |
|
. '`, `' |
|
67
|
2 |
|
. self::ID_LOCALISATION_KEY |
|
68
|
2 |
|
. '`, `' |
|
69
|
2 |
|
. self::LOCALE_KEY |
|
70
|
2 |
|
. '`, `' |
|
71
|
2 |
|
. self::VALUE_KEY |
|
72
|
2 |
|
. '` FROM ' |
|
73
|
2 |
|
. $this->table |
|
74
|
2 |
|
. ' WHERE `' |
|
75
|
2 |
|
. self::ID_RESOURCE_KEY |
|
76
|
2 |
|
. '` = :idResource AND `' |
|
77
|
2 |
|
. self::ID_LOCALISATION_KEY |
|
78
|
2 |
|
. '` = :idLocalization' |
|
79
|
2 |
|
; |
|
80
|
2 |
|
$stmt = $this->connection->prepare($sql); |
|
81
|
2 |
|
$stmt->bindValue('idResource', $idResource); |
|
82
|
2 |
|
$stmt->bindValue('idLocalization', $idLocalization); |
|
83
|
2 |
|
$stmt->execute(); |
|
84
|
|
|
|
|
85
|
2 |
|
$l10nResultList = array(); |
|
86
|
|
|
|
|
87
|
2 |
|
while ($result = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
88
|
2 |
|
$l10nResultList[] = $result; |
|
89
|
2 |
|
} |
|
90
|
|
|
|
|
91
|
2 |
|
$l10nResource = null; |
|
92
|
|
|
|
|
93
|
2 |
|
if (!empty($l10nResultList)) { |
|
94
|
2 |
|
$valueList = array(); |
|
95
|
2 |
|
foreach ($l10nResultList as $l10nResult) { |
|
96
|
2 |
View Code Duplication |
if (isset($l10nResult[self::LOCALE_KEY])) { |
|
97
|
1 |
|
$valueList[$l10nResult[self::LOCALE_KEY]] = $l10nResult[self::VALUE_KEY]; |
|
98
|
1 |
|
} else { |
|
99
|
1 |
|
$valueList[] = $l10nResult[self::VALUE_KEY]; |
|
100
|
|
|
} |
|
101
|
2 |
|
} |
|
102
|
2 |
|
$l10nResource = new L10nResource(); |
|
103
|
2 |
|
$l10nResource->setIdLocalization($idLocalization); |
|
104
|
2 |
|
$l10nResource->setIdResource($idResource); |
|
105
|
2 |
|
$l10nResource->setValueList($valueList); |
|
106
|
2 |
|
} |
|
107
|
|
|
|
|
108
|
2 |
|
return $l10nResource; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Return all L10nResources |
|
113
|
|
|
* |
|
114
|
|
|
* @return L10nResource[] $l10nResource |
|
115
|
|
|
*/ |
|
116
|
2 |
|
public function getAllL10nResourceList() |
|
117
|
|
|
{ |
|
118
|
|
|
$sql = 'SELECT `' |
|
119
|
|
|
. self::ID_RESOURCE_KEY |
|
120
|
2 |
|
. '`, `' |
|
121
|
2 |
|
. self::ID_LOCALISATION_KEY |
|
122
|
2 |
|
. '`, `' |
|
123
|
2 |
|
. self::LOCALE_KEY |
|
124
|
2 |
|
. '`, `' |
|
125
|
2 |
|
. self::VALUE_KEY |
|
126
|
2 |
|
. '` FROM ' |
|
127
|
2 |
|
. $this->table |
|
128
|
2 |
|
; |
|
129
|
2 |
|
$stmt = $this->connection->prepare($sql); |
|
130
|
2 |
|
$stmt->execute(); |
|
131
|
2 |
|
$l10nResultList = array(); |
|
132
|
2 |
|
while ($result = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
133
|
2 |
|
$l10nResultList[] = $result; |
|
134
|
2 |
|
} |
|
135
|
|
|
|
|
136
|
2 |
|
$l10nResourceList = array(); |
|
137
|
|
|
|
|
138
|
|
|
// tree list to manage multiples SQL entries |
|
139
|
|
|
// which represent the same resource |
|
140
|
|
|
// with several locales |
|
141
|
2 |
|
$l10nResourceTreeList = array(); |
|
142
|
|
|
|
|
143
|
2 |
|
if (!empty($l10nResultList)) { |
|
144
|
2 |
|
foreach ($l10nResultList as $l10nResult) { |
|
145
|
2 |
|
if (!isset($l10nResourceTreeList[$l10nResult[self::ID_RESOURCE_KEY]])) { |
|
146
|
2 |
|
$l10nResourceTreeList[$l10nResult[self::ID_RESOURCE_KEY]] = array(); |
|
147
|
2 |
|
} |
|
148
|
2 |
View Code Duplication |
if (!isset($l10nResourceTreeList[$l10nResult[self::ID_RESOURCE_KEY]][$l10nResult[self::ID_LOCALISATION_KEY]])) { |
|
149
|
2 |
|
$l10nResourceTreeList[$l10nResult[self::ID_RESOURCE_KEY]][$l10nResult[self::ID_LOCALISATION_KEY]] = array(); |
|
150
|
2 |
|
} |
|
151
|
2 |
|
$valueList = array(); |
|
152
|
2 |
|
if (isset($l10nResult[self::LOCALE_KEY])) { |
|
153
|
|
|
$l10nResourceTreeList |
|
154
|
1 |
|
[$l10nResult[self::ID_RESOURCE_KEY]] |
|
155
|
1 |
|
[$l10nResult[self::ID_LOCALISATION_KEY]] |
|
156
|
1 |
|
[$l10nResult[self::LOCALE_KEY]] |
|
157
|
1 |
|
= $l10nResult[self::VALUE_KEY]; |
|
158
|
1 |
|
} else { |
|
159
|
|
|
$l10nResourceTreeList |
|
160
|
1 |
|
[$l10nResult[self::ID_RESOURCE_KEY]] |
|
161
|
1 |
|
[$l10nResult[self::ID_LOCALISATION_KEY]] |
|
162
|
1 |
|
[] |
|
163
|
1 |
|
= $l10nResult[self::VALUE_KEY]; |
|
164
|
|
|
} |
|
165
|
2 |
|
} |
|
166
|
|
|
|
|
167
|
|
|
// flatten the tree |
|
168
|
2 |
|
foreach ($l10nResourceTreeList as $idResource => $l10nResourceTreeRest) { |
|
169
|
2 |
|
foreach ($l10nResourceTreeRest as $idLocalization => $valueList) { |
|
170
|
2 |
|
$l10nResource = new L10nResource(); |
|
171
|
2 |
|
$l10nResource->setIdLocalization($idLocalization); |
|
172
|
2 |
|
$l10nResource->setIdResource($idResource); |
|
173
|
2 |
|
$l10nResource->setValueList($valueList); |
|
174
|
2 |
|
$l10nResourceList[] = $l10nResource; |
|
175
|
2 |
|
} |
|
176
|
2 |
|
} |
|
177
|
2 |
|
} |
|
178
|
|
|
|
|
179
|
2 |
|
return $l10nResourceList; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Update a L10nResource |
|
184
|
|
|
* |
|
185
|
|
|
* @param L10nResource $l10nResource which valueList is a list of values. |
|
186
|
|
|
* array('value') if not internationalized, |
|
187
|
|
|
* array('locale_code' => 'value', …) if internationalized |
|
188
|
|
|
*/ |
|
189
|
2 |
|
public function setL10nResource(L10nResource $l10nResource) |
|
190
|
|
|
{ |
|
191
|
2 |
|
$idResource = $l10nResource->getIdResource(); |
|
192
|
2 |
|
$idLocalization = $l10nResource->getIdLocalization(); |
|
193
|
2 |
|
$valueList = $l10nResource->getValueList(); |
|
194
|
|
|
|
|
195
|
2 |
|
$sqlValueList = array(); |
|
196
|
2 |
|
foreach ($valueList as $locale => $value) { |
|
197
|
2 |
|
$sqlValue = ':idResource, :idLocalization, '; |
|
198
|
2 |
|
if ($locale) { |
|
199
|
1 |
|
$sqlValue .= '"' . $locale . '", "'; |
|
200
|
1 |
|
} else { |
|
201
|
1 |
|
$sqlValue .= 'null, "'; |
|
202
|
|
|
} |
|
203
|
2 |
|
$sqlValueList[] = $sqlValue . $value . '"'; |
|
204
|
2 |
|
} |
|
205
|
|
|
$sql = 'DELETE FROM ' |
|
206
|
2 |
|
. $this->table |
|
207
|
2 |
|
. ' WHERE `' |
|
208
|
2 |
|
. self::ID_RESOURCE_KEY |
|
209
|
2 |
|
. '` = :idResource And `' |
|
210
|
2 |
|
. self::ID_LOCALISATION_KEY |
|
211
|
2 |
|
. '` = :idLocalization ;' |
|
212
|
2 |
|
. 'INSERT into ' |
|
213
|
2 |
|
. $this->table |
|
214
|
2 |
|
. ' (`' |
|
215
|
2 |
|
. self::ID_RESOURCE_KEY |
|
216
|
2 |
|
. '`, `' |
|
217
|
2 |
|
. self::ID_LOCALISATION_KEY |
|
218
|
2 |
|
. '`, `' |
|
219
|
2 |
|
. self::LOCALE_KEY |
|
220
|
2 |
|
. '`, `' |
|
221
|
2 |
|
. self::VALUE_KEY |
|
222
|
2 |
|
. '`) VALUES (' |
|
223
|
2 |
|
. implode('), (', $sqlValueList) |
|
224
|
2 |
|
. ')' |
|
225
|
2 |
|
; |
|
226
|
|
|
|
|
227
|
2 |
|
$stmt = $this->connection->prepare($sql); |
|
228
|
2 |
|
$stmt->bindValue('idResource', $idResource); |
|
229
|
2 |
|
$stmt->bindValue('idLocalization', $idLocalization); |
|
230
|
2 |
|
$stmt->execute(); |
|
231
|
2 |
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|