Completed
Push — 3.2.x ( eabb04...b8f3d7 )
by Erwan
02:27
created

categories::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 15
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
	* @param	int		$sort_days	Specifies the maximum amount of days a link may be old
109
	* @param	string	$sort_key	is the key of $sort_by_sql for the selected sorting: a|t|r|s|v|p
110
	* @param	string	$sort_dir	is either a or d representing ASC and DESC (ascending|descending)
111
	* @param	string	$mode		watch|unwatch
112
	* @return	\Symfony\Component\HttpFoundation\Response	A Symfony Response object
113
	* @throws	\phpbb\exception\http_exception
114
	*/
115
	public function view($cat_id, $page, $sort_days, $sort_key, $sort_dir, $mode = '')
0 ignored issues
show
Unused Code introduced by
The parameter $mode is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
116
	{
117
		$url = $this->helper->route('ernadoo_phpbbdirectory_dynamic_route_' . $cat_id, array('page' => $page, 'sort_days' => $sort_days, 'sort_key' => $sort_key, 'sort_dir' => $sort_dir));
118
119
		return new RedirectResponse($url, 301);
120
	}
121
122
	/**
123
	* View controller for display a category
124
	*
125
	* @param	int		$cat_id		The category ID
126
	* @param	int		$page		Page number taken from the URL
127
	* @param	int		$sort_days	Specifies the maximum amount of days a link may be old
128
	* @param	string	$sort_key	is the key of $sort_by_sql for the selected sorting: a|t|r|s|v|p
129
	* @param	string	$sort_dir	is either a or d representing ASC and DESC (ascending|descending)
130
	* @param	string	$mode		watch|unwatch
131
	* @return	\Symfony\Component\HttpFoundation\Response	A Symfony Response object
132
	* @throws	\phpbb\exception\http_exception
133
	*/
134
	public function view_route($cat_id, $page, $sort_days, $sort_key, $sort_dir, $mode = '')
135
	{
136
		if (false === $this->categorie->get($cat_id))
137
		{
138
			throw new \phpbb\exception\http_exception(404, 'DIR_ERROR_NO_CATS');
139
		}
140
141
		$start = ($page - 1) * $this->config['dir_show'];
142
143
		$default_sort_days	= 0;
144
		$default_sort_key	= (string) substr($this->config['dir_default_order'], 0, 1);
145
		$default_sort_dir	= (string) substr($this->config['dir_default_order'], 2);
146
147
		$sort_days	= (!$sort_days) ? $this->request->variable('st', $default_sort_days) : $sort_days;
148
		$sort_key 	= (!$sort_key) ? $this->request->variable('sk', $default_sort_key) : $sort_key;
149
		$sort_dir	= (!$sort_dir) ? $this->request->variable('sd', $default_sort_dir) : $sort_dir;
150
		$link_list = $rowset = array();
151
152
		// Categorie ordering options
153
		$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'));
154
		$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'));
155
		$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');
156
157 View Code Duplication
		if ($this->config['dir_activ_pagerank'])
158
		{
159
			$sort_by_text['p'] = $this->language->lang('DIR_PR_ORDER');
160
			$sort_by_sql['p'] = 'l.link_pagerank';
161
		}
162
163
		$s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
164
		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);
165
166
		$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);
167
168
		// Are we watching this categorie?
169
		$s_watching_categorie = array(
170
			'link'			=> '',
171
			'link_toggle'	=> '',
172
			'title'			=> '',
173
			'title_toggle'	=> '',
174
			'is_watching'	=> false,
175
		);
176
177
		if ($this->config['email_enable'] && $this->user->data['is_registered'])
178
		{
179
			$notify_status = (isset($this->categorie->data['notify_status'])) ? $this->categorie->data['notify_status'] : null;
180
181
			if (($message = $this->categorie->watch_categorie($mode, $s_watching_categorie, $this->user->data['user_id'], $cat_id, $notify_status)))
182
			{
183
				return $this->helper->message($message);
184
			}
185
		}
