1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package CleverStyle Framework |
4
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
5
|
|
|
* @copyright Copyright (c) 2011-2017, Nazar Mokrynskyi |
6
|
|
|
* @license MIT License, see license.txt |
7
|
|
|
*/ |
8
|
|
|
namespace cs; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @method static $this instance($check = false) |
12
|
|
|
*/ |
13
|
|
|
class Text { |
14
|
|
|
use Singleton; |
15
|
|
|
/** |
16
|
|
|
* Gets text on current language |
17
|
|
|
* |
18
|
|
|
* @param int $database |
19
|
|
|
* @param int|null $id Getting may be done with group and label or with id |
20
|
|
|
* @param bool $store_in_cache If `true` - text will be stored in cache |
21
|
|
|
* |
22
|
|
|
* @return false|string |
23
|
|
|
*/ |
24
|
12 |
|
public function get ($database, $id = null, $store_in_cache = false) { |
25
|
12 |
|
$Cache = Cache::instance(); |
26
|
12 |
|
$L = Language::instance(); |
27
|
12 |
|
$id = (int)$id; |
28
|
12 |
|
$cache_key = "texts/$database/{$id}_$L->clang"; |
29
|
12 |
|
if ($store_in_cache && ($text = $Cache->$cache_key) !== false) { |
30
|
6 |
|
return $text; |
31
|
|
|
} |
32
|
12 |
|
$text = $this->get_text_by_id($database, $L->clang, $id); |
33
|
12 |
|
if ($text === false) { |
34
|
2 |
|
return false; |
35
|
|
|
} |
36
|
12 |
|
if ($store_in_cache) { |
37
|
12 |
|
$Cache->$cache_key = $text; |
38
|
|
|
} |
39
|
12 |
|
return $text; |
40
|
|
|
} |
41
|
|
|
/** |
42
|
|
|
* @param int $database |
43
|
|
|
* @param string $clang |
44
|
|
|
* @param int $id |
45
|
|
|
* |
46
|
|
|
* @return false|string |
47
|
|
|
*/ |
48
|
12 |
|
protected function get_text_by_id ($database, $clang, $id) { |
49
|
12 |
|
$cdb = DB::instance()->db($database); |
50
|
12 |
|
$text = $cdb->qfs( |
51
|
|
|
"SELECT `d`.`text` |
52
|
|
|
FROM `[prefix]texts` AS `t` |
53
|
|
|
LEFT JOIN `[prefix]texts_data` AS `d` |
54
|
|
|
ON `t`.`id` = `d`.`id` |
55
|
|
|
WHERE |
56
|
12 |
|
`t`.`id` = $id AND |
57
|
|
|
`d`.`lang` = '%s' |
58
|
|
|
LIMIT 1", |
59
|
12 |
|
$clang |
60
|
|
|
); |
61
|
12 |
|
if (!$text) { |
62
|
12 |
|
$text = $cdb->qfs( |
63
|
|
|
"SELECT `d`.`text` |
64
|
|
|
FROM `[prefix]texts` AS `t` |
65
|
|
|
LEFT JOIN `[prefix]texts_data` AS `d` |
66
|
|
|
ON `t`.`id` = `d`.`id` |
67
|
12 |
|
WHERE `t`.`id` = $id |
68
|
|
|
LIMIT 1" |
69
|
|
|
); |
70
|
|
|
} |
71
|
12 |
|
return $text; |
72
|
|
|
} |
73
|
|
|
/** |
74
|
|
|
* Sets text on current language |
75
|
|
|
* |
76
|
|
|
* @param int $database |
77
|
|
|
* @param string $group |
78
|
|
|
* @param string $label |
79
|
|
|
* @param string $text |
80
|
|
|
* |
81
|
|
|
* @return false|string If multilingual support enabled or was enabled and then disabled but translations remains - returns {¶<i>id</i>}, otherwise returns |
82
|
|
|
* original text |
83
|
|
|
*/ |
84
|
10 |
|
public function set ($database, $group, $label, $text) { |
85
|
10 |
|
$Cache = Cache::instance(); |
86
|
10 |
|
$Config = Config::instance(); |
87
|
10 |
|
$L = Language::instance(); |
88
|
10 |
|
$cdb = DB::instance()->db_prime($database); |
89
|
|
|
/** |
90
|
|
|
* Security check, do not allow to silently substitute text from another item |
91
|
|
|
*/ |
92
|
10 |
|
if (preg_match('/^\{¶(\d+)\}$/', $text)) { |
93
|
2 |
|
return false; |
94
|
|
|
} |
95
|
|
|
// Find existing text id |
96
|
10 |
|
$id = $cdb->qfs( |
97
|
10 |
|
"SELECT `id` |
98
|
|
|
FROM `[prefix]texts` |
99
|
|
|
WHERE |
100
|
|
|
`label` = '%s' AND |
101
|
|
|
`group` = '%s' |
102
|
|
|
LIMIT 1", |
103
|
10 |
|
$label, |
104
|
10 |
|
$group |
105
|
|
|
); |
106
|
10 |
|
if (!$id) { |
107
|
|
|
// If not found - either return text directly or add new text entry and obtain id |
108
|
10 |
|
if (!$Config->core['multilingual']) { |
109
|
4 |
|
return $text; |
110
|
|
|
} else { |
111
|
8 |
|
$cdb->q( |
112
|
8 |
|
"INSERT INTO `[prefix]texts` |
113
|
|
|
( |
114
|
|
|
`label`, |
115
|
|
|
`group` |
116
|
|
|
) VALUES ( |
117
|
|
|
'%s', |
118
|
|
|
'%s' |
119
|
|
|
)", |
120
|
8 |
|
$label, |
121
|
8 |
|
$group |
122
|
|
|
); |
123
|
8 |
|
$id = $cdb->id(); |
124
|
8 |
|
if (!$id) { |
125
|
|
|
return $text; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
8 |
|
$result = $this->set_text($id, $text, $cdb, $L->clang); |
130
|
8 |
|
$Cache->del("texts/$database/{$id}_$L->clang"); |
131
|
8 |
|
return $result; |
132
|
|
|
} |
133
|
|
|
/** |
134
|
|
|
* @param int $id |
135
|
|
|
* @param string $text |
136
|
|
|
* @param DB\_Abstract $cdb |
137
|
|
|
* @param string $clang |
138
|
|
|
* |
139
|
|
|
* @return mixed |
140
|
|
|
*/ |
141
|
8 |
|
protected function set_text ($id, $text, $cdb, $clang) { |
142
|
8 |
|
$exists_for_current_language = $cdb->qfs( |
143
|
8 |
|
"SELECT `id` |
144
|
|
|
FROM `[prefix]texts_data` |
145
|
|
|
WHERE |
146
|
|
|
`id` = '%s' AND |
147
|
|
|
`lang` = '%s' |
148
|
|
|
LIMIT 1", |
149
|
8 |
|
$id, |
150
|
8 |
|
$clang |
151
|
|
|
); |
152
|
8 |
|
if ($exists_for_current_language) { |
153
|
6 |
|
$result = $cdb->q( |
154
|
6 |
|
"UPDATE `[prefix]texts_data` |
155
|
|
|
SET |
156
|
|
|
`text` = '%s', |
157
|
|
|
`text_md5` = '%s' |
158
|
|
|
WHERE |
159
|
|
|
`id` = '%s' AND |
160
|
|
|
`lang` = '%s'", |
161
|
6 |
|
$text, |
162
|
6 |
|
md5($text), |
163
|
6 |
|
$id, |
164
|
6 |
|
$clang |
165
|
|
|
); |
166
|
|
|
} else { |
167
|
8 |
|
$result = $cdb->q( |
168
|
8 |
|
"INSERT INTO `[prefix]texts_data` |
169
|
|
|
( |
170
|
|
|
`id`, |
171
|
|
|
`id_`, |
172
|
|
|
`lang`, |
173
|
|
|
`text`, |
174
|
|
|
`text_md5` |
175
|
|
|
) VALUES ( |
176
|
|
|
'%s', |
177
|
|
|
'%s', |
178
|
|
|
'%s', |
179
|
|
|
'%s', |
180
|
|
|
'%s' |
181
|
|
|
)", |
182
|
8 |
|
$id, |
183
|
8 |
|
"{¶$id}", |
184
|
8 |
|
$clang, |
185
|
8 |
|
$text, |
186
|
8 |
|
md5($text) |
187
|
|
|
); |
188
|
|
|
} |
189
|
8 |
|
return $result ? "{¶$id}" : false; |
190
|
|
|
} |
191
|
|
|
/** |
192
|
|
|
* Deletes text on all languages |
193
|
|
|
* |
194
|
|
|
* @param int $database |
195
|
|
|
* @param string $group |
196
|
|
|
* @param string $label |
197
|
|
|
* |
198
|
|
|
* @return bool |
199
|
|
|
*/ |
200
|
2 |
|
public function del ($database, $group, $label) { |
201
|
2 |
|
$Cache = Cache::instance(); |
202
|
2 |
|
$cdb = DB::instance()->db_prime($database); |
203
|
2 |
|
$id = $cdb->qfs( |
204
|
2 |
|
"SELECT `id` |
205
|
|
|
FROM `[prefix]texts` |
206
|
|
|
WHERE |
207
|
|
|
`group` = '%s' AND |
208
|
|
|
`label` = '%s' |
209
|
|
|
LIMIT 1", |
210
|
2 |
|
$group, |
211
|
2 |
|
$label |
212
|
|
|
); |
213
|
2 |
|
if ($id) { |
214
|
2 |
|
$L = Language::instance(); |
215
|
2 |
|
$result = $cdb->q( |
216
|
|
|
[ |
217
|
2 |
|
"DELETE FROM `[prefix]texts` |
218
|
|
|
WHERE `id` = '%s'", |
219
|
|
|
"DELETE FROM `[prefix]texts_data` |
220
|
|
|
WHERE `id` = '%s'" |
221
|
|
|
], |
222
|
2 |
|
$id |
223
|
|
|
); |
224
|
2 |
|
$Cache->del("texts/$database/{$id}_$L->clang"); |
225
|
2 |
|
return $result; |
226
|
|
|
} |
227
|
2 |
|
return true; |
228
|
|
|
} |
229
|
|
|
/** |
230
|
|
|
* Process text, and replace {¶([0-9]+)} on real text, is used before showing multilingual information |
231
|
|
|
* |
232
|
|
|
* @param int $database |
233
|
|
|
* @param string|string[] $data |
234
|
|
|
* @param bool $store_in_cache If <b>true</b> - text will be stored in cache |
235
|
|
|
* |
236
|
|
|
* @return string|string[] |
237
|
|
|
*/ |
238
|
18 |
|
public function process ($database, $data, $store_in_cache = false) { |
239
|
18 |
|
if (is_array($data)) { |
240
|
2 |
|
foreach ($data as &$d) { |
241
|
2 |
|
$d = $this->process($database, $d); |
242
|
|
|
} |
243
|
2 |
|
return $data; |
244
|
|
|
} |
245
|
18 |
|
return preg_replace_callback( |
246
|
18 |
|
'/^\{¶(\d+)\}$/', |
247
|
18 |
|
function ($input) use ($database, $store_in_cache) { |
248
|
12 |
|
return $this->get($database, $input[1], $store_in_cache); |
249
|
18 |
|
}, |
250
|
18 |
|
$data |
251
|
|
|
); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|