template_show_list()   F
last analyzed

Complexity

Conditions 56
Paths > 20000

Size

Total Lines 215
Code Lines 87

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 56
eloc 87
c 0
b 0
f 0
nc 345600
nop 1
dl 0
loc 215
rs 0

How to fix   Long Method    Complexity   

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:

1
<?php
2
3
/**
4
 * @package   ElkArte Forum
5
 * @copyright ElkArte Forum contributors
6
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
7
 *
8
 * This file contains code covered by:
9
 * copyright: 2011 Simple Machines (http://www.simplemachines.org)
10
 *
11
 * @version 2.0 Beta 1
12
 *
13
 */
14
15
/**
16
 * Template used to show a list created with createlist
17
 *
18
 * @param string|null $list_id
19
 */
20
function template_show_list($list_id = null)
21
{
22
	global $context, $txt;
23
24
	// Get a shortcut to the current list.
25
	$list_id = $list_id ?? $context['default_list'];
26
	$cur_list = &$context[$list_id];
27
28
	if (isset($cur_list['form']))
29
	{
30
		echo '
31
	<form class="generic_list_wrapper" action="', $cur_list['form']['href'], '" method="post"', empty($cur_list['form']['name']) ? '' : ' name="' . $cur_list['form']['name'] . '" id="' . $cur_list['form']['name'] . '"', ' accept-charset="UTF-8">
32
		<div class="generic_list">';
33
	}
34
	else
35
	{
36
		echo '
37
		<div id="wrapper_', $list_id, '" class="generic_list_wrapper">';
38
	}
39
40
	// Show the title of the table (if any), with an icon (if defined)
41
	if (!empty($cur_list['title']))
42
	{
43
		echo '
44
			<h2 class="category_header', empty($cur_list['icon']) ? '' : ' hdicon ' . $cur_list['icon'], '">', $cur_list['title'], '</h2>';
45
	}
46
47
	// Show any data right after the title
48
	if (isset($cur_list['additional_rows']['after_title']))
49
	{
50
		echo '
51
			<div class="information flow_hidden">';
52
53
		template_additional_rows('after_title', $cur_list);
0 ignored issues
show
Bug introduced by
'after_title' of type string is incompatible with the type integer expected by parameter $row_position of template_additional_rows(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
		template_additional_rows(/** @scrutinizer ignore-type */ 'after_title', $cur_list);
Loading history...
54
55
		echo '
56
			</div>';
57
	}
58
59
	// Show some data above this list
60
	if (isset($cur_list['additional_rows']['top_of_list']))
61
	{
62
		template_additional_rows('top_of_list', $cur_list);
63
	}
64
65
	$close_div = false;
66
	if (isset($cur_list['additional_rows']['above_column_headers']))
67
	{
68
		$close_div = true;
69
		echo '
70
			<div class="flow_flex">', template_additional_rows('above_column_headers', $cur_list);
0 ignored issues
show
Bug introduced by
Are you sure the usage of template_additional_rows...mn_headers', $cur_list) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
Are you sure template_additional_rows...mn_headers', $cur_list) of type void can be used in echo? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
			<div class="flow_flex">', /** @scrutinizer ignore-type */ template_additional_rows('above_column_headers', $cur_list);
Loading history...
71
	}
72
73
	// These are the main tabs that is used all around the template.
74
	if (isset($cur_list['list_menu']['show_on']) && ($cur_list['list_menu']['show_on'] === 'both' || $cur_list['list_menu']['show_on'] === 'top'))
75
	{
76
		if (!$close_div)
77
		{
78
			echo '
79
			<div class="flow_flex">';
80
		}
81
82
		$close_div = true;
83
84
		template_create_list_menu($cur_list['list_menu']);
85
86
		// Menu buttons and additional buttons are on the same line, leaving no room for the index
87
		if (isset($cur_list['additional_rows']['above_column_headers']))
88
		{
89
			$page_index_new_line = true;
90
		}
91
	}
92
93
	// Show the page index (if this list doesn't intend to show all items).
94
	if (!empty($cur_list['items_per_page']) && !empty($cur_list['page_index']))
95
	{
96
		if (!$close_div || isset($page_index_new_line))
97
		{
98
			echo (isset($page_index_new_line) ? '
99
			</div>' : '') . '
100
			<div class="flow_flex">';
101
		}
102
103
		template_pagesection(false, '', ['page_index_markup' => $cur_list['page_index']]);
104
		$close_div = true;
105
	}
106
107
	if ($close_div)
108
	{
109
		echo '
110
			</div>';
111
	}
112
113
	// Start of the main table
114
	echo '
115
			<table id="' . $list_id . '" class="table_grid"', empty($cur_list['width']) ? '' : ' style="width: ' . $cur_list['width'] . '"', '>';
116
117
	// Show the column headers.
118
	$header_count = count($cur_list['headers']);
119
	if (!($header_count < 2 && empty($cur_list['headers'][0]['label'])))
120
	{
121
		echo '
122
			<thead>
123
				<tr class="table_head">';
124
125
		// Loop through each column and add a table header.
126
		$i = 0;
127
		foreach ($cur_list['headers'] as $col_header)
128
		{
129
			$i++;
130
			if ($i === 1)
131
			{
132
				$col_header['class'] = empty($col_header['class']) ? '' : $col_header['class'];
133
			}
134
			elseif ($i === $header_count)
135
			{
136
				$col_header['class'] = empty($col_header['class']) ? '' : $col_header['class'];
137
			}
138
139
			$sort_title = $col_header['sort_image'] === 'up' ? $txt['sort_desc'] : $txt['sort_asc'];
140
141
			echo '
142
					<th scope="col" id="header_', $list_id, '_', $col_header['id'], '"', empty($col_header['class']) ? '' : ' class="' . $col_header['class'] . '"', empty($col_header['style']) ? '' : ' style="' . $col_header['style'] . '"', empty($col_header['colspan']) ? '' : ' colspan="' . $col_header['colspan'] . '"', '>', empty($col_header['href']) ? '' : '<a href="' . $col_header['href'] . '" rel="nofollow">', empty($col_header['label']) ? '&nbsp;' : $col_header['label'], empty($col_header['href']) ? '' : (empty($col_header['sort_image']) ? '</a>' : ' <i class="sort icon i-sort-amount-' . $col_header['sort_image'] . '" title="' . $sort_title . '"></i></a>'), '</th>';
143
		}
144
145
		echo '
146
				</tr>
147
			</thead>';
148
	}
149
150
	echo '
151
			<tbody', empty($cur_list['sortable']) ? '' : ' id="table_grid_sortable"', '>';
152
153
	// Show a nice message informing there are no items in this list.
154
	if (empty($cur_list['rows']) && !empty($cur_list['no_items_label']))
155
	{
156
		echo '
157
				<tr>
158
					<td colspan="', $cur_list['num_columns'], '">
159
						<div class="', empty($cur_list['no_items_align']) ? 'centertext' : $cur_list['no_items_align'], '">', $cur_list['no_items_label'], '</div>
160
					</td>
161
				</tr>';
162
	}
163
164
	// Show the list rows.
165
	elseif (!empty($cur_list['rows']))
166
	{
167
		foreach ($cur_list['rows'] as $id => $row)
168
		{
169
			echo '
170
				<tr class="standard_row ', $row['class'], '" id="list_', $list_id, '_', str_replace(' ', '_', $id), '">';
171
172
			foreach ($row['data'] as $row_data)
173
			{
174
				echo '
175
					<td', empty($row_data['class']) ? '' : ' class="' . $row_data['class'] . '"', empty($row_data['style']) ? '' : ' style="' . $row_data['style'] . '"', '>', $row_data['value'], '</td>';
176
			}
177
178
			echo '
179
				</tr>';
180
		}
181
	}
182
183
	echo '
184
			</tbody>
185
			</table>';
186
187
	echo '
188
			<div class="flow_flex">';
189
190
	// Do we have multiple pages to show or data to show below the table
191
	if ((!empty($cur_list['items_per_page']) && !empty($cur_list['page_index'])) || isset($cur_list['additional_rows']['below_table_data']))
192
	{
193
		// Show the page index (if this list doesn't intend to show all items).
194
		if (!empty($cur_list['items_per_page']) && !empty($cur_list['page_index']))
195
		{
196
			template_pagesection(false, '', ['page_index_markup' => $cur_list['page_index']]);
197
		}
198
199
		if (isset($cur_list['additional_rows']['below_table_data']))
200
		{
201
			template_additional_rows('below_table_data', $cur_list);
202
		}
203
	}
204
205
	// Tabs at the bottom.  Usually bottom aligned.
206
	if (isset($cur_list['list_menu']['show_on']) && ($cur_list['list_menu']['show_on'] === 'both' || $cur_list['list_menu']['show_on'] === 'bottom'))
207
	{
208
		template_create_list_menu($cur_list['list_menu']);
209
	}
210
211
	echo '
212
			</div>';
213
214
	// Last chance to show more data, like buttons and links
215
	if (isset($cur_list['additional_rows']['bottom_of_list']))
216
	{
217
		template_additional_rows('bottom_of_list', $cur_list);
218
	}
219
220
	if (isset($cur_list['form']))
221
	{
222
		foreach ($cur_list['form']['hidden_fields'] as $name => $value)
223
		{
224
			echo '
225
			<input type="hidden" name="', $name, '" value="', $value, '" />';
226
		}
227
228
		echo '
229
		</div>
230
	</form>';
231
	}
232
	else
233
	{
234
		echo '
235
		</div>';
236
	}
237
}
238
239
/**
240
 * Generic template used to show additional rows of data (above/below)
241
 *
242
 * @param int $row_position
243
 * @param array $cur_list
244
 *
245
 * @return void an echoed output
246
 */
247
function template_additional_rows($row_position, $cur_list)
248
{
249
	foreach ($cur_list['additional_rows'][$row_position] as $row)
250
	{
251
		echo '
252
				<div class="additional_row ', $row_position, empty($row['class']) ? '' : ' ' . $row['class'], '"', empty($row['style']) ? '' : ' style="' . $row['style'] . '"', '>', $row['value'], '</div>';
253
	}
254
}
255
256
/**
257
 * Used this if you want your generic lists to have navigation menus.
258
 *
259
 * $cur_list['list_menu'] = array(
260
 *    'style' => '',
261
 *    'class' => '',
262
 *    // Links.  This is the core of the array.  It has all the info that we need.
263
 *    'links' => array(
264
 *      'name' => array(
265
 *        // This will tell use were to go when they click it.
266
 *        'href' => $scripturl . '?action=theaction',
267
 *        // The name that you want to appear for the link.
268
 *        'label' => $txt['name'],
269
 *        // If we use tabs instead of buttons we highlight the current tab.
270
 *        // Must use conditions to determine if it's selected or not.
271
 *        'is_selected' => isset($_REQUEST['name']),
272
 *      ),
273
 *    ),
274
 * );
275
 *
276
 * @param array $list_menu
277
 */
278
function template_create_list_menu($list_menu)
279
{
280
	echo '
281
		<ul class="generic_menu', empty($list_menu['class']) ? '' : ' ' . $list_menu['class'], '"', empty($list_menu['style']) ? '' : ' style="' . $list_menu['style'] . '"', '>';
282
283
	foreach ($list_menu['links'] as $link)
284
	{
285
		echo '
286
			<li class="listlevel1">
287
				<a class="linklevel1', $link['is_selected'] ? ' active' : '', '" href="', empty($link['href']) ? '#' : $link['href'], '">',
288
				$link['label'], empty($link['href']) ? '' : '</a>', '
289
			</li>';
290
	}
291
292
	echo '
293
		</ul>';
294
}
295