Completed
Push — 1.3.0 ( 842f1a...f854fa )
by Sylver
08:29
created

admin_controller::modify_smiley()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 2
b 0
f 0
nc 4
nop 3
dl 0
loc 17
rs 10
1
<?php
2
/**
3
*
4
* @package		Breizh Smilies Categories Extension
5
* @copyright	(c) 2020 Sylver35  https://breizhcode.com
6
* @license		http://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 phpbb\config\config;
14
use phpbb\db\driver\driver_interface as db;
15
use phpbb\pagination;
16
use phpbb\request\request;
17
use phpbb\template\template;
18
use phpbb\user;
19
use phpbb\language\language;
20
use phpbb\log\log;
21
22
class admin_controller
23
{
24
	/* @var \sylver35\smiliescat\core\category */
25
	protected $category;
26
27
	/** @var \phpbb\config\config */
28
	protected $config;
29
30
	/** @var \phpbb\db\driver\driver_interface */
31
	protected $db;
32
33
	/** @var \phpbb\pagination */
34
	protected $pagination;
35
36
	/** @var \phpbb\request\request */
37
	protected $request;
38
39
	/** @var \phpbb\template\template */
40
	protected $template;
41
42
	/** @var \phpbb\user */
43
	protected $user;
44
45
	/** @var \phpbb\language\language */
46
	protected $language;
47
48
	/** @var \phpbb\log\log */
49
	protected $log;
50
51
	/** @var string phpBB root path */
52
	protected $root_path;
53
54
	/** @var string Custom form action */
55
	protected $u_action;
56
57
	/**
58
	 * The database tables
59
	 *
60
	 * @var string */
61
	protected $smilies_category_table;
62
63
	/**
64
	 * Constructor
65
	 */
66
	public function __construct(category $category, config $config, db $db, pagination $pagination, request $request, template $template, user $user, language $language, log $log, $root_path, $smilies_category_table)
67
	{
68
		$this->category = $category;
69
		$this->config = $config;
70
		$this->db = $db;
71
		$this->pagination = $pagination;
72
		$this->request = $request;
73
		$this->template = $template;
74
		$this->user = $user;
75
		$this->language = $language;
76
		$this->log = $log;
77
		$this->root_path = $root_path;
78
		$this->smilies_category_table = $smilies_category_table;
79
	}
80
81
	public function acp_smilies_category()
82
	{
83
		$this->language->add_lang('acp/posting');
84
		$action = (string) $this->request->variable('action', '');
85
		$start = (int) $this->request->variable('start', 0);
86
		$select = (int) $this->request->variable('select', -1);
87
		$id = (int) $this->request->variable('id', -1);
88
		$form_key = 'sylver35/smiliescat';
89
		add_form_key($form_key);
90
91
		if ($action)
92
		{
93
			switch ($action)
94
			{
95
				case 'edit':
96
					$this->category->adm_edit_smiley($id, $this->u_action, $start);
97
				break;
98
99
				case 'modify':
100
					if (!check_form_key($form_key))
101
					{
102
						trigger_error($this->language->lang('FORM_INVALID') . adm_back_link($this->u_action), E_USER_WARNING);
103
					}
104
105
					$this->category->modify_smiley($id, (int) $this->request->variable('cat_id', 0), (int) $this->request->variable('ex_cat', 0));
106
					trigger_error($this->language->lang('SMILIES_EDITED', 1) . adm_back_link($this->u_action . '&amp;start=' . $start . '#acp_smilies_category'));
107
				break;
108
			}
109
110
			$this->template->assign_vars(array(
111
				'IN_ACTION'			=> true,
112
			));
113
		}
114
		else
115
		{
116
			$this->extract_list_smilies($select, $start);
117
118
			$this->template->assign_vars(array(
119
				'LIST_CATEGORY'		=> $this->category->select_categories($select, true),
120
				'U_SELECT_CAT'		=> $this->u_action . '&amp;select=' . $select,
121
				'U_BACK'			=> ($select) ? $this->u_action : '',
122
			));
123
		}
124
125
		$this->template->assign_vars(array(
126
			'CATEGORIE_SMILIES'		=> true,
127
		));
128
	}
