categories::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 1
nc 1
nop 11

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
*
4
* phpBB Directory extension for the phpBB Forum Software package.
5
*
6
* @copyright (c) 2014 ErnadoO <http://www.phpbb-services.com>
7
* @license GNU General Public License, version 2 (GPL-2.0)
8
*
9
*/
10
11
namespace ernadoo\phpbbdirectory\controller;
12
13
use Symfony\Component\HttpFoundation\RedirectResponse;
14
use \ernadoo\phpbbdirectory\core\helper;
15
use E1379\SpeakingUrl\SpeakingUrl;
16
17
class categories extends helper
18
{
19
	/** @var \phpbb\db\driver\driver_interface */
20
	protected $db;
21
22
	/** @var \phpbb\config\config */
23
	protected $config;
24
25
	/** @var \phpbb\language\language */
26
	protected $language;
27
28
	/** @var \phpbb\template\template */
29
	protected $template;
30
31
	/** @var \phpbb\user */
32
	protected $user;
33
34
	/** @var \phpbb\controller\helper */
35
	protected $helper;
36
37
	/** @var \phpbb\request\request */
38
	protected $request;
39
40
	/** @var \phpbb\auth\auth */
41
	protected $auth;
42
43
	/** @var \phpbb\pagination */
44
	protected $pagination;
45
46
	/** @var \ernadoo\phpbbdirectory\core\categorie */
47
	protected $categorie;
48
49
	/** @var \ernadoo\phpbbdirectory\core\link */
50
	protected $link;
51
52
	/**
53
	* Constructor
54
	*
55
	* @param \phpbb\db\driver\driver_interface					$db			Database object
56
	* @param \phpbb\config\config								$config		Config object
57
	* @param \phpbb\language\language							$language	Language object
58
	* @param \phpbb\template\template							$template	Template object
59
	* @param \phpbb\user										$user		User object
60
	* @param \phpbb\controller\helper							$helper		Controller helper object
61
	* @param \phpbb\request\request								$request	Request object
62
	* @param \phpbb\auth\auth									$auth		Auth object
63
	* @param \phpbb\pagination									$pagination	Pagination object
64
	* @param \ernadoo\phpbbdirectory\core\categorie				$categorie	PhpBB Directory extension categorie object
65
	* @param \ernadoo\phpbbdirectory\core\link					$link		PhpBB Directory extension link object
66
	*/
67
	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\config\config $config, \phpbb\language\language $language, \phpbb\template\template $template, \phpbb\user $user, \phpbb\controller\helper $helper, \phpbb\request\request $request, \phpbb\auth\auth $auth, \phpbb\pagination $pagination, \ernadoo\phpbbdirectory\core\categorie $categorie, \ernadoo\phpbbdirectory\core\link $link)
68
	{
69
		$this->db			= $db;
70
		$this->config		= $config;
71
		$this->language		= $language;
72
		$this->template		= $template;
73
		$this->user			= $user;
74
		$this->helper		= $helper;
75
		$this->request		= $request;
76
		$this->auth			= $auth;
77
		$this->pagination	= $pagination;
78
		$this->categorie	= $categorie;
79
		$this->link			= $link;
80
81
		$language->add_lang('directory', 'ernadoo/phpbbdirectory');
82
83
		$template->assign_vars(array(
84
			'S_PHPBB_DIRECTORY'	=> true,
85
		));
86
	}
87
88
	/**
89
	* Base controller to be accessed with the URL /directory
90
	*
91
	* @return	\Symfony\Component\HttpFoundation\Response	A Symfony Response object
92
	*/
93
	public function base()
94
	{
95
		$this->categorie->display();
96
		$this->link->recents();
97
98
		return $this->helper->render('body.html', $this->language->lang('DIRECTORY'));
99
	}
100
101
	/**
102
	* Legacy view controller for display a category
103
	* Used with /directory/categorie/{cat_id}
104
	* @deprecated 2.0.0 No longer used since dynamic routing.
105
	*
106
	* @param	int		$cat_id		The category ID
107
	* @param	int		$page		Page number taken from the URL
108
	* @return	\Symfony\Component\HttpFoundation\Response	A Symfony Response object
109
	* @throws	\phpbb\exception\http_exception
110
	*/
111
	public function view($cat_id, $page)
