Subs-Membergroups.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. <?php
  2. /**
  3. * This file contains functions regarding manipulation of and information about membergroups.
  4. *
  5. * Simple Machines Forum (SMF)
  6. *
  7. * @package SMF
  8. * @author Simple Machines http://www.simplemachines.org
  9. * @copyright 2012 Simple Machines Forum contributors
  10. * @license http://www.simplemachines.org/about/smf/license.php BSD
  11. *
  12. * @version 2.1 Alpha 1
  13. */
  14. if (!defined('SMF'))
  15. die('Hacking attempt...');
  16. /**
  17. * Delete one of more membergroups.
  18. * Requires the manage_membergroups permission.
  19. * Returns true on success or false on failure.
  20. * Has protection against deletion of protected membergroups.
  21. * Deletes the permissions linked to the membergroup.
  22. * Takes members out of the deleted membergroups.
  23. * @param array $groups
  24. * @return bool
  25. */
  26. function deleteMembergroups($groups)
  27. {
  28. global $sourcedir, $smcFunc, $modSettings;
  29. // Make sure it's an array.
  30. if (!is_array($groups))
  31. $groups = array((int) $groups);
  32. else
  33. {
  34. $groups = array_unique($groups);
  35. // Make sure all groups are integer.
  36. foreach ($groups as $key => $value)
  37. $groups[$key] = (int) $value;
  38. }
  39. // Some groups are protected (guests, administrators, moderators, newbies).
  40. $protected_groups = array(-1, 0, 1, 3, 4);
  41. // There maybe some others as well.
  42. if (!allowedTo('admin_forum'))
  43. {
  44. $request = $smcFunc['db_query']('', '
  45. SELECT id_group
  46. FROM {db_prefix}membergroups
  47. WHERE group_type = {int:is_protected}',
  48. array(
  49. 'is_protected' => 1,
  50. )
  51. );
  52. while ($row = $smcFunc['db_fetch_assoc']($request))
  53. $protected_groups[] = $row['id_group'];
  54. $smcFunc['db_free_result']($request);
  55. }
  56. // Make sure they don't delete protected groups!
  57. $groups = array_diff($groups, array_unique($protected_groups));
  58. if (empty($groups))
  59. return false;
  60. // Log the deletion.
  61. $request = $smcFunc['db_query']('', '
  62. SELECT group_name
  63. FROM {db_prefix}membergroups
  64. WHERE id_group IN ({array_int:group_list})',
  65. array(
  66. 'group_list' => $groups,
  67. )
  68. );
  69. while ($row = $smcFunc['db_fetch_assoc']($request))
  70. logAction('delete_group', array('group' => $row['group_name']), 'admin');
  71. $smcFunc['db_free_result']($request);
  72. call_integration_hook('integrate_delete_membergroups', array($groups));
  73. // Remove the membergroups themselves.
  74. $smcFunc['db_query']('', '
  75. DELETE FROM {db_prefix}membergroups
  76. WHERE id_group IN ({array_int:group_list})',
  77. array(
  78. 'group_list' => $groups,
  79. )
  80. );
  81. // Remove the permissions of the membergroups.
  82. $smcFunc['db_query']('', '
  83. DELETE FROM {db_prefix}permissions
  84. WHERE id_group IN ({array_int:group_list})',
  85. array(
  86. 'group_list' => $groups,
  87. )
  88. );
  89. $smcFunc['db_query']('', '
  90. DELETE FROM {db_prefix}board_permissions
  91. WHERE id_group IN ({array_int:group_list})',
  92. array(
  93. 'group_list' => $groups,
  94. )
  95. );
  96. $smcFunc['db_query']('', '
  97. DELETE FROM {db_prefix}group_moderators
  98. WHERE id_group IN ({array_int:group_list})',
  99. array(
  100. 'group_list' => $groups,
  101. )
  102. );
  103. // Delete any outstanding requests.
  104. $smcFunc['db_query']('', '
  105. DELETE FROM {db_prefix}log_group_requests
  106. WHERE id_group IN ({array_int:group_list})',
  107. array(
  108. 'group_list' => $groups,
  109. )
  110. );
  111. // Update the primary groups of members.
  112. $smcFunc['db_query']('', '
  113. UPDATE {db_prefix}members
  114. SET id_group = {int:regular_group}
  115. WHERE id_group IN ({array_int:group_list})',
  116. array(
  117. 'group_list' => $groups,
  118. 'regular_group' => 0,
  119. )
  120. );
  121. // Update any inherited groups (Lose inheritance).
  122. $smcFunc['db_query']('', '
  123. UPDATE {db_prefix}membergroups
  124. SET id_parent = {int:uninherited}
  125. WHERE id_parent IN ({array_int:group_list})',
  126. array(
  127. 'group_list' => $groups,
  128. 'uninherited' => -2,
  129. )
  130. );
  131. // Update the additional groups of members.
  132. $request = $smcFunc['db_query']('', '
  133. SELECT id_member, additional_groups
  134. FROM {db_prefix}members
  135. WHERE FIND_IN_SET({raw:additional_groups_explode}, additional_groups) != 0',
  136. array(
  137. 'additional_groups_explode' => implode(', additional_groups) != 0 OR FIND_IN_SET(', $groups),
  138. )
  139. );
  140. $updates = array();
  141. while ($row = $smcFunc['db_fetch_assoc']($request))
  142. $updates[$row['additional_groups']][] = $row['id_member'];
  143. $smcFunc['db_free_result']($request);
  144. foreach ($updates as $additional_groups => $memberArray)
  145. updateMemberData($memberArray, array('additional_groups' => implode(',', array_diff(explode(',', $additional_groups), $groups))));
  146. // No boards can provide access to these membergroups anymore.
  147. $request = $smcFunc['db_query']('', '
  148. SELECT id_board, member_groups
  149. FROM {db_prefix}boards
  150. WHERE FIND_IN_SET({raw:member_groups_explode}, member_groups) != 0',
  151. array(
  152. 'member_groups_explode' => implode(', member_groups) != 0 OR FIND_IN_SET(', $groups),
  153. )
  154. );
  155. $updates = array();
  156. while ($row = $smcFunc['db_fetch_assoc']($request))
  157. $updates[$row['member_groups']][] = $row['id_board'];
  158. $smcFunc['db_free_result']($request);
  159. foreach ($updates as $member_groups => $boardArray)
  160. $smcFunc['db_query']('', '
  161. UPDATE {db_prefix}boards
  162. SET member_groups = {string:member_groups}
  163. WHERE id_board IN ({array_int:board_lists})',
  164. array(
  165. 'board_lists' => $boardArray,
  166. 'member_groups' => implode(',', array_diff(explode(',', $member_groups), $groups)),
  167. )
  168. );
  169. // Recalculate the post groups, as they likely changed.
  170. updateStats('postgroups');
  171. // Make a note of the fact that the cache may be wrong.
  172. $settings_update = array('settings_updated' => time());
  173. // Have we deleted the spider group?
  174. if (isset($modSettings['spider_group']) && in_array($modSettings['spider_group'], $groups))
  175. $settings_update['spider_group'] = 0;
  176. updateSettings($settings_update);
  177. // It was a success.
  178. return true;
  179. }
  180. /**
  181. * Remove one or more members from one or more membergroups.
  182. * Requires the manage_membergroups permission.
  183. * Function includes a protection against removing from implicit groups.
  184. * Non-admins are not able to remove members from the admin group.
  185. * @param array $members
  186. * @param array $groups = null if groups is null, the specified members are stripped from all their membergroups.
  187. * @param bool $permissionCheckDone = false
  188. * @return bool
  189. */
  190. function removeMembersFromGroups($members, $groups = null, $permissionCheckDone = false)
  191. {
  192. global $smcFunc, $user_info, $modSettings, $sourcedir;
  193. // You're getting nowhere without this permission, unless of course you are the group's moderator.
  194. if (!$permissionCheckDone)
  195. isAllowedTo('manage_membergroups');
  196. // Assume something will happen.
  197. updateSettings(array('settings_updated' => time()));
  198. // Cleaning the input.
  199. if (!is_array($members))
  200. $members = array((int) $members);
  201. else
  202. {
  203. $members = array_unique($members);
  204. // Cast the members to integer.
  205. foreach ($members as $key => $value)
  206. $members[$key] = (int) $value;
  207. }
  208. // Before we get started, let's check we won't leave the admin group empty!
  209. if ($groups === null || $groups == 1 || (is_array($groups) && in_array(1, $groups)))
  210. {
  211. $admins = array();
  212. listMembergroupMembers_Href($admins, 1);
  213. // Remove any admins if there are too many.
  214. $non_changing_admins = array_diff(array_keys($admins), $members);
  215. if (empty($non_changing_admins))
  216. $members = array_diff($members, array_keys($admins));
  217. }
  218. // Just in case.
  219. if (empty($members))
  220. return false;
  221. elseif ($groups === null)
  222. {
  223. // Wanna remove all groups from these members? That's easy.
  224. $smcFunc['db_query']('', '
  225. UPDATE {db_prefix}members
  226. SET
  227. id_group = {int:regular_member},
  228. additional_groups = {string:blank_string}
  229. WHERE id_member IN ({array_int:member_list})' . (allowedTo('admin_forum') ? '' : '
  230. AND id_group != {int:admin_group}
  231. AND FIND_IN_SET({int:admin_group}, additional_groups) = 0'),
  232. array(
  233. 'member_list' => $members,
  234. 'regular_member' => 0,
  235. 'admin_group' => 1,
  236. 'blank_string' => '',
  237. )
  238. );
  239. updateStats('postgroups', $members);
  240. // Log what just happened.
  241. foreach ($members as $member)
  242. logAction('removed_all_groups', array('member' => $member), 'admin');
  243. return true;
  244. }
  245. elseif (!is_array($groups))
  246. $groups = array((int) $groups);
  247. else
  248. {
  249. $groups = array_unique($groups);
  250. // Make sure all groups are integer.
  251. foreach ($groups as $key => $value)
  252. $groups[$key] = (int) $value;
  253. }
  254. // Fetch a list of groups members cannot be assigned to explicitely, and the group names of the ones we want.
  255. $implicitGroups = array(-1, 0, 3);
  256. $request = $smcFunc['db_query']('', '
  257. SELECT id_group, group_name, min_posts
  258. FROM {db_prefix}membergroups
  259. WHERE id_group IN ({array_int:group_list})',
  260. array(
  261. 'group_list' => $groups,
  262. )
  263. );
  264. $group_names = array();
  265. while ($row = $smcFunc['db_fetch_assoc']($request))
  266. {
  267. if ($row['min_posts'] != -1)
  268. $implicitGroups[] = $row['id_group'];
  269. else
  270. $group_names[$row['id_group']] = $row['group_name'];
  271. }
  272. $smcFunc['db_free_result']($request);
  273. // Now get rid of those groups.
  274. $groups = array_diff($groups, $implicitGroups);
  275. // Don't forget the protected groups.
  276. if (!allowedTo('admin_forum'))
  277. {
  278. $request = $smcFunc['db_query']('', '
  279. SELECT id_group
  280. FROM {db_prefix}membergroups
  281. WHERE group_type = {int:is_protected}',
  282. array(
  283. 'is_protected' => 1,
  284. )
  285. );
  286. $protected_groups = array(1);
  287. while ($row = $smcFunc['db_fetch_assoc']($request))
  288. $protected_groups[] = $row['id_group'];
  289. $smcFunc['db_free_result']($request);
  290. // If you're not an admin yourself, you can't touch protected groups!
  291. $groups = array_diff($groups, array_unique($protected_groups));
  292. }
  293. // Only continue if there are still groups and members left.
  294. if (empty($groups) || empty($members))
  295. return false;
  296. // First, reset those who have this as their primary group - this is the easy one.
  297. $log_inserts = array();
  298. $request = $smcFunc['db_query']('', '
  299. SELECT id_member, id_group
  300. FROM {db_prefix}members AS members
  301. WHERE id_group IN ({array_int:group_list})
  302. AND id_member IN ({array_int:member_list})',
  303. array(
  304. 'group_list' => $groups,
  305. 'member_list' => $members,
  306. )
  307. );
  308. while ($row = $smcFunc['db_fetch_assoc']($request))
  309. $log_inserts[] = array('group' => $group_names[$row['id_group']], 'member' => $row['id_member']);
  310. $smcFunc['db_free_result']($request);
  311. $smcFunc['db_query']('', '
  312. UPDATE {db_prefix}members
  313. SET id_group = {int:regular_member}
  314. WHERE id_group IN ({array_int:group_list})
  315. AND id_member IN ({array_int:member_list})',
  316. array(
  317. 'group_list' => $groups,
  318. 'member_list' => $members,
  319. 'regular_member' => 0,
  320. )
  321. );
  322. // Those who have it as part of their additional group must be updated the long way... sadly.
  323. $request = $smcFunc['db_query']('', '
  324. SELECT id_member, additional_groups
  325. FROM {db_prefix}members
  326. WHERE (FIND_IN_SET({raw:additional_groups_implode}, additional_groups) != 0)
  327. AND id_member IN ({array_int:member_list})
  328. LIMIT ' . count($members),
  329. array(
  330. 'member_list' => $members,
  331. 'additional_groups_implode' => implode(', additional_groups) != 0 OR FIND_IN_SET(', $groups),
  332. )
  333. );
  334. $updates = array();
  335. while ($row = $smcFunc['db_fetch_assoc']($request))
  336. {
  337. // What log entries must we make for this one, eh?
  338. foreach (explode(',', $row['additional_groups']) as $group)
  339. if (in_array($group, $groups))
  340. $log_inserts[] = array('group' => $group_names[$group], 'member' => $row['id_member']);
  341. $updates[$row['additional_groups']][] = $row['id_member'];
  342. }
  343. $smcFunc['db_free_result']($request);
  344. foreach ($updates as $additional_groups => $memberArray)
  345. $smcFunc['db_query']('', '
  346. UPDATE {db_prefix}members
  347. SET additional_groups = {string:additional_groups}
  348. WHERE id_member IN ({array_int:member_list})',
  349. array(
  350. 'member_list' => $memberArray,
  351. 'additional_groups' => implode(',', array_diff(explode(',', $additional_groups), $groups)),
  352. )
  353. );
  354. // Their post groups may have changed now...
  355. updateStats('postgroups', $members);
  356. // Do the log.
  357. if (!empty($log_inserts) && !empty($modSettings['modlog_enabled']))
  358. {
  359. require_once($sourcedir . '/Logging.php');
  360. foreach ($log_inserts as $extra)
  361. logAction('removed_from_group', $extra, 'admin');
  362. }
  363. // Mission successful.
  364. return true;
  365. }
  366. /**
  367. * Add one or more members to a membergroup
  368. *
  369. * Requires the manage_membergroups permission.
  370. * Function has protection against adding members to implicit groups.
  371. * Non-admins are not able to add members to the admin group.
  372. *
  373. * @param string|array $members
  374. * @param int $group
  375. * @param string $type = 'auto' specifies whether the group is added as primary or as additional group.
  376. * Supported types:
  377. * - only_primary - Assigns a membergroup as primary membergroup, but only
  378. * if a member has not yet a primary membergroup assigned,
  379. * unless the member is already part of the membergroup.
  380. * - only_additional - Assigns a membergroup to the additional membergroups,
  381. * unless the member is already part of the membergroup.
  382. * - force_primary - Assigns a membergroup as primary membergroup no matter
  383. * what the previous primary membergroup was.
  384. * - auto - Assigns a membergroup to the primary group if it's still
  385. * available. If not, assign it to the additional group.
  386. * @param bool $permissionCheckDone
  387. * @return bool success or failure
  388. */
  389. function addMembersToGroup($members, $group, $type = 'auto', $permissionCheckDone = false)
  390. {
  391. global $smcFunc, $user_info, $modSettings, $sourcedir;
  392. // Show your licence, but only if it hasn't been done yet.
  393. if (!$permissionCheckDone)
  394. isAllowedTo('manage_membergroups');
  395. // Make sure we don't keep old stuff cached.
  396. updateSettings(array('settings_updated' => time()));
  397. if (!is_array($members))
  398. $members = array((int) $members);
  399. else
  400. {
  401. $members = array_unique($members);
  402. // Make sure all members are integer.
  403. foreach ($members as $key => $value)
  404. $members[$key] = (int) $value;
  405. }
  406. $group = (int) $group;
  407. // Some groups just don't like explicitly having members.
  408. $implicitGroups = array(-1, 0, 3);
  409. $request = $smcFunc['db_query']('', '
  410. SELECT id_group, group_name, min_posts
  411. FROM {db_prefix}membergroups
  412. WHERE id_group = {int:current_group}',
  413. array(
  414. 'current_group' => $group,
  415. )
  416. );
  417. $group_names = array();
  418. while ($row = $smcFunc['db_fetch_assoc']($request))
  419. {
  420. if ($row['min_posts'] != -1)
  421. $implicitGroups[] = $row['id_group'];
  422. else
  423. $group_names[$row['id_group']] = $row['group_name'];
  424. }
  425. $smcFunc['db_free_result']($request);
  426. // Sorry, you can't join an implicit group.
  427. if (in_array($group, $implicitGroups) || empty($members))
  428. return false;
  429. // Only admins can add admins...
  430. if (!allowedTo('admin_forum') && $group == 1)
  431. return false;
  432. // ... and assign protected groups!
  433. elseif (!allowedTo('admin_forum'))
  434. {
  435. $request = $smcFunc['db_query']('', '
  436. SELECT group_type
  437. FROM {db_prefix}membergroups
  438. WHERE id_group = {int:current_group}
  439. LIMIT {int:limit}',
  440. array(
  441. 'current_group' => $group,
  442. 'limit' => 1,
  443. )
  444. );
  445. list ($is_protected) = $smcFunc['db_fetch_row']($request);
  446. $smcFunc['db_free_result']($request);
  447. // Is it protected?
  448. if ($is_protected == 1)
  449. return false;
  450. }
  451. // Do the actual updates.
  452. if ($type == 'only_additional')
  453. $smcFunc['db_query']('', '
  454. UPDATE {db_prefix}members
  455. SET additional_groups = CASE WHEN additional_groups = {string:blank_string} THEN {string:id_group_string} ELSE CONCAT(additional_groups, {string:id_group_string_extend}) END
  456. WHERE id_member IN ({array_int:member_list})
  457. AND id_group != {int:id_group}
  458. AND FIND_IN_SET({int:id_group}, additional_groups) = 0',
  459. array(
  460. 'member_list' => $members,
  461. 'id_group' => $group,
  462. 'id_group_string' => (string) $group,
  463. 'id_group_string_extend' => ',' . $group,
  464. 'blank_string' => '',
  465. )
  466. );
  467. elseif ($type == 'only_primary' || $type == 'force_primary')
  468. $smcFunc['db_query']('', '
  469. UPDATE {db_prefix}members
  470. SET id_group = {int:id_group}
  471. WHERE id_member IN ({array_int:member_list})' . ($type == 'force_primary' ? '' : '
  472. AND id_group = {int:regular_group}
  473. AND FIND_IN_SET({int:id_group}, additional_groups) = 0'),
  474. array(
  475. 'member_list' => $members,
  476. 'id_group' => $group,
  477. 'regular_group' => 0,
  478. )
  479. );
  480. elseif ($type == 'auto')
  481. $smcFunc['db_query']('', '
  482. UPDATE {db_prefix}members
  483. SET
  484. id_group = CASE WHEN id_group = {int:regular_group} THEN {int:id_group} ELSE id_group END,
  485. additional_groups = CASE WHEN id_group = {int:id_group} THEN additional_groups
  486. WHEN additional_groups = {string:blank_string} THEN {string:id_group_string}
  487. ELSE CONCAT(additional_groups, {string:id_group_string_extend}) END
  488. WHERE id_member IN ({array_int:member_list})
  489. AND id_group != {int:id_group}
  490. AND FIND_IN_SET({int:id_group}, additional_groups) = 0',
  491. array(
  492. 'member_list' => $members,
  493. 'regular_group' => 0,
  494. 'id_group' => $group,
  495. 'blank_string' => '',
  496. 'id_group_string' => (string) $group,
  497. 'id_group_string_extend' => ',' . $group,
  498. )
  499. );
  500. // Ack!!? What happened?
  501. else
  502. trigger_error('addMembersToGroup(): Unknown type \'' . $type . '\'', E_USER_WARNING);
  503. // Update their postgroup statistics.
  504. updateStats('postgroups', $members);
  505. // Log the data.
  506. $log_inserts = array();
  507. require_once($sourcedir . '/Logging.php');
  508. foreach ($members as $member)
  509. logAction('added_to_group', array('group' => $group_names[$group], 'member' => $member), 'admin');
  510. call_integration_hook('integrate_add_members_to_group', array($log_inserts));
  511. return true;
  512. }
  513. /**
  514. *
  515. * @param array &$members
  516. * @param int $membergroup
  517. * @param int $limit = null
  518. * @return bool
  519. */
  520. function listMembergroupMembers_Href(&$members, $membergroup, $limit = null)
  521. {
  522. global $scripturl, $txt, $smcFunc;
  523. $request = $smcFunc['db_query']('', '
  524. SELECT id_member, real_name
  525. FROM {db_prefix}members
  526. WHERE id_group = {int:id_group} OR FIND_IN_SET({int:id_group}, additional_groups) != 0' . ($limit === null ? '' : '
  527. LIMIT ' . ($limit + 1)),
  528. array(
  529. 'id_group' => $membergroup,
  530. )
  531. );
  532. $members = array();
  533. while ($row = $smcFunc['db_fetch_assoc']($request))
  534. $members[$row['id_member']] = '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>';
  535. $smcFunc['db_free_result']($request);
  536. // If there are more than $limit members, add a 'more' link.
  537. if ($limit !== null && count($members) > $limit)
  538. {
  539. array_pop($members);
  540. return true;
  541. }
  542. else
  543. return false;
  544. }
  545. // Retrieve a list of (visible) membergroups used by the cache.
  546. function cache_getMembergroupList()
  547. {
  548. global $scripturl, $smcFunc;
  549. $request = $smcFunc['db_query']('', '
  550. SELECT id_group, group_name, online_color
  551. FROM {db_prefix}membergroups
  552. WHERE min_posts = {int:min_posts}
  553. AND hidden = {int:not_hidden}
  554. AND id_group != {int:mod_group}
  555. AND online_color != {string:blank_string}
  556. ORDER BY group_name',
  557. array(
  558. 'min_posts' => -1,
  559. 'not_hidden' => 0,
  560. 'mod_group' => 3,
  561. 'blank_string' => '',
  562. )
  563. );
  564. $groupCache = array();
  565. while ($row = $smcFunc['db_fetch_assoc']($request))
  566. $groupCache[] = '<a href="' . $scripturl . '?action=groups;sa=members;group=' . $row['id_group'] . '" ' . ($row['online_color'] ? 'style="color: ' . $row['online_color'] . '"' : '') . '>' . $row['group_name'] . '</a>';
  567. $smcFunc['db_free_result']($request);
  568. return array(
  569. 'data' => $groupCache,
  570. 'expires' => time() + 3600,
  571. 'refresh_eval' => 'return $GLOBALS[\'modSettings\'][\'settings_updated\'] > ' . time() . ';',
  572. );
  573. }
  574. function list_getMembergroups($start, $items_per_page, $sort, $membergroup_type)
  575. {
  576. global $txt, $scripturl, $context, $settings, $smcFunc;
  577. $groups = array();
  578. // Get the basic group data.
  579. $request = $smcFunc['db_query']('substring_membergroups', '
  580. SELECT id_group, group_name, min_posts, online_color, icons, 0 AS num_members
  581. FROM {db_prefix}membergroups
  582. WHERE min_posts ' . ($membergroup_type === 'post_count' ? '!=' : '=') . ' -1' . (allowedTo('admin_forum') ? '' : '
  583. AND group_type != {int:is_protected}') . '
  584. ORDER BY {raw:sort}',
  585. array(
  586. 'is_protected' => 1,
  587. 'sort' => $sort,
  588. )
  589. );
  590. while ($row = $smcFunc['db_fetch_assoc']($request))
  591. $groups[$row['id_group']] = array(
  592. 'id_group' => $row['id_group'],
  593. 'group_name' => $row['group_name'],
  594. 'min_posts' => $row['min_posts'],
  595. 'online_color' => $row['online_color'],
  596. 'icons' => $row['icons'],
  597. 'num_members' => $row['num_members'],
  598. );
  599. $smcFunc['db_free_result']($request);
  600. // If we found any membergroups, get the amount of members in them.
  601. if (!empty($groups))
  602. {
  603. if ($membergroup_type === 'post_count')
  604. {
  605. $query = $smcFunc['db_query']('', '
  606. SELECT id_post_group AS id_group, COUNT(*) AS num_members
  607. FROM {db_prefix}members
  608. WHERE id_post_group IN ({array_int:group_list})
  609. GROUP BY id_post_group',
  610. array(
  611. 'group_list' => array_keys($groups),
  612. )
  613. );
  614. while ($row = $smcFunc['db_fetch_assoc']($query))
  615. $groups[$row['id_group']]['num_members'] += $row['num_members'];
  616. $smcFunc['db_free_result']($query);
  617. }
  618. else
  619. {
  620. $query = $smcFunc['db_query']('', '
  621. SELECT id_group, COUNT(*) AS num_members
  622. FROM {db_prefix}members
  623. WHERE id_group IN ({array_int:group_list})
  624. GROUP BY id_group',
  625. array(
  626. 'group_list' => array_keys($groups),
  627. )
  628. );
  629. while ($row = $smcFunc['db_fetch_assoc']($query))
  630. $groups[$row['id_group']]['num_members'] += $row['num_members'];
  631. $smcFunc['db_free_result']($query);
  632. $query = $smcFunc['db_query']('', '
  633. SELECT mg.id_group, COUNT(*) AS num_members
  634. FROM {db_prefix}membergroups AS mg
  635. INNER JOIN {db_prefix}members AS mem ON (mem.additional_groups != {string:blank_string}
  636. AND mem.id_group != mg.id_group
  637. AND FIND_IN_SET(mg.id_group, mem.additional_groups) != 0)
  638. WHERE mg.id_group IN ({array_int:group_list})
  639. GROUP BY mg.id_group',
  640. array(
  641. 'group_list' => array_keys($groups),
  642. 'blank_string' => '',
  643. )
  644. );
  645. while ($row = $smcFunc['db_fetch_assoc']($query))
  646. $groups[$row['id_group']]['num_members'] += $row['num_members'];
  647. $smcFunc['db_free_result']($query);
  648. }
  649. }
  650. // Apply manual sorting if the 'number of members' column is selected.
  651. if (substr($sort, 0, 1) == '1' || strpos($sort, ', 1') !== false)
  652. {
  653. $sort_ascending = strpos($sort, 'DESC') === false;
  654. foreach ($groups as $group)
  655. $sort_array[] = $group['id_group'] != 3 ? (int) $group['num_members'] : -1;
  656. array_multisort($sort_array, $sort_ascending ? SORT_ASC : SORT_DESC, SORT_REGULAR, $groups);
  657. }
  658. return $groups;
  659. }
  660. ?>