Completed
Pull Request — patch_1-1-4 (#3210)
by Emanuele
12:56
created

Search.template.php ➔ template_searchform()   C

Complexity

Conditions 14
Paths 32

Size

Total Lines 167

Duplication

Lines 11
Ratio 6.59 %

Importance

Changes 0
Metric Value
cc 14
nc 32
nop 0
dl 11
loc 167
rs 5.0133
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
 * @name      ElkArte Forum
5
 * @copyright ElkArte Forum contributors
6
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
7
 *
8
 * This file contains code covered by:
9
 * copyright:	2011 Simple Machines (http://www.simplemachines.org)
10
 * license:  	BSD, See included LICENSE.TXT for terms and conditions.
11
 *
12
 * @version 1.1
13
 *
14
 */
15
16
/**
17
 * Lotsa boards we (may) have here!
18
 */
19
function template_Search_init()
20
{
21
	loadTemplate('GenericBoards');
22
}
23
24
/**
25
 * Main search page. Allows the user to search the forum according to criteria.
26
 */
27
function template_searchform()
28
{
29
	global $context, $settings, $txt, $scripturl, $modSettings;
30
31
	echo '
32
				<form id="searchform" action="', $scripturl, '?action=search;sa=results" method="post" accept-charset="UTF-8" name="searchform">
33
					<h2 class="category_header', !empty($settings['use_buttons']) ? ' hdicon cat_img_search' : '', '">
34
						', $txt['set_parameters'], '
35
					</h2>';
36
37
	// Any search errors to inform the user about
38 View Code Duplication
	if (!empty($context['search_errors']))
39
		echo '
40
					<p class="errorbox">', implode('<br />', $context['search_errors']['messages']), '</p>';
41
42
	// Start off showing our basic search form
43
	echo '
44
					<fieldset id="simple_search" class="content">
45
						<div id="search_term_input">
46
							<label for="search">
47
								<strong>', $txt['search_for'], '</strong>
48
							</label>:
49
							<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" />', '
50
							<input id="submit" type="submit" name="s_search" value="' . $txt['search'] . '" />
51
						</div>';
52
53
	if (empty($modSettings['search_simple_fulltext']))
54
		echo '
55
						<p class="smalltext">', $txt['search_example'], '</p>';
56
57
	// Does the search require a visual verification screen to annoy them?
58 View Code Duplication
	if (!empty($context['require_verification']))
59
	{
60
		template_verification_controls($context['visual_verification_id'], '
61
						<div class="verification">
62
							<strong>' . $txt['search_visual_verification_label'] . ':</strong>
63
							<br />', '
64
						</div>');
65
	}
66
67
	// All of the advanced options, this div is collapsed by the JS when available
68
	echo '
69
						<div id="advanced_search">
70
							<dl id="search_options">
71
								<dt class="righttext"><label for="searchtype">
72
									', $txt['search_match'], ':</label>
73
								</dt>
74
								<dd>
75
									<select name="searchtype" id="searchtype">
76
										<option value="1"', empty($context['search_params']['searchtype']) ? ' selected="selected"' : '', '>', $txt['all_words'], '</option>
77
										<option value="2"', !empty($context['search_params']['searchtype']) ? ' selected="selected"' : '', '>', $txt['any_words'], '</option>
78
									</select>
79
								</dd>
80
								<dt class="righttext">
81
									<label for="userspec">', $txt['by_user'], ':</label>
82
								</dt>
83
								<dd>
84
									<input id="userspec" type="text" name="userspec" value="', $context['search_params']['userspec'], '" size="40" class="input_text" />
85
								</dd>
86
								<dt class="righttext">
87
									<label for="sort">', $txt['search_order'], ':</label>
88
								</dt>
89
								<dd>
90
									<select id="sort" name="sort">
91
										<option value="relevance|desc">', $txt['search_orderby_relevant_first'], '</option>
92
										<option value="num_replies|desc">', $txt['search_orderby_large_first'], '</option>
93
										<option value="num_replies|asc">', $txt['search_orderby_small_first'], '</option>
94
										<option value="id_msg|desc">', $txt['search_orderby_recent_first'], '</option>
95
										<option value="id_msg|asc">', $txt['search_orderby_old_first'], '</option>
96
									</select>
97
								</dd>
98
								<dt class="righttext options">
99
									', $txt['search_options'], ':
100
								</dt>
101
								<dd class="options">
102
									<input type="checkbox" name="show_complete" id="show_complete" value="1"', !empty($context['search_params']['show_complete']) ? ' checked="checked"' : '', ' />
103
									<label for="show_complete">', $txt['search_show_complete_messages'], '</label><br />
104
									<input type="checkbox" name="subject_only" id="subject_only" value="1"', !empty($context['search_params']['subject_only']) ? ' checked="checked"' : '', ' />
105
									<label for="subject_only">', $txt['search_subject_only'], '</label>
106
								</dd>
107
								<dt class="righttext between">
108
									', $txt['search_post_age'], ':
109
								</dt>
110
								<dd>
111
									<label for="minage">', $txt['search_between'], '</label>&nbsp;
112
									<input type="text" name="minage" id="minage" value="', $context['search_params']['minage'], '" size="5" maxlength="4" class="input_text" />&nbsp;
113
									<label for="maxage">', $txt['search_and'], '&nbsp;</label>
114
									<input type="text" name="maxage" id="maxage" value="', $context['search_params']['maxage'], '" size="5" maxlength="4" class="input_text" /> ', $txt['days_word'], '
115
								</dd>
116
							</dl>
117
						</div>
118
						<a id="upshrink_link" href="', $scripturl, '?action=search;advanced" class="linkbutton hide">', $txt['search_simple'], '</a>';
119
120
	// Set the initial search style for the form
121
	echo '
122
						<input id="advanced" type="hidden" name="advanced" value="1" />';
123
124
	// If $context['search_params']['topic'] is set, that means we're searching just one topic.
125
	if (!empty($context['search_params']['topic']))
126
		echo '
127
						<p>', $txt['search_specific_topic'], ' &quot;', $context['search_topic']['link'], '&quot;.</p>
128
						<input type="hidden" name="topic" value="', $context['search_topic']['id'], '" />';
129
130
	echo '
131
					</fieldset>';
132
133
	// This starts our selection area to allow searching by specific boards
134
	if (empty($context['search_params']['topic']))
135
	{
136
		echo '
137
					<fieldset id="pick_boards" class="content">';
138
139
		template_pick_boards('searchform');
140
141
		echo '
142
					</fieldset>';
143
	}
144
145
	echo '
146
				</form>';
147
148
	// And now all the JS to make this work
149
	addInlineJavascript('
150
		createEventListener(window);
151
		window.addEventListener("load", initSearch, false);
152
153
		var oAddMemberSuggest = new smc_AutoSuggest({
154
			sSelf: \'oAddMemberSuggest\',
155
			sSessionId: elk_session_id,
156
			sSessionVar: elk_session_var,
157
			sControlId: \'userspec\',
158
			sSearchType: \'member\',
159
			bItemList: false
160
		});
161
162
		// Set the search style
163
		document.getElementById(\'advanced\').value = "' . (empty($context['minmax_preferences']['asearch']) ? '1' : '0') . '";
164
165
		// And allow for the collapsing of the advanced search options
166
		var oSearchToggle = new elk_Toggle({
167
			bToggleEnabled: true,
168
			bCurrentlyCollapsed: ' . (empty($context['minmax_preferences']['asearch']) ? 'false' : 'true') . ',
169
			funcOnBeforeCollapse: function () {
170
				document.getElementById(\'advanced\').value = \'0\';
171
			},
172
			funcOnBeforeExpand: function () {
173
				document.getElementById(\'advanced\').value = \'1\';
174
			},
175
			aSwappableContainers: [
176
				\'advanced_search\'
177
			],
178
			aSwapLinks: [
179
				{
180
					sId: \'upshrink_link\',
181
					msgExpanded: ' . JavaScriptEscape($txt['search_simple']) . ',
182
					msgCollapsed: ' . JavaScriptEscape($txt['search_advanced']) . '
183
				}
184
			],
185
			oThemeOptions: {
186
				bUseThemeSettings: ' . ($context['user']['is_guest'] ? 'false' : 'true') . ',
187
				sOptionName: \'minmax_preferences\',
188
				sSessionId: elk_session_id,
189
				sSessionVar: elk_session_var,
190
				sAdditionalVars: \';minmax_key=asearch\'
191
			},
192
		});', true);
193
}
194
195
/**
196
 * Displays the search results page.
197
 */
198
function template_results()
199
{
200
	global $context, $settings, $options, $txt, $scripturl, $message;
201
202
	// Let them know if we ignored a word in the search
203
	if (!empty($context['search_ignored']))
204
		echo '
205
		<div id="search_results">
206
			<h2 class="category_header">
207
				', $txt['generic_warning'], '
208
			</h2>
209
			<p class="warningbox">', $txt['search_warning_ignored_word' . (count($context['search_ignored']) == 1 ? '' : 's')], ': ', implode(', ', $context['search_ignored']), '</p>
210
		</div>';
211
212
	// Or perhaps they made a spelling error, lets give them a hint
213
	if (!empty($context['did_you_mean']) || empty($context['topics']))
214
	{
215
		echo '
216
			<div id="search_results">
217
				<h2 class="category_header">', $txt['search_adjust_query'], '</h2>
218
				<div class="well">';
219
220
		// Did they make any typos or mistakes, perhaps?
221
		if (isset($context['did_you_mean']))
222
			echo '
223
					<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>';
224
225
		echo '
226
					<form action="', $scripturl, '?action=search;sa=results" method="post" accept-charset="UTF-8">
227
						<dl class="settings">
228
							<dt class="righttext">
229
								<label for="search"><strong>', $txt['search_for'], ':</strong></label>
230
							</dt>
231
							<dd>
232
								<input type="text" id="search" name="search" value="', $context['search_params']['search'], '" maxlength="', $context['search_string_limit'], '" size="40" class="input_text" />
233
							</dd>
234
						</dl>
235
						<div class="submitbutton" >
236
							<input type="submit" name="edit_search" value="', $txt['search_adjust_submit'], '" />
237
							<input type="hidden" name="searchtype" value="', $context['search_params']['searchtype'], '" />
238
							<input type="hidden" name="userspec" value="', $context['search_params']['userspec'], '" />
239
							<input type="hidden" name="show_complete" value="', $context['search_params']['show_complete'], '" />
240
							<input type="hidden" name="subject_only" value="', $context['search_params']['subject_only'], '" />
241
							<input type="hidden" name="minage" value="', $context['search_params']['minage'], '" />
242
							<input type="hidden" name="maxage" value="', $context['search_params']['maxage'], '" />
243
							<input type="hidden" name="sort" value="', $context['search_params']['sort'], '" />
244
						</div>';
245
246
		if (!empty($context['search_params']['brd']))
247
			foreach ($context['search_params']['brd'] as $board_id)
248
				echo '
249
						<input type="hidden" name="brd[', $board_id, ']" value="', $board_id, '" />';
250
251
		echo '
252
					</form>
253
				</div>
254
			</div>
255
			<br />';
256
	}
257
258
	// Quick moderation set to checkboxes? Oh, how fun :/.
259
	if (!empty($options['display_quick_mod']) && $options['display_quick_mod'] == 1)
260
		echo '
261
			<form id="topicForm" class="search_results_posts', $context['compact'] ? ' compact_view' : '', '" action="', $scripturl, '?action=quickmod" method="post" accept-charset="UTF-8" name="topicForm">';
262
263
	echo '
264
				<h2 class="category_header hdicon cat_img_search">
265
					<span class="floatright">';
266
267
	if (!empty($options['display_quick_mod']) && $options['display_quick_mod'] == 1)
268
		echo '
269
						<input type="checkbox" onclick="invertAll(this, this.form, \'topics[]\');" />';
270
271
	echo '
272
					</span>
273
					', $txt['mlist_search_results'], ':&nbsp;', $context['search_params']['search'], '
274
				</h2>';
275
276
	// Was anything even found?
277
	if (!empty($context['topics']))
278
		template_pagesection();
279
	else
280
		echo '
281
				<div class="well">', $txt['find_no_results'], '</div>';
282
283
	if ($context['compact'])
284
		echo '
285
				<ul class="topic_listing compact_view search_results_posts">';
286
	else
287
		echo '
288
				<ul class="forumposts topic_listing search_results_posts">';
289
290
	// While we have results to show ...
291
	$controller = $context['get_topics'][0];
292
	while ($topic = $controller->{$context['get_topics'][1]}())
293
	{
294
		if ($context['compact'])
295
		{
296
			// We start with locked and sticky topics.
297 View Code Duplication
			if ($topic['is_sticky'] && $topic['is_locked'])
298
				$color_class = 'locked_row sticky_row';
299
			// Sticky topics should get a different color, too.
300
			elseif ($topic['is_sticky'])
301
				$color_class = 'sticky_row';
302
			// Locked topics get special treatment as well.
303
			elseif ($topic['is_locked'])
304
				$color_class = 'locked_row';
305
			// Last, but not least: regular topics.
306
			else
307
				$color_class = 'basic_row';
308
		}
309
		else
310
			$color_class = 'basic_row';
311
312
		foreach ($topic['matches'] as $message)
313
		{
314
			echo '
315
					<li class="', $color_class, '">
316
						<div class="topic_details">
317
							<div class="counter">', $message['counter'], '</div>
318
							<h5>', $topic['board']['link'], ' / <a href="', $scripturl, '?topic=', $topic['id'], '.msg', $message['id'], '#msg', $message['id'], '">', $message['subject_highlighted'], '</a></h5>
319
							<span class="smalltext">&#171;&nbsp;', $txt['by'], '&nbsp;<strong>', $message['member']['link'], '</strong> ', $txt['on'], '&nbsp;<em>', $message['time'], '</em>&nbsp;&#187;</span>';
320
			echo '
321
						</div>';
322
323
			if (!$context['compact'] || $message['body_highlighted'] != '')
324
				echo '
325
						<div class="topic_body">', $message['body_highlighted'], '</div>';
326
327
			if (!empty($topic['buttons']))
328
				template_quickbutton_strip($topic['buttons'], $topic['tests']);
329
330
			if (!empty($options['display_quick_mod']))
331
			{
332
				echo '
333
						<p class="topic_moderation">';
334
335
				if ($options['display_quick_mod'] == 1)
336
				{
337
					echo '
338
							<input type="checkbox" name="topics[]" value="', $topic['id'], '" />';
339
				}
340
				else
341
				{
342 View Code Duplication
					if ($topic['quick_mod']['remove'])
343
						echo '
344
							<a href="', $scripturl, '?action=quickmod;actions%5B', $topic['id'], '%5D=remove;', $context['session_var'], '=', $context['session_id'], '" onclick="return confirm(\'', $txt['quickmod_confirm'], '\');"><img src="', $settings['images_url'], '/icons/quick_remove.png" style="width: 16px;" alt="', $txt['remove_topic'], '" title="', $txt['remove_topic'], '" /></a>';
345
346 View Code Duplication
					if ($topic['quick_mod']['lock'])
347
						echo '
348
							<a href="', $scripturl, '?action=quickmod;actions%5B', $topic['id'], '%5D=lock;', $context['session_var'], '=', $context['session_id'], '" onclick="return confirm(\'', $txt['quickmod_confirm'], '\');"><img src="', $settings['images_url'], '/icons/quick_lock.png" style="width: 16px;" alt="', $txt[$topic['is_locked'] ? 'set_unlock' : 'set_lock'], '" title="', $txt[$topic['is_locked'] ? 'set_unlock' : 'set_lock'], '" /></a>';
349
350
					if ($topic['quick_mod']['lock'] || $topic['quick_mod']['remove'])
351
						echo '
352
							<br />';
353
354 View Code Duplication
					if ($topic['quick_mod']['sticky'])
355
						echo '
356
							<a href="', $scripturl, '?action=quickmod;actions%5B', $topic['id'], '%5D=sticky;', $context['session_var'], '=', $context['session_id'], '" onclick="return confirm(\'', $txt['quickmod_confirm'], '\');"><img src="', $settings['images_url'], '/icons/quick_sticky.png" style="width: 16px;" alt="', $txt[$topic['is_sticky'] ? 'set_nonsticky' : 'set_sticky'], '" title="', $txt[$topic['is_sticky'] ? 'set_nonsticky' : 'set_sticky'], '" /></a>';
357
358
					if ($topic['quick_mod']['move'])
359
						echo '
360
							<a href="', $scripturl, '?action=movetopic;topic=', $topic['id'], '.0"><img src="', $settings['images_url'], '/icons/quick_move.png" style="width: 16px;" alt="', $txt['move_topic'], '" title="', $txt['move_topic'], '" /></a>';
361
				}
362
363
				echo '
364
						</p>';
365
			}
366
367
			echo '
368
					</li>';
369
		}
370
	}
371
372
	echo '
373
				</ul>';
374
375
	// If we have results show a page index
376
	if (!empty($context['topics']))
377
		template_pagesection();
378
379
	// Quick moderation enabled, then show an action area
380
	if (!empty($context['topics']) && !empty($options['display_quick_mod']) && $options['display_quick_mod'] == 1)
381
	{
382
		echo '
383
				<div class="pagesection floatright">
384
					<select class="qaction" name="qaction"', $context['can_move'] ? ' onchange="this.form.move_to.disabled = (this.options[this.selectedIndex].value != \'move\');"' : '', '>
385
						<option value="">&nbsp;</option>';
386
387 View Code Duplication
		foreach ($context['qmod_actions'] as $qmod_action)
388
			if ($context['can_' . $qmod_action])
389
				echo '
390
						<option value="' . $qmod_action . '">&#10148;&nbsp;', $txt['quick_mod_' . $qmod_action] . '</option>';
391
392
		echo '
393
					</select>';
394
395
		// Show a list of boards they can move the topic to.
396
		if ($context['can_move'])
397
			echo '
398
					<span id="quick_mod_jump_to">&nbsp;</span>';
399
400
		echo '
401
					<input type="hidden" name="redirect_url" value="', $scripturl . '?action=search;sa=results;params=' . $context['params'], '" />
402
					<input type="submit" value="', $txt['quick_mod_go'], '" onclick="return document.forms.topicForm.qaction.value != \'\' &amp;&amp; confirm(\'', $txt['quickmod_confirm'], '\');" />
403
					<input type="hidden" name="' . $context['session_var'] . '" value="' . $context['session_id'] . '" />
404
				</div>
405
			</form>';
406
	}
407
408
	// Show a jump to box for easy navigation.
409
	echo '
410
			<div class="floatright" id="search_jump_to">&nbsp;</div>';
411
412
	if (!empty($options['display_quick_mod']) && $options['display_quick_mod'] == 1 && !empty($context['topics']) && $context['can_move'])
413
		addInlineJavascript('
414
		aJumpTo[aJumpTo.length] = new JumpTo({
415
			sContainerId: "quick_mod_jump_to",
416
			sClassName: "qaction",
417
			sJumpToTemplate: "%dropdown_list%",
418
			sCurBoardName: "' . $context['jump_to']['board_name'] . '",
419
			sBoardChildLevelIndicator: "&#8195;",
420
			sBoardPrefix: "&#10148;&nbsp;",
421
			sCatClass: "jump_to_header",
422
			sCatPrefix: "",
423
			bNoRedirect: true,
424
			bDisabled: true,
425
			sCustomName: "move_to"
426
		});', true);
427
428
	addInlineJavascript('
429
		aJumpTo[aJumpTo.length] = new JumpTo({
430
			sContainerId: "search_jump_to",
431
			sJumpToTemplate: "<label class=\"smalltext\" for=\"%select_id%\">' . $context['jump_to']['label'] . ':<" + "/label> %dropdown_list%",
432
			iCurBoardId: 0,
433
			iCurBoardChildLevel: 0,
434
			sCurBoardName: "' . $context['jump_to']['board_name'] . '",
435
			sBoardChildLevelIndicator: "&#8195;",
436
			sBoardPrefix: "&#10148;&nbsp;",
437
			sCatClass: "jump_to_header",
438
			sCatPrefix: "",
439
			sGoButtonLabel: "' . $txt['quick_mod_go'] . '"
440
		});', true);
441
}