Passed
Push — 1.3.0 ( 8f251a...b261a4 )
by Sylver
03:15
created

main::ajax_list_cat()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 49
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 37
nc 4
nop 0
dl 0
loc 49
rs 9.328
c 0
b 0
f 0
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\request\request;
14
use phpbb\config\config;
15
use phpbb\controller\helper;
16
use phpbb\db\driver\driver_interface as db;
17
use phpbb\template\template;
18
use phpbb\user;
19
use phpbb\language\language;
20
use phpbb\pagination;
21
22
class main
23
{
24
	/* @var \sylver35\smiliescat\core\category */
25
	protected $category;
26
27
	/** @var \phpbb\request\request */
28
	protected $request;
29
30
	/** @var \phpbb\config\config */
31
	protected $config;
32
33
	/* @var \phpbb\controller\helper */
34
	protected $helper;
35
36
	/** @var \phpbb\db\driver\driver_interface */
37
	protected $db;
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\pagination */
49
	protected $pagination;
50
51
	/** @var string phpBB root path */
52
	protected $root_path;
53
54
	/** @var string phpEx */
55
	protected $php_ext;
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, request $request, config $config, helper $helper, db $db, template $template, user $user, language $language, pagination $pagination, $root_path, $php_ext, $smilies_category_table)
67
	{
68
		$this->category = $category;
69
		$this->request = $request;
70
		$this->config = $config;
71
		$this->helper = $helper;
72
		$this->db = $db;
73
		$this->template = $template;
74
		$this->user = $user;
75
		$this->language = $language;
76
		$this->pagination = $pagination;
77
		$this->smilies_category_table = $smilies_category_table;
78
		$this->root_path = $root_path;
79
		$this->php_ext = $php_ext;
80
	}
81
82
	/**
83
	 * @return \Symfony\Component\HttpFoundation\Response A Symfony Response object
84
	 */
85
	public function popup_smilies_category()
86
	{
87
		$start = (int) $this->request->variable('start', 0);
88
		$cat = (int) $this->request->variable('select', $this->config['smilies_category_nb']);
89
		$count = (int) $this->category->smilies_count($cat);
90
		$title = $this->category->extract_list_categories($cat);
91
		$data = $this->category->get_version();
92
		$url = $this->helper->route('sylver35_smiliescat_smilies_pop');
93
94
		$sql = $this->db->sql_build_query('SELECT', array(
95
			'SELECT'	=> '*',
96
			'FROM'		=> array(SMILIES_TABLE => ''),
97
			'WHERE'		=> 'category = ' . $cat,
98
			'ORDER_BY'	=> 'smiley_order ASC',
99
		));
100
		$result = $this->db->sql_query_limit($sql, (int) $this->config['smilies_per_page_cat'], $start);
101
		while ($row = $this->db->sql_fetchrow($result))
102
		{
103
			$this->template->assign_block_vars('smilies', array(
104
				'SMILEY_CODE'		=> $row['code'],
105
				'SMILEY_EMOTION'	=> $row['emotion'],
106
				'SMILEY_WIDTH'		=> $row['smiley_width'],
107
				'SMILEY_HEIGHT'		=> $row['smiley_height'],
108
				'SMILEY_SRC'		=> generate_board_url() . '/' . $this->config['smilies_path'] . '/' . $row['smiley_url'],
109
			));
110
		}
111
		$this->db->sql_freeresult($result);
112
113
		$start = $this->pagination->validate_start($start, (int) $this->config['smilies_per_page_cat'], $count);
114
		$this->pagination->generate_template_pagination("{$url}?select={$cat}", 'pagination', 'start', $count, (int) $this->config['smilies_per_page_cat'], $start);
115
116
		$this->template->assign_vars(array(
117
			'U_SELECT_CAT'		=> $url,
118
			'LIST_CATEGORY'		=> $this->category->select_categories($cat, false, true),
119
			'POPUP_TITLE'		=> $this->language->lang('SC_CATEGORY_IN', $title),
120
			'SC_VERSION'		=> $this->language->lang('SC_VERSION_COPY', $data['homepage'], $data['version']),
121
		));
122
123
		page_header($this->language->lang('SC_CATEGORY_IN', $title));
124
125
		$this->template->set_filenames(array(
126
			'body' => '@sylver35_smiliescat/smilies_category.html')
127
		);
128
129
		page_footer();
130
	}
131
132
	public function ajax_list_cat()
133
	{
134
		$i = $cat = 0;
135
		$list_cat = array();
136
		$id = (int) $this->request->variable('id', 0);
137
		$action = (string) $this->request->variable('action', '');
138
		
139
		$this->category->move_cat($action, $id);
140
		$max = $this->category->get_max_order();
141
142
		$sql = $this->db->sql_build_query('SELECT', array(
143
			'SELECT'	=> 'l.lang_iso, l.lang_local_name, c.*',
144
			'FROM'		=> array(LANG_TABLE => 'l'),
145
			'LEFT_JOIN'	=> array(
146
				array(
147
					'FROM'	=> array($this->smilies_category_table => 'c'),
148
					'ON'	=> 'cat_lang = lang_iso',
149
				),
150
			),
151
			'ORDER_BY'	=> 'cat_order ASC, cat_lang_id ASC',
152
		));
153
		$result = $this->db->sql_query($sql);
154
		while ($row = $this->db->sql_fetchrow($result))
155
		{
156
			$list_cat[$i] = array(
157
				'catNr'			=> $i + 1,
158
				'langEmpty'		=> !$row['cat_id'] && !$row['cat_order'] && !$row['cat_name'],
159
				'spacerCat'		=> $this->language->lang('SC_CATEGORY_IN', $row['cat_title']),
160
				'catLang'		=> $row['lang_local_name'],
161
				'catIso'		=> $row['lang_iso'],
162
				'catTranslate'	=> $row['cat_name'],
163
				'catId'			=> (int) $row['cat_id'],
164
				'catOrder'		=> (int) $row['cat_order'],
165
				'catNb'			=> (int) $row['cat_nb'],
166
				'row'			=> (int) $row['cat_id'] !== $cat,
167
				'rowMax'		=> (int) $row['cat_order'] === $max,
168
				'uEdit'			=> '&amp;action=edit&amp;id=' . $row['cat_id'],
169
				'uDelete'		=> '&amp;action=delete&amp;id=' . $row['cat_id'],
170
			);
171
			$i++;
172
			// Keep this value in memory
173
			$cat = (int) $row['cat_id'];
174
		}
175
		$this->db->sql_freeresult($result);
176
		
177
		$json_response = new \phpbb\json_response;
178
		$json_response->send(array(
179
			'total'	=> $i,
180
			'datas'	=> $list_cat,
181
		));
182
	}
183
}
184