1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\Translatable\Repository; |
4
|
|
|
|
5
|
|
|
use Sludio\HelperBundle\Script\Repository\QuickInsertRepository as Quick; |
6
|
|
|
use Sludio\HelperBundle\Translatable\Entity\BaseEntity; |
7
|
|
|
use Sludio\HelperBundle\Translatable\Entity\Translation; |
8
|
|
|
use Symfony\Component\HttpKernel\HttpCache\HttpCache; |
9
|
|
|
|
10
|
|
|
class TranslatableRepository |
11
|
|
|
{ |
12
|
|
|
public static $defaultLocale; |
13
|
|
|
|
14
|
|
|
public static $redis; |
15
|
|
|
public static $table; |
16
|
|
|
public static $entityManager; |
17
|
|
|
|
18
|
|
|
public static $localeArr = [ |
19
|
|
|
'lv' => 'lv_LV', |
20
|
|
|
'en' => 'en_US', |
21
|
|
|
'ru' => 'ru_RU', |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
public static function updateTranslations($class, $locale, $field, $content, $id = 0) |
25
|
|
|
{ |
26
|
|
|
self::init($class, $className); |
27
|
|
|
$locale = self::getLocaleVar($locale); |
28
|
|
|
|
29
|
|
|
$doUpdate = false; |
30
|
|
|
if ((int)$id === 0) { |
31
|
|
|
$doUpdate = true; |
32
|
|
|
$id = Quick::findNextIdExt(self::$entityManager->getMetadataFactory()->getMetadataFor($class)); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$update = (int)self::findByLocale($class, $locale, $content, $field, null, $id); |
36
|
|
|
if ($update === 0 && $doUpdate === true) { |
37
|
|
|
$id = Quick::findNextIdExt(self::$entityManager->getMetadataFactory()->getMetadataFor($class)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$content = trim($content) !== '' ? $content : null; |
41
|
|
|
|
42
|
|
|
$translation = new Translation(); |
43
|
|
|
// @formatter:off |
44
|
|
|
$translation->setField($field) |
45
|
|
|
->setForeignKey((int)$id) |
46
|
|
|
->setLocale($locale) |
47
|
|
|
->setObjectClass($class) |
48
|
|
|
->setContent($content); |
49
|
|
|
// @formatter:on |
50
|
|
|
|
51
|
|
|
if ($update === 0) { |
52
|
|
|
Quick::persist($translation); |
53
|
|
|
} else { |
54
|
|
|
$where = [ |
55
|
|
|
'field' => $field, |
56
|
|
|
'foreign_key' => $id, |
57
|
|
|
'object_class' => $class, |
58
|
|
|
'locale' => $locale, |
59
|
|
|
]; |
60
|
|
|
$tId = Quick::get(new Translation(), true, $where); |
61
|
|
|
Quick::update($tId, $translation); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
self::getTranslations($class, $id, true); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public static function init($class = null, &$className = null) |
68
|
|
|
{ |
69
|
|
|
if ($class) { |
70
|
|
|
$class = explode('\\', $class); |
71
|
|
|
$className = end($class); |
72
|
|
|
} |
73
|
|
|
if (self::$redis) { |
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
global $kernel; |
77
|
|
|
|
78
|
|
|
if ($kernel instanceof HttpCache) { |
79
|
|
|
$kernel = $kernel->getKernel(); |
80
|
|
|
} |
81
|
|
|
$container = $kernel->getContainer(); |
82
|
|
|
self::$entityManager = $container->get('doctrine')->getManager(); |
83
|
|
|
self::$defaultLocale = $container->getParameter('sludio_helper.translatable.default_locale'); |
84
|
|
|
|
85
|
|
|
self::$redis = $container->get('snc_redis.'.$container->getParameter('sludio_helper.redis.translation')); |
86
|
|
|
self::$table = $container->getParameter('sludio_helper.translatable.table'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public static function getLocaleVar($locale) |
90
|
|
|
{ |
91
|
|
|
return isset(self::$localeArr[$locale]) ? self::$localeArr[$locale] : $locale; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public static function findByLocale($class, $locale, $content = null, $field = 'slug', $notId = null, $isId = null) |
95
|
|
|
{ |
96
|
|
|
self::init(); |
97
|
|
|
$locale = self::getLocaleVar($locale ?: self::getDefaultLocale()); |
98
|
|
|
|
99
|
|
|
$where = [ |
100
|
|
|
'object_class' => $class, |
101
|
|
|
'locale' => $locale, |
102
|
|
|
'field' => $field, |
103
|
|
|
]; |
104
|
|
|
if ($notId) { |
105
|
|
|
$where[] = ['foreign_key <> '.$notId]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if ($isId) { |
109
|
|
|
$where['foreign_key'] = $isId; |
110
|
|
|
} elseif ($content !== null) { |
111
|
|
|
$where['content'] = $content; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return Quick::get(new Translation(), false, $where, ['foreign_key']); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public static function getDefaultLocale() |
118
|
|
|
{ |
119
|
|
|
self::init(); |
120
|
|
|
|
121
|
|
|
return self::$defaultLocale; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public static function getTranslations($class, $id, $skip = false) |
125
|
|
|
{ |
126
|
|
|
$class = str_replace('Proxies\\__CG__\\', '', $class); |
127
|
|
|
self::init($class, $className); |
128
|
|
|
|
129
|
|
|
$key = strtolower($className).':translations:'.$id; |
130
|
|
|
$result = []; |
131
|
|
|
$checked = false; |
132
|
|
|
if ($skip === false) { |
133
|
|
|
self::getFromRedis($key, $result, $checked); |
134
|
|
|
} else { |
135
|
|
|
self::delFromRedis($key); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (empty($result) && !$checked) { |
139
|
|
|
$data = Quick::get(new Translation(), false, [ |
140
|
|
|
'object_class' => $class, |
141
|
|
|
'foreign_key' => $id, |
142
|
|
|
], ['*']); |
143
|
|
|
if ($data !== null) { |
144
|
|
|
/** @var $data array */ |
145
|
|
|
foreach ($data as $row) { |
146
|
|
|
$result[$row['locale']][$row['field']] = $row['content']; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
self::setToRedis($key, $result); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $result; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
private static function getFromRedis($key, &$result, &$checked) |
157
|
|
|
{ |
158
|
|
|
if (self::$redis !== null) { |
159
|
|
|
$result = unserialize(self::$redis->get(self::tKey($key))); |
160
|
|
|
$checked = unserialize(self::$redis->get(self::cKey($key))); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
private static function tKey(&$key) |
165
|
|
|
{ |
166
|
|
|
return $key.':translations'; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
private static function cKey(&$key) |
170
|
|
|
{ |
171
|
|
|
return $key.':checked'; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
private static function delFromRedis($key) |
175
|
|
|
{ |
176
|
|
|
if (self::$redis !== null) { |
177
|
|
|
self::$redis->del(self::tKey($key)); |
178
|
|
|
self::$redis->del(self::cKey($key)); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
private static function setToRedis($key, $result) |
183
|
|
|
{ |
184
|
|
|
if (!empty($result) && self::$redis !== null) { |
185
|
|
|
self::$redis->set(self::tKey($key), serialize($result)); |
186
|
|
|
self::$redis->set(self::cKey($key), serialize(true)); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public static function removeTranslations(BaseEntity $object) |
191
|
|
|
{ |
192
|
|
|
$class = \get_class($object); |
193
|
|
|
self::init($class, $className); |
194
|
|
|
$id = $object->getId(); |
195
|
|
|
|
196
|
|
|
$where = [ |
197
|
|
|
'object_class' => $class, |
198
|
|
|
'foreign_key' => $id, |
199
|
|
|
]; |
200
|
|
|
Quick::delete(new Translation(), $where); |
201
|
|
|
$key = strtolower($className).':translations:'.$id; |
202
|
|
|
self::delFromRedis($key); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public static function getAllTranslations() |
206
|
|
|
{ |
207
|
|
|
self::init(); |
208
|
|
|
$classes = Quick::get(new Translation(), false, [], ['object_class'], null, ['MODE' => 'DISTINCT']); |
209
|
|
|
if (!empty($classes)) { |
210
|
|
|
/** @var $classes array */ |
211
|
|
|
foreach ($classes as $class) { |
212
|
|
|
$ids = Quick::get(new Translation(), false, ['object_class' => $class], ['foreign_key'], null, ['MODE' => 'DISTINCT']); |
213
|
|
|
if (!empty($ids)) { |
214
|
|
|
/** @var $ids array */ |
215
|
|
|
foreach ($ids as $id) { |
216
|
|
|
self::getTranslations($class, $id, true); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|