Passed
Branch 1.5.0 (c959a1)
by Sylver
04:41
created

work::edit_cat()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 73
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 53
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 73
rs 9.0254

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 *
4
 * @package		Breizh Smilies Categories Extension
5
 * @copyright	(c) 2020-2023 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\log\log;
19
use phpbb\request\request;
20
use phpbb\controller\helper;
21
22
class work
23
{
24
	/* @var \sylver35\smiliescat\core\category */
25
	protected $category;
26
27
	/** @var \phpbb\db\driver\driver_interface */
28
	protected $db;
29
30
	/** @var \phpbb\config\config */
31
	protected $config;
32
33
	/** @var \phpbb\user */
34
	protected $user;
35
36
	/** @var \phpbb\language\language */
37
	protected $language;
38
39
	/** @var \phpbb\template\template */
40
	protected $template;
41
42
	/** @var \phpbb\log\log */
43
	protected $log;
44
45
	/** @var \phpbb\request\request */
46
	protected $request;
47
48
	/* @var \phpbb\controller\helper */
49
	protected $helper;
50
51
	/**
52
	 * The database tables
53
	 *
54
	 * @var string */
55
	protected $smilies_category_table;
56
57
	/**
58
	 * Constructor
59
	 */
60
	public function __construct(category $category, db $db, config $config, user $user, language $language, template $template, log $log, request $request, helper $helper, $smilies_category_table)
61
	{
62
		$this->category = $category;
63
		$this->db = $db;
64
		$this->config = $config;
65
		$this->user = $user;
66
		$this->language = $language;
67
		$this->template = $template;
68
		$this->log = $log;
69
		$this->request = $request;
70
		$this->helper = $helper;
71
		$this->smilies_category_table = $smilies_category_table;
72
	}
73
74
	public function delete_cat($id, $u_action)
75
	{
76
		$sql = 'SELECT cat_title, cat_order
77
			FROM ' . $this->smilies_category_table . '
78
				WHERE cat_id = ' . $id;
79
		$result = $this->db->sql_query($sql);
80
		$row = $this->db->sql_fetchrow($result);
81
		$title = $row['cat_title'];
82
		$order = (int) $row['cat_order'];
83
		$this->db->sql_freeresult($result);
84
85
		$this->db->sql_query('DELETE FROM ' . $this->smilies_category_table . ' WHERE cat_id = ' . $id);
86
87
		// Decrement orders if needed
88
		$sql_decrement = 'SELECT cat_id, cat_order
89
			FROM ' . $this->smilies_category_table . '
90
				WHERE cat_order > ' . $order;
91
		$result = $this->db->sql_query($sql_decrement);
92
		while ($row = $this->db->sql_fetchrow($result))
93
		{
94
			$new_order = (int) $row['cat_order'] - 1;
95
			$this->db->sql_query('UPDATE ' . $this->smilies_category_table . ' SET cat_order = ' . $new_order . ' WHERE cat_id = ' . $row['cat_id'] . ' AND cat_order = ' . $row['cat_order']);
96
		}
97
		$this->db->sql_freeresult($result);
98
99
		// Reset appropriate smilies category id
100
		$this->db->sql_query('UPDATE ' . SMILIES_TABLE . ' SET category = 0 WHERE category = ' . $id);
101
102
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_DELETE_CAT', time(), [$title]);
103
		trigger_error($this->language->lang('SC_DELETE_SUCCESS') . adm_back_link($u_action));
104
	}
105
106
	public function add_category($u_action)
107
	{
108
		$sql_ary = [];
109
		$title = (string) $this->request->variable('name_' . $this->user->lang_name, '', true);
110
		$cat_order = (int) $this->request->variable('order', 0);
111
		$cat_id = (int) $this->category->get_max_id() + 1;
112
113
		$sql = 'SELECT lang_id, lang_iso
114
			FROM ' . LANG_TABLE . "
115
				ORDER BY lang_id ASC";
116
		$result = $this->db->sql_query($sql);
117
		while ($row = $this->db->sql_fetchrow($result))
118
		{
119
			$iso = strtolower($row['lang_iso']);
120
			$lang = (string) $this->request->variable("lang_$iso", '', true);
121
			$name = (string) $this->request->variable("name_$iso", '', true);
122
			if ($name === '')
123
			{
124
				trigger_error($this->language->lang('SC_CATEGORY_ERROR') . adm_back_link($u_action . '&amp;action=add'), E_USER_WARNING);
125
			}
126
			else
127
			{
128
				$sql_ary[] = [
129
					'cat_id'		=> $cat_id,
130
					'cat_order'		=> $cat_order,
131
					'cat_lang'		=> $lang,
132
					'cat_name'		=> $this->category->capitalize($name),
133
					'cat_title'		=> $this->category->capitalize($title),
134
					'cat_nb'		=> 0,
135
				];
136
			}
137
		}
138
		$this->db->sql_freeresult($result);
139
140
		$this->db->sql_multi_insert($this->smilies_category_table, $sql_ary);
141
142
		if ($cat_order === 1)
143
		{
144
			$sql = 'SELECT cat_id, cat_nb
145
				FROM ' . $this->smilies_category_table . '
146
					WHERE cat_order = 1';
147
			$result = $this->db->sql_query_limit($sql, 1);
148
			$row = $this->db->sql_fetchrow($result);
149
			if ($row['cat_nb'] > 0)
150
			{
151
				$this->config->set('smilies_category_nb', $row['cat_id']);
152
			}
153
			$this->db->sql_freeresult($result);
154
		}
155
156
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_ADD_CAT', time(), [$title]);
157
		trigger_error($this->language->lang('SC_CREATE_SUCCESS') . adm_back_link($u_action));
158
	}
159
160
	public function edit_category($id, $u_action)
161
	{
162
		$sql_in = [];
163
		$title = $this->category->capitalize($this->request->variable('name_' . $this->user->lang_name, '', true));
164
		$order = (int) $this->request->variable('order', 0);
165
		$cat_nb = (int) $this->request->variable('cat_nb', 0);
166
167
		$sql = 'SELECT lang_id, lang_iso
168
			FROM ' . LANG_TABLE . "
169
				ORDER BY lang_id ASC";
170
		$result = $this->db->sql_query($sql);
171
		while ($row = $this->db->sql_fetchrow($result))
172
		{
173
			$iso = strtolower($row['lang_iso']);
174
			$lang = (string) $this->request->variable("lang_$iso", '', true);
175
			$sort = (string) $this->request->variable("sort_$iso", '');
176
			$name = $this->category->capitalize($this->request->variable("name_$iso", '', true));
177
178
			if ($name === '')
179
			{
180
				trigger_error($this->language->lang('SC_CATEGORY_ERROR') . adm_back_link($u_action . '&amp;action=edit&amp;id=' . $id), E_USER_WARNING);
181
			}
182
			else
183
			{
184
				if ($sort === 'edit')
185
				{
186
					$this->db->sql_query('UPDATE ' . $this->smilies_category_table . " SET cat_name = '" . $this->db->sql_escape($name) . "', cat_title = '" . $this->db->sql_escape($title) . "', cat_nb = $cat_nb WHERE cat_lang = '" . $this->db->sql_escape($lang) . "' AND cat_id = $id");
187
				}
188
				else if ($sort === 'create')
189
				{
190
					$sql_in[] = [
191
						'cat_id'		=> $id,
192
						'cat_order'		=> $order,
193
						'cat_lang'		=> $lang,
194
						'cat_name'		=> $name,
195
						'cat_title'		=> $title,
196
						'cat_nb'		=> $cat_nb,
197
					];
198
				}
199
			}
200
		}
201
		$this->db->sql_freeresult($result);
202
203
		$this->db->sql_multi_insert($this->smilies_category_table, $sql_in);
204
205
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_EDIT_CAT', time(), [$title]);
206
		trigger_error($this->language->lang('SC_EDIT_SUCCESS') . adm_back_link($u_action));
207
	}
208
209
	public function add_cat($u_action)
210
	{
211
		$max = (int) $this->category->get_max_order() + 1;
212
		$sql = 'SELECT lang_local_name, lang_iso
213
			FROM ' . LANG_TABLE . '
214
				ORDER BY lang_id ASC';
215
		$result = $this->db->sql_query($sql);
216
		while ($row = $this->db->sql_fetchrow($result))
217
		{
218
			$this->template->assign_block_vars('categories', [
219
				'CAT_LANG'		=> $row['lang_local_name'],
220
				'CAT_ISO'		=> $row['lang_iso'],
221
				'CAT_ORDER'		=> $max,
222
			]);
223
		}
224
		$this->db->sql_freeresult($result);
225
226
		$this->template->assign_vars([
227
			'IN_CAT_ACTION'		=> true,
228
			'IN_ADD_ACTION'		=> true,
229
			'CAT_ORDER'			=> $max,
230
			'U_BACK'			=> $u_action,
231
			'U_ADD_CAT'			=> $u_action . '&amp;action=add_cat',
232
		]);
233
	}
234
235
	public function edit_cat($id, $u_action)
236
	{
237
		// Get total lang id...
238
		$sql = 'SELECT COUNT(lang_id) as total
239
			FROM ' . LANG_TABLE;
240
		$result = $this->db->sql_query($sql);
241
		$total = (int) $this->db->sql_fetchfield('total');
242
		$this->db->sql_freeresult($result);
243
244
		$title = '';
245
		$list_id = [];
246
		$i = $cat_order = $cat_nb = 0;
247
		$sql = $this->db->sql_build_query('SELECT', [
248
			'SELECT'	=> 'l.*, c.*',
249
			'FROM'		=> [LANG_TABLE => 'l'],
250
			'LEFT_JOIN'	=> [
251
				[
252
					'FROM'	=> [$this->smilies_category_table => 'c'],
253
					'ON'	=> 'c.cat_lang = l.lang_iso',
254
				],
255
			],
256
			'WHERE'		=> 'cat_id = ' . $id,
257
			'ORDER_BY'	=> 'lang_id ASC',
258
		]);
259
		$result = $this->db->sql_query($sql);
260
		while ($row = $this->db->sql_fetchrow($result))
261
		{
262
			$this->template->assign_block_vars('category_lang', [
263
				'CAT_LANG'			=> $row['lang_local_name'],
264
				'CAT_ISO'			=> $row['lang_iso'],
265
				'CAT_ORDER'			=> $row['cat_order'],
266
				'CAT_ID'			=> $row['cat_id'],
267
				'CAT_TRANSLATE'		=> $row['cat_name'],
268
				'CAT_SORT'			=> 'edit',
269
			]);
270
			$i++;
271
			$list_id[$i] = $row['lang_id'];
272
			$cat_order = $row['cat_order'];
273
			$title = $row['cat_title'];
274
			$cat_nb = $row['cat_nb'];
275
		}
276
		$this->db->sql_freeresult($result);
277
278
		// Add rows for empty langs in this category
279
		if ($i !== $total)
280
		{
281
			$sql = $this->db->sql_build_query('SELECT', [
282
				'SELECT'	=> '*',
283
				'FROM'		=> [LANG_TABLE => 'l'],
284
				'WHERE'		=> $this->db->sql_in_set('lang_id', $list_id, true, true),
285
			]);
286
			$result = $this->db->sql_query($sql);
287
			while ($row = $this->db->sql_fetchrow($result))
288
			{
289
				$this->template->assign_block_vars('category_lang', [
290
					'CAT_LANG'			=> $row['lang_local_name'],
291
					'CAT_ISO'			=> $row['lang_iso'],
292
					'CAT_ORDER'			=> $cat_order,
293
					'CAT_ID'			=> $id,
294
					'CAT_TRANSLATE'		=> '',
295
					'CAT_SORT'			=> 'create',
296
				]);
297
			}
298
			$this->db->sql_freeresult($result);
299
		}
300
301
		$this->template->assign_vars([
302
			'IN_CAT_ACTION'	=> true,
303
			'CAT_ORDER'		=> $cat_order,
304
			'CAT_NB'		=> $cat_nb,
305
			'CAT_TITLE'		=> $title,
306
			'U_BACK'		=> $u_action,
307
			'U_EDIT_CAT'	=> $u_action . '&amp;action=edit_cat&amp;id=' . $id,
308
		]);
309
	}
310
311
	public function adm_list_cat($u_action)
312
	{
313
		$i = $cat = 0;
314
		$max = $this->category->get_max_order();
315
		$sql = $this->db->sql_build_query('SELECT', [
316
			'SELECT'	=> 'l.lang_iso, l.lang_local_name, c.*',
317
			'FROM'		=> [LANG_TABLE => 'l'],
318
			'LEFT_JOIN'	=> [
319
				[
320
					'FROM'	=> [$this->smilies_category_table => 'c'],
321
					'ON'	=> 'cat_lang = lang_iso',
322
				],
323
			],
324
			'ORDER_BY'	=> 'cat_order ASC, cat_lang_id ASC',
325
		]);
326
		$result = $this->db->sql_query($sql);
327
		while ($row = $this->db->sql_fetchrow($result))
328
		{
329
			if (!$row)
330
			{
331
				$this->template->assign_vars([
332
					'EMPTY_ROW' =>	true,
333
				]);
334
			}
335
			else
336
			{
337
				$this->template->assign_block_vars('categories', [
338
					'CAT_NR'			=> $i + 1,
339
					'LANG_EMPTY'		=> !$row['cat_id'] && !$row['cat_order'] && !$row['cat_name'],
340
					'SPACER_CAT'		=> $this->language->lang('SC_CATEGORY_IN', $row['cat_title']),
341
					'CAT_LANG'			=> $row['lang_local_name'],
342
					'CAT_ISO'			=> $row['lang_iso'],
343
					'CAT_ID'			=> $row['cat_id'],
344
					'CAT_ORDER'			=> $row['cat_order'],
345
					'CAT_TRANSLATE'		=> $row['cat_name'],
346
					'CAT_NB'			=> $row['cat_nb'],
347
					'ROW'				=> (int) $row['cat_id'] !== $cat,
348
					'ROW_MAX'			=> (int) $row['cat_order'] === $max,
349
					'U_EDIT'			=> $u_action . '&amp;action=edit&amp;id=' . $row['cat_id'],
350
					'U_DELETE'			=> $u_action . '&amp;action=delete&amp;id=' . $row['cat_id'],
351
				]);
352
				$i++;
353
				// Keep this value in memory
354
				$cat = (int) $row['cat_id'];
355
			}
356
		}
357
		$this->db->sql_freeresult($result);
358
359
		$this->template->assign_vars([
360
			'IN_LIST_CAT'	=> true,
361
			'U_ACTION'		=> $u_action,
362
			'U_MOVE_CATS'	=> $this->helper->route('sylver35_smiliescat_ajax_list_cat'),
363
		]);
364
	}
365
}
366