Passed
Pull Request — release-2.1 (#6262)
by Jeremy
04:04
created

template_create_list_menu()   D

Complexity

Conditions 24
Paths 24

Size

Total Lines 94
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 24
eloc 39
c 0
b 0
f 0
nop 2
dl 0
loc 94
rs 4.1666
nc 24

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
 * Simple Machines Forum (SMF)
4
 *
5
 * @package SMF
6
 * @author Simple Machines https://www.simplemachines.org
7
 * @copyright 2020 Simple Machines and individual contributors
8
 * @license https://www.simplemachines.org/about/smf/license.php BSD
9
 *
10
 * @version 2.1 RC3
11
 */
12
13
/**
14
 * This template handles displaying a list
15
 *
16
 * @param string $list_id The list ID. If null, uses $context['default_list'].
17
 */
18
function template_show_list($list_id = null)
19
{
20
	global $context;
21
22
	// Get a shortcut to the current list.
23
	$list_id = $list_id === null ? (!empty($context['default_list']) ? $context['default_list'] : '') : $list_id;
24
25
	if (empty($list_id) || empty($context[$list_id]))
26
		return;
27
28
	$cur_list = &$context[$list_id];
29
30
	// These are the main tabs that is used all around the template.
31
	if (isset($cur_list['list_menu'], $cur_list['list_menu']['show_on']) && ($cur_list['list_menu']['show_on'] == 'both' || $cur_list['list_menu']['show_on'] == 'top'))
32
		template_create_list_menu($cur_list['list_menu'], 'top');
33
34
	if (isset($cur_list['form']))
35
		echo '
36
	<form action="', $cur_list['form']['href'], '" method="post"', empty($cur_list['form']['name']) ? '' : ' name="' . $cur_list['form']['name'] . '" id="' . $cur_list['form']['name'] . '"', ' accept-charset="', $context['character_set'], '">';
37
38
	// Show the title of the table (if any).
39
	if (!empty($cur_list['title']))
40
		echo '
41
		<div class="cat_bar">
42
			<h3 class="catbg">
43
				', $cur_list['title'], '
44
			</h3>
45
		</div>';
46
47
	if (isset($cur_list['additional_rows']['after_title']))
48
	{
49
		echo '
50
		<div class="information flow_hidden">';
51
52
		template_additional_rows('after_title', $cur_list);
53
54
		echo '
55
		</div>';
56
	}
57
58
	if (isset($cur_list['additional_rows']['top_of_list']))
59
		template_additional_rows('top_of_list', $cur_list);
60
61
	if ((!empty($cur_list['items_per_page']) && !empty($cur_list['page_index'])) || isset($cur_list['additional_rows']['above_column_headers']))
62
	{
63
		// Show the page index (if this list doesn't intend to show all items).
64
		if (!empty($cur_list['items_per_page']) && !empty($cur_list['page_index']))
65
			echo '
66
		<div class="floatleft">
67
			<div class="pagesection">', $cur_list['page_index'], '</div>
68
		</div>';
69
70
		if (isset($cur_list['additional_rows']['above_column_headers']))
71
			template_additional_rows('above_column_headers', $cur_list);
72
	}
73
74
	echo '
75
		<table class="table_grid" ', !empty($list_id) ? 'id="' . $list_id . '"' : '', ' ', !empty($cur_list['width']) ? ' style="width:' . $cur_list['width'] . '"' : '', '>';
76
77
	// Show the column headers.
78
	$header_count = count($cur_list['headers']);
79
	if (!($header_count < 2 && empty($cur_list['headers'][0]['label'])))
80
	{
81
		echo '
82
			<thead>
83
				<tr class="title_bar">';
84
85
		// Loop through each column and add a table header.
86
		foreach ($cur_list['headers'] as $col_header)
87
			echo '
88
					<th scope="col" id="header_', $list_id, '_', $col_header['id'], '" class="', $col_header['id'], empty($col_header['class']) ? '' : ' ' . $col_header['class'], '"', empty($col_header['style']) ? '' : ' style="' . $col_header['style'] . '"', empty($col_header['colspan']) ? '' : ' colspan="' . $col_header['colspan'] . '"', '>
89
						', empty($col_header['href']) ? '' : '<a href="' . $col_header['href'] . '" rel="nofollow">', empty($col_header['label']) ? '' : $col_header['label'], empty($col_header['href']) ? '' : (empty($col_header['sort_image']) ? '</a>' : ' <span class="main_icons sort_' . $col_header['sort_image'] . '"></span></a>'), '
90
					</th>';
91
92
		echo '
93
				</tr>
94
			</thead>';
95
	}
96
97
	echo '
98
			<tbody>';
99
100
	// Show a nice message informing there are no items in this list.
101
	if (empty($cur_list['rows']) && !empty($cur_list['no_items_label']))
102
		echo '
103
				<tr class="windowbg">
104
					<td colspan="', $cur_list['num_columns'], '" class="', !empty($cur_list['no_items_align']) ? $cur_list['no_items_align'] : 'centertext', '">
105
						', $cur_list['no_items_label'], '
106
					</td>
107
				</tr>';
108
109
	// Show the list rows.
110
	elseif (!empty($cur_list['rows']))
111
	{
112
		foreach ($cur_list['rows'] as $id => $row)
113
		{
114
			echo '
115
				<tr class="windowbg', empty($row['class']) ? '' : ' ' . $row['class'], '"', empty($row['style']) ? '' : ' style="' . $row['style'] . '"', ' id="list_', $list_id, '_', $id, '">';
116
117
			if (!empty($row['data']))
118
				foreach ($row['data'] as $row_id => $row_data)
119
					echo '
120
					<td class="', $row_id, empty($row_data['class']) ? '' : ' ' . $row_data['class'] . '', '"', empty($row_data['style']) ? '' : ' style="' . $row_data['style'] . '"', '>
121
						', $row_data['value'], '
122
					</td>';
123
124
			echo '
125
				</tr>';
126
		}
127
	}
128
129
	echo '
130
			</tbody>
131
		</table>';
132
133
	if ((!empty($cur_list['items_per_page']) && !empty($cur_list['page_index'])) || isset($cur_list['additional_rows']['below_table_data']))
134
	{
135
		echo '
136
		<div class="flow_auto">';
137
138
		// Show the page index (if this list doesn't intend to show all items).
139
		if (!empty($cur_list['items_per_page']) && !empty($cur_list['page_index']))
140
			echo '
141
			<div class="floatleft">
142
				<div class="pagesection">', $cur_list['page_index'], '</div>
143
			</div>';
144
145
		if (isset($cur_list['additional_rows']['below_table_data']))
146
			template_additional_rows('below_table_data', $cur_list);
147
148
		echo '
149
		</div>';
150
	}
151
152
	if (isset($cur_list['additional_rows']['bottom_of_list']))
153
		template_additional_rows('bottom_of_list', $cur_list);
154
155
	if (isset($cur_list['form']))
156
	{
157
		foreach ($cur_list['form']['hidden_fields'] as $name => $value)
158
			echo '
159
		<input type="hidden" name="', $name, '" value="', $value, '">';
160
161
		if (isset($cur_list['form']['token']))
162
			echo '
163
		<input type="hidden" name="', $context[$cur_list['form']['token'] . '_token_var'], '" value="', $context[$cur_list['form']['token'] . '_token'], '">';
164
165
		echo '
166
	</form>';
167
	}
168
169
	// Tabs at the bottom.  Usually bottom aligned.
170
	if (isset($cur_list['list_menu'], $cur_list['list_menu']['show_on']) && ($cur_list['list_menu']['show_on'] == 'both' || $cur_list['list_menu']['show_on'] == 'bottom'))
171
		template_create_list_menu($cur_list['list_menu'], 'bottom');
172
173
	if (isset($cur_list['javascript']))
174
		echo '
175
	<script>
176
		', $cur_list['javascript'], '
177
	</script>';
178
}
179
180
/**
181
 * This template displays additional rows above or below the list.
182
 *
183
 * @param string $row_position The position ('top', 'bottom', etc.)
184
 * @param array $cur_list An array with the data for the current list
185
 */
186
function template_additional_rows($row_position, $cur_list)
187
{
188
	foreach ($cur_list['additional_rows'][$row_position] as $row)
189
		echo '
190
			<div class="additional_row', empty($row['class']) ? '' : ' ' . $row['class'], '"', empty($row['style']) ? '' : ' style="' . $row['style'] . '"', '>
191
				', $row['value'], '
192
			</div>';
193
}
194
195
/**
196
 * This function creates a menu
197
 *
198
 * @param array $list_menu An array of menu data
199
 * @param string $direction Which direction the items should go
200
 */
201
function template_create_list_menu($list_menu, $direction = 'top')
202
{
203
	global $context;
204
205
	/**
206
		// This is used if you want your generic lists to have tabs.
207
		$cur_list['list_menu'] = array(
208
			// This is the style to use.  Tabs or Buttons (Text 1 | Text 2).
209
			// By default tabs are selected if not set.
210
			// The main difference between tabs and buttons is that tabs get highlighted if selected.
211
			// If style is set to buttons and use tabs is disabled then we change the style to old styled tabs.
212
			'style' => 'tabs',
213
			// The position of the tabs/buttons.  Left or Right.  By default is set to left.
214
			'position' => 'left',
215
			// This is used by the old styled menu.  We *need* to know the total number of columns to span.
216
			'columns' => 0,
217
			// This gives you the option to show tabs only at the top, bottom or both.
218
			// By default they are just shown at the top.
219
			'show_on' => 'top',
220
			// Links.  This is the core of the array.  It has all the info that we need.
221
			'links' => array(
222
				'name' => array(
223
					// This will tell use were to go when they click it.
224
					'href' => $scripturl . '?action=theaction',
225
					// The name that you want to appear for the link.
226
					'label' => $txt['name'],
227
					// If we use tabs instead of buttons we highlight the current tab.
228
					// Must use conditions to determine if its selected or not.
229
					'is_selected' => isset($_REQUEST['name']),
230
				),
231
			),
232
		);
233
	*/
234
235
	// Are we using right-to-left orientation?
236
	$first = $context['right_to_left'] ? 'last' : 'first';
237
	$last = $context['right_to_left'] ? 'first' : 'last';
238
239
	if (!isset($list_menu['style']) || isset($list_menu['style']) && $list_menu['style'] == 'tabs')
240
	{
241
		echo '
242
		<table style="margin-', $list_menu['position'], ': 10px; width: 100%;">
243
			<tr>', $list_menu['position'] == 'right' ? '
244
				<td></td>' : '', '
245
				<td class="', $list_menu['position'], 'text">
246
					<table>
247
						<tr>
248
							<td class="', $direction == 'top' ? 'mirror' : 'main', 'tab_', $first, '"></td>';
249
250
		foreach ($list_menu['links'] as $link)
251
		{
252
			if ($link['is_selected'])
253
				echo '
254
							<td class="', $direction == 'top' ? 'mirror' : 'main', 'tab_active_', $first, '"></td>
255
							<td class="', $direction == 'top' ? 'mirrortab' : 'maintab', '_active_back">
256
								<a href="', $link['href'], '">', $link['label'], '</a>
257
							</td>
258
							<td class="', $direction == 'top' ? 'mirror' : 'main', 'tab_active_', $last, '"></td>';
259
			else
260
				echo '
261
							<td class="', $direction == 'top' ? 'mirror' : 'main', 'tab_back">
262
								<a href="', $link['href'], '">', $link['label'], '</a>
263
							</td>';
264
		}
265
266
		echo '
267
							<td class="', $direction == 'top' ? 'mirror' : 'main', 'tab_', $last, '"></td>
268
						</tr>
269
					</table>
270
				</td>', $list_menu['position'] == 'left' ? '
271
				<td></td>' : '', '
272
			</tr>
273
		</table>';
274
	}
275
	elseif (isset($list_menu['style']) && $list_menu['style'] == 'buttons')
276
	{
277
		$links = array();
278
		foreach ($list_menu['links'] as $link)
279
			$links[] = '<a href="' . $link['href'] . '">' . $link['label'] . '</a>';
280
281
		echo '
282
		<table style="margin-', $list_menu['position'], ': 10px; width: 100%;">
283
			<tr>', $list_menu['position'] == 'right' ? '
284
				<td></td>' : '', '
285
				<td class="', $list_menu['position'], 'text">
286
					<table>
287
						<tr>
288
							<td class="', $direction == 'top' ? 'mirror' : 'main', 'tab_', $first, '"></td>
289
							<td class="', $direction == 'top' ? 'mirror' : 'main', 'tab_back">', implode(' &nbsp;|&nbsp; ', $links), '</td>
290
							<td class="', $direction == 'top' ? 'mirror' : 'main', 'tab_', $last, '"></td>
291
						</tr>
292
					</table>
293
				</td>', $list_menu['position'] == 'left' ? '
294
				<td></td>' : '', '
295
			</tr>
296
		</table>';
297
	}
298
}
299
300
?>