Passed
Push — 1.6.0 ( d55b30...970b60 )
by Sylver
04:56
created

main::ajax_list_cat()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 73
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 52
nc 5
nop 0
dl 0
loc 73
rs 8.7361
c 1
b 0
f 0

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\controller;
11
12
use sylver35\smiliescat\core\category;
13
use sylver35\smiliescat\core\diffusion;
14
use phpbb\request\request;
15
use phpbb\config\config;
16
use phpbb\controller\helper;
17
use phpbb\db\driver\driver_interface as db;
18
use phpbb\template\template;
19
use phpbb\user;
20
use phpbb\language\language;
21
use phpbb\pagination;
22
23
class main
24
{
25
	/* @var \sylver35\smiliescat\core\category */
26
	protected $category;
27
28
	/* @var \sylver35\smiliescat\core\diffusion */
29
	protected $diffusion;
30
31
	/** @var \phpbb\request\request */
32
	protected $request;
33
34
	/** @var \phpbb\config\config */
35
	protected $config;
36
37
	/* @var \phpbb\controller\helper */
38
	protected $helper;
39
40
	/** @var \phpbb\db\driver\driver_interface */
41
	protected $db;
42
43
	/** @var \phpbb\template\template */
44
	protected $template;
45
46
	/** @var \phpbb\user */
47
	protected $user;
48
49
	/** @var \phpbb\language\language */
50
	protected $language;
51
52
	/** @var \phpbb\pagination */
53
	protected $pagination;
54
55
	/**
56
	 * The database tables
57
	 *
58
	 * @var string */
59
	protected $smilies_category_table;
60
61
	/**
62
	 * Constructor
63
	 */
64
	public function __construct(category $category, diffusion $diffusion, request $request, config $config, helper $helper, db $db, template $template, user $user, language $language, pagination $pagination, $smilies_category_table)
65
	{
66
		$this->category = $category;
67
		$this->diffusion = $diffusion;
68
		$this->request = $request;
69
		$this->config = $config;
70
		$this->helper = $helper;
71
		$this->db = $db;
72
		$this->template = $template;
73
		$this->user = $user;
74
		$this->language = $language;
75
		$this->pagination = $pagination;
76
		$this->smilies_category_table = $smilies_category_table;
77
	}
78
79
	/**
80
	 * @return \Symfony\Component\HttpFoundation\Response A Symfony Response object
81
	 */
82
	public function popup_smilies_category()
83
	{
84
		$start = (int) $this->request->variable('start', 0);
85
		$cat = (int) $this->request->variable('select', $this->config['smilies_category_nb']);
86
		$pagin = (int) $this->config['smilies_per_page_cat'];
87
		$count = (int) $this->category->smilies_count($cat);
88
		$title = $this->category->extract_list_categories($cat);
89
		$data = $this->category->get_version();
90
91
		$sql = $this->db->sql_build_query('SELECT', [
92
			'SELECT'	=> '*',
93
			'FROM'		=> [SMILIES_TABLE => ''],
94
			'WHERE'		=> 'category = ' . $cat,
95
			'ORDER_BY'	=> 'smiley_order ASC',
96
		]);
97
		$result = $this->db->sql_query_limit($sql, $pagin, $start);
98
		while ($row = $this->db->sql_fetchrow($result))
99
		{
100
			$this->template->assign_block_vars('smilies', [
101
				'SMILEY_CODE'		=> $row['code'],
102
				'SMILEY_EMOTION'	=> $row['emotion'],
103
				'SMILEY_WIDTH'		=> $row['smiley_width'],
104
				'SMILEY_HEIGHT'		=> $row['smiley_height'],
105
				'SMILEY_SRC'		=> $row['smiley_url'],
106
			]);
107
		}
108
		$this->db->sql_freeresult($result);
109
110
		$start = $this->pagination->validate_start($start, $pagin, $count);
111
		$this->pagination->generate_template_pagination($this->helper->route('sylver35_smiliescat_smilies_pop', ['select' => $cat]), 'pagination', 'start', $count, $pagin, $start);
112
113
		$this->template->assign_vars([
114
			'U_SMILIES_PATH'	=> generate_board_url() . '/' . $this->config['smilies_path'] . '/',
115
			'POPUP_TITLE'		=> $this->language->lang('SC_CATEGORY_IN', $title),
116
			'SC_VERSION'		=> $this->language->lang('SC_VERSION_COPY', $data['homepage'], $data['version']),
117
		]);
118
119
		page_header($this->language->lang('SC_CATEGORY_IN', $title));
120
121
		$this->template->set_filenames([
122
			'body' => '@sylver35_smiliescat/smilies_category.html']
123
		);
124
125
		page_footer();
126
	}
127
128
	/**
129
	 * @return \Symfony\Component\HttpFoundation\Response A Symfony Response object
130
	 */
131
	public function ajax_smilies()
132
	{
133
		$i = 0;
134
		$list_smilies = [];
135
		$cat = (int) $this->request->variable('cat', $this->config['smilies_category_nb']);
136
		$start = (int) $this->request->variable('start', 0);
137
		$pagin = (int) $this->config['smilies_per_page_cat'];
138
		$count = (int) $this->category->smilies_count($cat);
139
140
		$sql = $this->db->sql_build_query('SELECT', [
141
			'SELECT'	=> '*',
142
			'FROM'		=> [SMILIES_TABLE => ''],
143
			'WHERE'		=> 'category = ' . $cat,
144
			'ORDER_BY'	=> 'smiley_order ASC',
145
		]);
146
		$result = $this->db->sql_query_limit($sql, $pagin, $start);
147
		while ($row = $this->db->sql_fetchrow($result))
148
		{
149
			$list_smilies[$i] = [
150
				'code'		=> $row['code'],
151
				'emotion'	=> $row['emotion'],
152
				'width'		=> $row['smiley_width'],
153
				'height'	=> $row['smiley_height'],
154
				'src'		=> $row['smiley_url'],
155
			];
156
			$i++;
157
		}
158
		$this->db->sql_freeresult($result);
159
160
		$categories = $this->diffusion->list_cats($cat);
161
		$json_response = new \phpbb\json_response;
162
		$json_response->send([
163
			'title'			=> $this->diffusion->get_cat_name($cat),
164
			'nb_cats'		=> count($categories),
165
			'start'			=> $start,
166
			'pagination'	=> $count,
167
			'smilies_path'	=> generate_board_url() . '/' . $this->config['smilies_path'] . '/',
168
			'total'			=> $i,
169
			'list_smilies'	=> $list_smilies,
170
			'categories'	=> $categories,
171
		]);
172
	}
173
174
	/**
175
	 * @return \Symfony\Component\HttpFoundation\Response A Symfony Response object
176
	 */
177
	public function ajax_list_cat()
178
	{
179
		$i = $j = $cat = 0;
180
		$lang_cat = $list_cat = [];
181
		$id = (int) $this->request->variable('id', 0);
182
		$action = (string) $this->request->variable('action', '');
183
184
		$this->category->move_cat($id, $action);
185
		$max = $this->category->get_max_order();
186
		$langs = $this->category->get_langs();
187
		$total = $this->category->category_exist();
188
189
		$sql = $this->db->sql_build_query('SELECT', [
190
			'SELECT'	=> 'l.lang_id, l.lang_iso, l.lang_local_name, c.*',
191
			'FROM'		=> [LANG_TABLE => 'l'],
192
			'LEFT_JOIN'	=> [
193
				[
194
					'FROM'	=> [$this->smilies_category_table => 'c'],
195
					'ON'	=> 'c.cat_lang = l.lang_iso',
196
				],
197
			],
198
			'ORDER_BY'	=> 'c.cat_order ASC, c.cat_lang_id ASC',
199
		]);
200
		$result = $this->db->sql_query($sql);
201
		while ($row = $this->db->sql_fetchrow($result))
202
		{
203
			$title = '';
204
			if ((int) $row['cat_id'] !== $cat)
205
			{
206
				$return = $this->category->verify_cat_langs($langs, $cat, $i, $lang_cat, true);
207
				$list_cat[$j] = [
208
					'error'		=> $return['error'],
209
					'langEmpty'	=> $return['lang_empty'],
210
				];
211
				$j++;
212
				$title = $this->language->lang('SC_CATEGORY_IN', $this->category->cat_name($row['cat_id']));
213
			}
214
			$lang_cat[$row['cat_id']][$row['lang_id']] = $row['lang_iso'];
215
			$list_cat[$j] = [
216
				'titleCat'		=> $title,
217
				'catLang'		=> $row['lang_local_name'],
218
				'catIso'		=> $row['lang_iso'],
219
				'catTranslate'	=> $row['cat_name'],
220
				'catId'			=> (int) $row['cat_id'],
221
				'catOrder'		=> (int) $row['cat_order'],
222
				'catNb'			=> (int) $row['cat_nb'],
223
				'row'			=> (int) $row['cat_id'] !== $cat,
224
				'rowMax'		=> (int) $row['cat_order'] === $max,
225
				'uEdit'			=> '&amp;action=edit&amp;id=' . $row['cat_id'],
226
				'uDelete'		=> '&amp;action=delete&amp;id=' . $row['cat_id'],
227
			];
228
			$i++;
229
			$j++;
230
			// Keep this value in memory
231
			$cat = (int) $row['cat_id'];
232
233
			// Do this only for the last category
234
			if ((int) $row['cat_order'] === $max && ($i === $total))
235
			{
236
				$return = $this->category->verify_cat_langs($langs, $cat, $i, $lang_cat, true);
237
				$list_cat[$j] = [
238
					'error'		=> $return['error'],
239
					'langEmpty'	=> $return['lang_empty'],
240
				];
241
				$j++;
242
			}
243
		}
244
		$this->db->sql_freeresult($result);
245
246
		$json_response = new \phpbb\json_response;
247
		$json_response->send([
248
			'total'	=> $j,
249
			'datas'	=> $list_cat,
250
		]);
251
	}
252
}
253