2
0

Memberlist.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. <?php
  2. /**
  3. * This file contains the functions for displaying and searching in the
  4. * members list.
  5. *
  6. * Simple Machines Forum (SMF)
  7. *
  8. * @package SMF
  9. * @author Simple Machines http://www.simplemachines.org
  10. * @copyright 2014 Simple Machines and individual contributors
  11. * @license http://www.simplemachines.org/about/smf/license.php BSD
  12. *
  13. * @version 2.1 Alpha 1
  14. */
  15. if (!defined('SMF'))
  16. die('No direct access...');
  17. /**
  18. * Shows a listing of registered members.
  19. * - If a subaction is not specified, lists all registered members.
  20. * - It allows searching for members with the 'search' sub action.
  21. * - It calls MLAll or MLSearch depending on the sub action.
  22. * - Requires the view_mlist permission.
  23. * - Accessed via ?action=mlist.
  24. *
  25. * @uses Memberlist template, main sub template.
  26. */
  27. function Memberlist()
  28. {
  29. global $scripturl, $txt, $modSettings, $context;
  30. // Make sure they can view the memberlist.
  31. isAllowedTo('view_mlist');
  32. loadTemplate('Memberlist');
  33. $context['listing_by'] = !empty($_GET['sa']) ? $_GET['sa'] : 'all';
  34. // $subActions array format:
  35. // 'subaction' => array('label', 'function', 'is_selected')
  36. $subActions = array(
  37. 'all' => array($txt['view_all_members'], 'MLAll', $context['listing_by'] == 'all'),
  38. 'search' => array($txt['mlist_search'], 'MLSearch', $context['listing_by'] == 'search'),
  39. );
  40. // Set up the sort links.
  41. $context['sort_links'] = array();
  42. foreach ($subActions as $act => $text)
  43. {
  44. $context['sort_links'][] = array(
  45. 'label' => $text[0],
  46. 'action' => $act,
  47. 'selected' => $text[2],
  48. );
  49. }
  50. $context['num_members'] = $modSettings['totalMembers'];
  51. // Set up the columns...
  52. $context['columns'] = array(
  53. 'is_online' => array(
  54. 'label' => $txt['status'],
  55. 'class' => 'first_th',
  56. 'sort' => array(
  57. 'down' => allowedTo('moderate_forum') ? 'IFNULL(lo.log_time, 1) ASC, real_name ASC' : 'CASE WHEN mem.show_online THEN IFNULL(lo.log_time, 1) ELSE 1 END ASC, real_name ASC',
  58. 'up' => allowedTo('moderate_forum') ? 'IFNULL(lo.log_time, 1) DESC, real_name DESC' : 'CASE WHEN mem.show_online THEN IFNULL(lo.log_time, 1) ELSE 1 END DESC, real_name DESC'
  59. ),
  60. ),
  61. 'real_name' => array(
  62. 'label' => $txt['name'],
  63. 'sort' => array(
  64. 'down' => 'mem.real_name DESC',
  65. 'up' => 'mem.real_name ASC'
  66. ),
  67. ),
  68. 'website_url' => array(
  69. 'label' => $txt['website'],
  70. 'link_with' => 'website',
  71. 'sort' => array(
  72. 'down' => 'LENGTH(mem.website_url) > 0 ASC, IFNULL(mem.website_url, 1=1) DESC, mem.website_url DESC',
  73. 'up' => 'LENGTH(mem.website_url) > 0 DESC, IFNULL(mem.website_url, 1=1) ASC, mem.website_url ASC'
  74. ),
  75. ),
  76. 'id_group' => array(
  77. 'label' => $txt['position'],
  78. 'sort' => array(
  79. 'down' => 'IFNULL(mg.group_name, 1=1) DESC, mg.group_name DESC',
  80. 'up' => 'IFNULL(mg.group_name, 1=1) ASC, mg.group_name ASC'
  81. ),
  82. ),
  83. 'registered' => array(
  84. 'label' => $txt['date_registered'],
  85. 'sort' => array(
  86. 'down' => 'mem.date_registered DESC',
  87. 'up' => 'mem.date_registered ASC'
  88. ),
  89. ),
  90. 'posts' => array(
  91. 'label' => $txt['posts'],
  92. 'colspan' => 2,
  93. 'default_sort_rev' => true,
  94. 'sort' => array(
  95. 'down' => 'mem.posts DESC',
  96. 'up' => 'mem.posts ASC'
  97. ),
  98. )
  99. );
  100. $context['colspan'] = 0;
  101. $context['disabled_fields'] = isset($modSettings['disabled_profile_fields']) ? array_flip(explode(',', $modSettings['disabled_profile_fields'])) : array();
  102. foreach ($context['columns'] as $key => $column)
  103. {
  104. if (isset($context['disabled_fields'][$key]) || (isset($column['link_with']) && isset($context['disabled_fields'][$column['link_with']])))
  105. {
  106. unset($context['columns'][$key]);
  107. continue;
  108. }
  109. $context['colspan'] += isset($column['colspan']) ? $column['colspan'] : 1;
  110. }
  111. // Aesthetic stuff.
  112. end($context['columns']);
  113. $context['columns'][key($context['columns'])]['class'] = 'last_th';
  114. $context['linktree'][] = array(
  115. 'url' => $scripturl . '?action=mlist',
  116. 'name' => $txt['members_list']
  117. );
  118. $context['can_send_pm'] = allowedTo('pm_send');
  119. $context['can_send_email'] = allowedTo('send_email_to_members');
  120. // Build the memberlist button array.
  121. $context['memberlist_buttons'] = array(
  122. 'view_all_members' => array('text' => 'view_all_members', 'image' => 'mlist.png', 'lang' => true, 'url' => $scripturl . '?action=mlist' . ';sa=all', 'active'=> true),
  123. 'mlist_search' => array('text' => 'mlist_search', 'image' => 'mlist.png', 'lang' => true, 'url' => $scripturl . '?action=mlist' . ';sa=search'),
  124. );
  125. // Allow mods to add additional buttons here
  126. call_integration_hook('integrate_memberlist_buttons');
  127. // Jump to the sub action.
  128. if (isset($subActions[$context['listing_by']]))
  129. $subActions[$context['listing_by']][1]();
  130. else
  131. $subActions['all'][1]();
  132. }
  133. /**
  134. * List all members, page by page, with sorting.
  135. * Called from MemberList().
  136. * Can be passed a sort parameter, to order the display of members.
  137. * Calls printMemberListRows to retrieve the results of the query.
  138. */
  139. function MLAll()
  140. {
  141. global $txt, $scripturl;
  142. global $modSettings, $context, $smcFunc;
  143. // The chunk size for the cached index.
  144. $cache_step_size = 500;
  145. // Only use caching if:
  146. // 1. there are at least 2k members,
  147. // 2. the default sorting method (real_name) is being used,
  148. // 3. the page shown is high enough to make a DB filesort unprofitable.
  149. $use_cache = $modSettings['totalMembers'] > 2000 && (!isset($_REQUEST['sort']) || $_REQUEST['sort'] === 'real_name') && isset($_REQUEST['start']) && $_REQUEST['start'] > $cache_step_size;
  150. if ($use_cache)
  151. {
  152. // Maybe there's something cached already.
  153. if (!empty($modSettings['memberlist_cache']))
  154. $memberlist_cache = @unserialize($modSettings['memberlist_cache']);
  155. // The chunk size for the cached index.
  156. $cache_step_size = 500;
  157. // Only update the cache if something changed or no cache existed yet.
  158. if (empty($memberlist_cache) || empty($modSettings['memberlist_updated']) || $memberlist_cache['last_update'] < $modSettings['memberlist_updated'])
  159. {
  160. $request = $smcFunc['db_query']('', '
  161. SELECT real_name
  162. FROM {db_prefix}members
  163. WHERE is_activated = {int:is_activated}
  164. ORDER BY real_name',
  165. array(
  166. 'is_activated' => 1,
  167. )
  168. );
  169. $memberlist_cache = array(
  170. 'last_update' => time(),
  171. 'num_members' => $smcFunc['db_num_rows']($request),
  172. 'index' => array(),
  173. );
  174. for ($i = 0, $n = $smcFunc['db_num_rows']($request); $i < $n; $i += $cache_step_size)
  175. {
  176. $smcFunc['db_data_seek']($request, $i);
  177. list($memberlist_cache['index'][$i]) = $smcFunc['db_fetch_row']($request);
  178. }
  179. $smcFunc['db_data_seek']($request, $memberlist_cache['num_members'] - 1);
  180. list ($memberlist_cache['index'][$i]) = $smcFunc['db_fetch_row']($request);
  181. $smcFunc['db_free_result']($request);
  182. // Now we've got the cache...store it.
  183. updateSettings(array('memberlist_cache' => serialize($memberlist_cache)));
  184. }
  185. $context['num_members'] = $memberlist_cache['num_members'];
  186. }
  187. // Without cache we need an extra query to get the amount of members.
  188. else
  189. {
  190. $request = $smcFunc['db_query']('', '
  191. SELECT COUNT(*)
  192. FROM {db_prefix}members
  193. WHERE is_activated = {int:is_activated}',
  194. array(
  195. 'is_activated' => 1,
  196. )
  197. );
  198. list ($context['num_members']) = $smcFunc['db_fetch_row']($request);
  199. $smcFunc['db_free_result']($request);
  200. }
  201. // Set defaults for sort (real_name) and start. (0)
  202. if (!isset($_REQUEST['sort']) || !isset($context['columns'][$_REQUEST['sort']]))
  203. $_REQUEST['sort'] = 'real_name';
  204. if (!is_numeric($_REQUEST['start']))
  205. {
  206. if (preg_match('~^[^\'\\\\/]~' . ($context['utf8'] ? 'u' : ''), $smcFunc['strtolower']($_REQUEST['start']), $match) === 0)
  207. fatal_error('Hacker?', false);
  208. $_REQUEST['start'] = $match[0];
  209. $request = $smcFunc['db_query']('substring', '
  210. SELECT COUNT(*)
  211. FROM {db_prefix}members
  212. WHERE LOWER(SUBSTRING(real_name, 1, 1)) < {string:first_letter}
  213. AND is_activated = {int:is_activated}',
  214. array(
  215. 'is_activated' => 1,
  216. 'first_letter' => $_REQUEST['start'],
  217. )
  218. );
  219. list ($_REQUEST['start']) = $smcFunc['db_fetch_row']($request);
  220. $smcFunc['db_free_result']($request);
  221. }
  222. $context['letter_links'] = '';
  223. for ($i = 97; $i < 123; $i++)
  224. $context['letter_links'] .= '<a href="' . $scripturl . '?action=mlist;sa=all;start=' . chr($i) . '#letter' . chr($i) . '">' . strtoupper(chr($i)) . '</a> ';
  225. // Sort out the column information.
  226. foreach ($context['columns'] as $col => $column_details)
  227. {
  228. $context['columns'][$col]['href'] = $scripturl . '?action=mlist;sort=' . $col . ';start=0';
  229. if ((!isset($_REQUEST['desc']) && $col == $_REQUEST['sort']) || ($col != $_REQUEST['sort'] && !empty($column_details['default_sort_rev'])))
  230. $context['columns'][$col]['href'] .= ';desc';
  231. $context['columns'][$col]['link'] = '<a href="' . $context['columns'][$col]['href'] . '" rel="nofollow">' . $context['columns'][$col]['label'] . '</a>';
  232. $context['columns'][$col]['selected'] = $_REQUEST['sort'] == $col;
  233. }
  234. // Are we sorting the results
  235. $context['sort_by'] = $_REQUEST['sort'];
  236. $context['sort_direction'] = !isset($_REQUEST['desc']) ? 'up' : 'down';
  237. // Construct the page index.
  238. $context['page_index'] = constructPageIndex($scripturl . '?action=mlist;sort=' . $_REQUEST['sort'] . (isset($_REQUEST['desc']) ? ';desc' : ''), $_REQUEST['start'], $context['num_members'], $modSettings['defaultMaxMembers']);
  239. // Send the data to the template.
  240. $context['start'] = $_REQUEST['start'] + 1;
  241. $context['end'] = min($_REQUEST['start'] + $modSettings['defaultMaxMembers'], $context['num_members']);
  242. $context['can_moderate_forum'] = allowedTo('moderate_forum');
  243. $context['page_title'] = sprintf($txt['viewing_members'], $context['start'], $context['end']);
  244. $context['linktree'][] = array(
  245. 'url' => $scripturl . '?action=mlist;sort=' . $_REQUEST['sort'] . ';start=' . $_REQUEST['start'],
  246. 'name' => &$context['page_title'],
  247. 'extra_after' => '(' . sprintf($txt['of_total_members'], $context['num_members']) . ')'
  248. );
  249. $limit = $_REQUEST['start'];
  250. $query_parameters = array(
  251. 'regular_id_group' => 0,
  252. 'is_activated' => 1,
  253. 'sort' => $context['columns'][$_REQUEST['sort']]['sort'][$context['sort_direction']],
  254. );
  255. // Using cache allows to narrow down the list to be retrieved.
  256. if ($use_cache && $_REQUEST['sort'] === 'real_name' && !isset($_REQUEST['desc']))
  257. {
  258. $first_offset = $_REQUEST['start'] - ($_REQUEST['start'] % $cache_step_size);
  259. $second_offset = ceil(($_REQUEST['start'] + $modSettings['defaultMaxMembers']) / $cache_step_size) * $cache_step_size;
  260. $where = 'mem.real_name BETWEEN {string:real_name_low} AND {string:real_name_high}';
  261. $query_parameters['real_name_low'] = $memberlist_cache['index'][$first_offset];
  262. $query_parameters['real_name_high'] = $memberlist_cache['index'][$second_offset];
  263. $limit -= $first_offset;
  264. }
  265. // Reverse sorting is a bit more complicated...
  266. elseif ($use_cache && $_REQUEST['sort'] === 'real_name')
  267. {
  268. $first_offset = floor(($memberlist_cache['num_members'] - $modSettings['defaultMaxMembers'] - $_REQUEST['start']) / $cache_step_size) * $cache_step_size;
  269. if ($first_offset < 0)
  270. $first_offset = 0;
  271. $second_offset = ceil(($memberlist_cache['num_members'] - $_REQUEST['start']) / $cache_step_size) * $cache_step_size;
  272. $where = 'mem.real_name BETWEEN {string:real_name_low} AND {string:real_name_high}';
  273. $query_parameters['real_name_low'] = $memberlist_cache['index'][$first_offset];
  274. $query_parameters['real_name_high'] = $memberlist_cache['index'][$second_offset];
  275. $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);
  276. }
  277. // Select the members from the database.
  278. $request = $smcFunc['db_query']('', '
  279. SELECT mem.id_member
  280. FROM {db_prefix}members AS mem' . ($_REQUEST['sort'] === 'is_online' ? '
  281. LEFT JOIN {db_prefix}log_online AS lo ON (lo.id_member = mem.id_member)' : '') . ($_REQUEST['sort'] === 'id_group' ? '
  282. 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)' : '') . '
  283. WHERE mem.is_activated = {int:is_activated}' . (empty($where) ? '' : '
  284. AND ' . $where) . '
  285. ORDER BY {raw:sort}
  286. LIMIT ' . $limit . ', ' . $modSettings['defaultMaxMembers'],
  287. $query_parameters
  288. );
  289. printMemberListRows($request);
  290. $smcFunc['db_free_result']($request);
  291. // Add anchors at the start of each letter.
  292. if ($_REQUEST['sort'] == 'real_name')
  293. {
  294. $last_letter = '';
  295. foreach ($context['members'] as $i => $dummy)
  296. {
  297. $this_letter = $smcFunc['strtolower']($smcFunc['substr']($context['members'][$i]['name'], 0, 1));
  298. if ($this_letter != $last_letter && preg_match('~[a-z]~', $this_letter) === 1)
  299. {
  300. $context['members'][$i]['sort_letter'] = $smcFunc['htmlspecialchars']($this_letter);
  301. $last_letter = $this_letter;
  302. }
  303. }
  304. }
  305. }
  306. /**
  307. * Search for members, or display search results.
  308. * - Called by MemberList().
  309. * - If variable 'search' is empty displays search dialog box, using the search sub template.
  310. * - Calls printMemberListRows to retrieve the results of the query.
  311. */
  312. function MLSearch()
  313. {
  314. global $txt, $scripturl, $context, $user_info, $modSettings, $smcFunc;
  315. $context['page_title'] = $txt['mlist_search'];
  316. $context['can_moderate_forum'] = allowedTo('moderate_forum');
  317. // Can they search custom fields?
  318. $request = $smcFunc['db_query']('', '
  319. SELECT col_name, field_name, field_desc
  320. FROM {db_prefix}custom_fields
  321. WHERE active = {int:active}
  322. ' . (allowedTo('admin_forum') ? '' : ' AND private < {int:private_level}') . '
  323. AND can_search = {int:can_search}
  324. AND (field_type = {string:field_type_text} OR field_type = {string:field_type_textarea})',
  325. array(
  326. 'active' => 1,
  327. 'can_search' => 1,
  328. 'private_level' => 2,
  329. 'field_type_text' => 'text',
  330. 'field_type_textarea' => 'textarea',
  331. )
  332. );
  333. $context['custom_search_fields'] = array();
  334. while ($row = $smcFunc['db_fetch_assoc']($request))
  335. $context['custom_search_fields'][$row['col_name']] = array(
  336. 'colname' => $row['col_name'],
  337. 'name' => $row['field_name'],
  338. 'desc' => $row['field_desc'],
  339. );
  340. $smcFunc['db_free_result']($request);
  341. // They're searching..
  342. if (isset($_REQUEST['search']) && isset($_REQUEST['fields']))
  343. {
  344. $_POST['search'] = trim(isset($_GET['search']) ? $_GET['search'] : $_POST['search']);
  345. $_POST['fields'] = isset($_GET['fields']) ? explode(',', $_GET['fields']) : $_POST['fields'];
  346. $context['old_search'] = $_REQUEST['search'];
  347. $context['old_search_value'] = urlencode($_REQUEST['search']);
  348. // No fields? Use default...
  349. if (empty($_POST['fields']))
  350. $_POST['fields'] = array('name');
  351. // Set defaults for how the results are sorted
  352. if (!isset($_REQUEST['sort']) || !isset($context['columns'][$_REQUEST['sort']]))
  353. $_REQUEST['sort'] = 'real_name';
  354. // Build the column link / sort information.
  355. foreach ($context['columns'] as $col => $column_details)
  356. {
  357. $context['columns'][$col]['href'] = $scripturl . '?action=mlist;sa=search;start=0;sort=' . $col;
  358. if ((!isset($_REQUEST['desc']) && $col == $_REQUEST['sort']) || ($col != $_REQUEST['sort'] && !empty($column_details['default_sort_rev'])))
  359. $context['columns'][$col]['href'] .= ';desc';
  360. if (isset($_POST['search']) && isset($_POST['fields']))
  361. $context['columns'][$col]['href'] .= ';search=' . $_POST['search'] . ';fields=' . implode(',', $_POST['fields']);
  362. $context['columns'][$col]['link'] = '<a href="' . $context['columns'][$col]['href'] . '" rel="nofollow">' . $context['columns'][$col]['label'] . '</a>';
  363. $context['columns'][$col]['selected'] = $_REQUEST['sort'] == $col;
  364. }
  365. // set up some things for use in the template
  366. $context['sort_direction'] = !isset($_REQUEST['desc']) ? 'up' : 'down';
  367. $context['sort_by'] = $_REQUEST['sort'];
  368. $query_parameters = array(
  369. 'regular_id_group' => 0,
  370. 'is_activated' => 1,
  371. 'blank_string' => '',
  372. 'search' => '%' . strtr($smcFunc['htmlspecialchars']($_POST['search'], ENT_QUOTES), array('_' => '\\_', '%' => '\\%', '*' => '%')) . '%',
  373. 'sort' => $context['columns'][$_REQUEST['sort']]['sort'][$context['sort_direction']],
  374. );
  375. // Search for a name
  376. if (in_array('name', $_POST['fields']))
  377. $fields = allowedTo('moderate_forum') ? array('member_name', 'real_name') : array('real_name');
  378. else
  379. $fields = array();
  380. // Search for websites.
  381. if (in_array('website', $_POST['fields']))
  382. $fields += array(7 => 'website_title', 'website_url');
  383. // Search for groups.
  384. if (in_array('group', $_POST['fields']))
  385. $fields += array(9 => 'IFNULL(group_name, {string:blank_string})');
  386. // Search for an email address?
  387. if (in_array('email', $_POST['fields']))
  388. {
  389. $fields += array(2 => allowedTo('moderate_forum') ? 'email_address' : '(hide_email = 0 AND email_address');
  390. $condition = allowedTo('moderate_forum') ? '' : ')';
  391. }
  392. else
  393. $condition = '';
  394. if ($smcFunc['db_case_sensitive'])
  395. foreach ($fields as $key => $field)
  396. $fields[$key] = 'LOWER(' . $field . ')';
  397. $customJoin = array();
  398. $customCount = 10;
  399. // Any custom fields to search for - these being tricky?
  400. foreach ($_POST['fields'] as $field)
  401. {
  402. $curField = substr($field, 5);
  403. if (substr($field, 0, 5) == 'cust_' && isset($context['custom_search_fields'][$curField]))
  404. {
  405. $customJoin[] = 'LEFT JOIN {db_prefix}themes AS t' . $curField . ' ON (t' . $curField . '.variable = {string:t' . $curField . '} AND t' . $curField . '.id_theme = 1 AND t' . $curField . '.id_member = mem.id_member)';
  406. $query_parameters['t' . $curField] = $curField;
  407. $fields += array($customCount++ => 'IFNULL(t' . $curField . '.value, {string:blank_string})');
  408. }
  409. }
  410. $query = $_POST['search'] == '' ? '= {string:blank_string}' : ($smcFunc['db_case_sensitive'] ? 'LIKE LOWER({string:search})' : 'LIKE {string:search}');
  411. $request = $smcFunc['db_query']('', '
  412. SELECT COUNT(*)
  413. FROM {db_prefix}members AS mem
  414. 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)' .
  415. (empty($customJoin) ? '' : implode('
  416. ', $customJoin)) . '
  417. WHERE (' . implode( ' ' . $query . ' OR ', $fields) . ' ' . $query . $condition . ')
  418. AND mem.is_activated = {int:is_activated}',
  419. $query_parameters
  420. );
  421. list ($numResults) = $smcFunc['db_fetch_row']($request);
  422. $smcFunc['db_free_result']($request);
  423. $context['page_index'] = constructPageIndex($scripturl . '?action=mlist;sa=search;search=' . $_POST['search'] . ';fields=' . implode(',', $_POST['fields']), $_REQUEST['start'], $numResults, $modSettings['defaultMaxMembers']);
  424. // Find the members from the database.
  425. $request = $smcFunc['db_query']('', '
  426. SELECT mem.id_member
  427. FROM {db_prefix}members AS mem
  428. LEFT JOIN {db_prefix}log_online AS lo ON (lo.id_member = mem.id_member)
  429. 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)' .
  430. (empty($customJoin) ? '' : implode('
  431. ', $customJoin)) . '
  432. WHERE (' . implode( ' ' . $query . ' OR ', $fields) . ' ' . $query . $condition . ')
  433. AND mem.is_activated = {int:is_activated}
  434. ORDER BY {raw:sort}
  435. LIMIT ' . $_REQUEST['start'] . ', ' . $modSettings['defaultMaxMembers'],
  436. $query_parameters
  437. );
  438. printMemberListRows($request);
  439. $smcFunc['db_free_result']($request);
  440. }
  441. else
  442. {
  443. // These are all the possible fields.
  444. $context['search_fields'] = array(
  445. 'name' => $txt['mlist_search_name'],
  446. 'email' => $txt['mlist_search_email'],
  447. 'website' => $txt['mlist_search_website'],
  448. 'group' => $txt['mlist_search_group'],
  449. );
  450. foreach ($context['custom_search_fields'] as $field)
  451. $context['search_fields']['cust_' . $field['colname']] = sprintf($txt['mlist_search_by'], $field['name']);
  452. // What do we search for by default?
  453. $context['search_defaults'] = array('name', 'email');
  454. $context['sub_template'] = 'search';
  455. $context['old_search'] = isset($_GET['search']) ? $_GET['search'] : (isset($_POST['search']) ? $smcFunc['htmlspecialchars']($_POST['search']) : '');
  456. // Since we're nice we also want to default focus on to the search field.
  457. addInlineJavascript('
  458. $(\'input[name="search"]\').focus();', true);
  459. }
  460. $context['linktree'][] = array(
  461. 'url' => $scripturl . '?action=mlist;sa=search',
  462. 'name' => &$context['page_title']
  463. );
  464. // Highlight the correct button, too!
  465. unset($context['memberlist_buttons']['view_all_members']['active']);
  466. $context['memberlist_buttons']['mlist_search']['active'] = true;
  467. }
  468. /**
  469. * Retrieves results of the request passed to it
  470. * Puts results of request into the context for the sub template.
  471. *
  472. * @param resource $request
  473. */
  474. function printMemberListRows($request)
  475. {
  476. global $context, $memberContext, $smcFunc;
  477. // Get the most posts.
  478. $result = $smcFunc['db_query']('', '
  479. SELECT MAX(posts)
  480. FROM {db_prefix}members',
  481. array(
  482. )
  483. );
  484. list ($most_posts) = $smcFunc['db_fetch_row']($result);
  485. $smcFunc['db_free_result']($result);
  486. // Avoid division by zero...
  487. if ($most_posts == 0)
  488. $most_posts = 1;
  489. $members = array();
  490. while ($row = $smcFunc['db_fetch_assoc']($request))
  491. $members[] = $row['id_member'];
  492. // Load all the members for display.
  493. loadMemberData($members);
  494. $context['members'] = array();
  495. foreach ($members as $member)
  496. {
  497. if (!loadMemberContext($member))
  498. continue;
  499. $context['members'][$member] = $memberContext[$member];
  500. $context['members'][$member]['post_percent'] = round(($context['members'][$member]['real_posts'] * 100) / $most_posts);
  501. $context['members'][$member]['registered_date'] = strftime('%Y-%m-%d', $context['members'][$member]['registered_timestamp']);
  502. }
  503. }
  504. ?>