functions.php ➔ generate_portal_pagination()   F
last analyzed

Complexity

Conditions 25
Paths 8008

Size

Total Lines 98
Code Lines 52

Duplication

Lines 24
Ratio 24.49 %

Code Coverage

Tests 1
CRAP Score 621.5673

Importance

Changes 0
Metric Value
cc 25
eloc 52
nc 8008
nop 8
dl 24
loc 98
ccs 1
cts 65
cp 0.0154
crap 621.5673
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity    Many Parameters   

Long Method

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:

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
// @codingStandardsIgnoreFile
3
/**
4
*
5
* @package Board3 Portal v2.1
6
* @copyright (c) 2013 Board3 Group ( www.board3.de )
7
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
8
*
9
*/
10
11
/**
12
 * @ignore
13
 */
14 1
if (!defined('IN_PHPBB'))
15 1
{
16
	exit;
17
}
18
19
// Get portal config
20
function obtain_portal_config()
21
{
22 25
	global $db, $cache, $portal_config;
23
24 25
	if (($portal_config = $cache->get('portal_config')) === false)
25 25
	{
26 24
		$portal_config = $portal_cached_config = array();
27
28
		$sql = 'SELECT config_name, config_value
29 24
			FROM ' . PORTAL_CONFIG_TABLE;
30 24
		$result = $db->sql_query($sql);
31
32 24
		while ($row = $db->sql_fetchrow($result))
33
		{
34 14
			$portal_cached_config[$row['config_name']] = $row['config_value'];
35
36 14
			$portal_config[$row['config_name']] = $row['config_value'];
37 14
		}
38 24
		$db->sql_freeresult($result);
39
40 24
		$cache->put('portal_config', $portal_cached_config);
41 24
	}
42
43 25
	return $portal_config;
44
}
45
46
/**
47
* Set config value. Creates missing config entry.
48
* Only use this if your config value might exceed 255 characters, otherwise please use set_config
49
*/
50
function set_portal_config($config_name, $config_value)
51
{
52 15
	global $db, $cache, $portal_config;
53
54 15
	$sql = 'UPDATE ' . PORTAL_CONFIG_TABLE . "
55 15
		SET config_value = '" . $db->sql_escape($config_value) . "'
56 15
		WHERE config_name = '" . $db->sql_escape($config_name) . "'";
57 15
	$db->sql_query($sql);
58
59 15
	if (!$db->sql_affectedrows() && !isset($portal_config[$config_name]))
60 15
	{
61 15
		$sql = 'INSERT INTO ' . PORTAL_CONFIG_TABLE . ' ' . $db->sql_build_array('INSERT', array(
62 15
			'config_name'	=> $config_name,
63 15
			'config_value'	=> $config_value));
64 15
		$db->sql_query($sql);
65 15
	}
66
67 15
	$portal_config[$config_name] = $config_value;
68
69 15
	$cache->destroy('portal_config');
70 15
}
71
72
/**
73
 * Get portal modules
74
 *
75
 * @return array Portal modules array
76
 */
