Passed
Push — master ( 8a6a30...214e27 )
by Sylver
03:52
created

diffusion::extract_first_cat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 23
nc 2
nop 0
dl 0
loc 33
rs 9.552
c 0
b 0
f 0
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 sylver35\smiliescat\core\category;
13
use phpbb\db\driver\driver_interface as db;
14
use phpbb\config\config;
15
use phpbb\user;
16
use phpbb\language\language;
17
use phpbb\template\template;
18
use phpbb\controller\helper;
19
use phpbb\log\log;
20
21
class diffusion
22
{
23
	private const DEFAULT_CAT = 9998;
24
	private const NOT_DISPLAY = 9999;
25
26
	/* @var \sylver35\smiliescat\core\category */
27
	protected $category;
28
29
	/** @var \phpbb\db\driver\driver_interface */
30
	protected $db;
31
32
	/** @var \phpbb\config\config */
33
	protected $config;
34
35
	/** @var \phpbb\user */
36
	protected $user;
37
38
	/** @var \phpbb\language\language */
39
	protected $language;
40
41
	/** @var \phpbb\template\template */
42
	protected $template;
43
44
	/* @var \phpbb\controller\helper */
45
	protected $helper;
46
47
	/** @var \phpbb\log\log */
48
	protected $log;
49
50
	/**
51
	 * The database tables
52
	 *
53
	 * @var string */
54
	protected $smilies_category_table;
55
56
	/**
57
	 * Constructor
58
	 */
59
	public function __construct(category $category, db $db, config $config, user $user, language $language, template $template, helper $helper, log $log, $smilies_category_table)
60
	{
61
		$this->category = $category;
62
		$this->db = $db;
63
		$this->config = $config;
64
		$this->user = $user;
65
		$this->language = $language;
66
		$this->template = $template;
67
		$this->helper = $helper;
68
		$this->log = $log;
69
		$this->smilies_category_table = $smilies_category_table;
70
	}
71
72
	public function url_to_page()
73
	{
74
		$this->template->assign_var('U_CATEGORY_POPUP', $this->helper->route('sylver35_smiliescat_smilies_pop', ['select' => $this->config['smilies_first_cat']]));
75
	}
76
77
	public function cats_to_posting_form($event)
78
	{
79
		if (in_array($event['mode'], ['post', 'reply', 'edit', 'quote', 'rules']))
80
		{
81
			$this->template->assign_vars([
82
				'U_CATEGORY_AJAX'	=> $this->helper->route('sylver35_smiliescat_ajax_smilies'),
83
				'ID_FIRST_CAT'		=> $this->config['smilies_first_cat'],
84
				'NB_FIRST_CAT'		=> $this->category->smilies_count($this->config['smilies_first_cat']),
85
				'PER_PAGE'			=> $this->config['smilies_per_page_cat'],
86
				'U_SMILIES_PATH'	=> generate_board_url() . '/' . $this->config['smilies_path'] . '/',
87
				'IN_CATEGORIES'		=> true,
88
			]);
89
		}
90
	}
91
92
	public function delete_categories_lang($lang)
93
	{
94
		$i = 0;
95
		$list = [];
96
		$sql = 'SELECT cat_name
97
			FROM ' . $this->smilies_category_table . "
98
				WHERE cat_lang = '$lang'
99
				ORDER BY cat_order ASC";
100
		$result = $this->db->sql_query($sql);
101
		while ($row = $this->db->sql_fetchrow($result))
102
		{
103
			$list[$i] = $row['cat_name'];
104
			$i++;
105
		}
106
		$list = implode(', ', $list);
107
		
108
		$this->db->sql_query('DELETE FROM ' . $this->smilies_category_table . " WHERE cat_lang = '$lang'");
109
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_DELETE_CAT_LANG', time(), [$lang, $list]);
110
	}
111
112
	public function list_cats($cat)
113
	{
114
		$i = 0;
115
		$list_cat = [];
116
		$lang = (string) $this->user->lang_name;
117
118
		$sql = 'SELECT * 
119
			FROM ' . $this->smilies_category_table . "
120
				WHERE cat_lang = '$lang' AND cat_nb > 0
121
				ORDER BY cat_order ASC";
122
		$result = $this->db->sql_query($sql);
123
		while ($row = $this->db->sql_fetchrow($result))
124
		{
125
			$list_cat[$i] = [
126
				'cat_id'		=> (int) $row['cat_id'],
127
				'cat_nb'		=> (int) $row['cat_nb'],
128
				'cat_name'		=> (string) $row['cat_name'],
129
				'css'			=> ($row['cat_id'] == $cat) ? 'cat-active' : 'cat-inactive',
130
			];
131
			$i++;
132
		}
133
		$this->db->sql_freeresult($result);
134
135
		// Add the Unclassified category if not empty
136
		if ($nb = $this->category->smilies_count(self::DEFAULT_CAT))
137
		{
138
			$list_cat[$i] = [
139
				'cat_id'		=> self::DEFAULT_CAT,
140
				'cat_nb'		=> $nb,
141
				'cat_name'		=> $this->language->lang('SC_CATEGORY_DEFAUT'),
142
				'css'			=> ($cat == self::DEFAULT_CAT) ? 'cat-active' : 'cat-inactive',
143
			];
144
			$i++;
145
		}
146
147
		return ['list_cat' => $list_cat, 'nb_cats' => $i];
148
	}
149
150
	public function smilies_popup($cat, $start)
151
	{
152
		if ($cat)
153
		{
154
			$i = 0;
155
			$smilies = [];
156
			$pagin = (int) $this->config['shout_smilies_per_page'];
157
158
			$sql = [
159
				'SELECT'	=> 'smiley_id, smiley_url, code, smiley_order, emotion, smiley_width, smiley_height',
160
				'FROM'		=> [SMILIES_TABLE => ''],
161
				'WHERE'		=> 'display_on_cat = 1 AND category = ' . $cat,
162
				'ORDER_BY'	=> 'smiley_order ASC',
163
			];
164
			$result = $this->db->sql_query_limit($this->db->sql_build_query('SELECT', $sql), $pagin, $start);
165
			while ($row = $this->db->sql_fetchrow($result))
166
			{
167
				$smilies[$i] = [
168
					'nb'		=> (int) $i,
169
					'code'		=> (string) $row['code'],
170
					'emotion'	=> (string) $row['emotion'],
171
					'image'		=> (string) $row['smiley_url'],
172
					'width'		=> (int) $row['smiley_width'],
173
					'height'	=> (int) $row['smiley_height'],
174
				];
175
				$i++;
176
			}
177
			$this->db->sql_freeresult($result);
178
179
			return [
180
				'in_cat'		=> true,
181
				'total'			=> $i,
182
				'cat'			=> $cat,
183
				'smilies'		=> $smilies,
184
				'emptyRow'		=> ($i === 0) ? $this->language->lang('SC_SMILIES_EMPTY_CATEGORY') : '',
185
				'title'			=> $this->language->lang('SC_CATEGORY_IN', '<span class="cat-title">' . $this->category->return_name($cat, '', true) . '</span>'),
186
				'start'			=> $start,
187
				'pagination'	=> $this->category->smilies_count($cat),
188
			];
189
		}
190
191
		return ['in_cat' => false];
192
	}
193
194
	public function extract_list_categories($cat)
195
	{
196
		$title = '';
197
		$i = 0;
198
		$lang = (string) $this->user->lang_name;
199
		$sql = $this->db->sql_build_query('SELECT', [
200
			'SELECT'	=> '*',
201
			'FROM'		=> [$this->smilies_category_table => ''],
202
			'WHERE'		=> "cat_lang = '$lang' AND cat_nb > 0",
203
			'ORDER_BY'	=> 'cat_order ASC',
204
		]);
205
		$result = $this->db->sql_query($sql);
206
		while ($row = $this->db->sql_fetchrow($result))
207
		{
208
			$actual_cat = $row['cat_id'] == $cat;
209
			$this->template->assign_block_vars('categories', [
210
				'CAT_ID'		=> $row['cat_id'],
211
				'CAT_NAME'		=> $row['cat_name'] ?: $row['cat_title'],
212
				'CAT_NB'		=> $row['cat_nb'],
213
				'SEPARATE'		=> ($i > 0) ? ' - ' : '',
214
				'CLASS'			=> $actual_cat ? 'cat-active' : 'cat-inactive',
215
				'U_CAT'			=> $this->helper->route('sylver35_smiliescat_smilies_pop', ['select' => $row['cat_id']]),
216
			]);
217
			$i++;
218
219
			// Keep these values in memory
220
			$title = $actual_cat ? $row['cat_name'] : $title;
221
		}
222
		$this->db->sql_freeresult($result);
223
224
		// Add the Unclassified category if not empty
225
		if ($nb = $this->category->smilies_count(self::DEFAULT_CAT))
226
		{
227
			$title = ($cat == self::DEFAULT_CAT) ? $this->language->lang('SC_CATEGORY_DEFAUT') : $title;
228
			$this->template->assign_block_vars('categories', [
229
				'CAT_ID'		=> self::DEFAULT_CAT,
230
				'CAT_NAME'		=> $this->language->lang('SC_CATEGORY_DEFAUT'),
231
				'CAT_NB'		=> $nb,
232
				'SEPARATE'		=> ($i > 0) ? ' - ' : '',
233
				'CLASS'			=> ($cat === self::DEFAULT_CAT) ? 'cat-active' : 'cat-inactive',
234
				'U_CAT'			=> $this->helper->route('sylver35_smiliescat_smilies_pop', ['select' => self::DEFAULT_CAT]),
235
			]);
236
		}
237
238
		return $title;
239
	}
240
241
	public function category_smilies($event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

241
	public function category_smilies(/** @scrutinizer ignore-unused */ $event)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
242
	{
243
		$this->extract_list_categories($this->config['smilies_first_cat']);
244
		$this->template->assign_vars([
245
			'U_CATEGORY_AJAX'	=> $this->helper->route('sylver35_smiliescat_ajax_smilies'),
246
			'ID_FIRST_CAT'		=> $this->config['smilies_first_cat'],
247
			'NB_FIRST_CAT'		=> $this->category->smilies_count($this->config['smilies_first_cat']),
248
			'PER_PAGE'			=> $this->config['smilies_per_page_cat'],
249
			'U_SMILIES_PATH'	=> generate_board_url() . '/' . $this->config['smilies_path'] . '/',
250
			'CATEGORY'			=> $this->extract_first_cat(),
251
			'IN_CATEGORIES'		=> true,
252
		]);
253
	}
254
255
	private function extract_first_cat()
256
	{
257
		$lang = $this->user->lang_name;
258
		$sql = $this->db->sql_build_query('SELECT', [
259
			'SELECT'	=> 's.*, c.*',
260
			'FROM'		=> [SMILIES_TABLE => 's'],
261
			'LEFT_JOIN'	=> [
262
				[
263
					'FROM'	=> [$this->smilies_category_table => 'c'],
264
					'ON'	=> "c.cat_id = s.category AND c.cat_lang = '$lang'",
265
				],
266
			],
267
			'WHERE'		=> 's.category = ' . $this->config['smilies_first_cat'],
268
			'ORDER_BY'	=> 's.smiley_order ASC',
269
		]);
270
		$result = $this->db->sql_query_limit($sql, (int) $this->config['smilies_per_page'], 0);
271
		while ($row = $this->db->sql_fetchrow($result))
272
		{
273
			$this->template->assign_block_vars('items', [
274
				'SMILEY_SRC'		=> $row['smiley_url'],
275
				'SMILEY_WIDTH'		=> $row['smiley_width'],
276
				'SMILEY_HEIGHT'		=> $row['smiley_height'],
277
				'SMILEY_ID'			=> $row['smiley_id'],
278
				'CAT_ID'			=> $row['category'],
279
				'SMILEY_CODE'		=> $row['code'],
280
				'SMILEY_EMOTION'	=> $row['emotion'],
281
				'CATEGORY'			=> $row['cat_name'],
282
			]);
283
			$category = $row['cat_name'];
284
		}
285
		$this->db->sql_freeresult($result);
286
		
287
		return $this->language->lang('SC_CATEGORY_IN', $category);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $category does not seem to be defined for all execution paths leading up to this point.
Loading history...
288
	}
289
}
290