main::ajax_smilies()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 41
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 33
nc 2
nop 0
dl 0
loc 41
rs 9.392
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\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
		$pagin = (int) $this->config['smilies_per_page_cat'];
86
		$first = (int) $this->config['smilies_first_cat'];
87
		$cat = (int) $this->request->variable('select', $first);
88
		$count = $this->category->smilies_count($cat);
89
		$title = $this->diffusion->extract_list_categories($cat);
90
		$data = $this->category->get_version();
91
92
		$sql = $this->db->sql_build_query('SELECT', [
93
			'SELECT'	=> '*',
94
			'FROM'		=> [SMILIES_TABLE => ''],
95
			'WHERE'		=> 'category = ' . $cat,
96
			'ORDER_BY'	=> 'smiley_order ASC',
97
		]);
98
		$result = $this->db->sql_query_limit($sql, $pagin, $start);
99
		while ($row = $this->db->sql_fetchrow($result))
100
		{
101
			$this->template->assign_block_vars('smilies', [
102
				'SMILEY_CODE'		=> $row['code'],
103
				'SMILEY_EMOTION'	=> $row['emotion'],
104
				'SMILEY_WIDTH'		=> $row['smiley_width'],
105
				'SMILEY_HEIGHT'		=> $row['smiley_height'],
106
				'SMILEY_SRC'		=> $row['smiley_url'],
107
			]);
108
		}
109
		$this->db->sql_freeresult($result);
110
111
		$start = $this->pagination->validate_start($start, $pagin, $count);
112
		$this->pagination->generate_template_pagination($this->helper->route('sylver35_smiliescat_smilies_pop', ['select' => $cat]), 'pagination', 'start', $count, $pagin, $start);
113
114
		$this->template->assign_vars([
115
			'U_SMILIES_PATH'	=> generate_board_url() . '/' . $this->config['smilies_path'] . '/',
116
			'POPUP_TITLE'		=> $this->language->lang('SC_CATEGORY_IN', $title),
117
			'SC_VERSION'		=> $this->language->lang('SC_VERSION_COPY', $data['homepage'], $data['version']),
118
		]);
119
120
		page_header($this->language->lang('SC_CATEGORY_IN', $title));
121
122
		$this->template->set_filenames([
123
			'body' => '@sylver35_smiliescat/smilies_category.html']
124
		);
125
126
		page_footer();
127
	}
128
129
	/**
130
	 * @return \Symfony\Component\HttpFoundation\Response A Symfony Response object
131
	 */
132
	public function ajax_smilies()
133
	{
134
		$i = 0;
135
		$list_smilies = [];
136
		$start = (int) $this->request->variable('start', 0);
137
		$pagin = (int) $this->config['smilies_per_page_cat'];
138
		$first = (int) $this->config['smilies_first_cat'];
139
		$cat = (int) $this->request->variable('cat', $first);
140
		$count = $this->category->smilies_count($cat);
141
142
		$sql = $this->db->sql_build_query('SELECT', [
143
			'SELECT'	=> '*',
144
			'FROM'		=> [SMILIES_TABLE => ''],
145
			'WHERE'		=> 'category = ' . $cat,
146
			'ORDER_BY'	=> 'smiley_order ASC',
147
		]);
148
		$result = $this->db->sql_query_limit($sql, $pagin, $start);
149
		while ($row = $this->db->sql_fetchrow($result))
150
		{
151
			$list_smilies[$i] = [
152
				'code'		=> $row['code'],
153
				'emotion'	=> $row['emotion'],
154
				'width'		=> $row['smiley_width'],
155
				'height'	=> $row['smiley_height'],
156
				'src'		=> $row['smiley_url'],
157
			];
158
			$i++;
159
		}
160
		$this->db->sql_freeresult($result);
161
162
		$categories = $this->diffusion->list_cats($cat);
163
		$json_response = new \phpbb\json_response;
164
		$json_response->send([
165
			'total'			=> $i,
166
			'title'			=> $this->category->return_name($cat, '', true),
167
			'nb_cats'		=> $categories['nb_cats'],
168
			'start'			=> $start,
169
			'pagination'	=> $count,
170
			'smilies_path'	=> generate_board_url() . '/' . $this->config['smilies_path'] . '/',
171
			'list_smilies'	=> $list_smilies,
172
			'categories'	=> $categories['list_cat'],
173
		]);
174
	}