77
function obtain_portal_modules()
78
{
79 44
	global $db;
80
81 44
	$portal_modules = array();
82
83
	$sql = 'SELECT *
84 44
		FROM ' . PORTAL_MODULES_TABLE . '
85 44
		ORDER BY module_order ASC';
86 44
	$result = $db->sql_query($sql, 3600);
87
88 44
	while ($row = $db->sql_fetchrow($result))
89
	{
90 44
		$portal_modules[] = $row;
91 44
	}
92 44
	$db->sql_freeresult($result);
93 44
	return $portal_modules;
94
}
95
96
/**
97
* Fetch post for news & announce
98
*
99
* @deprecated 2.1.0-b1 (To be removed: 2.2.0)
100
*/
101
function phpbb_fetch_posts($module_id, $forum_from, $permissions, $number_of_posts, $text_length, $time, $type, $start = 0, $invert = false)
102
{
103
	global $phpbb_container;
104
105
	$fetch_posts = $phpbb_container->get('board3.portal.fetch_posts');
106
	$fetch_posts->set_module_id($module_id);
107
108
	return $fetch_posts->get_posts($forum_from, $permissions, $number_of_posts, $text_length, $time, $type, $start, $invert);
109
}
110
111
/**
112
* Censor title, return short title
113
*
114
* @param $title string title to censor
115
* @param $limit int short title character limit
116
*
117
*/
118
function character_limit(&$title, $limit = 0)
119
{
120 4
	$title = censor_text($title);
121 4
	if ($limit > 0)
122 4
	{
123 3
		return (strlen(utf8_decode($title)) > $limit + 3) ? truncate_string($title, $limit) . '...' : $title;
124
	}
125
	else
126
	{
127 1
		return $title;
128
	}
129
}
130
131
function ap_validate($str)
132
{
133 13
	$s = str_replace('<br />', '<br/>', $str);
134 13
	return str_replace('</li><br/>', '</li>', $s);
135
}
136
137
/**
138
* Pagination routine, generates archive number sequence
139
*/
140
function generate_portal_pagination($base_url, $num_items, $per_page, $start_item, $type, $module_id = 0, $add_prevnext_text = false, $tpl_prefix = '')
141
{
142
	global $template, $user;
143
144
	switch ($type)
145
	{
146
		case "announcements":
147
			$pagination_type = 'ap_' . $module_id;
148
			$anker = '#a_' . $module_id;
149
		break;
150
		case "news":
151
		case "news_all":
152
			$pagination_type = 'np_' . $module_id;
153
			$anker = '#n_' . $module_id;
154
		break;
155
156
		default:
157
			// this shouldn't happen but default to announcements
158
			$pagination_type = 'ap_' . $module_id;
159
			$anker = '#a_' . $module_id;
160
	}
161
162
	// Make sure $per_page is a valid value
163
	$per_page = ($per_page <= 0) ? 1 : $per_page;
164
165
	$seperator = '<li>&nbsp;</li>';
166
	$total_pages = ceil($num_items / $per_page);
167
168
	if ($total_pages == 1 || !$num_items)
169
	{
170
		return false;
171
	}
172
173
	$on_page = floor($start_item / $per_page) + 1;
174
	$url_delim = (strpos($base_url, '?') === false) ? '?' : '&amp;';
175
176
	$page_string = ($on_page == 1) ? '<ul><li class="active"><span>1</span></li>' : '<ul><li><a href="' . $base_url . $anker .'" role="button">1</a></li>';
177
178
	if ($total_pages > 5)
179
	{
180
		$start_cnt = min(max(1, $on_page - 4), $total_pages - 5);
181
		$end_cnt = max(min($total_pages, $on_page + 4), 6);
182
183
		// Add ... separator to pagination
184
		$page_string .= ($start_cnt > 1) ? '<li class="ellipsis" role="separator"><span>' . $user->lang['ELLIPSIS'] . '</span></li>' : $seperator;
185
186 View Code Duplication
		for ($i = $start_cnt + 1; $i < $end_cnt; ++$i)
187
		{
188
			$page_string .= ($i == $on_page) ? '<li class="active"><span>' . $i . '</span></li>' : '<li><a href="' . $base_url . "{$url_delim}" . $pagination_type . '=' . (($i - 1) * $per_page) . $anker . '" role="button">' . $i . '</a></li>';
189
			if ($i < $end_cnt - 1)
190
			{
191
				$page_string .= $seperator;
192
			}
193 1
		}
194
195
		// Add ... separator to pagination
196
		$page_string .= ($end_cnt < $total_pages) ? '<li class="ellipsis" role="separator"><span>' . $user->lang['ELLIPSIS'] . '</span></li>' : $seperator;
197
	}
198
	else
199
	{
200
		$page_string .= $seperator;
201
202 View Code Duplication
		for ($i = 2; $i < $total_pages; ++$i)
203
		{
204
			$page_string .= ($i == $on_page) ? '<li class="active"><span>' . $i . '</span></li>' : '<li><a href="' . $base_url . "{$url_delim}" . $pagination_type . '=' . (($i - 1) * $per_page) . $anker . '" role="button">' . $i . '</a></li>';
205
			if ($i < $total_pages)
206
			{
207
				$page_string .= $seperator;
208
			}
209
		}
210
	}
211
	$page_string .= ($on_page == $total_pages) ? '<li class="active"><span>' . $total_pages . '</span></li></ul>' : '<li><a href="' . $base_url . "{$url_delim}" . $pagination_type . '=' . (($total_pages - 1) * $per_page) . $anker . '" role="button">' . $total_pages . '</a></li></ul>';
212
213
	if ($add_prevnext_text)
214
	{
215 View Code Duplication
		if ($on_page != 1)
216
		{
217
			$page_string = '<a href="' . $base_url . "{$url_delim}" . $pagination_type . '=' . (($on_page - 2) * $per_page) . $anker . '" role="button">' . $user->lang['PREVIOUS'] . '</a>&nbsp;&nbsp;' . $page_string;
218
		}
219
220 View Code Duplication
		if ($on_page != $total_pages)
221
		{
222
			$page_string .= '&nbsp;&nbsp;<a href="' . $base_url . "{$url_delim}" . $pagination_type . '=' . ($on_page * $per_page) . $anker . '" role="button">' . $user->lang['NEXT'] . '</a>';
223
		}
224
	}
225
226
	$template->assign_vars(array(
227
		$tpl_prefix . 'BASE_URL'      => $base_url,
228
		'A_' . $tpl_prefix . 'BASE_URL'   => is_string($base_url) ? $base_url : '',
229
		$tpl_prefix . 'PER_PAGE'      => $per_page,
230
231
		$tpl_prefix . 'PREVIOUS_PAGE'   => ($on_page == 1) ? '' : $base_url . "{$url_delim}" . $pagination_type . '=' . (($on_page - 2) * $per_page) . $anker,
232
		$tpl_prefix . 'NEXT_PAGE'      => ($on_page == $total_pages) ? '' : $base_url . "{$url_delim}" . $pagination_type . '=' . ($on_page * $per_page) . $anker,
233
		$tpl_prefix . 'TOTAL_PAGES'      => $total_pages,
234
	));
235
236
	return $page_string;
237
}
238
239
/**
240
* get topic tracking info for news
241
* based on get_complete_tracking_info of phpBB3
242
* this should reduce the queries for the news and announcements block
243
*/
244
function get_portal_tracking_info($fetch_news)
245
{
246
	global $config, $request, $user;
247
248
	$last_read = $topic_ids = $forum_ids = $tracking_info = $rev_forum_ids = $user_lastmark = array();
249
250
	/**
251
	* group everything by the forum IDs
252
	*/
253
	$count = $fetch_news['topic_count'];
254
	for ($i = 0; $i < $count; ++$i)
255
	{
256
		$tracking_info[$fetch_news[$i]['forum_id']][] = $fetch_news[$i]['topic_id'];
257
		$topic_ids[] = $fetch_news[$i]['topic_id'];
258
		$forum_ids[] = $fetch_news[$i]['forum_id'];
259
		$rev_forum_ids[$fetch_news[$i]['topic_id']] = $fetch_news[$i]['forum_id']; // the other way round also helps
260
	}
261
262
	foreach ($tracking_info as $forum_id => $current_forum)
263
	{
264
		if ($config['load_db_lastread'] && $user->data['is_registered'])
265
		{
266
			global $db;
267
268
			$mark_time = array();
269
270
			$sql = 'SELECT topic_id, mark_time
271
				FROM ' . TOPICS_TRACK_TABLE . '
272
				WHERE user_id =  ' . (int) $user->data['user_id'] . '
273
					AND ' . $db->sql_in_set('topic_id', $current_forum);
274
			$result = $db->sql_query($sql);
275
276
			while ($row = $db->sql_fetchrow($result))
277
			{
278
				$last_read[$row['topic_id']] = $row['mark_time'];
279
			}
280
			$db->sql_freeresult($result);
281
282
			$current_forum = array_diff($current_forum, array_keys($last_read));
283
284
			if (sizeof($topic_ids))
285
			{
286
				$sql = 'SELECT forum_id, mark_time
287
					FROM ' . FORUMS_TRACK_TABLE . '
288
					WHERE user_id =  ' . (int) $user->data['user_id'] . '
289
						AND ' . $db->sql_in_set('forum_id', $forum_ids);
290
				$result = $db->sql_query($sql);
291
292
				while ($row = $db->sql_fetchrow($result))
293
				{
294
					$mark_time[$row['forum_id']] = $row['mark_time'];
295
				}
296
				$db->sql_freeresult($result);
297
298
				// Set user last mark time
299
				foreach ($forum_ids as $current_forum_id)
300
				{
301
					$user_lastmark[$current_forum_id] = (isset($mark_time[$current_forum_id])) ? $mark_time[$current_forum_id] : $user->data['user_lastmark'];
302
				}
303
304
				// @todo: also check if $user_lastmark has been defined for this specific forum_id
305
				foreach ($topic_ids as $topic_id)
306
				{
307
					if (!isset($last_read[$topic_id]) || (isset($user_lastmark[$rev_forum_ids[$topic_id]]) && $user_lastmark[$rev_forum_ids[$topic_id]] > $last_read[$topic_id]))
308
					{
309
						$last_read[$topic_id] =  $user_lastmark[$rev_forum_ids[$topic_id]];
310
					}
311
				}
312
			}
313
		}
314
		else if ($config['load_anon_lastread'] || $user->data['is_registered'])
315
		{
316
			global $tracking_topics;
317
318
			if (!isset($tracking_topics) || !sizeof($tracking_topics))
319
			{
320
				if ($request->is_set($config['cookie_name'] . '_track', \phpbb\request\request_interface::COOKIE))
321
				{
322
					$tracking_topics = $request->variable($config['cookie_name'] . '_track', '', true, \phpbb\request\request_interface::COOKIE);
323
				}
324
				else
325
				{
326
					$tracking_topics = '';
327
				}
328
				$tracking_topics = ($tracking_topics) ? tracking_unserialize($tracking_topics) : array();
329
			}
330
331
			if (!$user->data['is_registered'])
332
			{
333
				$user_lastmark = (isset($tracking_topics['l'])) ? base_convert($tracking_topics['l'], 36, 10) + $config['board_startdate'] : 0;
334
			}
335
			else
336
			{
337
				$user_lastmark = $user->data['user_lastmark'];
338
			}
339
340
			foreach ($topic_ids as $topic_id)
341
			{
342
				$topic_id36 = base_convert($topic_id, 10, 36);
343
344
				if (isset($tracking_topics['t'][$topic_id36]))
345
				{
346
					$last_read[$topic_id] = base_convert($tracking_topics['t'][$topic_id36], 36, 10) + $config['board_startdate'];
347
				}
348
			}
349
350
			$topic_ids = array_diff($topic_ids, array_keys($last_read));
351
352
			if (sizeof($topic_ids))
353
			{
354
				$mark_time = array();
355
356
				if (isset($tracking_topics['f'][$forum_id]))
357
				{
358
					$mark_time[$forum_id] = base_convert($tracking_topics['f'][$forum_id], 36, 10) + $config['board_startdate'];
359
				}
360
361
				$user_lastmark = (isset($mark_time[$forum_id])) ? $mark_time[$forum_id] : $user_lastmark;
362
363
				foreach ($topic_ids as $topic_id)
364
				{
365
					$last_read[$topic_id] = $user_lastmark;
366
				}
367
			}
368
		}
369
	}
370
371
	return $last_read;
372
}
373
374
/**
375
* Check if the entered source file actually exists
376
*
377
* @param string	$value		Filename of file to check
378
* @param string	$key		Key of the acp setting (unused here)
379
* @param int	$module_id	Module ID of this module
380
* @param bool	$force_error	Whether an error message should be triggered on
381
*				errors.
382
* @return bool|string False if file exists, an error string if file doesn't exist.
383
*/
384
function check_file_src($value, $key, $module_id, $force_error = true)
385
{
386 2
	global $phpbb_admin_path, $portal_root_path, $phpEx, $user;
387
388 2
	$error = '';
389
390
	// We check if the chosen file is present in the styles/all/ folder
391 2
	if (!file_exists($portal_root_path . 'styles/all/theme/images/portal/' . $value))
392 2
	{
393 2
		$error .= $user->lang['B3P_FILE_NOT_FOUND'] . ': styles/all/theme/images/portal/' . $value . '<br />';
394 2
	}
395
396 2
	if (!empty($error))
397 2
	{
398
		if ($force_error)
399 2
		{
400 1
			trigger_error($error . adm_back_link(append_sid("{$phpbb_admin_path}index.$phpEx", 'i=\board3\portal\acp\portal_module&amp;mode=config&amp;module_id=' . $module_id)), E_USER_WARNING);
401
		}
402
403 1
		return $error;
404
	}
405
	else
406
	{
407 1
		return false;
408
	}
409
}
410
411
/**
412
* Get the groups a user is in
413
*
414
* @return array Array containing the user's groups
415
*/
416
function get_user_groups()
417
{
418 3
	global $cache, $db, $user;
419
420 3
	$groups_ary = $cache->get('_user_groups_' . $user->data['user_id']);
421
422 3
	if ($groups_ary === false)
423 3
	{
424 3
		$groups_ary = array();
425
426
		// get user's groups
427
		$sql = 'SELECT group_id
428 3
				FROM ' . USER_GROUP_TABLE . '
429 3
				WHERE user_id = ' . (int) $user->data['user_id'] . '
430 3
				ORDER BY group_id ASC';
431 3
		$result = $db->sql_query($sql);
432 3
		while ($row = $db->sql_fetchrow($result))
433
		{
434 2
			$groups_ary[] = $row['group_id'];
435 2
		}
436 3
		$db->sql_freeresult($result);
437
438
		// save data in cache for 60 seconds
439 3
		$cache->put('_user_groups_' . $user->data['user_id'], $groups_ary, 60);
440 3
	}
441
442 3
	return $groups_ary;
443
}
444