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