Conditions | 52 |
Paths | > 20000 |
Total Lines | 256 |
Code Lines | 138 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
195 | function MembergroupMembers() |
||
196 | { |
||
197 | global $txt, $scripturl, $context, $modSettings, $sourcedir, $user_info, $settings, $smcFunc; |
||
198 | |||
199 | $_REQUEST['group'] = isset($_REQUEST['group']) ? (int) $_REQUEST['group'] : 0; |
||
200 | |||
201 | // No browsing of guests, membergroup 0 or moderators. |
||
202 | if (in_array($_REQUEST['group'], array(-1, 0, 3))) |
||
203 | fatal_lang_error('membergroup_does_not_exist', false); |
||
204 | |||
205 | // Load up the group details. |
||
206 | $request = $smcFunc['db_query']('', ' |
||
207 | SELECT id_group AS id, group_name AS name, CASE WHEN min_posts = {int:min_posts} THEN 1 ELSE 0 END AS assignable, hidden, online_color, |
||
208 | icons, description, CASE WHEN min_posts != {int:min_posts} THEN 1 ELSE 0 END AS is_post_group, group_type |
||
209 | FROM {db_prefix}membergroups |
||
210 | WHERE id_group = {int:id_group} |
||
211 | LIMIT 1', |
||
212 | array( |
||
213 | 'min_posts' => -1, |
||
214 | 'id_group' => $_REQUEST['group'], |
||
215 | ) |
||
216 | ); |
||
217 | // Doesn't exist? |
||
218 | if ($smcFunc['db_num_rows']($request) == 0) |
||
219 | fatal_lang_error('membergroup_does_not_exist', false); |
||
220 | $context['group'] = $smcFunc['db_fetch_assoc']($request); |
||
221 | $smcFunc['db_free_result']($request); |
||
222 | |||
223 | // Fix the membergroup icons. |
||
224 | $context['group']['icons'] = explode('#', $context['group']['icons']); |
||
225 | $context['group']['icons'] = !empty($context['group']['icons'][0]) && !empty($context['group']['icons'][1]) ? str_repeat('<img src="' . $settings['images_url'] . '/membericons/' . $context['group']['icons'][1] . '" alt="*">', $context['group']['icons'][0]) : ''; |
||
226 | $context['group']['can_moderate'] = allowedTo('manage_membergroups') && (allowedTo('admin_forum') || $context['group']['group_type'] != 1); |
||
227 | |||
228 | $context['linktree'][] = array( |
||
229 | 'url' => $scripturl . '?action=groups;sa=members;group=' . $context['group']['id'], |
||
230 | 'name' => $context['group']['name'], |
||
231 | ); |
||
232 | $context['can_send_email'] = allowedTo('moderate_forum'); |
||
233 | |||
234 | // Load all the group moderators, for fun. |
||
235 | $request = $smcFunc['db_query']('', ' |
||
236 | SELECT mem.id_member, mem.real_name |
||
237 | FROM {db_prefix}group_moderators AS mods |
||
238 | INNER JOIN {db_prefix}members AS mem ON (mem.id_member = mods.id_member) |
||
239 | WHERE mods.id_group = {int:id_group}', |
||
240 | array( |
||
241 | 'id_group' => $_REQUEST['group'], |
||
242 | ) |
||
243 | ); |
||
244 | $context['group']['moderators'] = array(); |
||
245 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
246 | { |
||
247 | $context['group']['moderators'][] = array( |
||
248 | 'id' => $row['id_member'], |
||
249 | 'name' => $row['real_name'] |
||
250 | ); |
||
251 | |||
252 | if ($user_info['id'] == $row['id_member'] && $context['group']['group_type'] != 1) |
||
253 | $context['group']['can_moderate'] = true; |
||
254 | } |
||
255 | $smcFunc['db_free_result']($request); |
||
256 | |||
257 | // If this group is hidden then it can only "exists" if the user can moderate it! |
||
258 | if ($context['group']['hidden'] && !$context['group']['can_moderate']) |
||
259 | fatal_lang_error('membergroup_does_not_exist', false); |
||
260 | |||
261 | // You can only assign membership if you are the moderator and/or can manage groups! |
||
262 | if (!$context['group']['can_moderate']) |
||
263 | $context['group']['assignable'] = 0; |
||
264 | // Non-admins cannot assign admins. |
||
265 | elseif ($context['group']['id'] == 1 && !allowedTo('admin_forum')) |
||
266 | $context['group']['assignable'] = 0; |
||
267 | |||
268 | // Removing member from group? |
||
269 | if (isset($_POST['remove']) && !empty($_REQUEST['rem']) && is_array($_REQUEST['rem']) && $context['group']['assignable']) |
||
270 | { |
||
271 | checkSession(); |
||
272 | validateToken('mod-mgm'); |
||
273 | |||
274 | // Only proven admins can remove admins. |
||
275 | if ($context['group']['id'] == 1) |
||
276 | validateSession(); |
||
277 | |||
278 | // Make sure we're dealing with integers only. |
||
279 | foreach ($_REQUEST['rem'] as $key => $group) |
||
280 | $_REQUEST['rem'][$key] = (int) $group; |
||
281 | |||
282 | require_once($sourcedir . '/Subs-Membergroups.php'); |
||
283 | removeMembersFromGroups($_REQUEST['rem'], $_REQUEST['group'], true); |
||
284 | } |
||
285 | // Must be adding new members to the group... |
||
286 | elseif (isset($_REQUEST['add']) && (!empty($_REQUEST['toAdd']) || !empty($_REQUEST['member_add'])) && $context['group']['assignable']) |
||
287 | { |
||
288 | // Demand an admin password before adding new admins -- every time, no matter what. |
||
289 | if ($context['group']['id'] == 1) |
||
290 | validateSession('admin', true); |
||
|
|||
291 | |||
292 | checkSession(); |
||
293 | validateToken('mod-mgm'); |
||
294 | |||
295 | $member_query = array(); |
||
296 | $member_parameters = array(); |
||
297 | |||
298 | // Get all the members to be added... taking into account names can be quoted ;) |
||
299 | $_REQUEST['toAdd'] = strtr($smcFunc['htmlspecialchars']($_REQUEST['toAdd'], ENT_QUOTES), array('"' => '"')); |
||
300 | preg_match_all('~"([^"]+)"~', $_REQUEST['toAdd'], $matches); |
||
301 | $member_names = array_unique(array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $_REQUEST['toAdd'])))); |
||
302 | |||
303 | foreach ($member_names as $index => $member_name) |
||
304 | { |
||
305 | $member_names[$index] = trim($smcFunc['strtolower']($member_names[$index])); |
||
306 | |||
307 | if (strlen($member_names[$index]) == 0) |
||
308 | unset($member_names[$index]); |
||
309 | } |
||
310 | |||
311 | // Any passed by ID? |
||
312 | $member_ids = array(); |
||
313 | if (!empty($_REQUEST['member_add'])) |
||
314 | foreach ($_REQUEST['member_add'] as $id) |
||
315 | if ($id > 0) |
||
316 | $member_ids[] = (int) $id; |
||
317 | |||
318 | // Construct the query pelements. |
||
319 | if (!empty($member_ids)) |
||
320 | { |
||
321 | $member_query[] = 'id_member IN ({array_int:member_ids})'; |
||
322 | $member_parameters['member_ids'] = $member_ids; |
||
323 | } |
||
324 | if (!empty($member_names)) |
||
325 | { |
||
326 | $member_query[] = 'LOWER(member_name) IN ({array_string:member_names})'; |
||
327 | $member_query[] = 'LOWER(real_name) IN ({array_string:member_names})'; |
||
328 | $member_parameters['member_names'] = $member_names; |
||
329 | } |
||
330 | |||
331 | $members = array(); |
||
332 | if (!empty($member_query)) |
||
333 | { |
||
334 | $request = $smcFunc['db_query']('', ' |
||
335 | SELECT id_member |
||
336 | FROM {db_prefix}members |
||
337 | WHERE (' . implode(' OR ', $member_query) . ') |
||
338 | AND id_group != {int:id_group} |
||
339 | AND FIND_IN_SET({int:id_group}, additional_groups) = 0', |
||
340 | array_merge($member_parameters, array( |
||
341 | 'id_group' => $_REQUEST['group'], |
||
342 | )) |
||
343 | ); |
||
344 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
345 | $members[] = $row['id_member']; |
||
346 | $smcFunc['db_free_result']($request); |
||
347 | } |
||
348 | |||
349 | // @todo Add $_POST['additional'] to templates! |
||
350 | |||
351 | // Do the updates... |
||
352 | if (!empty($members)) |
||
353 | { |
||
354 | require_once($sourcedir . '/Subs-Membergroups.php'); |
||
355 | addMembersToGroup($members, $_REQUEST['group'], isset($_POST['additional']) || $context['group']['hidden'] ? 'only_additional' : 'auto', true); |
||
356 | } |
||
357 | } |
||
358 | |||
359 | // Sort out the sorting! |
||
360 | $sort_methods = array( |
||
361 | 'name' => 'real_name', |
||
362 | 'email' => 'email_address', |
||
363 | 'active' => 'last_login', |
||
364 | 'registered' => 'date_registered', |
||
365 | 'posts' => 'posts', |
||
366 | ); |
||
367 | |||
368 | // They didn't pick one, default to by name.. |
||
369 | if (!isset($_REQUEST['sort']) || !isset($sort_methods[$_REQUEST['sort']])) |
||
370 | { |
||
371 | $context['sort_by'] = 'name'; |
||
372 | $querySort = 'real_name'; |
||
373 | } |
||
374 | // Otherwise default to ascending. |
||
375 | else |
||
376 | { |
||
377 | $context['sort_by'] = $_REQUEST['sort']; |
||
378 | $querySort = $sort_methods[$_REQUEST['sort']]; |
||
379 | } |
||
380 | |||
381 | $context['sort_direction'] = isset($_REQUEST['desc']) ? 'down' : 'up'; |
||
382 | |||
383 | // The where on the query is interesting. Non-moderators should only see people who are in this group as primary. |
||
384 | if ($context['group']['can_moderate']) |
||
385 | $where = $context['group']['is_post_group'] ? 'id_post_group = {int:group}' : 'id_group = {int:group} OR FIND_IN_SET({int:group}, additional_groups) != 0'; |
||
386 | else |
||
387 | $where = $context['group']['is_post_group'] ? 'id_post_group = {int:group}' : 'id_group = {int:group}'; |
||
388 | |||
389 | // Count members of the group. |
||
390 | $request = $smcFunc['db_query']('', ' |
||
391 | SELECT COUNT(*) |
||
392 | FROM {db_prefix}members |
||
393 | WHERE ' . $where, |
||
394 | array( |
||
395 | 'group' => $_REQUEST['group'], |
||
396 | ) |
||
397 | ); |
||
398 | list ($context['total_members']) = $smcFunc['db_fetch_row']($request); |
||
399 | $smcFunc['db_free_result']($request); |
||
400 | |||
401 | // Create the page index. |
||
402 | $context['page_index'] = constructPageIndex($scripturl . '?action=' . ($context['group']['can_moderate'] ? 'moderate;area=viewgroups' : 'groups') . ';sa=members;group=' . $_REQUEST['group'] . ';sort=' . $context['sort_by'] . (isset($_REQUEST['desc']) ? ';desc' : ''), $_REQUEST['start'], $context['total_members'], $modSettings['defaultMaxMembers']); |
||
403 | $context['total_members'] = comma_format($context['total_members']); |
||
404 | $context['start'] = $_REQUEST['start']; |
||
405 | $context['can_moderate_forum'] = allowedTo('moderate_forum'); |
||
406 | |||
407 | // Load up all members of this group. |
||
408 | $request = $smcFunc['db_query']('', ' |
||
409 | SELECT id_member, member_name, real_name, email_address, member_ip, date_registered, last_login, |
||
410 | posts, is_activated, real_name |
||
411 | FROM {db_prefix}members |
||
412 | WHERE ' . $where . ' |
||
413 | ORDER BY ' . $querySort . ' ' . ($context['sort_direction'] == 'down' ? 'DESC' : 'ASC') . ' |
||
414 | LIMIT {int:start}, {int:max}', |
||
415 | array( |
||
416 | 'group' => $_REQUEST['group'], |
||
417 | 'start' => $context['start'], |
||
418 | 'max' => $modSettings['defaultMaxMembers'], |
||
419 | ) |
||
420 | ); |
||
421 | $context['members'] = array(); |
||
422 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
423 | { |
||
424 | $row['member_ip'] = inet_dtop($row['member_ip']); |
||
425 | $last_online = empty($row['last_login']) ? $txt['never'] : timeformat($row['last_login']); |
||
426 | |||
427 | // Italicize the online note if they aren't activated. |
||
428 | if ($row['is_activated'] % 10 != 1) |
||
429 | $last_online = '<em title="' . $txt['not_activated'] . '">' . $last_online . '</em>'; |
||
430 | |||
431 | $context['members'][] = array( |
||
432 | 'id' => $row['id_member'], |
||
433 | 'name' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>', |
||
434 | 'email' => $row['email_address'], |
||
435 | 'ip' => '<a href="' . $scripturl . '?action=trackip;searchip=' . $row['member_ip'] . '">' . $row['member_ip'] . '</a>', |
||
436 | 'registered' => timeformat($row['date_registered']), |
||
437 | 'last_online' => $last_online, |
||
438 | 'posts' => comma_format($row['posts']), |
||
439 | 'is_activated' => $row['is_activated'] % 10 == 1, |
||
440 | ); |
||
441 | } |
||
442 | $smcFunc['db_free_result']($request); |
||
443 | |||
444 | // Select the template. |
||
445 | $context['sub_template'] = 'group_members'; |
||
446 | $context['page_title'] = $txt['membergroups_members_title'] . ': ' . $context['group']['name']; |
||
447 | createToken('mod-mgm'); |
||
448 | |||
449 | if ($context['group']['assignable']) |
||
450 | loadJavaScriptFile('suggest.js', array('defer' => false, 'minimize' => true), 'smf_suggest'); |
||
451 | } |
||
786 | ?> |