template_results()   F
last analyzed

Complexity

Conditions 39
Paths > 20000

Size

Total Lines 273
Code Lines 112

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 39
eloc 112
nc 1153600
nop 0
dl 0
loc 273
rs 0
c 0
b 0
f 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 dev
12
 *
13
 */
14
15
/**
16
 * Lotsa boards we (may) have here!
17
 */
18
function template_Search_init()
19
{
20
	theme()->getTemplates()->load('GenericBoards');
21
}
22
23
/**
24
 * Main search page. Allows the user to search the forum according to criteria.
25
 */
26
function template_searchform()
27
{
28
	global $context, $settings, $txt, $scripturl, $modSettings;
29
30
	echo '
31
				<form id="searchform" action="', $scripturl, '?action=search;sa=results" method="post" accept-charset="UTF-8" name="searchform">
32
					<h2 class="category_header', empty($settings['use_buttons']) ? '' : ' hdicon i-search', '">
33
						', $txt['set_parameters'], '
34
					</h2>';
35
36
	// Any search errors to inform the user about
37
	if (!empty($context['search_errors']))
38
	{
39
		echo '
40
					<p class="errorbox">', implode('<br />', $context['search_errors']['messages']), '</p>';
41
	}
42
43
	// Start off showing our basic search form
44
	echo '
45
					<fieldset id="simple_search" class="content">
46
						<div id="search_term_input">
47
							<label for="search">
48
								<strong>', $txt['search_for'], '</strong>
49
							</label>:
50
							<input type="search" id="search" class="input_text" name="search" value="', $context['search_params']['search'], '" maxlength="', $context['search_string_limit'], '" size="40" placeholder="' . $txt['search'] . '" required="required" autofocus="autofocus" />', '
51
							<input id="submit" type="submit" name="s_search" value="' . $txt['search'] . '" />
52
						</div>';
53
54
	if (empty($modSettings['search_simple_fulltext']))
55
	{
56
		echo '
57
						<p class="smalltext">', $txt['search_example'], '</p>';
58
	}
59
60
	// If $context['search_params']['topic'] is set, that means we're searching just one topic.
61
	if (!empty($context['search_params']['topic']))
62
	{
63
		echo '
64
						<span class="alert">
65
							&#10148;', $txt['search_specific_topic'], ' &quot;', $context['search_topic']['link'], '&quot;.
66
						</span>
67
						<input type="hidden" name="topic" value="', $context['search_topic']['id'], '" />';
68
	}
69
70
	// Does the search require a visual verification screen to annoy them?
71
	if (!empty($context['require_verification']))
72
	{
73
		template_verification_controls($context['visual_verification_id'], '
74
						<div class="verification">
75
							<strong>' . $txt['search_visual_verification_label'] . ':</strong>
76
							<br />', '
77
						</div>');
78
	}
79
80
	// All the advanced options, this div is collapsed by JS when available
81
	echo '
82
						<div id="advanced_search">
83
							<dl id="search_options">
84
								<dt class="righttext">
85
									<label for="searchtype">', $txt['search_match'], ':</label>
86
								</dt>
87
								<dd>
88
									<select name="searchtype" id="searchtype">
89
										<option value="1"', empty($context['search_params']['searchtype']) ? ' selected="selected"' : '', '>', $txt['all_words'], '</option>
90
										<option value="2"', empty($context['search_params']['searchtype']) ? '' : ' selected="selected"', '>', $txt['any_words'], '</option>
91
									</select>
92
								</dd>
93
								<dt class="righttext">
94
									<label for="userspec">', $txt['by_user'], ':</label>
95
								</dt>
96
								<dd>
97
									<input id="userspec" type="text" name="userspec" value="', $context['search_params']['userspec'], '" size="40" class="input_text" />
98
								</dd>
99
								<dt class="righttext">
100
									<label for="sort">', $txt['search_order'], ':</label>
101
								</dt>
102
								<dd>
103
									<select id="sort" name="sort">
104
										<option value="relevance|desc">', $txt['search_orderby_relevant_first'], '</option>
105
										<option value="num_replies|desc">', $txt['search_orderby_large_first'], '</option>
106
										<option value="num_replies|asc">', $txt['search_orderby_small_first'], '</option>
107
										<option value="id_msg|desc">', $txt['search_orderby_recent_first'], '</option>
108
										<option value="id_msg|asc">', $txt['search_orderby_old_first'], '</option>
109
									</select>
110
								</dd>
111
								<dt class="righttext options">
112
									', $txt['search_options'], ':
113
								</dt>
114
								<dd class="options">
115
									<input type="checkbox" name="show_complete" id="show_complete" value="1"', empty($context['search_params']['show_complete']) ? '' : ' checked="checked"', ' />
116
									<label for="show_complete">', $txt['search_show_complete_messages'], '</label><br />
117
									<input type="checkbox" name="subject_only" id="subject_only" value="1"', empty($context['search_params']['subject_only']) ? '' : ' checked="checked"', ' />
118
									<label for="subject_only">', $txt['search_subject_only'], '</label>
119
								</dd>
120
								<dt class="righttext between">
121
									', $txt['date'], ' ', $txt['search_between'], ': 
122
								</dt>
123
								<dd>
124
									<label for="minage" hidden>', $txt['search_between'], '</label>&nbsp;
125
									<input type="date" name="minage" id="minage" value="', $context['search_params']['minage'], '" class="input_text" />&nbsp;
126
									<label for="maxage">', $txt['search_and'], '&nbsp;</label>
127
									<input type="date" name="maxage" id="maxage" value="', $context['search_params']['maxage'], '" class="input_text" />
128
								</dd>
129
							</dl>
130
						</div>
131
						<a id="upshrink_link" href="', $scripturl, '?action=search;advanced" class="linkbutton hide">', $txt['search_simple'], '</a>';
132
133
	// Set the initial search style for the form
134
	echo '
135
						<input id="advanced" type="hidden" name="advanced" value="1" />';
136
137
	echo '
138
					</fieldset>';
139
140
	// This starts our selection area to allow searching by specific boards
141
	if (empty($context['search_params']['topic']))
142
	{
143
		echo '
144
					<fieldset id="pick_boards" class="content">';
145
146
		template_pick_boards('searchform');
147
148
		echo '
149
					</fieldset>';
150
	}
151
152
	echo '
153
				</form>';
154
155
	// And now all the JS to make this work
156
	theme()->addInlineJavascript('
157
		createEventListener(window);
158
		window.addEventListener("load", initSearch, false);
159
160
		isFunctionLoaded("elk_AutoSuggest").then((available) => { 
161
			if (available) {
162
				new elk_AutoSuggest({
163
					sSessionId: elk_session_id,
164
					sSessionVar: elk_session_var,
165
					sControlId: \'userspec\',
166
					sSearchType: \'member\',
167
					bItemList: false
168
				});
169
			}
170
		});		
171
172
		// Set the search style
173
		document.getElementById(\'advanced\').value = "' . (empty($context['minmax_preferences']['asearch']) ? '1' : '0') . '";
174
175
		// And allow for the collapsing of the advanced search options
176
		var oSearchToggle = new elk_Toggle({
177
			bToggleEnabled: true,
178
			bCurrentlyCollapsed: ' . (empty($context['minmax_preferences']['asearch']) ? 'false' : 'true') . ',
179
			funcOnBeforeCollapse: function () {
180
				document.getElementById(\'advanced\').value = \'0\';
181
			},
182
			funcOnBeforeExpand: function () {
183
				document.getElementById(\'advanced\').value = \'1\';
184
			},
185
			aSwappableContainers: [
186
				\'advanced_search\'
187
			],
188
			aSwapLinks: [
189
				{
190
					sId: \'upshrink_link\',
191
					msgExpanded: ' . JavaScriptEscape($txt['search_simple']) . ',
192
					msgCollapsed: ' . JavaScriptEscape($txt['search_advanced']) . '
193
				}
194
			],
195
			oThemeOptions: {
196
				bUseThemeSettings: ' . ($context['user']['is_guest'] ? 'false' : 'true') . ',
197
				sOptionName: \'minmax_preferences\',
198
				sSessionId: elk_session_id,
199
				sSessionVar: elk_session_var,
200
				sAdditionalVars: \';minmax_key=asearch\'
201
			},
202
		});', true);
203
}
204
205
/**
206
 * Displays the search results page.
207
 */
208
function template_results()
209
{
210
	global $context, $options, $txt, $scripturl, $message;
211
212
	// Let them know if we ignored a word in the search
213
	if (!empty($context['search_ignored']))
214
	{
215
		echo '
216
		<div id="search_results">
217
			<h2 class="category_header">
218
				', $txt['generic_warning'], '
219
			</h2>
220
			<p class="warningbox">', $txt['search_warning_ignored_word' . (count($context['search_ignored']) === 1 ? '' : 's')], ': ', implode(', ', $context['search_ignored']), '</p>
221
		</div>';
222
	}
223
224
	// Or perhaps they made a spelling error, lets give them a hint
225
	if (!empty($context['did_you_mean']) || empty($context['topics']))
226
	{
227
		echo '
228
			<div id="search_results">
229
				<h2 class="category_header">', $txt['search_adjust_query'], '</h2>
230
				<div class="well">';
231
232
		// Did they make any typos or mistakes, perhaps?
233
		if (isset($context['did_you_mean']))
234
		{
235
			echo '
236
					<p>', $txt['search_did_you_mean'], ' <a href="', $scripturl, '?action=search;sa=results;params=', $context['did_you_mean_params'], '">', $context['did_you_mean'], '</a>.</p>';
237
		}
238
239
		echo '
240
					<form action="', $scripturl, '?action=search;sa=results" method="post" accept-charset="UTF-8">
241
						<dl class="settings">
242
							<dt class="righttext">
243
								<label for="search"><strong>', $txt['search_for'], ':</strong></label>
244
							</dt>
245
							<dd>
246
								<input type="text" id="search" name="search" value="', $context['search_params']['search'], '" maxlength="', $context['search_string_limit'], '" size="40" class="input_text" />
247
							</dd>
248
						</dl>
249
						<div class="submitbutton" >
250
							<input type="submit" name="edit_search" value="', $txt['search_adjust_submit'], '" />
251
							<input type="hidden" name="searchtype" value="', $context['search_params']['searchtype'], '" />
252
							<input type="hidden" name="userspec" value="', $context['search_params']['userspec'], '" />
253
							<input type="hidden" name="show_complete" value="', $context['search_params']['show_complete'], '" />
254
							<input type="hidden" name="subject_only" value="', $context['search_params']['subject_only'], '" />
255
							<input type="hidden" name="minage" value="', $context['search_params']['minage'], '" />
256
							<input type="hidden" name="maxage" value="', $context['search_params']['maxage'], '" />
257
							<input type="hidden" name="sort" value="', $context['search_params']['sort'], '" />';
258
259
		if (!empty($context['search_params']['brd']))
260
		{
261
			foreach ($context['search_params']['brd'] as $board_id)
262
			{
263
				echo '
264
							<input type="hidden" name="brd[', $board_id, ']" value="', $board_id, '" />';
265
			}
266
		}
267
268
		echo '
269
						</div>
270
					</form>
271
				</div>
272
			</div>
273
			<br />';
274
	}
275
276
	// Quick moderation set to checkboxes? Oh, how fun :/.
277
	if (!empty($options['display_quick_mod']))
278
	{
279
		echo '
280
			<form id="quickModForm" class="search_results_posts', $context['compact'] ? ' compact_view' : '', '" action="', $scripturl, '?action=quickmod" method="post" accept-charset="UTF-8" name="quickModForm">';
281
	}
282
283
	echo '
284
				<h2 class="category_header hdicon i-search">
285
					<span class="floatright">';
286
287
	if (!empty($options['display_quick_mod']))
288
	{
289
		echo '
290
						<input id="select_all" type="checkbox" onclick="invertAll(this, this.form, \'topics[]\');" />';
291
	}
292
293
	echo '
294
					</span>
295
					', $txt['mlist_search_results'], ':&nbsp;', $context['search_params']['search'], '
296
				</h2>';
297
298
	// Was anything even found?
299
	if (!empty($context['topics']))
300
	{
301
		template_pagesection();
302
	}
303
	else
304
	{
305
		echo '
306
				<div class="well">', $txt['find_no_results'], '</div>';
307
	}
308
309
	if ($context['compact'])
310
	{
311
		echo '
312
				<ul class="compact_view search_results_posts">';
313
	}
314
	else
315
	{
316
		echo '
317
				<ul class="forumposts search_results_posts">';
318
	}
319
320
	// Quick mod counters
321
	$context['allow_qm'] = [];
322
323
	// While we have results to show ...
324
	$controller = $context['get_topics'][0];
325
	while (($topic = $controller->{$context['get_topics'][1]}()))
326
	{
327
		$context['allow_qm']['can_remove'][] = isset($topic['quick_mod']['remove']) ? $topic['id'] : null;
328
		$context['allow_qm']['can_move'][] = isset($topic['quick_mod']['move']) ? $topic['id'] : null;
329
		$context['allow_qm']['can_lock'][] = isset($topic['quick_mod']['lock']) ? $topic['id'] : null;
330
		$context['allow_qm']['can_sticky'][] = isset($topic['quick_mod']['sticky']) ? $topic['id'] : null;
331
332
		if ($context['compact'])
333
		{
334
			// We start with locked and sticky topics.
335
			if ($topic['is_sticky'] && $topic['is_locked'])
336
			{
337
				$color_class = 'locked_row sticky_row';
338
			}
339
			// Sticky topics should get a different color, too.
340
			elseif ($topic['is_sticky'])
341
			{
342
				$color_class = 'sticky_row';
343
			}
344
			// Locked topics get special treatment as well.
345
			elseif ($topic['is_locked'])
346
			{
347
				$color_class = 'locked_row';
348
			}
349
			// Last, but not least: regular topics.
350
			else
351
			{
352
				$color_class = 'basic_row';
353
			}
354
		}
355
		else
356
		{
357
			$color_class = 'basic_row';
358
		}
359
360
		foreach ($topic['matches'] as $message)
361
		{
362
			echo '
363
					<li class="', $color_class, '">
364
						<div class="topic_details">
365
							<div class="counter">', $message['counter'] + 1, '</div>
366
							<h5>', $topic['board']['link'], ' / <a href="', getUrl('topic', ['topic' => $topic['id'], 'subject' => $topic['subject'], 'start' => 'msg' . $message['id']]), '#msg', $message['id'], '">', $message['subject_highlighted'], '</a></h5>
367
							<span class="smalltext">', $txt['by'], ' <strong>', $message['member']['link'], '</strong> ', $txt['on'], ' <em>', $message['time'], '</em></span>
368
						</div>';
369
370
			if (!$context['compact'] || $message['body_highlighted'] !== '')
371
			{
372
				echo '
373
						<div class="topic_body">', $message['body_highlighted'], '</div>';
374
			}
375
376
			// Quote, Reply, etc ... only when not viewing compact
377
			if (!empty($topic['buttons']))
378
			{
379
				template_button_strip($topic['buttons'], 'quickbuttons no_js', ['no-class' => true]);
380
			}
381
382
			// Show QM checkbox, by the count indicator, only if compact view is on
383
			if (!empty($options['display_quick_mod']) && $context['compact'])
384
			{
385
				echo '
386
						<p class="topic_moderation">
387
							<input type="checkbox" class="inline_mod_check" name="topics[]" value="', $topic['id'], '" />
388
						</p>';
389
			}
390
391
			echo '
392
					</li>';
393
		}
394
	}
395
396
	echo '
397
				</ul>';
398
399
	// Quick moderation enabled, then show an action area
400
	if (!empty($context['topics']) && !empty($options['display_quick_mod']))
401
	{
402
		echo '
403
				<div id="moderationbuttons">';
404
405
		template_button_strip($context['mod_buttons'], '', ['id' => 'moderationbuttons_strip']);
406
407
		// Show a list of boards they can move the topic to.
408
		if ($context['can_move'])
409
		{
410
			echo '
411
					<span id="quick_mod_jump_to">&nbsp;</span>';
412
		}
413
414
		echo '
415
					<input type="hidden" name="qaction" id="qaction" value="na" />
416
					<input type="hidden" name="redirect_url" value="', $scripturl . '?action=search;sa=results;params=' . $context['params'], '" />
417
					<input type="hidden" name="' . $context['session_var'] . '" value="' . $context['session_id'] . '" />
418
				</div>
419
			</form>';
420
421
		// Show the move to box?
422
		if ($context['can_move'])
423
		{
424
			theme()->addInlineJavascript('
425
			aJumpTo[aJumpTo.length] = new JumpTo({
426
				sContainerId: "quick_mod_jump_to",
427
				sClassName: "qaction",
428
				sJumpToTemplate: "%dropdown_list%",
429
				sCurBoardName: "' . $context['jump_to']['board_name'] . '",
430
				sBoardChildLevelIndicator: "&#8195;",
431
				sBoardPrefix: "&#10148;&nbsp;",
432
				sCatClass: "jump_to_header",
433
				sCatPrefix: "",
434
				bNoRedirect: true,
435
				bDisabled: false,
436
				sCustomName: "move_to"
437
			});', true);
438
		}
439
440
		theme()->addInlineJavascript('
441
		let oInTopicListModeration = new InTopicListModeration({
442
			aQmActions: ["remove", "lock", "sticky", "move", "markread"],
443
			sButtonStrip: "moderationbuttons",
444
			sButtonStripDisplay: "moderationbuttons_strip",
445
			bUseImageButton: false,
446
			sFormId: "quickModForm",
447
			
448
			bCanRemove: ' . (empty($context['can_remove']) ? 'false' : 'true') . ',
449
			aActionRemove: [' . implode(',', array_filter(array_unique($context['allow_qm']['can_remove']))) . '],
450
			sRemoveButtonLabel: "' . $txt['remove_topic'] . '",
451
			sRemoveButtonImage: "i-delete",
452
			sRemoveButtonConfirm: "' . $txt['quickmod_confirm'] . '",
453
			
454
			bCanMove: ' . (empty($context['can_move']) ? 'false' : 'true') . ',
455
			aActionMove: [' . implode(',', array_filter(array_unique($context['allow_qm']['can_move']))) . '],
456
			sMoveButtonLabel: "' . $txt['move_topic'] . '",
457
			sMoveButtonImage: "i-move",
458
			sMoveButtonConfirm: "' . $txt['quickmod_confirm'] . '",
459
460
			bCanLock: ' . ($context['can_lock'] ? 'true' : 'false') . ',
461
			aActionLock: [' . implode(',', array_filter(array_unique($context['allow_qm']['can_lock']))) . '],
462
			sLockButtonLabel: "' . $txt['set_lock'] . '",
463
			sLockButtonImage: "i-lock",
464
			
465
			bCanSticky: ' . ($context['can_sticky'] ? 'true' : 'false') . ',
466
			aActionSticky: [' . implode(',', array_filter(array_unique($context['allow_qm']['can_sticky']))) . '],
467
			sStickyButtonLabel: "' . $txt['set_sticky'] . '",
468
			sStickyButtonImage: "i-pin",
469
			
470
			bCanMarkread: ' . ($context['can_markread'] ? 'true' : 'false') . ',
471
			sMarkreadButtonLabel: "' . $txt['mark_read_short'] . '",
472
			sMarkreadButtonImage: "i-view",
473
			sMarkreadButtonConfirm: "' . $txt['mark_these_as_read_confirm'] . '",				
474
		});', true);
475
	}
476
477
	// If we have results show a page index
478
	if (!empty($context['topics']))
479
	{
480
		template_pagesection();
481
	}
482
}
483