Subs-Membergroups.php 23 KB

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