Subs-Membergroups.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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 2011 Simple Machines
  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;
  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(
  310. time(), 3, $user_info['id'], $user_info['ip'], 'removed_from_group',
  311. 0, 0, 0, serialize(array('group' => $group_names[$row['id_group']], 'member' => $row['id_member'])),
  312. );
  313. $smcFunc['db_free_result']($request);
  314. $smcFunc['db_query']('', '
  315. UPDATE {db_prefix}members
  316. SET id_group = {int:regular_member}
  317. WHERE id_group IN ({array_int:group_list})
  318. AND id_member IN ({array_int:member_list})',
  319. array(
  320. 'group_list' => $groups,
  321. 'member_list' => $members,
  322. 'regular_member' => 0,
  323. )
  324. );
  325. // Those who have it as part of their additional group must be updated the long way... sadly.
  326. $request = $smcFunc['db_query']('', '
  327. SELECT id_member, additional_groups
  328. FROM {db_prefix}members
  329. WHERE (FIND_IN_SET({raw:additional_groups_implode}, additional_groups) != 0)
  330. AND id_member IN ({array_int:member_list})
  331. LIMIT ' . count($members),
  332. array(
  333. 'member_list' => $members,
  334. 'additional_groups_implode' => implode(', additional_groups) != 0 OR FIND_IN_SET(', $groups),
  335. )
  336. );
  337. $updates = array();
  338. while ($row = $smcFunc['db_fetch_assoc']($request))
  339. {
  340. // What log entries must we make for this one, eh?
  341. foreach (explode(',', $row['additional_groups']) as $group)
  342. if (in_array($group, $groups))
  343. $log_inserts[] = array(
  344. time(), 3, $user_info['id'], $user_info['ip'], 'removed_from_group',
  345. 0, 0, 0, serialize(array('group' => $group_names[$group], 'member' => $row['id_member'])),
  346. );
  347. $updates[$row['additional_groups']][] = $row['id_member'];
  348. }
  349. $smcFunc['db_free_result']($request);
  350. foreach ($updates as $additional_groups => $memberArray)
  351. $smcFunc['db_query']('', '
  352. UPDATE {db_prefix}members
  353. SET additional_groups = {string:additional_groups}
  354. WHERE id_member IN ({array_int:member_list})',
  355. array(
  356. 'member_list' => $memberArray,
  357. 'additional_groups' => implode(',', array_diff(explode(',', $additional_groups), $groups)),
  358. )
  359. );
  360. // Their post groups may have changed now...
  361. updateStats('postgroups', $members);
  362. // Do the log.
  363. if (!empty($log_inserts) && !empty($modSettings['modlog_enabled']))
  364. $smcFunc['db_insert']('',
  365. '{db_prefix}log_actions',
  366. array(
  367. 'log_time' => 'int', 'id_log' => 'int', 'id_member' => 'int', 'ip' => 'string-16', 'action' => 'string',
  368. 'id_board' => 'int', 'id_topic' => 'int', 'id_msg' => 'int', 'extra' => 'string-65534',
  369. ),
  370. $log_inserts,
  371. array('id_action')
  372. );
  373. // Mission successful.
  374. return true;
  375. }
  376. /**
  377. * Add one or more members to a membergroup
  378. *
  379. * Requires the manage_membergroups permission.
  380. * Function has protection against adding members to implicit groups.
  381. * Non-admins are not able to add members to the admin group.
  382. *
  383. * @param string|array $members
  384. * @param int $group
  385. * @param string $type = 'auto' specifies whether the group is added as primary or as additional group.
  386. * Supported types:
  387. * - only_primary - Assigns a membergroup as primary membergroup, but only
  388. * if a member has not yet a primary membergroup assigned,
  389. * unless the member is already part of the membergroup.
  390. * - only_additional - Assigns a membergroup to the additional membergroups,
  391. * unless the member is already part of the membergroup.
  392. * - force_primary - Assigns a membergroup as primary membergroup no matter
  393. * what the previous primary membergroup was.
  394. * - auto - Assigns a membergroup to the primary group if it's still
  395. * available. If not, assign it to the additional group.
  396. * @param bool $permissionCheckDone
  397. * @return bool success or failure
  398. */
  399. function addMembersToGroup($members, $group, $type = 'auto', $permissionCheckDone = false)
  400. {
  401. global $smcFunc, $user_info, $modSettings;
  402. // Show your licence, but only if it hasn't been done yet.
  403. if (!$permissionCheckDone)
  404. isAllowedTo('manage_membergroups');
  405. // Make sure we don't keep old stuff cached.
  406. updateSettings(array('settings_updated' => time()));
  407. if (!is_array($members))
  408. $members = array((int) $members);
  409. else
  410. {
  411. $members = array_unique($members);
  412. // Make sure all members are integer.
  413. foreach ($members as $key => $value)
  414. $members[$key] = (int) $value;
  415. }
  416. $group = (int) $group;
  417. // Some groups just don't like explicitly having members.
  418. $implicitGroups = array(-1, 0, 3);
  419. $request = $smcFunc['db_query']('', '
  420. SELECT id_group, group_name, min_posts
  421. FROM {db_prefix}membergroups
  422. WHERE id_group = {int:current_group}',
  423. array(
  424. 'current_group' => $group,
  425. )
  426. );
  427. $group_names = array();
  428. while ($row = $smcFunc['db_fetch_assoc']($request))
  429. {
  430. if ($row['min_posts'] != -1)
  431. $implicitGroups[] = $row['id_group'];
  432. else
  433. $group_names[$row['id_group']] = $row['group_name'];
  434. }
  435. $smcFunc['db_free_result']($request);
  436. // Sorry, you can't join an implicit group.
  437. if (in_array($group, $implicitGroups) || empty($members))
  438. return false;
  439. // Only admins can add admins...
  440. if (!allowedTo('admin_forum') && $group == 1)
  441. return false;
  442. // ... and assign protected groups!
  443. elseif (!allowedTo('admin_forum'))
  444. {
  445. $request = $smcFunc['db_query']('', '
  446. SELECT group_type
  447. FROM {db_prefix}membergroups
  448. WHERE id_group = {int:current_group}
  449. LIMIT {int:limit}',
  450. array(
  451. 'current_group' => $group,
  452. 'limit' => 1,
  453. )
  454. );
  455. list ($is_protected) = $smcFunc['db_fetch_row']($request);
  456. $smcFunc['db_free_result']($request);
  457. // Is it protected?
  458. if ($is_protected == 1)
  459. return false;
  460. }
  461. // Do the actual updates.
  462. if ($type == 'only_additional')
  463. $smcFunc['db_query']('', '
  464. UPDATE {db_prefix}members
  465. 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
  466. WHERE id_member IN ({array_int:member_list})
  467. AND id_group != {int:id_group}
  468. AND FIND_IN_SET({int:id_group}, additional_groups) = 0',
  469. array(
  470. 'member_list' => $members,
  471. 'id_group' => $group,
  472. 'id_group_string' => (string) $group,
  473. 'id_group_string_extend' => ',' . $group,
  474. 'blank_string' => '',
  475. )
  476. );
  477. elseif ($type == 'only_primary' || $type == 'force_primary')
  478. $smcFunc['db_query']('', '
  479. UPDATE {db_prefix}members
  480. SET id_group = {int:id_group}
  481. WHERE id_member IN ({array_int:member_list})' . ($type == 'force_primary' ? '' : '
  482. AND id_group = {int:regular_group}
  483. AND FIND_IN_SET({int:id_group}, additional_groups) = 0'),
  484. array(
  485. 'member_list' => $members,
  486. 'id_group' => $group,
  487. 'regular_group' => 0,
  488. )
  489. );
  490. elseif ($type == 'auto')
  491. $smcFunc['db_query']('', '
  492. UPDATE {db_prefix}members
  493. SET
  494. id_group = CASE WHEN id_group = {int:regular_group} THEN {int:id_group} ELSE id_group END,
  495. additional_groups = CASE WHEN id_group = {int:id_group} THEN additional_groups
  496. WHEN additional_groups = {string:blank_string} THEN {string:id_group_string}
  497. ELSE CONCAT(additional_groups, {string:id_group_string_extend}) END
  498. WHERE id_member IN ({array_int:member_list})
  499. AND id_group != {int:id_group}
  500. AND FIND_IN_SET({int:id_group}, additional_groups) = 0',
  501. array(
  502. 'member_list' => $members,
  503. 'regular_group' => 0,
  504. 'id_group' => $group,
  505. 'blank_string' => '',
  506. 'id_group_string' => (string) $group,
  507. 'id_group_string_extend' => ',' . $group,
  508. )
  509. );
  510. // Ack!!? What happened?
  511. else
  512. trigger_error('addMembersToGroup(): Unknown type \'' . $type . '\'', E_USER_WARNING);
  513. // Update their postgroup statistics.
  514. updateStats('postgroups', $members);
  515. // Log the data.
  516. $log_inserts = array();
  517. foreach ($members as $member)
  518. $log_inserts[] = array(
  519. time(), 3, $user_info['id'], $user_info['ip'], 'added_to_group',
  520. 0, 0, 0, serialize(array('group' => $group_names[$group], 'member' => $member)),
  521. );
  522. if (!empty($log_inserts) && !empty($modSettings['modlog_enabled']))
  523. $smcFunc['db_insert']('',
  524. '{db_prefix}log_actions',
  525. array(
  526. 'log_time' => 'int', 'id_log' => 'int', 'id_member' => 'int', 'ip' => 'string-16', 'action' => 'string',
  527. 'id_board' => 'int', 'id_topic' => 'int', 'id_msg' => 'int', 'extra' => 'string-65534',
  528. ),
  529. $log_inserts,
  530. array('id_action')
  531. );
  532. call_integration_hook('integrate_add_members_to_group', array($log_inserts));
  533. return true;
  534. }
  535. /**
  536. *
  537. * @param array &$members
  538. * @param int $membergroup
  539. * @param int $limit = null
  540. * @return bool
  541. */
  542. function listMembergroupMembers_Href(&$members, $membergroup, $limit = null)
  543. {
  544. global $scripturl, $txt, $smcFunc;
  545. $request = $smcFunc['db_query']('', '
  546. SELECT id_member, real_name
  547. FROM {db_prefix}members
  548. WHERE id_group = {int:id_group} OR FIND_IN_SET({int:id_group}, additional_groups) != 0' . ($limit === null ? '' : '
  549. LIMIT ' . ($limit + 1)),
  550. array(
  551. 'id_group' => $membergroup,
  552. )
  553. );
  554. $members = array();
  555. while ($row = $smcFunc['db_fetch_assoc']($request))
  556. $members[$row['id_member']] = '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>';
  557. $smcFunc['db_free_result']($request);
  558. // If there are more than $limit members, add a 'more' link.
  559. if ($limit !== null && count($members) > $limit)
  560. {
  561. array_pop($members);
  562. return true;
  563. }
  564. else
  565. return false;
  566. }
  567. // Retrieve a list of (visible) membergroups used by the cache.
  568. function cache_getMembergroupList()
  569. {
  570. global $scripturl, $smcFunc;
  571. $request = $smcFunc['db_query']('', '
  572. SELECT id_group, group_name, online_color
  573. FROM {db_prefix}membergroups
  574. WHERE min_posts = {int:min_posts}
  575. AND hidden = {int:not_hidden}
  576. AND id_group != {int:mod_group}
  577. AND online_color != {string:blank_string}
  578. ORDER BY group_name',
  579. array(
  580. 'min_posts' => -1,
  581. 'not_hidden' => 0,
  582. 'mod_group' => 3,
  583. 'blank_string' => '',
  584. )
  585. );
  586. $groupCache = array();
  587. while ($row = $smcFunc['db_fetch_assoc']($request))
  588. $groupCache[] = '<a href="' . $scripturl . '?action=groups;sa=members;group=' . $row['id_group'] . '" ' . ($row['online_color'] ? 'style="color: ' . $row['online_color'] . '"' : '') . '>' . $row['group_name'] . '</a>';
  589. $smcFunc['db_free_result']($request);
  590. return array(
  591. 'data' => $groupCache,
  592. 'expires' => time() + 3600,
  593. 'refresh_eval' => 'return $GLOBALS[\'modSettings\'][\'settings_updated\'] > ' . time() . ';',
  594. );
  595. }
  596. function list_getMembergroups($start, $items_per_page, $sort, $membergroup_type)
  597. {
  598. global $txt, $scripturl, $context, $settings, $smcFunc;
  599. $groups = array();
  600. // Get the basic group data.
  601. $request = $smcFunc['db_query']('substring_membergroups', '
  602. SELECT id_group, group_name, min_posts, online_color, stars, 0 AS num_members
  603. FROM {db_prefix}membergroups
  604. WHERE min_posts ' . ($membergroup_type === 'post_count' ? '!=' : '=') . ' -1' . (allowedTo('admin_forum') ? '' : '
  605. AND group_type != {int:is_protected}') . '
  606. ORDER BY {raw:sort}',
  607. array(
  608. 'is_protected' => 1,
  609. 'sort' => $sort,
  610. )
  611. );
  612. while ($row = $smcFunc['db_fetch_assoc']($request))
  613. $groups[$row['id_group']] = array(
  614. 'id_group' => $row['id_group'],
  615. 'group_name' => $row['group_name'],
  616. 'min_posts' => $row['min_posts'],
  617. 'online_color' => $row['online_color'],
  618. 'stars' => $row['stars'],
  619. 'num_members' => $row['num_members'],
  620. );
  621. $smcFunc['db_free_result']($request);
  622. // If we found any membergroups, get the amount of members in them.
  623. if (!empty($groups))
  624. {
  625. if ($membergroup_type === 'post_count')
  626. {
  627. $query = $smcFunc['db_query']('', '
  628. SELECT id_post_group AS id_group, COUNT(*) AS num_members
  629. FROM {db_prefix}members
  630. WHERE id_post_group IN ({array_int:group_list})
  631. GROUP BY id_post_group',
  632. array(
  633. 'group_list' => array_keys($groups),
  634. )
  635. );
  636. while ($row = $smcFunc['db_fetch_assoc']($query))
  637. $groups[$row['id_group']]['num_members'] += $row['num_members'];
  638. $smcFunc['db_free_result']($query);
  639. }
  640. else
  641. {
  642. $query = $smcFunc['db_query']('', '
  643. SELECT id_group, COUNT(*) AS num_members
  644. FROM {db_prefix}members
  645. WHERE id_group IN ({array_int:group_list})
  646. GROUP BY id_group',
  647. array(
  648. 'group_list' => array_keys($groups),
  649. )
  650. );
  651. while ($row = $smcFunc['db_fetch_assoc']($query))
  652. $groups[$row['id_group']]['num_members'] += $row['num_members'];
  653. $smcFunc['db_free_result']($query);
  654. $query = $smcFunc['db_query']('', '
  655. SELECT mg.id_group, COUNT(*) AS num_members
  656. FROM {db_prefix}membergroups AS mg
  657. INNER JOIN {db_prefix}members AS mem ON (mem.additional_groups != {string:blank_string}
  658. AND mem.id_group != mg.id_group
  659. AND FIND_IN_SET(mg.id_group, mem.additional_groups) != 0)
  660. WHERE mg.id_group IN ({array_int:group_list})
  661. GROUP BY mg.id_group',
  662. array(
  663. 'group_list' => array_keys($groups),
  664. 'blank_string' => '',
  665. )
  666. );
  667. while ($row = $smcFunc['db_fetch_assoc']($query))
  668. $groups[$row['id_group']]['num_members'] += $row['num_members'];
  669. $smcFunc['db_free_result']($query);
  670. }
  671. }
  672. // Apply manual sorting if the 'number of members' column is selected.
  673. if (strpos($sort, '1') === 0 || strpos($sort, ', 1') !== false)
  674. {
  675. $sort_ascending = strpos($sort, 'DESC') === false;
  676. foreach ($groups as $group)
  677. $sort_array[] = $group['id_group'] != 3 ? (int) $group['num_members'] : -1;
  678. array_multisort($sort_array, $sort_ascending ? SORT_ASC : SORT_DESC, SORT_REGULAR, $groups);
  679. }
  680. return $groups;
  681. }
  682. ?>