129
130
	public function acp_categories_config()
131
	{
132
		$this->language->add_lang('acp/language');
133
		$mode = (string) $this->request->variable('mode', '');
134
		$action = (string) $this->request->variable('action', '');
135
		$id = (int) $this->request->variable('id', 0);
136
		$form_key = 'sylver35/smiliescat';
137
		add_form_key($form_key);
138
139
		if ($action)
140
		{
141
			if (in_array($action, array('config_cat', 'add_cat', 'edit_cat')) && !check_form_key($form_key))
142
			{
143
				trigger_error($this->language->lang('FORM_INVALID') . adm_back_link($this->u_action), E_USER_WARNING);
144
			}
145
146
			switch ($action)
147
			{
148
				case 'config_cat':
149
					$this->config->set('smilies_per_page_cat', (int) $this->request->variable('smilies_per_page_cat', 15));
150
151
					$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_CONFIG', time());
152
					trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
153
				break;
154
155
				case 'add':
156
					$this->category->adm_add_cat($this->u_action);
157
				break;
158
159
				case 'add_cat':
160
					$this->add_category();
161
				break;
162
163
				case 'edit':
164
					$this->category->adm_edit_cat($id, $this->u_action);
165
				break;
166
167
				case 'edit_cat':
168
					$this->edit_category($id);
169
				break;
170
171
				case 'delete':
172
					if (confirm_box(true))
173
					{
174
						$this->delete_cat($id);
175
					}
176
					else
177
					{
178
						confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), build_hidden_fields(array(
179
							'mode'		=> $mode,
180
							'id'		=> $id,
181
							'action'	=> $action,
182
						)));
183
					}
184
				break;
185
				
186
			}
187
188
			$this->template->assign_vars(array(
189
				'IN_ACTION'		=> true,
190
			));
191
		}
192
		else
193
		{
194
			$this->category->adm_list_cat($this->u_action);
195
		}
196
197
		$this->template->assign_vars(array(
198
			'CATEGORIE_CONFIG'		=> true,
199
			'SMILIES_PER_PAGE_CAT'	=> $this->config['smilies_per_page_cat'],
200
			'U_ACTION_CONFIG'		=> $this->u_action . '&amp;action=config_cat',
201
			'U_ADD'					=> $this->u_action . '&amp;action=add',
202
		));
203
	}
204
205
	private function extract_list_smilies($select, $start)
