You are here

function pm_block_user_block_validate in Privatemsg 7

Same name and namespace in other branches
  1. 6.2 pm_block_user/pm_block_user.pages.inc \pm_block_user_block_validate()
  2. 7.2 pm_block_user/pm_block_user.pages.inc \pm_block_user_block_validate()

Validate user names.

1 string reference to 'pm_block_user_block_validate'
pm_block_user_list in pm_block_user/pm_block_user.pages.inc
Formbuilder function to build a simple form to block users.

File

pm_block_user/pm_block_user.pages.inc, line 137
User menu callbacks for pm_block_user.module.

Code

function pm_block_user_block_validate($form, &$form_state) {
  global $user;
  list($accounts, $invalid) = _privatemsg_parse_userstring($form_state['values']['name'], array(
    'user',
  ));

  // Remove accounts that can not be blocked.
  if (!empty($accounts)) {
    foreach ($accounts as $id => $account) {

      // Only authors can be blocked.
      if ($account->type != 'user') {
        drupal_set_message(t('Only users can be blocked.'));
        unset($accounts[$id]);
        continue;
      }

      // Check if the user can not be blocked because of a rule.
      if (_pm_block_user_rule_exists($account, $user, PM_BLOCK_USER_DISALLOW_BLOCKING)) {
        drupal_set_message(t('You are not allowed to block !account.', array(
          '!account' => theme('username', array(
            'account' => $account,
          )),
        )), 'warning');
        unset($accounts[$id]);
        continue;
      }

      // Check if the user is already blocked.
      if (pm_block_user_has_blocked($account, $user)) {
        drupal_set_message(t('You have already blocked !account.', array(
          '!account' => theme('username', array(
            'account' => $account,
          )),
        )), 'warning');
        unset($accounts[$id]);
        continue;
      }

      // Do not allow users to block themself.
      if ($user->uid == $account->uid) {
        drupal_set_message(t('You can not block yourself.'), 'warning');
        unset($accounts[$id]);
        continue;
      }
    }
  }

  // Display warning about invalid user names.
  if (!empty($invalid)) {
    drupal_set_message(t('The following users do not exist: @invalid.', array(
      '@invalid' => implode(", ", $invalid),
    )), 'warning');
  }

  // If there are no accounts left, display error.
  if (empty($accounts)) {
    form_set_error('name', t('You are either not allowed to block these users or the users do not exist.'));
  }
  else {
    $form_state['valid_accounts'] = $accounts;
  }
}