112
	{
113
		if (false === $this->categorie->get($cat_id))
114
		{
115
			throw new \phpbb\exception\http_exception(404, 'DIR_ERROR_NO_CATS');
116
		}
117
118
		$url = $this->helper->route('ernadoo_phpbbdirectory_dynamic_route_' . $cat_id, array('page' => $page));
119
120
		return new RedirectResponse($url, 301);
121
	}
122
123
	/**
124
	* View controller for display a category
125
	*
126
	* @param	int		$cat_id		The category ID
127
	* @param	int		$page		Page number taken from the URL
128
	* @param	int		$sort_days	Specifies the maximum amount of days a link may be old
129
	* @param	string	$sort_key	is the key of $sort_by_sql for the selected sorting: a|t|r|s|v|p
130
	* @param	string	$sort_dir	is either a or d representing ASC and DESC (ascending|descending)
131
	* @param	string	$mode		watch|unwatch
132
	* @return	\Symfony\Component\HttpFoundation\Response	A Symfony Response object
133
	* @throws	\phpbb\exception\http_exception
134
	*/
135
	public function view_route($cat_id, $page = 1, $sort_days = 0, $sort_key = '', $sort_dir = '', $mode = '')
136
	{
137
		if (false === $this->categorie->get($cat_id))
138
		{
139
			throw new \phpbb\exception\http_exception(404, 'DIR_ERROR_NO_CATS');
140
		}
141
142
		$start = ($page - 1) * $this->config['dir_show'];
143
144
		$default_sort_days	= 0;
145
		$default_sort_key	= (string) substr($this->config['dir_default_order'], 0, 1);
146
		$default_sort_dir	= (string) substr($this->config['dir_default_order'], 2);
147
148
		$sort_days	= (!$sort_days) ? $this->request->variable('st', $default_sort_days) : $sort_days;
149
		$sort_key 	= (!$sort_key) ? $this->request->variable('sk', $default_sort_key) : $sort_key;
150
		$sort_dir	= (!$sort_dir) ? $this->request->variable('sd', $default_sort_dir) : $sort_dir;
151
		$link_list = $rowset = array();
152
153
		// Categorie ordering options
154
		$limit_days		= array(0 => $this->language->lang('SEE_ALL'), 1 => $this->language->lang('1_DAY'), 7 => $this->language->lang('7_DAYS'), 14 => $this->language->lang('2_WEEKS'), 30 => $this->language->lang('1_MONTH'), 90 => $this->language->lang('3_MONTHS'), 180 => $this->language->lang('6_MONTHS'), 365 => $this->language->lang('1_YEAR'));
155
		$sort_by_text	= array('a' => $this->language->lang('AUTHOR'), 't' => $this->language->lang('POST_TIME'), 'r' => $this->language->lang('DIR_COMMENTS_ORDER'), 's' =>  $this->language->lang('DIR_NAME_ORDER'), 'v' => $this->language->lang('DIR_NB_CLICKS_ORDER'));
156
		$sort_by_sql	= array('a' => 'u.username_clean', 't' => array('l.link_time', 'l.link_id'), 'r' => 'l.link_comment', 's' => 'LOWER(l.link_name)', 'v' => 'l.link_view');
157
158
		$s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
159
		gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param, $default_sort_days, $default_sort_key, $default_sort_dir);
160
161
		$u_sort_param = ($sort_days === $default_sort_days && $sort_key == $default_sort_key && $sort_dir == $default_sort_dir) ? array() : array('sort_days' => $sort_days, 'sort_key' => $sort_key, 'sort_dir' => $sort_dir);
162
163
		// Are we watching this categorie?
164
		$s_watching_categorie = array(
165
			'link'			=> '',
166
			'link_toggle'	=> '',
167
			'title'			=> '',
168
			'title_toggle'	=> '',
169
			'is_watching'	=> false,
170
		);
171
172
		if ($this->config['email_enable'] && $this->user->data['is_registered'])
173
		{
174
			$notify_status = (isset($this->categorie->data['notify_status'])) ? $this->categorie->data['notify_status'] : null;
175
176
			if (($message = $this->categorie->watch_categorie($mode, $s_watching_categorie, $this->user->data['user_id'], $cat_id, $notify_status)))
177
			{
178
				return $this->helper->message($message);
179
			}
180
		}
