Passed
Push — release-2.1 ( 0e5bfc...908430 )
by Mathias
07:53 queued 12s
created

printMemberListRows()   D

Complexity

Conditions 18
Paths 88

Size

Total Lines 72
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
eloc 41
nc 88
nop 1
dl 0
loc 72
rs 4.8666
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
 * This file contains the functions for displaying and searching in the
5
 * members list.
6
 *
7
 * Simple Machines Forum (SMF)
8
 *
9
 * @package SMF
10
 * @author Simple Machines https://www.simplemachines.org
11
 * @copyright 2021 Simple Machines and individual contributors
12
 * @license https://www.simplemachines.org/about/smf/license.php BSD
13
 *
14
 * @version 2.1 RC4
15
 */
16
17
if (!defined('SMF'))
18
	die('No direct access...');
19
20
/**
21
 * Shows a listing of registered members.
22
 * - If a subaction is not specified, lists all registered members.
23
 * - It allows searching for members with the 'search' sub action.
24
 * - It calls MLAll or MLSearch depending on the sub action.
25
 * - Requires the view_mlist permission.
26
 * - Accessed via ?action=mlist.
27
 *
28
 * Uses Memberlist template, main sub template.
29
 */
30
function Memberlist()
31
{
32
	global $scripturl, $txt, $modSettings, $context;
33
34
	// Make sure they can view the memberlist.
35
	isAllowedTo('view_mlist');
36
37
	loadTemplate('Memberlist');
38
39
	$context['listing_by'] = !empty($_GET['sa']) ? $_GET['sa'] : 'all';
40
41
	// $subActions array format:
42
	// 'subaction' => array('label', 'function', 'is_selected')
43
	$subActions = array(
44
		'all' => array($txt['view_all_members'], 'MLAll', $context['listing_by'] == 'all'),
45
		'search' => array($txt['mlist_search'], 'MLSearch', $context['listing_by'] == 'search'),
46
	);
47
48
	// Set up the sort links.
49
	$context['sort_links'] = array();
50
	foreach ($subActions as $act => $text)
51
	{
52
		$context['sort_links'][] = array(
53
			'label' => $text[0],
54
			'action' => $act,
55
			'selected' => $text[2],
56
		);
57
	}
58
59
	$context['num_members'] = $modSettings['totalMembers'];
60
61
	// Set up the columns...
62
	$context['columns'] = array(
63
		'is_online' => array(
64
			'label' => $txt['status'],
65
			'sort' => array(
66
				'down' => allowedTo('moderate_forum') ? 'COALESCE(lo.log_time, 1) ASC, real_name ASC' : 'CASE WHEN mem.show_online THEN COALESCE(lo.log_time, 1) ELSE 1 END ASC, real_name ASC',
67
				'up' => allowedTo('moderate_forum') ? 'COALESCE(lo.log_time, 1) DESC, real_name DESC' : 'CASE WHEN mem.show_online THEN COALESCE(lo.log_time, 1) ELSE 1 END DESC, real_name DESC'
68
			),
69
		),
70
		'real_name' => array(
71
			'label' => $txt['name'],
72
			'class' => 'lefttext',
73
			'sort' => array(
74
				'down' => 'mem.real_name DESC',
75
				'up' => 'mem.real_name ASC'
76
			),
77
		),
78
		'website_url' => array(
79
			'label' => $txt['website'],
80
			'link_with' => 'website',
81
			'sort' => array(
82
				'down' => 'mem.website_url = \'\', mem.website_url is null, mem.website_url DESC',
83
				'up' => 'mem.website_url != \'\', mem.website_url is not null, mem.website_url ASC'
84
			),
85
		),
86
		'id_group' => array(
87
			'label' => $txt['position'],
88
			'sort' => array(
89
				'down' => 'mg.group_name is null, mg.group_name DESC',
90
				'up' => 'mg.group_name is not null, mg.group_name ASC'
91
			),
92
		),
93
		'registered' => array(
94
			'label' => $txt['date_registered'],
95
			'sort' => array(
96
				'down' => 'mem.date_registered DESC',
97
				'up' => 'mem.date_registered ASC'
98
			),
99
		),
100
		'post_count' => array(
101
			'label' => $txt['posts'],
102
			'default_sort_rev' => true,
103
			'sort' => array(
104
				'down' => 'mem.posts DESC',
105
				'up' => 'mem.posts ASC'
106
			),
107
		)
108
	);
109
110
	$context['custom_profile_fields'] = getCustFieldsMList();
111
112
	if (!empty($context['custom_profile_fields']['columns']))
113
		$context['columns'] += $context['custom_profile_fields']['columns'];
114
115
	$context['colspan'] = 0;
116
	$context['disabled_fields'] = isset($modSettings['disabled_profile_fields']) ? array_flip(explode(',', $modSettings['disabled_profile_fields'])) : array();
117
	foreach ($context['columns'] as $key => $column)
118
	{
119
		if (isset($context['disabled_fields'][$key]) || (isset($column['link_with']) && isset($context['disabled_fields'][$column['link_with']])))
120
		{
121
			unset($context['columns'][$key]);
122
			continue;
123
		}
124
125
		$context['colspan'] += isset($column['colspan']) ? $column['colspan'] : 1;
126
	}
127
128
	// Aesthetic stuff.
129
	end($context['columns']);
130
131
	$context['linktree'][] = array(
132
		'url' => $scripturl . '?action=mlist',
133
		'name' => $txt['members_list']
134
	);
135
136
	$context['can_send_pm'] = allowedTo('pm_send');
137
	$context['can_send_email'] = allowedTo('moderate_forum');
138
139
	// Build the memberlist button array.
140
	$context['memberlist_buttons'] = array(
141
		'view_all_members' => array('text' => 'view_all_members', 'image' => 'mlist.png', 'url' => $scripturl . '?action=mlist' . ';sa=all', 'active' => true),
142
		'mlist_search' => array('text' => 'mlist_search', 'image' => 'mlist.png', 'url' => $scripturl . '?action=mlist' . ';sa=search'),
143
	);
144
145
	// Allow mods to add additional buttons here
146
	call_integration_hook('integrate_memberlist_buttons');
147
148
	// Jump to the sub action.
149
	if (isset($subActions[$context['listing_by']]))
150
		call_helper($subActions[$context['listing_by']][1]);
151
152
	else
153
		call_helper($subActions['all'][1]);
154
}
155
156
/**
157
 * List all members, page by page, with sorting.
158
 * Called from MemberList().
159
 * Can be passed a sort parameter, to order the display of members.
160
 * Calls printMemberListRows to retrieve the results of the query.
161
 */
