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