181
182
		// A deadline has been selected
183
		if ($sort_days)
184
		{
185
			$min_post_time = time() - ($sort_days * 86400);
186
187
			$sql = 'SELECT COUNT(link_id) AS nb_links
188
				FROM ' . $this->links_table . '
189
				WHERE link_cat = ' . (int) $cat_id . '
190
					AND link_time >= ' . (int) $min_post_time;
191
			$result = $this->db->sql_query($sql);
192
			$nb_links = (int) $this->db->sql_fetchfield('nb_links');
193
			$this->db->sql_freeresult($result);
194
195
			if ($this->request->is_set_post('sort'))
196
			{
197
				$start = 0;
198
			}
199
			$sql_limit_time = " AND l.link_time >= $min_post_time";
200
		}
201
		else
202
		{
203
			$sql_limit_time = '';
204
			$nb_links		= (int) $this->categorie->data['cat_links'];
205
		}
206
207
		// Make sure $start is set to the last page if it exceeds the amount
208
		$start = $this->pagination->validate_start($start, $this->config['dir_show'], $nb_links);
209
210
		// Build navigation links
211
		$this->categorie->generate_dir_nav($this->categorie->data);
212
213
		// Jumpbox
214
		$this->categorie->make_cat_jumpbox();
215
216
		$base_url = array(
217
			'routes'	=> 'ernadoo_phpbbdirectory_dynamic_route_' . $cat_id,
218
			'params'	=> array_merge(array('cat_id' => $cat_id), $u_sort_param),
219
		);
220
221
		$this->pagination->generate_template_pagination($base_url, 'pagination', 'page', $nb_links, $this->config['dir_show'], $start);
222
223
		$this->template->assign_vars(array(
224
			'CAT_NAME'				=> $this->categorie->data['cat_name'],
225
226
			'S_SELECT_SORT_DIR'		=> $s_sort_dir,
227
			'S_SELECT_SORT_KEY'		=> $s_sort_key,
228
			'S_SELECT_SORT_DAYS'	=> $s_limit_days,
229
			'S_CATLIST'				=> $this->categorie->make_cat_select($cat_id),
230
			'S_PAGE_ACTION'			=> $this->helper->route('ernadoo_phpbbdirectory_dynamic_route_' . $cat_id, array('page' => $page)),
231
			'S_CAT_ID'				=> $cat_id,
232
233
			'TOTAL_LINKS'			=> $this->language->lang('DIR_NB_LINKS', (int) $nb_links),
234
235
			'U_NEW_SITE' 			=> $this->helper->route('ernadoo_phpbbdirectory_new_controller', array('cat_id' => $cat_id)),
236
237
			'U_VIEW_CAT'			=> $this->helper->route('ernadoo_phpbbdirectory_dynamic_route_' . $cat_id),
238
			'U_WATCH_CAT'			=> $s_watching_categorie['link'],
239
			'U_WATCH_CAT_TOGGLE'	=> $s_watching_categorie['link_toggle'],
240
			'S_WATCH_CAT_TITLE'		=> $s_watching_categorie['title'],
241
			'S_WATCH_CAT_TOGGLE'	=> $s_watching_categorie['title_toggle'],
242
			'S_WATCHING_CAT'		=> $s_watching_categorie['is_watching'],
243
		));
244
245
		// If the user is trying to reach late pages, start searching from the end
246
		$store_reverse = false;
247
		$sql_limit = $this->config['dir_show'];
248
		if ($start > $nb_links / 2)
249
		{
250
			$store_reverse = true;
251
252
			// Select the sort order
253
			$direction = (($sort_dir == 'd') ? 'ASC' : 'DESC');
254
255
			$sql_limit = $this->pagination->reverse_limit($start, $sql_limit, $nb_links);
256
			$sql_start = $this->pagination->reverse_start($start, $sql_limit, $nb_links);
257
		}
258
		else
259
		{
260
			// Select the sort order
261
			$direction = (($sort_dir == 'd') ? 'DESC' : 'ASC');
262
			$sql_start = $start;
263
		}
264
265 View Code Duplication
		if (is_array($sort_by_sql[$sort_key]))
266
		{
267
			$sql_sort_order = implode(' ' . $direction . ', ', $sort_by_sql[$sort_key]) . ' ' . $direction;
268
		}
