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