206
	{
207
		$i = 0;
208
		$cat = -1;
209
		$lang = $this->user->lang_name;
210
		$smilies_count = (int) $this->category->smilies_count($select);
211
212
		if ($select === 0)
213
		{
214
			$sql = $this->db->sql_build_query('SELECT', array(
215
				'SELECT'	=> '*',
216
				'FROM'		=> array(SMILIES_TABLE => ''),
217
				'WHERE'		=> 'category = 0',
218
				'ORDER_BY'	=> 'smiley_order ASC',
219
			));
220
		}
221
		else
222
		{
223
			$sql = $this->db->sql_build_query('SELECT', array(
224
				'SELECT'	=> 's.*, c.*',
225
				'FROM'		=> array(SMILIES_TABLE => 's'),
226
				'LEFT_JOIN'	=> array(
227
					array(
228
						'FROM'	=> array($this->smilies_category_table => 'c'),
229
						'ON'	=> "cat_id = category AND cat_lang = '$lang'",
230
					),
231
				),
232
				'WHERE'		=> ($select == -1) ? "code <> ''" : "cat_id = $select AND code <> ''",
233
				'ORDER_BY'	=> 'cat_order ASC, smiley_order ASC',
234
			));
235
		}
236
		$result = $this->db->sql_query_limit($sql, (int) $this->config['smilies_per_page_cat'], $start);
237
		while ($row = $this->db->sql_fetchrow($result))
238
		{
239
			$row['category'] = isset($row['category']) ? $row['category'] : 0;
240
			$row['cat_name'] = ($row['category']) ? $row['cat_name'] : $this->language->lang('SC_CATEGORY_DEFAUT');
241
242
			$this->template->assign_block_vars('items', array(
243
				'SPACER_CAT'	=> ($cat !== (int) $row['category']) ? $this->language->lang('SC_CATEGORY_IN', $row['cat_name']) : '',
244
				'IMG_SRC'		=> $row['smiley_url'],
245
				'WIDTH'			=> $row['smiley_width'],
246
				'HEIGHT'		=> $row['smiley_height'],
247
				'CODE'			=> $row['code'],
248
				'EMOTION'		=> $row['emotion'],
249
				'CATEGORY'		=> $row['cat_name'],
250
				'U_EDIT'		=> $this->u_action . '&amp;action=edit&amp;id=' . $row['smiley_id'] . '&amp;start=' . $start,
251
			));
252
			$i++;
253
254
			// Keep this value in memory
255
			$cat = (int) $row['category'];
256
		}
257
		$this->db->sql_freeresult($result);
258
259
		$this->template->assign_vars(array(
260
			'NB_SMILIES'	=> $smilies_count,
261
			'U_SMILIES'		=> $this->root_path . $this->config['smilies_path'] . '/',
262
		));
263
264
		$this->pagination->generate_template_pagination($this->u_action . '&amp;select=' . $select, 'pagination', 'start', $smilies_count, (int) $this->config['smilies_per_page_cat'], $start);
265
	}
266
267
	private function delete_cat($id)
268
	{
269
		$id = (int) $id;
270
		$sql = 'SELECT cat_title, cat_order
271
			FROM ' . $this->smilies_category_table . '
272
				WHERE cat_id = ' . $id;
273
		$result = $this->db->sql_query($sql);
274
		$row = $this->db->sql_fetchrow($result);
275
		$title = $row['cat_title'];
276
		$order = $row['cat_order'];
277
		$this->db->sql_freeresult($result);
278
279
		$sql_delete = 'DELETE FROM ' . $this->smilies_category_table . ' WHERE cat_id = ' . $id;
280
		$this->db->sql_query($sql_delete);
281
282
		// Decrement orders if needed
283
		$sql_decrement = 'SELECT cat_id, cat_order
284
			FROM ' . $this->smilies_category_table . '
285
				WHERE cat_order > ' . (int) $order;
286
		$result = $this->db->sql_query($sql_decrement);
287
		while ($row = $this->db->sql_fetchrow($result))
288
		{
289
			$new_order = (int) $row['cat_order'] - 1;
290
			$sql_order = 'UPDATE ' . $this->smilies_category_table . '
291
				SET cat_order = ' . $new_order . '
292
					WHERE cat_id = ' . $row['cat_id'] . ' AND cat_order = ' . $row['cat_order'];
293
			$this->db->sql_query($sql_order);
294
		}
295
		$this->db->sql_freeresult($result);
296
297
		// Reset appropriate smilies category id
298
		$sql_update = 'UPDATE ' . SMILIES_TABLE . ' SET category = 0 WHERE category = ' . $id;
299
		$this->db->sql_query($sql_update);
300
301
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_DELETE_CAT', time(), array($title));
302
		trigger_error($this->language->lang('SC_DELETE_SUCCESS') . adm_back_link($this->u_action));
303
	}
304
305
	private function add_category()