186
187
		// A deadline has been selected
188
		if ($sort_days)
189
		{
190
			$min_post_time = time() - ($sort_days * 86400);
191
192
			$sql = 'SELECT COUNT(link_id) AS nb_links
193
				FROM ' . $this->links_table . '
194
				WHERE link_cat = ' . (int) $cat_id . '
195
					AND link_time >= ' . (int) $min_post_time;
196
			$result = $this->db->sql_query($sql);
197
			$nb_links = (int) $this->db->sql_fetchfield('nb_links');
198
			$this->db->sql_freeresult($result);
199
200
			if ($this->request->is_set_post('sort'))
201
			{
202
				$start = 0;
203
			}
204
			$sql_limit_time = " AND l.link_time >= $min_post_time";
205
		}
206
		else
207
		{
208
			$sql_limit_time = '';
209
			$nb_links		= (int) $this->categorie->data['cat_links'];
210
		}
211
212
		// Make sure $start is set to the last page if it exceeds the amount
213
		$start = $this->pagination->validate_start($start, $this->config['dir_show'], $nb_links);
214
215
		// Build navigation links
216
		$this->categorie->generate_dir_nav($this->categorie->data);
217
218
		// Jumpbox
219
		$this->categorie->make_cat_jumpbox();
220
221
		$base_url = array(
222
			'routes'	=> 'ernadoo_phpbbdirectory_dynamic_route_' . $cat_id,
223
			'params'	=> array_merge(array('cat_id' => $cat_id), $u_sort_param),
224
		);
225
226
		$this->pagination->generate_template_pagination($base_url, 'pagination', 'page', $nb_links, $this->config['dir_show'], $start);
227
228
		$this->template->assign_vars(array(
229
			'CAT_NAME'				=> $this->categorie->data['cat_name'],
230
231
			'S_SELECT_SORT_DIR'		=> $s_sort_dir,
232
			'S_SELECT_SORT_KEY'		=> $s_sort_key,
233
			'S_SELECT_SORT_DAYS'	=> $s_limit_days,
234
			'S_CATLIST'				=> $this->categorie->make_cat_select($cat_id),
235
			'S_PAGE_ACTION'			=> $this->helper->route('ernadoo_phpbbdirectory_dynamic_route_' . $cat_id, array('page' => $page)),
236
			'S_CAT_ID'				=> $cat_id,
237
238
			'TOTAL_LINKS'			=> $this->language->lang('DIR_NB_LINKS', (int) $nb_links),
239
240
			'U_NEW_SITE' 			=> $this->helper->route('ernadoo_phpbbdirectory_new_controller', array('cat_id' => $cat_id)),
241
242
			'U_VIEW_CAT'			=> $this->helper->route('ernadoo_phpbbdirectory_dynamic_route_' . $cat_id),
243
			'U_WATCH_CAT'			=> $s_watching_categorie['link'],
244
			'U_WATCH_CAT_TOGGLE'	=> $s_watching_categorie['link_toggle'],
245
			'S_WATCH_CAT_TITLE'		=> $s_watching_categorie['title'],
246
			'S_WATCH_CAT_TOGGLE'	=> $s_watching_categorie['title_toggle'],
247
			'S_WATCHING_CAT'		=> $s_watching_categorie['is_watching'],
248
		));
249
250
		// If the user is trying to reach late pages, start searching from the end
251
		$store_reverse = false;
252
		$sql_limit = $this->config['dir_show'];
253
		if ($start > $nb_links / 2)
254
		{
255
			$store_reverse = true;
256
257
			// Select the sort order
258
			$direction = (($sort_dir == 'd') ? 'ASC' : 'DESC');
259
260
			$sql_limit = $this->pagination->reverse_limit($start, $sql_limit, $nb_links);
261
			$sql_start = $this->pagination->reverse_start($start, $sql_limit, $nb_links);
262
		}
