Passed
Push — master ( 7a05a5...807ba8 )
by Sylver
03:18
created

diffusion::get_cat_name()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 1
dl 0
loc 17
rs 9.8666
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
20
class diffusion
21
{
22
	/* @var \sylver35\smiliescat\core\category */
23
	protected $category;
24
25
	/** @var \phpbb\db\driver\driver_interface */
26
	protected $db;
27
28
	/** @var \phpbb\config\config */
29
	protected $config;
30
31
	/** @var \phpbb\user */
32
	protected $user;
33
34
	/** @var \phpbb\language\language */
35
	protected $language;
36
37
	/** @var \phpbb\template\template */
38
	protected $template;
39
40
	/* @var \phpbb\controller\helper */
41
	protected $helper;
42
43
	/**
44
	 * The database tables
45
	 *
46
	 * @var string */
47
	protected $smilies_category_table;
48
49
	/**
50
	 * Constructor
51
	 */
52
	public function __construct(category $category, db $db, config $config, user $user, language $language, template $template, helper $helper, $smilies_category_table)
53
	{
54
		$this->category = $category;
55
		$this->db = $db;
56
		$this->config = $config;
57
		$this->user = $user;
58
		$this->language = $language;
59
		$this->template = $template;
60
		$this->helper = $helper;
61
		$this->smilies_category_table = $smilies_category_table;
62
	}
63
64
	public function url_to_page()
65
	{
66
		$first = $this->category->get_first_order();
67
		$this->template->assign_var('U_CATEGORY_POPUP', $this->helper->route('sylver35_smiliescat_smilies_pop', ['select' => $first['first']]));
68
	}
69
70
	public function cats_to_posting_form($event)
71
	{
72
		if (in_array($event['mode'], ['post', 'reply', 'edit', 'quote']))
73
		{
74
			$first = $this->category->get_first_order();
75
			$this->template->assign_vars([
76
				'U_CATEGORY_AJAX'	=> $this->helper->route('sylver35_smiliescat_ajax_smilies'),
77
				'ID_FIRST_CAT'		=> $first['cat_nb'],
78
				'PER_PAGE'			=> $this->config['smilies_per_page_cat'],
79
				'U_SMILIES_PATH'	=> generate_board_url() . '/' . $this->config['smilies_path'] . '/',
80
				'IN_CATEGORIES'		=> true,
81
			]);
82
		}
83
	}
84
85
	public function list_cats($cat)
86
	{
87
		$i = 0;
88
		$cat_order = 1;
89
		$list_cat = [];
90
		$lang = (string) $this->user->lang_name;
91
92
		$sql = 'SELECT * 
93
			FROM ' . $this->smilies_category_table . "
94
				WHERE cat_lang = '$lang'
95
				ORDER BY cat_order ASC";
96
		$result = $this->db->sql_query($sql);
97
		while ($row = $this->db->sql_fetchrow($result))
98
		{
99
			// Select only non-empty categories
100
			if ($row['cat_nb'])
101
			{
102
				$list_cat[$i] = [
103
					'cat_id'		=> (int) $row['cat_id'],
104
					'cat_order'		=> (int) $row['cat_order'],
105
					'cat_nb'		=> (int) $row['cat_nb'],
106
					'cat_name'		=> (string) $row['cat_name'],
107
					'css'			=> ($row['cat_id'] == $cat) ? 'cat-active' : 'cat-inactive',
108
				];
109
				$i++;
110
			}
111
			// Keep this value in memory
112
			$cat_order = (int) $row['cat_order'];
113
		}
114
		$this->db->sql_freeresult($result);
115
116
		// Add the Unclassified category if not empty
117
		if ($nb = $this->category->smilies_count(0))
118
		{
119
			$list_cat[$i] = [
120
				'cat_id'		=> 0,
121
				'cat_order'		=> $cat_order,
122
				'cat_nb'		=> $nb,
123
				'cat_name'		=> $this->language->lang('SC_CATEGORY_DEFAUT'),
124
				'css'			=> ($cat == 0) ? 'cat-active' : 'cat-inactive',
125
			];
126
		}
127
128
		return $list_cat;
129
	}
130
131
	public function smilies_popup($cat, $start)
132
	{
133
		if ($cat !== -1)
134
		{
135
			$i = 0;
136
			$smilies = [];
137
			$pagin = (int) $this->config['shout_smilies_per_page'];
138
139
			$sql = [
140
				'SELECT'	=> 'smiley_url, smiley_id, code, smiley_order, emotion, smiley_width, smiley_height',
141
				'FROM'		=> [SMILIES_TABLE => ''],
142
				'WHERE'		=> "category = $cat",
143
				'ORDER_BY'	=> 'smiley_order ASC',
144
			];
145
			$result = $this->db->sql_query_limit($this->db->sql_build_query('SELECT', $sql), $pagin, $start);
146
			while ($row = $this->db->sql_fetchrow($result))
147
			{
148
				$smilies[$i] = [
149
					'nb'		=> (int) $i,
150
					'code'		=> (string) $row['code'],
151
					'emotion'	=> (string) $row['emotion'],
152
					'image'		=> (string) $row['smiley_url'],
153
					'width'		=> (int) $row['smiley_width'],
154
					'height'	=> (int) $row['smiley_height'],
155
				];
156
				$i++;
157
			}
158
			$this->db->sql_freeresult($result);
159
160
			return [
161
				'in_cat'		=> true,
162
				'total'			=> $i,
163
				'cat'			=> $cat,
164
				'smilies'		=> $smilies,
165
				'emptyRow'		=> ($i === 0) ? $this->language->lang('SC_SMILIES_EMPTY_CATEGORY') : '',
166
				'title'			=> $this->language->lang('SC_CATEGORY_IN', '<span class="cat-title">' . $this->category->cat_name($cat) . '</span>'),
167
				'start'			=> $start,
168
				'pagination'	=> $this->category->smilies_count($cat),
169
			];
170
		}
171
172
		return ['in_cat' => false];
173
	}
174
}
175