162
function MLAll()
163
{
164
	global $txt, $scripturl;
165
	global $modSettings, $context, $smcFunc;
166
167
	// The chunk size for the cached index.
168
	$cache_step_size = 500;
169
170
	// Only use caching if:
171
	// 1. there are at least 2k members,
172
	// 2. the default sorting method (real_name) is being used,
173
	// 3. the page shown is high enough to make a DB filesort unprofitable.
174
	$use_cache = $modSettings['totalMembers'] > 2000 && (!isset($_REQUEST['sort']) || $_REQUEST['sort'] === 'real_name') && isset($_REQUEST['start']) && $_REQUEST['start'] > $cache_step_size;
175
176
	if ($use_cache)
177
	{
178
		// Maybe there's something cached already.
179
		if (!empty($modSettings['memberlist_cache']))
180
			$memberlist_cache = $smcFunc['json_decode']($modSettings['memberlist_cache'], true);
181
182
		// The chunk size for the cached index.
183
		$cache_step_size = 500;
184
185
		// Only update the cache if something changed or no cache existed yet.
186
		if (empty($memberlist_cache) || empty($modSettings['memberlist_updated']) || $memberlist_cache['last_update'] < $modSettings['memberlist_updated'])
187
		{
188
			$request = $smcFunc['db_query']('', '
189
				SELECT real_name
190
				FROM {db_prefix}members
191
				WHERE is_activated = {int:is_activated}
192
				ORDER BY real_name',
193
				array(
194
					'is_activated' => 1,
195
				)
196
			);
197
198
			$memberlist_cache = array(
199
				'last_update' => time(),
200
				'num_members' => $smcFunc['db_num_rows']($request),
201
				'index' => array(),
202
			);
203
204
			for ($i = 0, $n = $smcFunc['db_num_rows']($request); $i < $n; $i += $cache_step_size)
205
			{
206
				$smcFunc['db_data_seek']($request, $i);
207
				list($memberlist_cache['index'][$i]) = $smcFunc['db_fetch_row']($request);
208
			}
209
			$smcFunc['db_data_seek']($request, $memberlist_cache['num_members'] - 1);
210
			list ($memberlist_cache['index'][$memberlist_cache['num_members'] - 1]) = $smcFunc['db_fetch_row']($request);
211
			$smcFunc['db_free_result']($request);
212
213
			// Now we've got the cache...store it.
214
			updateSettings(array('memberlist_cache' => $smcFunc['json_encode']($memberlist_cache)));
215
		}
216
217
		$context['num_members'] = $memberlist_cache['num_members'];
218
	}
219
220
	// Without cache we need an extra query to get the amount of members.
221
	else
222
	{
223
		$request = $smcFunc['db_query']('', '
224
			SELECT COUNT(*)
225
			FROM {db_prefix}members
226
			WHERE is_activated = {int:is_activated}',
227
			array(
228
				'is_activated' => 1,
229
			)
230
		);
231
		list ($context['num_members']) = $smcFunc['db_fetch_row']($request);
232
		$smcFunc['db_free_result']($request);
233
	}
234
235
	// Set defaults for sort (real_name) and start. (0)
236
	if (!isset($_REQUEST['sort']) || !isset($context['columns'][$_REQUEST['sort']]))
237
		$_REQUEST['sort'] = 'real_name';
238
239
	if (!is_numeric($_REQUEST['start']))
240
	{
241
		if (preg_match('~^[^\'\\\\/]~' . ($context['utf8'] ? 'u' : ''), $smcFunc['strtolower']($_REQUEST['start']), $match) === 0)
242
			fatal_error('Hacker?', false);
243
244
		$_REQUEST['start'] = $match[0];
245
246
		$request = $smcFunc['db_query']('substring', '
247
			SELECT COUNT(*)
248
			FROM {db_prefix}members
249
			WHERE LOWER(SUBSTRING(real_name, 1, 1)) < {string:first_letter}
250
				AND is_activated = {int:is_activated}',
251
			array(
252
				'is_activated' => 1,
253
				'first_letter' => $_REQUEST['start'],
254
			)
255
		);
256
		list ($_REQUEST['start']) = $smcFunc['db_fetch_row']($request);
257
		$smcFunc['db_free_result']($request);
258
	}
259
260
	$context['letter_links'] = '';
261
	for ($i = 97; $i < 123; $i++)
262
		$context['letter_links'] .= '<a href="' . $scripturl . '?action=mlist;sa=all;start=' . chr($i) . '#letter' . chr($i) . '">' . strtoupper(chr($i)) . '</a> ';
263
264
	// Sort out the column information.
265
	foreach ($context['columns'] as $col => $column_details)
266
	{
267
		$context['columns'][$col]['href'] = $scripturl . '?action=mlist;sort=' . $col . ';start=' . $_REQUEST['start'];
268
269
		if ((!isset($_REQUEST['desc']) && $col == $_REQUEST['sort']) || ($col != $_REQUEST['sort'] && !empty($column_details['default_sort_rev'])))
270
			$context['columns'][$col]['href'] .= ';desc';
271
272
		$context['columns'][$col]['link'] = '<a href="' . $context['columns'][$col]['href'] . '" rel="nofollow">' . $context['columns'][$col]['label'] . '</a>';
273
		$context['columns'][$col]['selected'] = $_REQUEST['sort'] == $col;
274
	}
275
276
	// Are we sorting the results
277
	$context['sort_by'] = $_REQUEST['sort'];
278
	$context['sort_direction'] = !isset($_REQUEST['desc']) ? 'up' : 'down';
279
280
	// Construct the page index.
281
	$context['page_index'] = constructPageIndex($scripturl . '?action=mlist;sort=' . $_REQUEST['sort'] . (isset($_REQUEST['desc']) ? ';desc' : ''), $_REQUEST['start'], $context['num_members'], $modSettings['defaultMaxMembers']);
282
283
	// Send the data to the template.
284
	$context['start'] = $_REQUEST['start'] + 1;
285
	$context['end'] = min($_REQUEST['start'] + $modSettings['defaultMaxMembers'], $context['num_members']);
286
287
	$context['can_moderate_forum'] = allowedTo('moderate_forum');
288
	$context['page_title'] = sprintf($txt['viewing_members'], $context['start'], $context['end']);
289
	$context['linktree'][] = array(
290
		'url' => $scripturl . '?action=mlist;sort=' . $_REQUEST['sort'] . ';start=' . $_REQUEST['start'],
291
		'name' => &$context['page_title'],
292
		'extra_after' => '(' . sprintf($txt['of_total_members'], $context['num_members']) . ')'
293
	);
294
295
	$limit = $_REQUEST['start'];
296
	$query_parameters = array(
297
		'regular_id_group' => 0,
298
		'is_activated' => 1,
299
		'sort' => $context['columns'][$_REQUEST['sort']]['sort'][$context['sort_direction']],
300
		'blank_string' => '',
301
	);
302
303
	// Using cache allows to narrow down the list to be retrieved.
304
	if ($use_cache && $_REQUEST['sort'] === 'real_name' && !isset($_REQUEST['desc']))
305
	{
306
		$first_offset = $_REQUEST['start'] - ($_REQUEST['start'] % $cache_step_size);
307
		if ($first_offset < 0)
308
			$first_offset = 0;
309
		$second_offset = ceil(($_REQUEST['start'] + $modSettings['defaultMaxMembers']) / $cache_step_size) * $cache_step_size;
310
		if ($second_offset >= $memberlist_cache['num_members'])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $memberlist_cache does not seem to be defined for all execution paths leading up to this point.
Loading history...
311
			$second_offset = $memberlist_cache['num_members'] - 1;
312
313
		$where = 'mem.real_name BETWEEN {string:real_name_low} AND {string:real_name_high}';
314
		$query_parameters['real_name_low'] = $memberlist_cache['index'][$first_offset];
315
		$query_parameters['real_name_high'] = $memberlist_cache['index'][$second_offset];
316
		$limit -= $first_offset;
317
	}
318
319
	// Reverse sorting is a bit more complicated...
320
	elseif ($use_cache && $_REQUEST['sort'] === 'real_name')
321
	{
322
		$first_offset = floor(($memberlist_cache['num_members'] - $modSettings['defaultMaxMembers'] - $_REQUEST['start']) / $cache_step_size) * $cache_step_size;
323
		if ($first_offset < 0)
324
			$first_offset = 0;
325
		$second_offset = ceil(($memberlist_cache['num_members'] - $_REQUEST['start']) / $cache_step_size) * $cache_step_size;
326
		if ($second_offset >= $memberlist_cache['num_members'])
327
			$second_offset = $memberlist_cache['num_members'] - 1;
328
329
		$where = 'mem.real_name BETWEEN {string:real_name_low} AND {string:real_name_high}';
330
		$query_parameters['real_name_low'] = $memberlist_cache['index'][$first_offset];
331
		$query_parameters['real_name_high'] = $memberlist_cache['index'][$second_offset];
332
		$limit = $second_offset - ($memberlist_cache['num_members'] - $_REQUEST['start']) - ($second_offset > $memberlist_cache['num_members'] ? $cache_step_size - ($memberlist_cache['num_members'] % $cache_step_size) : 0);
333
	}
334
335
	$custom_fields_qry = '';
336
	if (!empty($context['custom_profile_fields']['join'][$_REQUEST['sort']]))
337
		$custom_fields_qry = $context['custom_profile_fields']['join'][$_REQUEST['sort']];
338
339
	// Select the members from the database.
340
	$request = $smcFunc['db_query']('', '
341
		SELECT mem.id_member
342
		FROM {db_prefix}members AS mem' . ($_REQUEST['sort'] === 'is_online' ? '
343
			LEFT JOIN {db_prefix}log_online AS lo ON (lo.id_member = mem.id_member)' : '') . ($_REQUEST['sort'] === 'id_group' ? '
344
			LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = CASE WHEN mem.id_group = {int:regular_id_group} THEN mem.id_post_group ELSE mem.id_group END)' : '') . '
345
			' . $custom_fields_qry . '
346
		WHERE mem.is_activated = {int:is_activated}' . (empty($where) ? '' : '
347
			AND ' . $where) . '
348
		ORDER BY {raw:sort}
349
		LIMIT {int:start}, {int:max}',
350
		array_merge($query_parameters, array(
351
			'sort' => $query_parameters['sort'],
352
			'start' => $limit,
353
			'max' => $modSettings['defaultMaxMembers'],
354
		))
355
	);
