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