306
	{
307
		$title = $this->request->variable('name_' . $this->user->lang_name, '', true);
308
		$cat_order = (int) $this->request->variable('order', 0);
309
		$cat_id = (int) $this->category->get_max_id() + 1;
310
		$sql_in = array();
311
		$i = 0;
312
313
		$sql = 'SELECT lang_id, lang_iso
314
			FROM ' . LANG_TABLE . "
315
				ORDER BY lang_id ASC";
316
		$result = $this->db->sql_query($sql);
317
		while ($row = $this->db->sql_fetchrow($result))
318
		{
319
			$iso = $row['lang_iso'];
320
			$lang = (string) $this->request->variable("lang_$iso", '', true);
321
			$name = (string) $this->request->variable("name_$iso", '', true);
322
			if ($name === '')
323
			{
324
				trigger_error($this->language->lang('SC_CATEGORY_ERROR') . adm_back_link($this->u_action . '&amp;action=add'), E_USER_WARNING);
325
			}
326
			else
327
			{
328
				$sql_in[$i] = array(
329
					'cat_id'		=> $cat_id,
330
					'cat_order'		=> $cat_order,
331
					'cat_lang'		=> $lang,
332
					'cat_name'		=> $this->category->capitalize($name),
333
					'cat_title'		=> $this->category->capitalize($title),
334
				);
335
			}
336
			$i++;
337
		}
338
339
		for ($j = 0; $j < $i; $j++)
340
		{
341
			$this->db->sql_query('INSERT INTO ' . $this->smilies_category_table . $this->db->sql_build_array('INSERT', $sql_in[$j]));
342
		}
343
344
		if ($cat_order === 1)
345
		{
346
			$this->config->set('smilies_category_nb', $cat_id);
347
		}
348
349
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_ADD_CAT', time(), array($title));
350
		trigger_error($this->language->lang('SC_CREATE_SUCCESS') . adm_back_link($this->u_action));
351
	}
352
353
	private function edit_category($id)
354
	{
355
		$id = (int) $id;
356
		$title = $this->category->capitalize($this->request->variable('name_' . $this->user->lang_name, '', true));
357
358
		$sql = 'SELECT lang_id, lang_iso
359
			FROM ' . LANG_TABLE . "
360
				ORDER BY lang_id ASC";
361
		$result = $this->db->sql_query($sql);
362
		while ($row = $this->db->sql_fetchrow($result))
363
		{
364
			$iso = $row['lang_iso'];
365
			$lang = (string) $this->request->variable("lang_$iso", '', true);
366
			$sort = (string) $this->request->variable("sort_$iso", '');
367
			$order = (int) $this->request->variable('order', 0);
368
			$name = $this->category->capitalize($this->request->variable("name_$iso", '', true));
369
			if ($name === '')
370
			{
371
				trigger_error($this->language->lang('SC_CATEGORY_ERROR') . adm_back_link($this->u_action . '&amp;action=edit&amp;id=' . $id), E_USER_WARNING);
372
			}
373
			else
374
			{
375
				if ($sort === 'edit')
376
				{
377
					$sql = 'UPDATE ' . $this->smilies_category_table . "
378
						SET cat_name = '" . $name . "', cat_title = '" . $title . "'
379
							WHERE cat_lang = '" . $this->db->sql_escape($lang) . "'
380
							AND cat_id = $id";
381
					$this->db->sql_query($sql);
382
				}
383
				else if ($sort === 'create')
384
				{
385
					$sql_in = array(
386
						'cat_id'		=> $id,
387
						'cat_order'		=> $order,
388
						'cat_lang'		=> $lang,
389
						'cat_name'		=> $name,
390
						'cat_title'		=> $title,
391
					);
392
					$this->db->sql_query('INSERT INTO ' . $this->smilies_category_table . $this->db->sql_build_array('INSERT', $sql_in));
393
				}
394
			}
395
		}
396
397
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_EDIT_CAT', time(), array($title));
398
		trigger_error($this->language->lang('SC_EDIT_SUCCESS') . adm_back_link($this->u_action));
399
	}
400
401
	/**
402
	 * Set page url
403
	 *
404
	 * @param string $u_action Custom form action
405
	 * @return null
406
	 * @access public
407
	 */
408
	public function set_page_url($u_action)
409
	{
410
		$this->u_action = $u_action;
411
	}
412
}
413