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