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