269
		else
270
		{
271
			$sql_sort_order = $sort_by_sql[$sort_key] . ' ' . $direction;
272
		}
273
274
		// Grab just the sorted link ids
275
		$sql_array = array(
276
			'SELECT'	=> 'l.link_id',
277
			'FROM'		=> array(
278
					$this->links_table	=> 'l'),
279
			'LEFT_JOIN'	=> array(
280
					array(
281
						'FROM'	=> array(USERS_TABLE	=> 'u'),
282
						'ON'	=> 'l.link_user_id = u.user_id'
283
					),
284
			),
285
			'WHERE'		=> "l.link_cat = $cat_id
286
				AND l.link_active = 1
287
					$sql_limit_time",
288
			'ORDER_BY'	=> $sql_sort_order
289
		);
290
291
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
292
		$result = $this->db->sql_query_limit($sql, $sql_limit, $sql_start);
293
294
		while ($row = $this->db->sql_fetchrow($result))
295
		{
296
			$link_list[] = (int) $row['link_id'];
297
		}
298
		$this->db->sql_freeresult($result);
299
300
		if (count($link_list))
301
		{
302
			// We get links, informations about poster, votes and number of comments
303
			$sql_array = array(
304
				'SELECT'	=> 'l.link_id, l.link_cat, l.link_url, l.link_user_id, l.link_comment, l. link_description, l.link_banner, l.link_rss, l. link_uid, l.link_bitfield, l.link_flags, l.link_vote, l.link_note, l.link_view, l.link_time, l.link_name, l.link_flag, l.link_thumb, u.user_id, u.username, u.user_colour, v.vote_user_id',
305
				'FROM'		=> array(
306
						$this->links_table	=> 'l'),
307
				'LEFT_JOIN'	=> array(
308
						array(
309
							'FROM'	=> array(USERS_TABLE	=> 'u'),
310
							'ON'	=> 'l.link_user_id = u.user_id'
311
						),
312
						array(
313
							'FROM'	=> array($this->votes_table => 'v'),
314
							'ON'	=> 'l.link_id = v.vote_link_id AND v.vote_user_id = ' . $this->user->data['user_id']
315
						)
316
				),
317
				'WHERE'		=> $this->db->sql_in_set('l.link_id', $link_list)
318
			);
319
320
			$sql = $this->db->sql_build_query('SELECT', $sql_array);
321
			$result = $this->db->sql_query($sql);
322
323
			while ($site = $this->db->sql_fetchrow($result))
324
			{
325
				$rowset[$site['link_id']] = $site;
326
			}
327
			$this->db->sql_freeresult($result);
328
329
			$link_list = ($store_reverse) ? array_reverse($link_list) : $link_list;
330
331
			$votes_status 		= ($this->categorie->data['cat_allow_votes']) ? true : false;
332
			$comments_status 	= ($this->categorie->data['cat_allow_comments']) ? true : false;
333
334
			foreach ($link_list as $link_id)
335
			{
336
				$site = &$rowset[$link_id];
337
338
				$s_flag		= $this->link->display_flag($site);
339
				$s_note		= $this->link->display_note($site['link_note'], $site['link_vote'], $votes_status);
340
				$s_thumb	= $this->link->display_thumb($site);
341
				$s_vote		= $this->link->display_vote($site);
342
				$s_banner	= $this->link->display_bann($site);
343
				$s_rss		= $this->link->display_rss($site);
344
345
				$edit_allowed 	= ($this->user->data['is_registered'] && ($this->auth->acl_get('m_edit_dir') || ($this->user->data['user_id'] == $site['link_user_id'] && $this->auth->acl_get('u_edit_dir'))));
346
				$delete_allowed = ($this->user->data['is_registered'] && ($this->auth->acl_get('m_delete_dir') || ($this->user->data['user_id'] == $site['link_user_id'] && $this->auth->acl_get('u_delete_dir'))));
347
348
				$this->template->assign_block_vars('site', array(
349
					'BANNER'		=> $s_banner,
350
					'COUNT'			=> $this->language->lang('DIR_NB_CLICKS', (int) $site['link_view']),
351
					'DESCRIPTION' 	=> generate_text_for_display($site['link_description'], $site['link_uid'], $site['link_bitfield'], $site['link_flags']),
352
					'LINK_ID'		=> $site['link_id'],
353
					'NAME'			=> $site['link_name'],
354
					'NB_COMMENT'	=> ($comments_status) ? $this->language->lang('DIR_NB_COMMS', (int) $site['link_comment']) : '',
355
					'NB_VOTE'		=> $this->language->lang('DIR_NB_VOTES', (int) $site['link_vote']),
356
					'NOTE'			=> $s_note,
357
					'RSS'			=> $s_rss,
358
					'TIME'			=> ($site['link_time']) ? $this->user->format_date($site['link_time']) : '',
359
					'USER'			=> get_username_string('full', $site['link_user_id'], $site['username'], $site['user_colour']),
360
					'VOTE_LIST'		=> ($votes_status) ? $s_vote : '',
361
362
					'IMG_FLAG'		=> $s_flag,
363
					'ON_CLICK' 		=> "onclick=\"window.open('".$this->helper->route('ernadoo_phpbbdirectory_view_controller', array('link_id' => (int) $site['link_id']))."'); return false;\"",
364
365
					'S_NEW_LINK'	=> (((time() - $site['link_time']) / 86400) <= $this->config['dir_new_time']) ? true : false,
366
367
					'U_COMMENT'		=> ($comments_status) ? $this->helper->route('ernadoo_phpbbdirectory_comment_view_controller', array('link_id' => (int) $site['link_id'])) : '',
368
					'U_DELETE'		=> $delete_allowed ? $this->helper->route('ernadoo_phpbbdirectory_delete_controller', array('cat_id' => (int) $cat_id, 'link_id' => (int) $site['link_id'], '_referer' => $this->helper->get_current_url())) : '',
369
					'U_EDIT'		=> $edit_allowed ? $this->helper->route('ernadoo_phpbbdirectory_edit_controller', array('cat_id' => (int) $cat_id, 'link_id' => (int) $site['link_id'])) : '',
370
					'U_FORM_VOTE'	=> ($votes_status) ? $this->helper->route('ernadoo_phpbbdirectory_vote_controller', array('cat_id' => (int) $site['link_cat'], 'link_id' => (int) $site['link_id'])) : '',
371
					'U_LINK'		=> $site['link_url'],
372
					'U_THUMB'		=> $s_thumb,
373
				));
374
			}
375
		}