175
176
	/**
177
	 * @return \Symfony\Component\HttpFoundation\Response A Symfony Response object
178
	 */
179
	public function ajax_list_cat()
180
	{
181
		$i = $j = $cat = 0;
182
		$lang_cat = $list_cat = [];
183
		$id = (int) $this->request->variable('id', 0);
184
		$action = (string) $this->request->variable('action', '');
185
186
		$this->category->move_cat($id, $action);
187
		$max = $this->category->get_max_order();
188
		$langs = $this->category->get_langs();
189
		$total = $this->category->category_exist();
190
191
		$sql = $this->db->sql_build_query('SELECT', [
192
			'SELECT'	=> 'l.lang_id, l.lang_iso, l.lang_local_name, c.*',
193
			'FROM'		=> [LANG_TABLE => 'l'],
194
			'LEFT_JOIN'	=> [
195
				[
196
					'FROM'	=> [$this->smilies_category_table => 'c'],
197
					'ON'	=> 'c.cat_lang = l.lang_iso',
198
				],
199
			],
200
			'ORDER_BY'	=> 'c.cat_order ASC, c.cat_lang_id ASC',
201
		]);
202
		$result = $this->db->sql_query($sql);
203
		while ($row = $this->db->sql_fetchrow($result))
204
		{
205
			$title = '';
206
			if ((int) $row['cat_id'] !== $cat)
207
			{
208
				$return = $this->category->verify_cat_langs($langs, $cat, $i, $lang_cat, true);
209
				$list_cat[$j] = [
210
					'error'		=> $return['error'],
211
					'langEmpty'	=> $return['lang_empty'],
212
				];
213
				$j++;
214
				$title = $this->language->lang('SC_CATEGORY_IN', $this->category->return_name($row['cat_id'], $row['cat_name'], true));
215
			}
216
			$lang_cat[$row['cat_id']][$row['lang_id']] = $row['lang_iso'];
217
			$list_cat[$j] = [
218
				'titleCat'		=> $title,
219
				'catLang'		=> $row['lang_local_name'],
220
				'catIso'		=> $row['lang_iso'],
221
				'catTranslate'	=> $row['cat_name'],
222
				'catId'			=> (int) $row['cat_id'],
223
				'catOrder'		=> (int) $row['cat_order'],
224
				'catNb'			=> (int) $row['cat_nb'],
225
				'nbCss'			=> $row['cat_nb'] ? 'green' : 'orange',
226
				'row'			=> (int) $row['cat_id'] !== $cat,
227
				'rowMax'		=> (int) $row['cat_order'] === $max,
228
				'uEdit'			=> '&amp;action=edit&amp;id=' . $row['cat_id'],
229
				'uDelete'		=> '&amp;action=delete&amp;id=' . $row['cat_id'],
230
			];
231
			$i++;
232
			$j++;
233
			// Keep this value in memory
234
			$cat = (int) $row['cat_id'];
235
236
			// Do this only for the last category
237
			if ((int) $row['cat_order'] === $max && ($i === $total))
238
			{
239
				$return = $this->category->verify_cat_langs($langs, $cat, $i, $lang_cat, true);
240
				$list_cat[$j] = [
241
					'error'		=> $return['error'],
242
					'langEmpty'	=> $return['lang_empty'],
243
				];
244
				$j++;
245
			}
246
		}
247
		$this->db->sql_freeresult($result);
248
249
		$json_response = new \phpbb\json_response;
250
		$json_response->send([
251
			'total'	=> $j,
252
			'datas'	=> $list_cat,
253
		]);
254
	}
255
}
256