Conditions | 39 |
Paths | > 20000 |
Total Lines | 252 |
Code Lines | 153 |
Lines | 8 |
Ratio | 3.17 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
139 | public function view_route($cat_id, $page = 1, $sort_days = 0, $sort_key = '', $sort_dir = '', $mode = '') |
||
140 | { |
||
141 | if (false === $this->categorie->get($cat_id)) |
||
142 | { |
||
143 | throw new \phpbb\exception\http_exception(404, 'DIR_ERROR_NO_CATS'); |
||
144 | } |
||
145 | |||
146 | $start = ($page - 1) * $this->config['dir_show']; |
||
147 | |||
148 | $default_sort_days = 0; |
||
149 | $default_sort_key = (string) substr($this->config['dir_default_order'], 0, 1); |
||
150 | $default_sort_dir = (string) substr($this->config['dir_default_order'], 2); |
||
151 | |||
152 | $sort_days = (!$sort_days) ? $this->request->variable('st', $default_sort_days) : $sort_days; |
||
153 | $sort_key = (!$sort_key) ? $this->request->variable('sk', $default_sort_key) : $sort_key; |
||
154 | $sort_dir = (!$sort_dir) ? $this->request->variable('sd', $default_sort_dir) : $sort_dir; |
||
155 | $link_list = $rowset = array(); |
||
156 | |||
157 | // Categorie ordering options |
||
158 | $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')); |
||
159 | $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')); |
||
160 | $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'); |
||
161 | |||
162 | $s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = ''; |
||
163 | 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); |
||
164 | |||
165 | $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); |
||
166 | |||
167 | // Are we watching this categorie? |
||
168 | $s_watching_categorie = array( |
||
169 | 'link' => '', |
||
170 | 'link_toggle' => '', |
||
171 | 'title' => '', |
||
172 | 'title_toggle' => '', |
||
173 | 'is_watching' => false, |
||
174 | ); |
||
175 | |||
176 | if ($this->config['email_enable'] && $this->user->data['is_registered']) |
||
177 | { |
||
178 | $notify_status = (isset($this->categorie->data['notify_status'])) ? $this->categorie->data['notify_status'] : null; |
||
179 | |||
180 | if (($message = $this->categorie->watch_categorie($mode, $s_watching_categorie, $this->user->data['user_id'], $cat_id, $notify_status))) |
||
181 | { |
||
182 | return $this->helper->message($message); |
||
183 | } |
||
184 | } |
||
185 | |||
186 | // A deadline has been selected |
||
187 | if ($sort_days) |
||
188 | { |
||
189 | $min_post_time = time() - ($sort_days * 86400); |
||
190 | |||
191 | $sql = 'SELECT COUNT(link_id) AS nb_links |
||
192 | FROM ' . $this->links_table . ' |
||
193 | WHERE link_cat = ' . (int) $cat_id . ' |
||
194 | AND link_time >= ' . $min_post_time; |
||
195 | $result = $this->db->sql_query($sql); |
||
196 | $nb_links = (int) $this->db->sql_fetchfield('nb_links'); |
||
197 | $this->db->sql_freeresult($result); |
||
198 | |||
199 | if ($this->request->is_set_post('sort')) |
||
200 | { |
||
201 | $start = 0; |
||
202 | } |
||
203 | $sql_limit_time = " AND l.link_time >= $min_post_time"; |
||
204 | } |
||
205 | else |
||
206 | { |
||
207 | $sql_limit_time = ''; |
||
208 | $nb_links = (int) $this->categorie->data['cat_links']; |
||
209 | } |
||
210 | |||
211 | // Make sure $start is set to the last page if it exceeds the amount |
||
212 | $start = $this->pagination->validate_start($start, $this->config['dir_show'], $nb_links); |
||
213 | |||
214 | // Build navigation links |
||
215 | $this->categorie->generate_dir_nav($this->categorie->data); |
||
216 | |||
217 | // Jumpbox |
||
218 | $this->categorie->make_cat_jumpbox(); |
||
219 | |||
220 | $base_url = array( |
||
221 | 'routes' => 'ernadoo_phpbbdirectory_dynamic_route_' . $cat_id, |
||
222 | 'params' => array_merge(array('cat_id' => $cat_id), $u_sort_param), |
||
223 | ); |
||
224 | |||
225 | $this->pagination->generate_template_pagination($base_url, 'pagination', 'page', $nb_links, $this->config['dir_show'], $start); |
||
226 | |||
227 | $this->template->assign_vars(array( |
||
228 | 'CAT_NAME' => $this->categorie->data['cat_name'], |
||
229 | |||
230 | 'S_SELECT_SORT_DIR' => $s_sort_dir, |
||
231 | 'S_SELECT_SORT_KEY' => $s_sort_key, |
||
232 | 'S_SELECT_SORT_DAYS' => $s_limit_days, |
||
233 | 'S_CATLIST' => $this->categorie->make_cat_select($cat_id), |
||
234 | 'S_PAGE_ACTION' => $this->helper->route('ernadoo_phpbbdirectory_dynamic_route_' . $cat_id, array('page' => $page)), |
||
235 | 'S_CAT_ID' => $cat_id, |
||
236 | |||
237 | 'TOTAL_LINKS' => $this->language->lang('DIR_NB_LINKS', (int) $nb_links), |
||
238 | |||
239 | 'U_NEW_SITE' => $this->helper->route('ernadoo_phpbbdirectory_new_controller', array('cat_id' => $cat_id)), |
||
240 | |||
241 | 'U_VIEW_CAT' => $this->helper->route('ernadoo_phpbbdirectory_dynamic_route_' . $cat_id), |
||
242 | 'U_WATCH_CAT' => $s_watching_categorie['link'], |
||
243 | 'U_WATCH_CAT_TOGGLE' => $s_watching_categorie['link_toggle'], |
||
244 | 'S_WATCH_CAT_TITLE' => $s_watching_categorie['title'], |
||
245 | 'S_WATCH_CAT_TOGGLE' => $s_watching_categorie['title_toggle'], |
||
246 | 'S_WATCHING_CAT' => $s_watching_categorie['is_watching'], |
||
247 | )); |
||
248 | |||
249 | // If the user is trying to reach late pages, start searching from the end |
||
250 | $store_reverse = false; |
||
251 | $sql_limit = $this->config['dir_show']; |
||
252 | if ($start > $nb_links / 2) |
||
253 | { |
||
254 | $store_reverse = true; |
||
255 | |||
256 | // Select the sort order |
||
257 | $direction = (($sort_dir == 'd') ? 'ASC' : 'DESC'); |
||
258 | |||
259 | $sql_limit = $this->pagination->reverse_limit($start, $sql_limit, $nb_links); |
||
260 | $sql_start = $this->pagination->reverse_start($start, $sql_limit, $nb_links); |
||
261 | } |
||
262 | else |
||
263 | { |
||
264 | // Select the sort order |
||
265 | $direction = (($sort_dir == 'd') ? 'DESC' : 'ASC'); |
||
266 | $sql_start = $start; |
||
267 | } |
||
268 | |||
269 | View Code Duplication | if (is_array($sort_by_sql[$sort_key])) |
|
270 | { |
||
271 | $sql_sort_order = implode(' ' . $direction . ', ', $sort_by_sql[$sort_key]) . ' ' . $direction; |
||
272 | } |
||
273 | else |
||
274 | { |
||
275 | $sql_sort_order = $sort_by_sql[$sort_key] . ' ' . $direction; |
||
276 | } |
||
277 | |||
278 | // Grab just the sorted link ids |
||
279 | $sql_array = array( |
||
280 | 'SELECT' => 'l.link_id', |
||
281 | 'FROM' => array( |
||
282 | $this->links_table => 'l'), |
||
283 | 'LEFT_JOIN' => array( |
||
284 | array( |
||
285 | 'FROM' => array(USERS_TABLE => 'u'), |
||
286 | 'ON' => 'l.link_user_id = u.user_id' |
||
287 | ), |
||
288 | ), |
||
289 | 'WHERE' => "l.link_cat = $cat_id |
||
290 | AND l.link_active = 1 |
||
291 | $sql_limit_time", |
||
292 | 'ORDER_BY' => $sql_sort_order |
||
293 | ); |
||
294 | |||
295 | $sql = $this->db->sql_build_query('SELECT', $sql_array); |
||
296 | $result = $this->db->sql_query_limit($sql, $sql_limit, $sql_start); |
||
297 | |||
298 | while ($row = $this->db->sql_fetchrow($result)) |
||
299 | { |
||
300 | $link_list[] = (int) $row['link_id']; |
||
301 | } |
||
302 | $this->db->sql_freeresult($result); |
||
303 | |||
304 | if (sizeof($link_list)) |
||
305 | { |
||
306 | // We get links, informations about poster, votes and number of comments |
||
307 | $sql_array = array( |
||
308 | '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', |
||
309 | 'FROM' => array( |
||
310 | $this->links_table => 'l'), |
||
311 | 'LEFT_JOIN' => array( |
||
312 | array( |
||
313 | 'FROM' => array(USERS_TABLE => 'u'), |
||
314 | 'ON' => 'l.link_user_id = u.user_id' |
||
315 | ), |
||
316 | array( |
||
317 | 'FROM' => array($this->votes_table => 'v'), |
||
318 | 'ON' => 'l.link_id = v.vote_link_id AND v.vote_user_id = ' . $this->user->data['user_id'] |
||
319 | ) |
||
320 | ), |
||
321 | 'WHERE' => $this->db->sql_in_set('l.link_id', $link_list) |
||
322 | ); |
||
323 | |||
324 | $sql = $this->db->sql_build_query('SELECT', $sql_array); |
||
325 | $result = $this->db->sql_query($sql); |
||
326 | |||
327 | while ($site = $this->db->sql_fetchrow($result)) |
||
328 | { |
||
329 | $rowset[$site['link_id']] = $site; |
||
330 | } |
||
331 | $this->db->sql_freeresult($result); |
||
332 | |||
333 | $link_list = ($store_reverse) ? array_reverse($link_list) : $link_list; |
||
334 | |||
335 | $votes_status = ($this->categorie->data['cat_allow_votes']) ? true : false; |
||
336 | $comments_status = ($this->categorie->data['cat_allow_comments']) ? true : false; |
||
337 | |||
338 | foreach ($link_list as $link_id) |
||
339 | { |
||
340 | $site = &$rowset[$link_id]; |
||
341 | |||
342 | $s_flag = $this->link->display_flag($site); |
||
343 | $s_note = $this->link->display_note($site['link_note'], $site['link_vote'], $votes_status); |
||
344 | $s_thumb = $this->link->display_thumb($site); |
||
345 | $s_vote = $this->link->display_vote($site); |
||
346 | $s_banner = $this->link->display_bann($site); |
||
347 | $s_rss = $this->link->display_rss($site); |
||
348 | |||
349 | $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')))); |
||
350 | $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')))); |
||
351 | |||
352 | $this->template->assign_block_vars('site', array( |
||
353 | 'BANNER' => $s_banner, |
||
354 | 'COUNT' => $this->language->lang('DIR_NB_CLICKS', (int) $site['link_view']), |
||
355 | 'DESCRIPTION' => generate_text_for_display($site['link_description'], $site['link_uid'], $site['link_bitfield'], $site['link_flags']), |
||
356 | 'LINK_ID' => $site['link_id'], |
||
357 | 'NAME' => $site['link_name'], |
||
358 | 'NB_COMMENT' => ($comments_status) ? $this->language->lang('DIR_NB_COMMS', (int) $site['link_comment']) : '', |
||
359 | 'NB_VOTE' => $this->language->lang('DIR_NB_VOTES', (int) $site['link_vote']), |
||
360 | 'NOTE' => $s_note, |
||
361 | 'RSS' => $s_rss, |
||
362 | 'TIME' => ($site['link_time']) ? $this->user->format_date($site['link_time']) : '', |
||
363 | 'USER' => get_username_string('full', $site['link_user_id'], $site['username'], $site['user_colour']), |
||
364 | 'VOTE_LIST' => ($votes_status) ? $s_vote : '', |
||
365 | |||
366 | 'IMG_FLAG' => $s_flag, |
||
367 | 'ON_CLICK' => "onclick=\"window.open('".$this->helper->route('ernadoo_phpbbdirectory_view_controller', array('link_id' => (int) $site['link_id']))."'); return false;\"", |
||
368 | |||
369 | 'S_NEW_LINK' => (((time() - $site['link_time']) / 86400) <= $this->config['dir_new_time']) ? true : false, |
||
370 | |||
371 | 'U_COMMENT' => ($comments_status) ? $this->helper->route('ernadoo_phpbbdirectory_comment_view_controller', array('link_id' => (int) $site['link_id'])) : '', |
||
372 | '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())) : '', |
||
373 | 'U_EDIT' => $edit_allowed ? $this->helper->route('ernadoo_phpbbdirectory_edit_controller', array('cat_id' => (int) $cat_id, 'link_id' => (int) $site['link_id'])) : '', |
||
374 | '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'])) : '', |
||
375 | 'U_LINK' => $site['link_url'], |
||
376 | 'U_THUMB' => $s_thumb, |
||
377 | )); |
||
378 | } |
||
379 | } |
||
380 | else |
||
381 | { |
||
382 | $this->template->assign_block_vars('no_draw_link', array()); |
||
383 | } |
||
384 | |||
385 | $page_title = $this->language->lang('DIRECTORY') . ' - ' . $this->categorie->data['cat_name']; |
||
386 | |||
387 | $this->categorie->display(); |
||
388 | |||
389 | return $this->helper->render('view_cat.html', $page_title); |
||
390 | } |
||
391 | |||
436 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.