376
		else
377
		{
378
			$this->template->assign_block_vars('no_draw_link', array());
379
		}
380
381
		$page_title = $this->language->lang('DIRECTORY') . ' - ' . $this->categorie->data['cat_name'];
382
383
		$this->categorie->display();
384
385
		return $this->helper->render('view_cat.html', $page_title);
386
	}
387
388
	/**
389
	* date controller for return a date
390
	*
391
	* @return	\phpbb\json_response	A Json Response
392
	* @throws	\phpbb\exception\http_exception
393
	*/
394
	public function return_date()
395
	{
396
		if (!$this->request->is_ajax())
397
		{
398
			throw new \phpbb\exception\http_exception(403, 'DIR_ERROR_NOT_AUTH');
399
		}
400
401
		$timestamp = $this->request->variable('timestamp', 0);
402
		$json_response = new \phpbb\json_response;
403
		$json_response->send(array(
404
			'success'	=> true,
405
			'DATE'		=> $this->user->format_date((int) $timestamp),
406
		));
407
	}
408
409
	/**
410
	* slug controller for return a slugify category name
411
	*
412
	* @return	\phpbb\json_response	A Json Response
413
	* @throws	\phpbb\exception\http_exception
414
	*/
415
	public function return_slug()
416
	{
417
		if (!$this->request->is_ajax())
418
		{
419
			throw new \phpbb\exception\http_exception(403, 'DIR_ERROR_NOT_AUTH');
420
		}
421
422
		$slug = new SpeakingUrl();
423
		$cat_name = $this->request->variable('cat_name', '', true);
424
425
		$json_response = new \phpbb\json_response;
426
		$json_response->send(array(
427
			'success'	=> true,
428
			'SLUG'		=> $slug->getSlug(html_entity_decode($cat_name), array('lang' => $this->config['default_lang'], 'symbols' => true)),
429
		));
430
	}
431
}
432