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