263
		else
264
		{
265
			// Select the sort order
266
			$direction = (($sort_dir == 'd') ? 'DESC' : 'ASC');
267
			$sql_start = $start;
268
		}
269
270 View Code Duplication
		if (is_array($sort_by_sql[$sort_key]))
271
		{
272
			$sql_sort_order = implode(' ' . $direction . ', ', $sort_by_sql[$sort_key]) . ' ' . $direction;
273
		}
274
		else
275
		{
276
			$sql_sort_order = $sort_by_sql[$sort_key] . ' ' . $direction;
277
		}
278
279
		// Grab just the sorted link ids
280
		$sql_array = array(
281
			'SELECT'	=> 'l.link_id',
282
			'FROM'		=> array(
283
					$this->links_table	=> 'l'),
284
			'LEFT_JOIN'	=> array(
285
					array(
286
						'FROM'	=> array(USERS_TABLE	=> 'u'),
287
						'ON'	=> 'l.link_user_id = u.user_id'
288
					),
289
			),
290
			'WHERE'		=> "l.link_cat = $cat_id
291
				AND l.link_active = 1
292
					$sql_limit_time",
293
			'ORDER_BY'	=> $sql_sort_order
294
		);
295
296
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
297
		$result = $this->db->sql_query_limit($sql, $sql_limit, $sql_start);
298
299
		while ($row = $this->db->sql_fetchrow($result))
300
		{
301
			$link_list[] = (int) $row['link_id'];
302
		}
303
		$this->db->sql_freeresult($result);
304
305
		if (sizeof($link_list))
306
		{
307
			// We get links, informations about poster, votes and number of comments
308
			$sql_array = array(
309
				'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_pagerank, l.link_thumb, u.user_id, u.username, u.user_colour, v.vote_user_id',
310
				'FROM'		=> array(
311
						$this->links_table	=> 'l'),
312
				'LEFT_JOIN'	=> array(
313
						array(
314
							'FROM'	=> array(USERS_TABLE	=> 'u'),
315
							'ON'	=> 'l.link_user_id = u.user_id'
316
						),
317
						array(
318
							'FROM'	=> array($this->votes_table => 'v'),
319
							'ON'	=> 'l.link_id = v.vote_link_id AND v.vote_user_id = ' . $this->user->data['user_id']
320
						)
321
				),
322
				'WHERE'		=> $this->db->sql_in_set('l.link_id', $link_list)
323
			);
324
325
			$sql = $this->db->sql_build_query('SELECT', $sql_array);
326
			$result = $this->db->sql_query($sql);
327
328
			while ($site = $this->db->sql_fetchrow($result))
329
			{
330
				$rowset[$site['link_id']] = $site;
331
			}
332
			$this->db->sql_freeresult($result);
333
334
			$link_list = ($store_reverse) ? array_reverse($link_list) : $link_list;
335
336
			$votes_status 		= ($this->categorie->data['cat_allow_votes']) ? true : false;
337
			$comments_status 	= ($this->categorie->data['cat_allow_comments']) ? true : false;
338
339
			foreach ($link_list as $link_id)
340
			{
341
				$site = &$rowset[$link_id];
342
343
				$s_flag		= $this->link->display_flag($site);
344
				$s_note		= $this->link->display_note($site['link_note'], $site['link_vote'], $votes_status);
345
				$s_thumb	= $this->link->display_thumb($site);
346
				$s_vote		= $this->link->display_vote($site);
347
				$s_banner	= $this->link->display_bann($site);
348
				$s_pr		= $this->link->display_pagerank($site);
349
				$s_rss		= $this->link->display_rss($site);
350
351
				$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'))));
352
				$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'))));
353
354
				$this->template->assign_block_vars('site', array(
355
					'BANNER'		=> $s_banner,
356
					'COUNT'			=> $this->language->lang('DIR_NB_CLICKS', (int) $site['link_view']),
357
					'DESCRIPTION' 	=> generate_text_for_display($site['link_description'], $site['link_uid'], $site['link_bitfield'], $site['link_flags']),
358
					'LINK_ID'		=> $site['link_id'],
359
					'NAME'			=> $site['link_name'],
360
					'NB_COMMENT'	=> ($comments_status) ? $this->language->lang('DIR_NB_COMMS', (int) $site['link_comment']) : '',
361
					'NB_VOTE'		=> $this->language->lang('DIR_NB_VOTES', (int) $site['link_vote']),
362
					'NOTE'			=> $s_note,
363
					'PAGERANK'		=> $s_pr,
364
					'RSS'			=> $s_rss,
365
					'TIME'			=> ($site['link_time']) ? $this->user->format_date($site['link_time']) : '',
366
					'USER'			=> get_username_string('full', $site['link_user_id'], $site['username'], $site['user_colour']),
367
					'VOTE_LIST'		=> ($votes_status) ? $s_vote : '',
368
369
					'IMG_FLAG'		=> $s_flag,
370
					'ON_CLICK' 		=> "onclick=\"window.open('".$this->helper->route('ernadoo_phpbbdirectory_view_controller', array('link_id' => (int) $site['link_id']))."'); return false;\"",
371
372
					'S_NEW_LINK'	=> (((time() - $site['link_time']) / 86400) <= $this->config['dir_new_time']) ? true : false,
373
374
					'U_COMMENT'		=> ($comments_status) ? $this->helper->route('ernadoo_phpbbdirectory_comment_view_controller', array('link_id' => (int) $site['link_id'])) : '',
375
					'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())) : '',
376
					'U_EDIT'		=> $edit_allowed ? $this->helper->route('ernadoo_phpbbdirectory_edit_controller', array('cat_id' => (int) $cat_id, 'link_id' => (int) $site['link_id'])) : '',
377
					'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'])) : '',
378
					'U_LINK'		=> $site['link_url'],
379
					'U_THUMB'		=> $s_thumb,
380
				));
