Skip to content

Instantly share code, notes, and snippets.

@Nicofuma
Last active September 24, 2016 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nicofuma/a884e1f1d311ab39026d533c4c33fb56 to your computer and use it in GitHub Desktop.
Save Nicofuma/a884e1f1d311ab39026d533c4c33fb56 to your computer and use it in GitHub Desktop.
<?php
function phpbb_get_max_setting_from_group(\phpbb\db\driver\driver_interface $db, $user_id, $setting)
{
if ($setting !== 'max_recipients' && $setting !== 'message_limit')
{
throw new InvalidArgumentException('Setting "' . $setting . '" is not supported');
}
// Get maximum number of allowed recipients
$sql = 'SELECT MAX(g.group_' . $setting . ') as max_setting
FROM ' . GROUPS_TABLE . ' g, ' . USER_GROUP_TABLE . ' ug
WHERE ug.user_id = ' . (int) $user_id . '
AND ug.user_pending = 0
AND ug.group_id = g.group_id';
$result = $db->sql_query($sql);
$max_setting = (int) $db->sql_fetchfield('max_setting');
$db->sql_freeresult($result);
if ($max_setting)
{
// If the user is limited by a group, check whether he is also set to
// unlimited in another group (value 0)
$sql = 'SELECT g.group_' . $setting . ' as max_setting
FROM ' . GROUPS_TABLE . ' g, ' . USER_GROUP_TABLE . ' ug
WHERE ug.user_id = ' . (int) $user_id . '
AND ug.user_pending = 0
AND ug.group_id = g.group_id
AND g.group_max_recipients = 0';
$result = $db->sql_query($sql);
$is_unlimited = $db->sql_fetchfield('max_setting');
$db->sql_freeresult($result);
$max_setting = ($is_unlimited === false) ? $max_setting : 0;
}
return $max_setting;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment