Memberlist.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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['custom_profile_fields'] = getCustFieldsMList();
  101. if (!empty($context['custom_profile_fields']['columns']))
  102. $context['columns'] += $context['custom_profile_fields']['columns'];
  103. $context['colspan'] = 0;
  104. $context['disabled_fields'] = isset($modSettings['disabled_profile_fields']) ? array_flip(explode(',', $modSettings['disabled_profile_fields'])) : array();
  105. foreach ($context['columns'] as $key => $column)
  106. {
  107. if (isset($context['disabled_fields'][$key]) || (isset($column['link_with']) && isset($context['disabled_fields'][$column['link_with']])))
  108. {
  109. unset($context['columns'][$key]);
  110. continue;
  111. }
  112. $context['colspan'] += isset($column['colspan']) ? $column['colspan'] : 1;
  113. }
  114. // Aesthetic stuff.
  115. end($context['columns']);
  116. $context['columns'][key($context['columns'])]['class'] = 'last_th';
  117. $context['linktree'][] = array(
  118. 'url' => $scripturl . '?action=mlist',
  119. 'name' => $txt['members_list']
  120. );
  121. $context['can_send_pm'] = allowedTo('pm_send');
  122. $context['can_send_email'] = allowedTo('send_email_to_members');
  123. // Build the memberlist button array.
  124. $context['memberlist_buttons'] = array(
  125. 'view_all_members' => array('text' => 'view_all_members', 'image' => 'mlist.png', 'lang' => true, 'url' => $scripturl . '?action=mlist' . ';sa=all', 'active'=> true),
  126. 'mlist_search' => array('text' => 'mlist_search', 'image' => 'mlist.png', 'lang' => true, 'url' => $scripturl . '?action=mlist' . ';sa=search'),
  127. );
  128. // Allow mods to add additional buttons here
  129. call_integration_hook('integrate_memberlist_buttons');
  130. // Jump to the sub action.
  131. if (isset($subActions[$context['listing_by']]))
  132. $subActions[$context['listing_by']][1]();
  133. else
  134. $subActions['all'][1]();
  135. }
  136. /**
  137. * List all members, page by page, with sorting.
  138. * Called from MemberList().
  139. * Can be passed a sort parameter, to order the display of members.
  140. * Calls printMemberListRows to retrieve the results of the query.
  141. */
  142. function MLAll()
  143. {
  144. global $txt, $scripturl;
  145. global $modSettings, $context, $smcFunc;
  146. // The chunk size for the cached index.
  147. $cache_step_size = 500;
  148. // Only use caching if:
  149. // 1. there are at least 2k members,
  150. // 2. the default sorting method (real_name) is being used,
  151. // 3. the page shown is high enough to make a DB filesort unprofitable.
  152. $use_cache = $modSettings['totalMembers'] > 2000 && (!isset($_REQUEST['sort']) || $_REQUEST['sort'] === 'real_name') && isset($_REQUEST['start']) && $_REQUEST['start'] > $cache_step_size;
  153. if ($use_cache)
  154. {
  155. // Maybe there's something cached already.
  156. if (!empty($modSettings['memberlist_cache']))
  157. $memberlist_cache = @unserialize($modSettings['memberlist_cache']);
  158. // The chunk size for the cached index.
  159. $cache_step_size = 500;
  160. // Only update the cache if something changed or no cache existed yet.
  161. if (empty($memberlist_cache) || empty($modSettings['memberlist_updated']) || $memberlist_cache['last_update'] < $modSettings['memberlist_updated'])
  162. {
  163. $request = $smcFunc['db_query']('', '
  164. SELECT real_name
  165. FROM {db_prefix}members
  166. WHERE is_activated = {int:is_activated}
  167. ORDER BY real_name',
  168. array(
  169. 'is_activated' => 1,
  170. )
  171. );
  172. $memberlist_cache = array(
  173. 'last_update' => time(),
  174. 'num_members' => $smcFunc['db_num_rows']($request),
  175. 'index' => array(),
  176. );
  177. for ($i = 0, $n = $smcFunc['db_num_rows']($request); $i < $n; $i += $cache_step_size)
  178. {
  179. $smcFunc['db_data_seek']($request, $i);
  180. list($memberlist_cache['index'][$i]) = $smcFunc['db_fetch_row']($request);
  181. }
  182. $smcFunc['db_data_seek']($request, $memberlist_cache['num_members'] - 1);
  183. list ($memberlist_cache['index'][$i]) = $smcFunc['db_fetch_row']($request);
  184. $smcFunc['db_free_result']($request);
  185. // Now we've got the cache...store it.
  186. updateSettings(array('memberlist_cache' => serialize($memberlist_cache)));
  187. }
  188. $context['num_members'] = $memberlist_cache['num_members'];
  189. }
  190. // Without cache we need an extra query to get the amount of members.
  191. else
  192. {
  193. $request = $smcFunc['db_query']('', '
  194. SELECT COUNT(*)
  195. FROM {db_prefix}members
  196. WHERE is_activated = {int:is_activated}',
  197. array(
  198. 'is_activated' => 1,
  199. )
  200. );
  201. list ($context['num_members']) = $smcFunc['db_fetch_row']($request);
  202. $smcFunc['db_free_result']($request);
  203. }
  204. // Set defaults for sort (real_name) and start. (0)
  205. if (!isset($_REQUEST['sort']) || !isset($context['columns'][$_REQUEST['sort']]))
  206. $_REQUEST['sort'] = 'real_name';
  207. if (!is_numeric($_REQUEST['start']))
  208. {
  209. if (preg_match('~^[^\'\\\\/]~' . ($context['utf8'] ? 'u' : ''), $smcFunc['strtolower']($_REQUEST['start']), $match) === 0)
  210. fatal_error('Hacker?', false);
  211. $_REQUEST['start'] = $match[0];
  212. $request = $smcFunc['db_query']('substring', '
  213. SELECT COUNT(*)
  214. FROM {db_prefix}members
  215. WHERE LOWER(SUBSTRING(real_name, 1, 1)) < {string:first_letter}
  216. AND is_activated = {int:is_activated}',
  217. array(
  218. 'is_activated' => 1,
  219. 'first_letter' => $_REQUEST['start'],
  220. )
  221. );
  222. list ($_REQUEST['start']) = $smcFunc['db_fetch_row']($request);
  223. $smcFunc['db_free_result']($request);
  224. }
  225. $context['letter_links'] = '';
  226. for ($i = 97; $i < 123; $i++)
  227. $context['letter_links'] .= '<a href="' . $scripturl . '?action=mlist;sa=all;start=' . chr($i) . '#letter' . chr($i) . '">' . strtoupper(chr($i)) . '</a> ';
  228. // Sort out the column information.
  229. foreach ($context['columns'] as $col => $column_details)
  230. {
  231. $context['columns'][$col]['href'] = $scripturl . '?action=mlist;sort=' . $col . ';start=0';
  232. if ((!isset($_REQUEST['desc']) && $col == $_REQUEST['sort']) || ($col != $_REQUEST['sort'] && !empty($column_details['default_sort_rev'])))
  233. $context['columns'][$col]['href'] .= ';desc';
  234. $context['columns'][$col]['link'] = '<a href="' . $context['columns'][$col]['href'] . '" rel="nofollow">' . $context['columns'][$col]['label'] . '</a>';
  235. $context['columns'][$col]['selected'] = $_REQUEST['sort'] == $col;
  236. }
  237. // Are we sorting the results
  238. $context['sort_by'] = $_REQUEST['sort'];
  239. $context['sort_direction'] = !isset($_REQUEST['desc']) ? 'up' : 'down';
  240. // Construct the page index.
  241. $context['page_index'] = constructPageIndex($scripturl . '?action=mlist;sort=' . $_REQUEST['sort'] . (isset($_REQUEST['desc']) ? ';desc' : ''), $_REQUEST['start'], $context['num_members'], $modSettings['defaultMaxMembers']);
  242. // Send the data to the template.
  243. $context['start'] = $_REQUEST['start'] + 1;
  244. $context['end'] = min($_REQUEST['start'] + $modSettings['defaultMaxMembers'], $context['num_members']);
  245. $context['can_moderate_forum'] = allowedTo('moderate_forum');
  246. $context['page_title'] = sprintf($txt['viewing_members'], $context['start'], $context['end']);
  247. $context['linktree'][] = array(
  248. 'url' => $scripturl . '?action=mlist;sort=' . $_REQUEST['sort'] . ';start=' . $_REQUEST['start'],
  249. 'name' => &$context['page_title'],
  250. 'extra_after' => '(' . sprintf($txt['of_total_members'], $context['num_members']) . ')'
  251. );
  252. $limit = $_REQUEST['start'];
  253. $query_parameters = array(
  254. 'regular_id_group' => 0,
  255. 'is_activated' => 1,
  256. 'sort' => $context['columns'][$_REQUEST['sort']]['sort'][$context['sort_direction']],
  257. 'blank_string' => '',
  258. );
  259. // Using cache allows to narrow down the list to be retrieved.
  260. if ($use_cache && $_REQUEST['sort'] === 'real_name' && !isset($_REQUEST['desc']))
  261. {
  262. $first_offset = $_REQUEST['start'] - ($_REQUEST['start'] % $cache_step_size);
  263. $second_offset = ceil(($_REQUEST['start'] + $modSettings['defaultMaxMembers']) / $cache_step_size) * $cache_step_size;
  264. $where = 'mem.real_name BETWEEN {string:real_name_low} AND {string:real_name_high}';
  265. $query_parameters['real_name_low'] = $memberlist_cache['index'][$first_offset];
  266. $query_parameters['real_name_high'] = $memberlist_cache['index'][$second_offset];
  267. $limit -= $first_offset;
  268. }
  269. // Reverse sorting is a bit more complicated...
  270. elseif ($use_cache && $_REQUEST['sort'] === 'real_name')
  271. {
  272. $first_offset = floor(($memberlist_cache['num_members'] - $modSettings['defaultMaxMembers'] - $_REQUEST['start']) / $cache_step_size) * $cache_step_size;
  273. if ($first_offset < 0)
  274. $first_offset = 0;
  275. $second_offset = ceil(($memberlist_cache['num_members'] - $_REQUEST['start']) / $cache_step_size) * $cache_step_size;
  276. $where = 'mem.real_name BETWEEN {string:real_name_low} AND {string:real_name_high}';
  277. $query_parameters['real_name_low'] = $memberlist_cache['index'][$first_offset];
  278. $query_parameters['real_name_high'] = $memberlist_cache['index'][$second_offset];
  279. $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);
  280. }
  281. // Select the members from the database.
  282. $request = $smcFunc['db_query']('', '
  283. SELECT mem.id_member
  284. FROM {db_prefix}members AS mem' . ($_REQUEST['sort'] === 'is_online' ? '
  285. LEFT JOIN {db_prefix}log_online AS lo ON (lo.id_member = mem.id_member)' : '') . ($_REQUEST['sort'] === 'id_group' ? '
  286. 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)' : '') . '
  287. ' . (!empty($context['custom_profile_fields']['join']) ? implode(' ', $context['custom_profile_fields']['join']) : '') . '
  288. WHERE mem.is_activated = {int:is_activated}' . (empty($where) ? '' : '
  289. AND ' . $where) . '
  290. ORDER BY {raw:sort}
  291. LIMIT ' . $limit . ', ' . $modSettings['defaultMaxMembers'],
  292. $query_parameters
  293. );
  294. printMemberListRows($request);
  295. $smcFunc['db_free_result']($request);
  296. // Add anchors at the start of each letter.
  297. if ($_REQUEST['sort'] == 'real_name')
  298. {
  299. $last_letter = '';
  300. foreach ($context['members'] as $i => $dummy)
  301. {
  302. $this_letter = $smcFunc['strtolower']($smcFunc['substr']($context['members'][$i]['name'], 0, 1));
  303. if ($this_letter != $last_letter && preg_match('~[a-z]~', $this_letter) === 1)
  304. {
  305. $context['members'][$i]['sort_letter'] = $smcFunc['htmlspecialchars']($this_letter);
  306. $last_letter = $this_letter;
  307. }
  308. }
  309. }
  310. }
  311. /**
  312. * Search for members, or display search results.
  313. * - Called by MemberList().
  314. * - If variable 'search' is empty displays search dialog box, using the search sub template.
  315. * - Calls printMemberListRows to retrieve the results of the query.
  316. */
  317. function MLSearch()
  318. {
  319. global $txt, $scripturl, $context, $user_info, $modSettings, $smcFunc;
  320. $context['page_title'] = $txt['mlist_search'];
  321. $context['can_moderate_forum'] = allowedTo('moderate_forum');
  322. // Can they search custom fields?
  323. $request = $smcFunc['db_query']('', '
  324. SELECT col_name, field_name, field_desc
  325. FROM {db_prefix}custom_fields
  326. WHERE active = {int:active}
  327. ' . (allowedTo('admin_forum') ? '' : ' AND private < {int:private_level}') . '
  328. AND can_search = {int:can_search}
  329. AND (field_type = {string:field_type_text} OR field_type = {string:field_type_textarea})',
  330. array(
  331. 'active' => 1,
  332. 'can_search' => 1,
  333. 'private_level' => 2,
  334. 'field_type_text' => 'text',
  335. 'field_type_textarea' => 'textarea',
  336. )
  337. );
  338. $context['custom_search_fields'] = array();
  339. while ($row = $smcFunc['db_fetch_assoc']($request))
  340. $context['custom_search_fields'][$row['col_name']] = array(
  341. 'colname' => $row['col_name'],
  342. 'name' => $row['field_name'],
  343. 'desc' => $row['field_desc'],
  344. );
  345. $smcFunc['db_free_result']($request);
  346. // They're searching..
  347. if (isset($_REQUEST['search']) && isset($_REQUEST['fields']))
  348. {
  349. $_POST['search'] = trim(isset($_GET['search']) ? $_GET['search'] : $_POST['search']);
  350. $_POST['fields'] = isset($_GET['fields']) ? explode(',', $_GET['fields']) : $_POST['fields'];
  351. $context['old_search'] = $_REQUEST['search'];
  352. $context['old_search_value'] = urlencode($_REQUEST['search']);
  353. // No fields? Use default...
  354. if (empty($_POST['fields']))
  355. $_POST['fields'] = array('name');
  356. // Set defaults for how the results are sorted
  357. if (!isset($_REQUEST['sort']) || !isset($context['columns'][$_REQUEST['sort']]))
  358. $_REQUEST['sort'] = 'real_name';
  359. // Build the column link / sort information.
  360. foreach ($context['columns'] as $col => $column_details)
  361. {
  362. $context['columns'][$col]['href'] = $scripturl . '?action=mlist;sa=search;start=0;sort=' . $col;
  363. if ((!isset($_REQUEST['desc']) && $col == $_REQUEST['sort']) || ($col != $_REQUEST['sort'] && !empty($column_details['default_sort_rev'])))
  364. $context['columns'][$col]['href'] .= ';desc';
  365. if (isset($_POST['search']) && isset($_POST['fields']))
  366. $context['columns'][$col]['href'] .= ';search=' . $_POST['search'] . ';fields=' . implode(',', $_POST['fields']);
  367. $context['columns'][$col]['link'] = '<a href="' . $context['columns'][$col]['href'] . '" rel="nofollow">' . $context['columns'][$col]['label'] . '</a>';
  368. $context['columns'][$col]['selected'] = $_REQUEST['sort'] == $col;
  369. }
  370. // set up some things for use in the template
  371. $context['sort_direction'] = !isset($_REQUEST['desc']) ? 'up' : 'down';
  372. $context['sort_by'] = $_REQUEST['sort'];
  373. $query_parameters = array(
  374. 'regular_id_group' => 0,
  375. 'is_activated' => 1,
  376. 'blank_string' => '',
  377. 'search' => '%' . strtr($smcFunc['htmlspecialchars']($_POST['search'], ENT_QUOTES), array('_' => '\\_', '%' => '\\%', '*' => '%')) . '%',
  378. 'sort' => $context['columns'][$_REQUEST['sort']]['sort'][$context['sort_direction']],
  379. );
  380. // Search for a name
  381. if (in_array('name', $_POST['fields']))
  382. $fields = allowedTo('moderate_forum') ? array('member_name', 'real_name') : array('real_name');
  383. else
  384. $fields = array();
  385. // Search for websites.
  386. if (in_array('website', $_POST['fields']))
  387. $fields += array(7 => 'website_title', 'website_url');
  388. // Search for groups.
  389. if (in_array('group', $_POST['fields']))
  390. $fields += array(9 => 'IFNULL(group_name, {string:blank_string})');
  391. // Search for an email address?
  392. if (in_array('email', $_POST['fields']))
  393. {
  394. $fields += array(2 => allowedTo('moderate_forum') ? 'email_address' : '(hide_email = 0 AND email_address');
  395. $condition = allowedTo('moderate_forum') ? '' : ')';
  396. }
  397. else
  398. $condition = '';
  399. if ($smcFunc['db_case_sensitive'])
  400. foreach ($fields as $key => $field)
  401. $fields[$key] = 'LOWER(' . $field . ')';
  402. $customJoin = array();
  403. $customCount = 10;
  404. // Any custom fields to search for - these being tricky?
  405. foreach ($_POST['fields'] as $field)
  406. {
  407. $row['col_name'] = substr($field, 5);
  408. if (substr($field, 0, 5) == 'cust_' && isset($context['custom_search_fields'][$row['col_name']]))
  409. {
  410. $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)';
  411. $query_parameters['t' . $row['col_name']] = $row['col_name'];
  412. $fields += array($customCount++ => 'IFNULL(t' . $row['col_name'] . '.value, {string:blank_string})');
  413. }
  414. }
  415. $query = $_POST['search'] == '' ? '= {string:blank_string}' : ($smcFunc['db_case_sensitive'] ? 'LIKE LOWER({string:search})' : 'LIKE {string:search}');
  416. $request = $smcFunc['db_query']('', '
  417. SELECT COUNT(*)
  418. FROM {db_prefix}members AS mem
  419. 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)' .
  420. (empty($customJoin) ? '' : implode('
  421. ', $customJoin)) . '
  422. WHERE (' . implode( ' ' . $query . ' OR ', $fields) . ' ' . $query . $condition . ')
  423. AND mem.is_activated = {int:is_activated}',
  424. $query_parameters
  425. );
  426. list ($numResults) = $smcFunc['db_fetch_row']($request);
  427. $smcFunc['db_free_result']($request);
  428. $context['page_index'] = constructPageIndex($scripturl . '?action=mlist;sa=search;search=' . $_POST['search'] . ';fields=' . implode(',', $_POST['fields']), $_REQUEST['start'], $numResults, $modSettings['defaultMaxMembers']);
  429. // Find the members from the database.
  430. $request = $smcFunc['db_query']('', '
  431. SELECT mem.id_member
  432. FROM {db_prefix}members AS mem
  433. LEFT JOIN {db_prefix}log_online AS lo ON (lo.id_member = mem.id_member)
  434. 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)' .
  435. (empty($customJoin) ? '' : implode('
  436. ', $customJoin)) . '
  437. WHERE (' . implode( ' ' . $query . ' OR ', $fields) . ' ' . $query . $condition . ')
  438. AND mem.is_activated = {int:is_activated}
  439. ORDER BY {raw:sort}
  440. LIMIT ' . $_REQUEST['start'] . ', ' . $modSettings['defaultMaxMembers'],
  441. $query_parameters
  442. );
  443. printMemberListRows($request);
  444. $smcFunc['db_free_result']($request);
  445. }
  446. else
  447. {
  448. // These are all the possible fields.
  449. $context['search_fields'] = array(
  450. 'name' => $txt['mlist_search_name'],
  451. 'email' => $txt['mlist_search_email'],
  452. 'website' => $txt['mlist_search_website'],
  453. 'group' => $txt['mlist_search_group'],
  454. );
  455. foreach ($context['custom_search_fields'] as $field)
  456. $context['search_fields']['cust_' . $field['colname']] = sprintf($txt['mlist_search_by'], $field['name']);
  457. // What do we search for by default?
  458. $context['search_defaults'] = array('name', 'email');
  459. $context['sub_template'] = 'search';
  460. $context['old_search'] = isset($_GET['search']) ? $_GET['search'] : (isset($_POST['search']) ? $smcFunc['htmlspecialchars']($_POST['search']) : '');
  461. // Since we're nice we also want to default focus on to the search field.
  462. addInlineJavascript('
  463. $(\'input[name="search"]\').focus();', true);
  464. }
  465. $context['linktree'][] = array(
  466. 'url' => $scripturl . '?action=mlist;sa=search',
  467. 'name' => &$context['page_title']
  468. );
  469. // Highlight the correct button, too!
  470. unset($context['memberlist_buttons']['view_all_members']['active']);
  471. $context['memberlist_buttons']['mlist_search']['active'] = true;
  472. }
  473. /**
  474. * Retrieves results of the request passed to it
  475. * Puts results of request into the context for the sub template.
  476. *
  477. * @param resource $request
  478. */
  479. function printMemberListRows($request)
  480. {
  481. global $context, $memberContext, $smcFunc, $txt;
  482. global $scripturl, $settings;
  483. // Get the most posts.
  484. $result = $smcFunc['db_query']('', '
  485. SELECT MAX(posts)
  486. FROM {db_prefix}members',
  487. array(
  488. )
  489. );
  490. list ($most_posts) = $smcFunc['db_fetch_row']($result);
  491. $smcFunc['db_free_result']($result);
  492. // Avoid division by zero...
  493. if ($most_posts == 0)
  494. $most_posts = 1;
  495. $members = array();
  496. while ($row = $smcFunc['db_fetch_assoc']($request))
  497. $members[] = $row['id_member'];
  498. // Load all the members for display.
  499. loadMemberData($members);
  500. $context['members'] = array();
  501. foreach ($members as $member)
  502. {
  503. if (!loadMemberContext($member))
  504. continue;
  505. $context['members'][$member] = $memberContext[$member];
  506. $context['members'][$member]['post_percent'] = round(($context['members'][$member]['real_posts'] * 100) / $most_posts);
  507. $context['members'][$member]['registered_date'] = strftime('%Y-%m-%d', $context['members'][$member]['registered_timestamp']);
  508. if (!empty($context['custom_profile_fields']['columns']))
  509. {
  510. foreach ($context['custom_profile_fields']['columns'] as $key => $column)
  511. {
  512. // Don't show anything if there isn't anything to show.
  513. if (!isset($context['members'][$member]['options'][$key]))
  514. {
  515. $context['members'][$member]['options'][$key] = '';
  516. continue;
  517. }
  518. if ($column['bbc'] && !empty($context['members'][$member]['options'][$key]))
  519. $context['members'][$member]['options'][$key] = strip_tags(parse_bbc($context['members'][$member]['options'][$key]));
  520. elseif ($column['type'] == 'check')
  521. $context['members'][$member]['options'][$key] = $context['members'][$member]['options'][$key] == 0 ? $txt['no'] : $txt['yes'];
  522. // Enclosing the user input within some other text?
  523. if (!empty($column['enclose']))
  524. $context['members'][$member]['options'][$key] = strtr($column['enclose'], array(
  525. '{SCRIPTURL}' => $scripturl,
  526. '{IMAGES_URL}' => $settings['images_url'],
  527. '{DEFAULT_IMAGES_URL}' => $settings['default_images_url'],
  528. '{INPUT}' => $context['members'][$member]['options'][$key],
  529. ));
  530. }
  531. }
  532. }
  533. }
  534. /**
  535. * Sets the label, sort and join info for every custom field column.
  536. *
  537. * @return array
  538. */
  539. function getCustFieldsMList()
  540. {
  541. global $smcFunc;
  542. $cpf = array();
  543. $request = $smcFunc['db_query']('', '
  544. SELECT col_name, field_name, field_desc, field_type, bbc, enclose
  545. FROM {db_prefix}custom_fields
  546. WHERE active = {int:active}
  547. AND show_mlist = {int:show}
  548. AND private < {int:private_level}',
  549. array(
  550. 'active' => 1,
  551. 'show' => 1,
  552. 'private_level' => 2,
  553. )
  554. );
  555. while ($row = $smcFunc['db_fetch_assoc']($request))
  556. {
  557. // Get all the data we're gonna need.
  558. $cpf['columns'][$row['col_name']] = array(
  559. 'label' => $row['field_name'],
  560. 'type' => $row['field_type'],
  561. 'bbc' => !empty($row['bbc']),
  562. 'enclose' => $row['enclose'],
  563. );
  564. // Get the right sort method depending on the cust field type.
  565. if ($row['field_type'] != 'check')
  566. $cpf['columns'][$row['col_name']]['sort'] = array(
  567. 'down' => 'LENGTH(t' . $row['col_name'] . '.value) > 0 ASC, IFNULL(t' . $row['col_name'] . '.value, "") DESC',
  568. 'up' => 'LENGTH(t' . $row['col_name'] . '.value) > 0 DESC, IFNULL(t' . $row['col_name'] . '.value, "") ASC'
  569. );
  570. else
  571. $cpf['columns'][$row['col_name']]['sort'] = array(
  572. 'down' => 't' . $row['col_name'] . '.value DESC',
  573. 'up' => 't' . $row['col_name'] . '.value ASC'
  574. );
  575. $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)';
  576. }
  577. $smcFunc['db_free_result']($request);
  578. return $cpf;
  579. }
  580. ?>