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