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