356
	printMemberListRows($request);
357
	$smcFunc['db_free_result']($request);
358
359
	// Add anchors at the start of each letter.
360
	if ($_REQUEST['sort'] == 'real_name')
361
	{
362
		$last_letter = '';
363
		foreach ($context['members'] as $i => $dummy)
364
		{
365
			$this_letter = $smcFunc['strtolower']($smcFunc['substr']($context['members'][$i]['name'], 0, 1));
366
367
			if ($this_letter != $last_letter && preg_match('~[a-z]~', $this_letter) === 1)
368
			{
369
				$context['members'][$i]['sort_letter'] = $smcFunc['htmlspecialchars']($this_letter);
370
				$last_letter = $this_letter;
371
			}
372
		}
373
	}
374
}
375
376
/**
377
 * Search for members, or display search results.
378
 * - Called by MemberList().
379
 * - If variable 'search' is empty displays search dialog box, using the search sub template.
380
 * - Calls printMemberListRows to retrieve the results of the query.
381
 */
382
function MLSearch()
383
{
384
	global $txt, $scripturl, $context, $modSettings, $smcFunc;
385
386
	$context['page_title'] = $txt['mlist_search'];
387
	$context['can_moderate_forum'] = allowedTo('moderate_forum');
388
389
	// Can they search custom fields?
390
	$request = $smcFunc['db_query']('', '
391
		SELECT col_name, field_name, field_desc
392
		FROM {db_prefix}custom_fields
393
		WHERE active = {int:active}
394
			' . (allowedTo('admin_forum') ? '' : ' AND private < {int:private_level}') . '
395
			AND can_search = {int:can_search}
396
			AND (field_type = {string:field_type_text} OR field_type = {string:field_type_textarea} OR field_type = {string:field_type_select})',
397
		array(
398
			'active' => 1,
399
			'can_search' => 1,
400
			'private_level' => 2,
401
			'field_type_text' => 'text',
402
			'field_type_textarea' => 'textarea',
403
			'field_type_select' => 'select',
404
		)
405
	);
406
	$context['custom_search_fields'] = array();
407
	while ($row = $smcFunc['db_fetch_assoc']($request))
408
		$context['custom_search_fields'][$row['col_name']] = array(
409
			'colname' => $row['col_name'],
410
			'name' => $row['field_name'],
411
			'desc' => $row['field_desc'],
412
		);
413
	$smcFunc['db_free_result']($request);
414
415
	// They're searching..
416
	if (isset($_REQUEST['search']) && isset($_REQUEST['fields']))
417
	{
418
		$_POST['search'] = trim(isset($_GET['search']) ? $_GET['search'] : $_POST['search']);
419
		$_POST['fields'] = isset($_GET['fields']) ? explode(',', $_GET['fields']) : $_POST['fields'];
420
421
		$context['old_search'] = $_REQUEST['search'];
422
		$context['old_search_value'] = urlencode($_REQUEST['search']);
423
424
		// No fields?  Use default...
425
		if (empty($_POST['fields']))
426
			$_POST['fields'] = array('name');
427
428
		// Set defaults for how the results are sorted
429
		if (!isset($_REQUEST['sort']) || !isset($context['columns'][$_REQUEST['sort']]))
430
			$_REQUEST['sort'] = 'real_name';
431
432
		// Build the column link / sort information.
433
		foreach ($context['columns'] as $col => $column_details)
434
		{
435
			$context['columns'][$col]['href'] = $scripturl . '?action=mlist;sa=search;start=' . (int) $_REQUEST['start'] . ';sort=' . $col;
436
437
			if ((!isset($_REQUEST['desc']) && $col == $_REQUEST['sort']) || ($col != $_REQUEST['sort'] && !empty($column_details['default_sort_rev'])))
438
				$context['columns'][$col]['href'] .= ';desc';
439
440
			if (isset($_POST['search']) && isset($_POST['fields']))
441
				$context['columns'][$col]['href'] .= ';search=' . $_POST['search'] . ';fields=' . implode(',', $_POST['fields']);
442
443
			$context['columns'][$col]['link'] = '<a href="' . $context['columns'][$col]['href'] . '" rel="nofollow">' . $context['columns'][$col]['label'] . '</a>';
444
			$context['columns'][$col]['selected'] = $_REQUEST['sort'] == $col;
445
		}
446
447
		// set up some things for use in the template
448
		$context['sort_direction'] = !isset($_REQUEST['desc']) ? 'up' : 'down';
449
		$context['sort_by'] = $_REQUEST['sort'];
450
451
		$query_parameters = array(
452
			'regular_id_group' => 0,
453
			'is_activated' => 1,
454
			'blank_string' => '',
455
			'search' => '%' . strtr($smcFunc['htmlspecialchars']($_POST['search'], ENT_QUOTES), array('_' => '\\_', '%' => '\\%', '*' => '%')) . '%',
456
			'sort' => $context['columns'][$_REQUEST['sort']]['sort'][$context['sort_direction']],
457
		);
458
459
		// Search for a name
460
		if (in_array('name', $_POST['fields']))
461
		{
462
			$fields = allowedTo('moderate_forum') ? array('member_name', 'real_name') : array('real_name');
463
			$search_fields[] = 'name';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$search_fields was never initialized. Although not strictly required by PHP, it is generally a good practice to add $search_fields = array(); before regardless.
Loading history...
464
		}
465
		else
466
		{
467
			$fields = array();
468
			$search_fields = array();
469
		}
470
471
		// Search for websites.
472
		if (in_array('website', $_POST['fields']))
473
		{
474
			$fields += array(7 => 'website_title', 'website_url');
475
			$search_fields[] = 'website';
476
		}
477
		// Search for groups.
478
		if (in_array('group', $_POST['fields']))
479
		{
480
			$fields += array(9 => 'COALESCE(group_name, {string:blank_string})');
481
			$search_fields[] = 'group';
482
		}
483
		// Search for an email address?
484
		if (in_array('email', $_POST['fields']) && allowedTo('moderate_forum'))
485
		{
486
			$fields += array(2 => 'email_address');
487
			$search_fields[] = 'email';
488
		}
489
490
		if ($smcFunc['db_case_sensitive'])
491
			foreach ($fields as $key => $field)
492
				$fields[$key] = 'LOWER(' . $field . ')';
493
494
		$customJoin = array();
495
		$customCount = 10;
496
497
		// Any custom fields to search for - these being tricky?
498
		foreach ($_POST['fields'] as $field)
499
		{
500
			$row['col_name'] = substr($field, 5);
501
			if (substr($field, 0, 5) == 'cust_' && isset($context['custom_search_fields'][$row['col_name']]))
502
			{
503
				$customJoin[] = 'LEFT JOIN {db_prefix}themes AS t' . $row['col_name'] . ' ON (t' . $row['col_name'] . '.variable = {string:t' . $row['col_name'] . '} AND t' . $row['col_name'] . '.id_theme = 1 AND t' . $row['col_name'] . '.id_member = mem.id_member)';
504
				$query_parameters['t' . $row['col_name']] = $row['col_name'];
505
				$fields += array($customCount++ => 'COALESCE(t' . $row['col_name'] . '.value, {string:blank_string})');
506
				$search_fields[] = $field;
507
			}
508
		}
509
510
		// No search fields? That means you're trying to hack things
511
		if (empty($search_fields))
512
			fatal_lang_error('invalid_search_string', false);
513
514
		$query = $_POST['search'] == '' ? '= {string:blank_string}' : ($smcFunc['db_case_sensitive'] ? 'LIKE LOWER({string:search})' : 'LIKE {string:search}');
515
516
		$request = $smcFunc['db_query']('', '
517
			SELECT COUNT(*)
518
			FROM {db_prefix}members AS mem
519
				LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = CASE WHEN mem.id_group = {int:regular_id_group} THEN mem.id_post_group ELSE mem.id_group END)' .
520
				(empty($customJoin) ? '' : implode('
521
				', $customJoin)) . '
522
			WHERE (' . implode(' ' . $query . ' OR ', $fields) . ' ' . $query . ')
523
				AND mem.is_activated = {int:is_activated}',
524
			$query_parameters
525
		);
526
		list ($numResults) = $smcFunc['db_fetch_row']($request);
527
		$smcFunc['db_free_result']($request);
528
529
		$context['page_index'] = constructPageIndex($scripturl . '?action=mlist;sa=search;search=' . $_POST['search'] . ';fields=' . implode(',', $_POST['fields']), $_REQUEST['start'], $numResults, $modSettings['defaultMaxMembers']);
530
531
		$custom_fields_qry = '';
532
		if (array_search('cust_' . $_REQUEST['sort'], $_POST['fields']) === false && !empty($context['custom_profile_fields']['join'][$_REQUEST['sort']]))
533
			$custom_fields_qry = $context['custom_profile_fields']['join'][$_REQUEST['sort']];
534
535
		// Find the members from the database.
536
		$request = $smcFunc['db_query']('', '
537
			SELECT mem.id_member
538
			FROM {db_prefix}members AS mem
539
				LEFT JOIN {db_prefix}log_online AS lo ON (lo.id_member = mem.id_member)
540
				LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = CASE WHEN mem.id_group = {int:regular_id_group} THEN mem.id_post_group ELSE mem.id_group END)' .
541
				$custom_fields_qry .
542
				(empty($customJoin) ? '' : implode('
543
				', $customJoin)) . '
544
			WHERE (' . implode(' ' . $query . ' OR ', $fields) . ' ' . $query . ')
545
				AND mem.is_activated = {int:is_activated}
546
			ORDER BY {raw:sort}
547
			LIMIT {int:start}, {int:max}',
548
			array_merge($query_parameters, array(
549
				'start' => $_REQUEST['start'],
550
				'max' => $modSettings['defaultMaxMembers'],
551
			))
552
		);
553
		printMemberListRows($request);
554
		$smcFunc['db_free_result']($request);
555
	}
556
	else
557
	{
558
		// These are all the possible fields.
559
		$context['search_fields'] = array(
560
			'name' => $txt['mlist_search_name'],
561
			'email' => $txt['mlist_search_email'],
562
			'website' => $txt['mlist_search_website'],
563
			'group' => $txt['mlist_search_group'],
564
		);
565
566
		// Sorry, but you can't search by email unless you can view emails
567
		if (!allowedTo('moderate_forum'))
568
		{
569
			unset($context['search_fields']['email']);
570
			$context['search_defaults'] = array('name');
571
		}
572
		else
573
		{
574
			$context['search_defaults'] = array('name', 'email');
575
		}
576
577
		foreach ($context['custom_search_fields'] as $field)
578
			$context['search_fields']['cust_' . $field['colname']] = sprintf($txt['mlist_search_by'], $field['name']);
579
580
		$context['sub_template'] = 'search';
581
		$context['old_search'] = isset($_GET['search']) ? $_GET['search'] : (isset($_POST['search']) ? $smcFunc['htmlspecialchars']($_POST['search']) : '');
582
583
		// Since we're nice we also want to default focus on to the search field.
584
		addInlineJavaScript('
585
	$(\'input[name="search"]\').focus();', true);
586
	}
587
588
	$context['linktree'][] = array(
589
		'url' => $scripturl . '?action=mlist;sa=search',
590
		'name' => &$context['page_title']
591
	);
592
593
	// Highlight the correct button, too!
594
	unset($context['memberlist_buttons']['view_all_members']['active']);
595
	$context['memberlist_buttons']['mlist_search']['active'] = true;
596
}
597
598
/**
599
 * Retrieves results of the request passed to it
600
 * Puts results of request into the context for the sub template.
601
 *
602
 * @param resource $request An SQL result resource
603
 */
604
function printMemberListRows($request)
605
{
606
	global $context, $memberContext, $smcFunc, $txt;
607
	global $scripturl, $settings;
608
609
	// Get the most posts.
610
	$result = $smcFunc['db_query']('', '
611
		SELECT MAX(posts)
612
		FROM {db_prefix}members',
613
		array(
614
		)
615
	);
616
	list ($most_posts) = $smcFunc['db_fetch_row']($result);
617
	$smcFunc['db_free_result']($result);
618
619
	// Avoid division by zero...
620
	if ($most_posts == 0)
621
		$most_posts = 1;
622
623
	$members = array();
624
	while ($row = $smcFunc['db_fetch_assoc']($request))
625
		$members[] = $row['id_member'];
626
627
	// Load all the members for display.
628
	loadMemberData($members);
629
630
	$context['members'] = array();
631
	foreach ($members as $member)
632
	{
633
		if (!loadMemberContext($member))
634
			continue;
635
636
		$context['members'][$member] = $memberContext[$member];
637
		$context['members'][$member]['post_percent'] = round(($context['members'][$member]['real_posts'] * 100) / $most_posts);
638
		$context['members'][$member]['registered_date'] = strftime('%Y-%m-%d', $context['members'][$member]['registered_timestamp']);
639
640
		if (!empty($context['custom_profile_fields']['columns']))
641
		{
642
			foreach ($context['custom_profile_fields']['columns'] as $key => $column)
643
			{
644
				// Don't show anything if there isn't anything to show.
645
				if (!isset($context['members'][$member]['options'][$key]))
646
				{
647
					$context['members'][$member]['options'][$key] = isset($column['default_value']) ? $column['default_value'] : '';
648
					continue;
649
				}
650
651
				$currentKey = 0;
652
				if (!empty($column['options']))
653
				{
654
					$fieldOptions = explode(',', $column['options']);
655
					foreach ($fieldOptions as $k => $v)
656
					{
657
						if (empty($currentKey))
658
							$currentKey = $v === $context['members'][$member]['options'][$key] ? $k : 0;
659
					}
660
				}
661
662
				if ($column['bbc'] && !empty($context['members'][$member]['options'][$key]))
663
					$context['members'][$member]['options'][$key] = strip_tags(parse_bbc($context['members'][$member]['options'][$key]));
664
665
				elseif ($column['type'] == 'check')
666
					$context['members'][$member]['options'][$key] = $context['members'][$member]['options'][$key] == 0 ? $txt['no'] : $txt['yes'];
667
668
				// Enclosing the user input within some other text?
669
				if (!empty($column['enclose']))
670
					$context['members'][$member]['options'][$key] = strtr($column['enclose'], array(
671
						'{SCRIPTURL}' => $scripturl,
672
						'{IMAGES_URL}' => $settings['images_url'],
673
						'{DEFAULT_IMAGES_URL}' => $settings['default_images_url'],
674
						'{INPUT}' => $context['members'][$member]['options'][$key],
675
						'{KEY}' => $currentKey
676
					));
677
			}
678
		}
679
	}
680
}
681
682
/**
683
 * Sets the label, sort and join info for every custom field column.
684
 *
685
 * @return array An array of info about the custom fields for the member list
686
 */
687
function getCustFieldsMList()
688
{
689
	global $smcFunc;
690
691
	$cpf = array();
692
693
	$request = $smcFunc['db_query']('', '
694
		SELECT col_name, field_name, field_desc, field_type, field_options, bbc, enclose, default_value
695
		FROM {db_prefix}custom_fields
696
		WHERE active = {int:active}
697
			AND show_mlist = {int:show}
698
			AND private < {int:private_level}',
699
		array(
700
			'active' => 1,
701
			'show' => 1,
702
			'private_level' => 2,
703
		)
704
	);
705
706
	while ($row = $smcFunc['db_fetch_assoc']($request))
707
	{
708
		// Get all the data we're gonna need.
709
		$cpf['columns'][$row['col_name']] = array(
710
			'label' => $row['field_name'],
711
			'type' => $row['field_type'],
712
			'options' => $row['field_options'],
713
			'bbc' => !empty($row['bbc']),
714
			'enclose' => $row['enclose'],
715
			'default_value' => $row['default_value'],
716
		);
717
718
		// Get the right sort method depending on the cust field type.
719
		if ($row['field_type'] != 'check')
720
			$cpf['columns'][$row['col_name']]['sort'] = array(
721
				'down' => 'LENGTH(t' . $row['col_name'] . '.value) > 0 ASC, COALESCE(t' . $row['col_name'] . '.value, \'\') DESC',
722
				'up' => 'LENGTH(t' . $row['col_name'] . '.value) > 0 DESC, COALESCE(t' . $row['col_name'] . '.value, \'\') ASC'
723
			);
724
725
		else
726
			$cpf['columns'][$row['col_name']]['sort'] = array(
727
				'down' => 't' . $row['col_name'] . '.value DESC',
728
				'up' => 't' . $row['col_name'] . '.value ASC'
729
			);
730
731
		$cpf['join'][$row['col_name']] = 'LEFT JOIN {db_prefix}themes AS t' . $row['col_name'] . ' ON (t' . $row['col_name'] . '.variable = {literal:' . $row['col_name'] . '} AND t' . $row['col_name'] . '.id_theme = 1 AND t' . $row['col_name'] . '.id_member = mem.id_member)';
732
	}
733
	$smcFunc['db_free_result']($request);
734
735
	return $cpf;
736
}
737
738
?>