381
			}
382
		}
383
		else
384
		{
385
			$this->template->assign_block_vars('no_draw_link', array());
386
		}
387
388
		$page_title = $this->language->lang('DIRECTORY') . ' - ' . $this->categorie->data['cat_name'];
389
390
		$this->categorie->display();
391
392
		return $this->helper->render('view_cat.html', $page_title);
393
	}
394
395
	/**
396
	* date controller for return a date
397
	*
398
	* @return	\phpbb\json_response	A Json Response
399
	* @throws	\phpbb\exception\http_exception
400
	*/
401
	public function return_date()
402
	{
403
		if (!$this->request->is_ajax())
404
		{
405
			throw new \phpbb\exception\http_exception(403, 'DIR_ERROR_NOT_AUTH');
406
		}
407
408
		$timestamp = $this->request->variable('timestamp', 0);
409
		$json_response = new \phpbb\json_response;
410
		$json_response->send(array(
411
			'success'	=> true,
412
			'DATE'		=> $this->user->format_date((int) $timestamp),
413
		));
414
	}
415
416
	/**
417
	* slug controller for return a slugify category name
418
	*
419
	* @return	\phpbb\json_response	A Json Response
420
	* @throws	\phpbb\exception\http_exception
421
	*/
422
	public function return_slug()
423
	{
424
		if (!$this->request->is_ajax())
425
		{
426
			throw new \phpbb\exception\http_exception(403, 'DIR_ERROR_NOT_AUTH');
427
		}
428
429
		$slug = new SpeakingUrl();
430
		$cat_name = $this->request->variable('cat_name', '', true);
431
432
		$json_response = new \phpbb\json_response;
433
		$json_response->send(array(
434
			'success'	=> true,
435
			'SLUG'		=> $slug->getSlug(html_entity_decode($cat_name), array('lang' => $this->config['default_lang'], 'symbols' => true)),
436
		));
437
	}
438
}
439