|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* @package Breizh Smilies Categories Extension |
|
5
|
|
|
* @copyright (c) 2020-2024 Sylver35 https://breizhcode.com |
|
6
|
|
|
* @license https://opensource.org/licenses/gpl-license.php GNU Public License |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace sylver35\smiliescat\core; |
|
11
|
|
|
|
|
12
|
|
|
use phpbb\cache\driver\driver_interface as cache; |
|
13
|
|
|
use phpbb\db\driver\driver_interface as db; |
|
14
|
|
|
use phpbb\config\config; |
|
15
|
|
|
use phpbb\controller\helper; |
|
16
|
|
|
use phpbb\user; |
|
17
|
|
|
use phpbb\language\language; |
|
18
|
|
|
use phpbb\template\template; |
|
19
|
|
|
use phpbb\extension\manager; |
|
20
|
|
|
use phpbb\log\log; |
|
21
|
|
|
|
|
22
|
|
|
class category |
|
23
|
|
|
{ |
|
24
|
|
|
private const DEFAULT_CAT = 9998; |
|
25
|
|
|
private const NOT_DISPLAY = 9999; |
|
26
|
|
|
|
|
27
|
|
|
/** @var \phpbb\cache\driver\driver_interface */ |
|
28
|
|
|
protected $cache; |
|
29
|
|
|
|
|
30
|
|
|
/** @var \phpbb\db\driver\driver_interface */ |
|
31
|
|
|
protected $db; |
|
32
|
|
|
|
|
33
|
|
|
/** @var \phpbb\config\config */ |
|
34
|
|
|
protected $config; |
|
35
|
|
|
|
|
36
|
|
|
/* @var \phpbb\controller\helper */ |
|
37
|
|
|
protected $helper; |
|
38
|
|
|
|
|
39
|
|
|
/** @var \phpbb\user */ |
|
40
|
|
|
protected $user; |
|
41
|
|
|
|
|
42
|
|
|
/** @var \phpbb\language\language */ |
|
43
|
|
|
protected $language; |
|
44
|
|
|
|
|
45
|
|
|
/** @var \phpbb\template\template */ |
|
46
|
|
|
protected $template; |
|
47
|
|
|
|
|
48
|
|
|
/** @var \phpbb\extension\manager "Extension Manager" */ |
|
49
|
|
|
protected $ext_manager; |
|
50
|
|
|
|
|
51
|
|
|
/** @var \phpbb\log\log */ |
|
52
|
|
|
protected $log; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* The database tables |
|
56
|
|
|
* |
|
57
|
|
|
* @var string */ |
|
58
|
|
|
protected $smilies_category_table; |
|
59
|
|
|
|
|
60
|
|
|
/** @var string phpBB root path */ |
|
61
|
|
|
protected $root_path; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Constructor |
|
65
|
|
|
*/ |
|
66
|
|
|
public function __construct(cache $cache, db $db, config $config, helper $helper, user $user, language $language, template $template, manager $ext_manager, log $log, $smilies_category_table, $root_path) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->cache = $cache; |
|
69
|
|
|
$this->db = $db; |
|
70
|
|
|
$this->config = $config; |
|
71
|
|
|
$this->helper = $helper; |
|
72
|
|
|
$this->user = $user; |
|
73
|
|
|
$this->language = $language; |
|
74
|
|
|
$this->template = $template; |
|
75
|
|
|
$this->ext_manager = $ext_manager; |
|
76
|
|
|
$this->log = $log; |
|
77
|
|
|
$this->smilies_category_table = $smilies_category_table; |
|
78
|
|
|
$this->root_path = $root_path; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function get_version() |
|
82
|
|
|
{ |
|
83
|
|
|
if (($data = $this->cache->get('_smiliescat_version')) === false) |
|
84
|
|
|
{ |
|
85
|
|
|
$md_manager = $this->ext_manager->create_extension_metadata_manager('sylver35/smiliescat'); |
|
86
|
|
|
$meta = $md_manager->get_metadata(); |
|
87
|
|
|
|
|
88
|
|
|
$data = [ |
|
89
|
|
|
'version' => $meta['version'], |
|
90
|
|
|
'homepage' => $meta['homepage'], |
|
91
|
|
|
]; |
|
92
|
|
|
// cache for 7 days |
|
93
|
|
|
$this->cache->put('_smiliescat_version', $data, 604800); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $data; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function smilies_count($cat) |
|
100
|
|
|
{ |
|
101
|
|
|
$sql = $this->db->sql_build_query('SELECT', [ |
|
102
|
|
|
'SELECT' => 'COUNT(DISTINCT smiley_id) AS smilies_count', |
|
103
|
|
|
'FROM' => [SMILIES_TABLE => ''], |
|
104
|
|
|
'WHERE' => ($cat > 0) ? 'category = ' . $cat : "code <> ''", |
|
105
|
|
|
]); |
|
106
|
|
|
$result = $this->db->sql_query($sql); |
|
107
|
|
|
$nb = (int) $this->db->sql_fetchfield('smilies_count'); |
|
108
|
|
|
$this->db->sql_freeresult($result); |
|
109
|
|
|
|
|
110
|
|
|
return $nb; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function capitalize($var) |
|
114
|
|
|
{ |
|
115
|
|
|
return ucfirst(strtolower(trim($var))); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function get_langs() |
|
119
|
|
|
{ |
|
120
|
|
|
$data = []; |
|
121
|
|
|
$sql = 'SELECT * |
|
122
|
|
|
FROM ' . LANG_TABLE; |
|
123
|
|
|
$result = $this->db->sql_query($sql); |
|
124
|
|
|
while ($row = $this->db->sql_fetchrow($result)) |
|
125
|
|
|
{ |
|
126
|
|
|
$data[$row['lang_id']] = $row['lang_iso']; |
|
127
|
|
|
} |
|
128
|
|
|
$this->db->sql_freeresult($result); |
|
129
|
|
|
|
|
130
|
|
|
return $data; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function verify_cat_langs($langs, $cat, $i, $lang_cat, $ajax) |
|
134
|
|
|
{ |
|
135
|
|
|
$data = []; |
|
136
|
|
|
foreach ($langs as $id => $iso) |
|
137
|
|
|
{ |
|
138
|
|
|
if (!isset($lang_cat[$cat][$id])) |
|
139
|
|
|
{ |
|
140
|
|
|
$data[] = $iso; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
if ($ajax === false) |
|
145
|
|
|
{ |
|
146
|
|
|
$this->cat_to_template($i, $data); |
|
147
|
|
|
} |
|
148
|
|
|
else |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->cat_to_ajax($i, $data); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
private function cat_to_template($i, $data) |
|
155
|
|
|
{ |
|
156
|
|
|
if (($i !== 0) && !empty($data)) |
|
157
|
|
|
{ |
|
158
|
|
|
$this->template->assign_block_vars('categories', [ |
|
159
|
|
|
'ERROR' => true, |
|
160
|
|
|
'LANG_EMPTY' => $this->language->lang('SC_LANGUAGE_EMPTY', (count($data) > 1) ? 2 : 1) . implode(', ', $data), |
|
161
|
|
|
]); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
private function cat_to_ajax($i, $data) |
|
166
|
|
|
{ |
|
167
|
|
|
$values = [ |
|
168
|
|
|
'error' => false, |
|
169
|
|
|
'lang_empty' => '', |
|
170
|
|
|
]; |
|
171
|
|
|
|
|
172
|
|
|
if (($i !== 0) && !empty($data)) |
|
173
|
|
|
{ |
|
174
|
|
|
$values = [ |
|
175
|
|
|
'error' => true, |
|
176
|
|
|
'lang_empty' => $this->language->lang('SC_LANGUAGE_EMPTY', (count($data) > 1) ? 2 : 1) . implode(', ', $data), |
|
177
|
|
|
]; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return $values; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
public function category_sort($category) |
|
184
|
|
|
{ |
|
185
|
|
|
// Determine the type of categories |
|
186
|
|
|
switch ($category) |
|
187
|
|
|
{ |
|
188
|
|
|
case self::DEFAULT_CAT: |
|
189
|
|
|
$sort = 2;// Unclassified category |
|
190
|
|
|
break; |
|
191
|
|
|
case self::NOT_DISPLAY: |
|
192
|
|
|
$sort = 3;// Undisplayed category |
|
193
|
|
|
break; |
|
194
|
|
|
default: |
|
195
|
|
|
$sort = 1;// user category |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
return $sort; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
public function category_exist() |
|
202
|
|
|
{ |
|
203
|
|
|
$sql = 'SELECT COUNT(cat_id) AS total |
|
204
|
|
|
FROM ' . $this->smilies_category_table; |
|
205
|
|
|
$result = $this->db->sql_query($sql); |
|
206
|
|
|
$total = (int) $this->db->sql_fetchfield('total'); |
|
207
|
|
|
$this->db->sql_freeresult($result); |
|
208
|
|
|
|
|
209
|
|
|
return $total; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
public function return_name($cat, $name = '', $all = false) |
|
213
|
|
|
{ |
|
214
|
|
|
if ($cat == self::DEFAULT_CAT) |
|
215
|
|
|
{ |
|
216
|
|
|
return $this->language->lang('SC_CATEGORY_DEFAUT'); |
|
217
|
|
|
} |
|
218
|
|
|
if ($cat == self::NOT_DISPLAY) |
|
219
|
|
|
{ |
|
220
|
|
|
return $this->language->lang('SC_CATEGORY_NOT'); |
|
221
|
|
|
} |
|
222
|
|
|
else if ($all) |
|
223
|
|
|
{ |
|
224
|
|
|
return $this->cat_name($cat); |
|
225
|
|
|
} |
|
226
|
|
|
else if ($name) |
|
227
|
|
|
{ |
|
228
|
|
|
return $name; |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
private function cat_name($cat) |
|
233
|
|
|
{ |
|
234
|
|
|
$lang = (string) $this->user->lang_name; |
|
235
|
|
|
$sql = 'SELECT cat_name |
|
236
|
|
|
FROM ' . $this->smilies_category_table . " |
|
237
|
|
|
WHERE cat_id = $cat |
|
238
|
|
|
AND cat_lang = '$lang'"; |
|
239
|
|
|
$result = $this->db->sql_query_limit($sql, 1); |
|
240
|
|
|
$cat_name = (string) $this->db->sql_fetchfield('cat_name'); |
|
241
|
|
|
$this->db->sql_freeresult($result); |
|
242
|
|
|
if ($cat_name) |
|
243
|
|
|
{ |
|
244
|
|
|
return $cat_name; |
|
245
|
|
|
} |
|
246
|
|
|
else |
|
247
|
|
|
{ |
|
248
|
|
|
$sql2 = 'SELECT cat_name |
|
249
|
|
|
FROM ' . $this->smilies_category_table . " |
|
250
|
|
|
WHERE cat_id = $cat |
|
251
|
|
|
AND cat_lang = 'en'"; |
|
252
|
|
|
$result2 = $this->db->sql_query_limit($sql2, 1); |
|
253
|
|
|
$cat_name = (string) $this->db->sql_fetchfield('cat_name'); |
|
254
|
|
|
$this->db->sql_freeresult($result2); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
return $cat_name; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
public function get_max_order() |
|
261
|
|
|
{ |
|
262
|
|
|
// Get max order id... |
|
263
|
|
|
$sql = 'SELECT MAX(cat_order) AS maxi |
|
264
|
|
|
FROM ' . $this->smilies_category_table; |
|
265
|
|
|
$result = $this->db->sql_query($sql); |
|
266
|
|
|
$max = (int) $this->db->sql_fetchfield('maxi'); |
|
267
|
|
|
$this->db->sql_freeresult($result); |
|
268
|
|
|
|
|
269
|
|
|
return $max; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
public function get_max_id() |
|
273
|
|
|
{ |
|
274
|
|
|
// Get max id... |
|
275
|
|
|
$sql = 'SELECT MAX(cat_id) AS id_max |
|
276
|
|
|
FROM ' . $this->smilies_category_table; |
|
277
|
|
|
$result = $this->db->sql_query($sql); |
|
278
|
|
|
$id_max = (int) $this->db->sql_fetchfield('id_max'); |
|
279
|
|
|
$this->db->sql_freeresult($result); |
|
280
|
|
|
|
|
281
|
|
|
return $id_max; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
public function get_first_order() |
|
285
|
|
|
{ |
|
286
|
|
|
// Get first order id non empty... |
|
287
|
|
|
$sql = 'SELECT cat_id, cat_order, cat_nb |
|
288
|
|
|
FROM ' . $this->smilies_category_table . ' |
|
289
|
|
|
WHERE cat_nb > 0 |
|
290
|
|
|
ORDER BY cat_order ASC'; |
|
291
|
|
|
$result = $this->db->sql_query_limit($sql, 1); |
|
292
|
|
|
$first = (int) $this->db->sql_fetchfield('cat_id'); |
|
293
|
|
|
$this->db->sql_freeresult($result); |
|
294
|
|
|
$first = !$first ? self::DEFAULT_CAT : $first; |
|
295
|
|
|
|
|
296
|
|
|
return $first; |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
public function get_cat_id($id) |
|
300
|
|
|
{ |
|
301
|
|
|
$sql = 'SELECT category |
|
302
|
|
|
FROM ' . SMILIES_TABLE . ' |
|
303
|
|
|
WHERE smiley_id = ' . $id; |
|
304
|
|
|
$result = $this->db->sql_query($sql); |
|
305
|
|
|
$category = (int) $this->db->sql_fetchfield('category'); |
|
306
|
|
|
$this->db->sql_freeresult($result); |
|
307
|
|
|
|
|
308
|
|
|
return $category; |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
public function get_cat_nb($id) |
|
312
|
|
|
{ |
|
313
|
|
|
$sql = 'SELECT cat_nb |
|
314
|
|
|
FROM ' . $this->smilies_category_table . ' |
|
315
|
|
|
WHERE cat_id = ' . $id; |
|
316
|
|
|
$result = $this->db->sql_query($sql); |
|
317
|
|
|
$cat_nb = (int) $this->db->sql_fetchfield('cat_nb'); |
|
318
|
|
|
$this->db->sql_freeresult($result); |
|
319
|
|
|
|
|
320
|
|
|
return $cat_nb; |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
public function extract_list_categories($cat) |
|
324
|
|
|
{ |
|
325
|
|
|
$title = ''; |
|
326
|
|
|
$cat_order = $i = 0; |
|
327
|
|
|
$lang = (string) $this->user->lang_name; |
|
328
|
|
|
$sql = $this->db->sql_build_query('SELECT', [ |
|
329
|
|
|
'SELECT' => '*', |
|
330
|
|
|
'FROM' => [$this->smilies_category_table => ''], |
|
331
|
|
|
'WHERE' => "cat_nb <> 0 AND cat_lang = '$lang'", |
|
332
|
|
|
'ORDER_BY' => 'cat_order ASC', |
|
333
|
|
|
]); |
|
334
|
|
|
$result = $this->db->sql_query($sql); |
|
335
|
|
|
while ($row = $this->db->sql_fetchrow($result)) |
|
336
|
|
|
{ |
|
337
|
|
|
$actual_cat = (int) $row['cat_id'] === $cat; |
|
338
|
|
|
$this->template->assign_block_vars('categories', [ |
|
339
|
|
|
'CLASS' => $actual_cat ? 'cat-active' : 'cat-inactive', |
|
340
|
|
|
'SEPARATE' => ($i > 0) ? ' - ' : '', |
|
341
|
|
|
'CAT_NAME' => $row['cat_name'] ?: $row['cat_title'], |
|
342
|
|
|
'CAT_ORDER' => $row['cat_order'], |
|
343
|
|
|
'CAT_ID' => $row['cat_id'], |
|
344
|
|
|
'CAT_NB' => $row['cat_nb'], |
|
345
|
|
|
'U_CAT' => $this->helper->route('sylver35_smiliescat_smilies_pop', ['select' => $row['cat_id']]), |
|
346
|
|
|
]); |
|
347
|
|
|
$i++; |
|
348
|
|
|
|
|
349
|
|
|
// Keep these values in memory |
|
350
|
|
|
$title = $actual_cat ? $row['cat_name'] : $title; |
|
351
|
|
|
$cat_order = $row['cat_order']; |
|
352
|
|
|
} |
|
353
|
|
|
$this->db->sql_freeresult($result); |
|
354
|
|
|
|
|
355
|
|
|
// Add the Unclassified category if not empty |
|
356
|
|
|
if ($nb = $this->smilies_count(self::DEFAULT_CAT)) |
|
357
|
|
|
{ |
|
358
|
|
|
$title = ($cat == self::DEFAULT_CAT) ? $this->language->lang('SC_CATEGORY_DEFAUT') : $title; |
|
359
|
|
|
$this->template->assign_block_vars('categories', [ |
|
360
|
|
|
'CLASS' => ($cat === self::DEFAULT_CAT) ? 'cat-active' : 'cat-inactive', |
|
361
|
|
|
'SEPARATE' => ($i > 0) ? ' - ' : '', |
|
362
|
|
|
'CAT_NAME' => $this->language->lang('SC_CATEGORY_DEFAUT'), |
|
363
|
|
|
'CAT_ORDER' => $cat_order + 1, |
|
364
|
|
|
'CAT_ID' => self::DEFAULT_CAT, |
|
365
|
|
|
'CAT_NB' => $nb, |
|
366
|
|
|
'U_CAT' => $this->helper->route('sylver35_smiliescat_smilies_pop', ['select' => self::DEFAULT_CAT]), |
|
367
|
|
|
]); |
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
|
|
return $title; |
|
371
|
|
|
} |
|
372
|
|
|
|
|
373
|
|
|
public function set_order($action, $current_order) |
|
374
|
|
|
{ |
|
375
|
|
|
// Never move up the first |
|
376
|
|
|
if ($current_order === 1 && $action === 'move_up') |
|
377
|
|
|
{ |
|
378
|
|
|
return 0; |
|
379
|
|
|
} |
|
380
|
|
|
|
|
381
|
|
|
// Never move down the last |
|
382
|
|
|
$max_order = $this->get_max_order(); |
|
383
|
|
|
if (($current_order === $max_order) && ($action === 'move_down')) |
|
384
|
|
|
{ |
|
385
|
|
|
return 0; |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
// on move_down, switch position with next order_id... |
|
389
|
|
|
// on move_up, switch position with previous order_id... |
|
390
|
|
|
$switch_order_id = ($action === 'move_down') ? $current_order + 1 : $current_order - 1; |
|
391
|
|
|
|
|
392
|
|
|
// Return the new position |
|
393
|
|
|
return $switch_order_id; |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
|
|
public function move_cat($id, $action) |
|
397
|
|
|
{ |
|
398
|
|
|
// Get current order id and title... |
|
399
|
|
|
$sql = 'SELECT cat_order, cat_title |
|
400
|
|
|
FROM ' . $this->smilies_category_table . ' |
|
401
|
|
|
WHERE cat_id = ' . $id; |
|
402
|
|
|
$result = $this->db->sql_query($sql); |
|
403
|
|
|
$row = $this->db->sql_fetchrow($result); |
|
404
|
|
|
$current_order = (int) $row['cat_order']; |
|
405
|
|
|
$title = $row['cat_title']; |
|
406
|
|
|
$this->db->sql_freeresult($result); |
|
407
|
|
|
|
|
408
|
|
|
$switch_order_id = (int) $this->set_order($action, $current_order); |
|
409
|
|
|
if ($switch_order_id === 0) |
|
410
|
|
|
{ |
|
411
|
|
|
return; |
|
412
|
|
|
} |
|
413
|
|
|
|
|
414
|
|
|
$this->db->sql_query('UPDATE ' . $this->smilies_category_table . " SET cat_order = $current_order WHERE cat_order = $switch_order_id AND cat_id <> $id"); |
|
415
|
|
|
$move_executed = (bool) $this->db->sql_affectedrows(); |
|
416
|
|
|
|
|
417
|
|
|
// Only update the other entry too if the previous entry got updated |
|
418
|
|
|
if ($move_executed) |
|
419
|
|
|
{ |
|
420
|
|
|
$this->db->sql_query('UPDATE ' . $this->smilies_category_table . " SET cat_order = $switch_order_id WHERE cat_order = $current_order AND cat_id = $id"); |
|
421
|
|
|
} |
|
422
|
|
|
|
|
423
|
|
|
$this->config->set('smilies_first_cat', $this->get_first_order()); |
|
424
|
|
|
|
|
425
|
|
|
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_' . strtoupper($action) . '_CAT', time(), [$title]); |
|
426
|
|
|
} |
|
427
|
|
|
} |
|
428
|
|
|
|