You are here

class CleantalkCheckUsersForm in Anti Spam by CleanTalk 8.3

Same name and namespace in other branches
  1. 8.4 src/Form/CleantalkCheckUsersForm.php \Drupal\cleantalk\Form\CleantalkCheckUsersForm
  2. 9.1.x src/Form/CleantalkCheckUsersForm.php \Drupal\cleantalk\Form\CleantalkCheckUsersForm

Hierarchy

Expanded class hierarchy of CleantalkCheckUsersForm

1 string reference to 'CleantalkCheckUsersForm'
cleantalk.routing.yml in ./cleantalk.routing.yml
cleantalk.routing.yml

File

src/Form/CleantalkCheckUsersForm.php, line 11

Namespace

Drupal\cleantalk\Form
View source
class CleantalkCheckUsersForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'cleantalk_check_users_form';
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'cleantalk.check_users',
    ];
  }
  public function buildForm(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
    if (!empty(\Drupal::config('cleantalk.settings')
      ->get('cleantalk_authkey'))) {
      $form_state
        ->setStorage(array(
        'spam_users' => $this
          ->cleantalk_find_spammers_users(),
      ));
      if (isset($form_state
        ->getStorage()['spam_users'])) {
        $form['spam_users'] = array(
          '#type' => 'table',
          '#header' => array(
            'Username',
            'E-mail',
            'Status',
            'Registered',
            'Last visit',
            '',
          ),
          '#empty' => $this
            ->t('No users found'),
        );
        $spam_users = $form_state
          ->getStorage()['spam_users'];
        foreach ($spam_users as $user) {

          // Show user name
          $form['spam_users'][$user['id']][0] = array(
            '#type' => 'label',
            '#title' => $user['name'],
          );

          // Show user mail
          $form['spam_users'][$user['id']][1] = array(
            '#type' => 'label',
            '#title' => '<a target="_blank" href = "https://cleantalk.org/blacklists/' . $user['mail'] . '">' . $user['mail'] . '</a>',
          );

          // Show user status
          $form['spam_users'][$user['id']][2] = array(
            '#type' => 'label',
            '#title' => $user['status'],
          );

          // Show user date created
          $form['spam_users'][$user['id']][3] = array(
            '#type' => 'label',
            '#title' => $user['created'],
          );

          // Show user login
          $form['spam_users'][$user['id']][4] = array(
            '#type' => 'label',
            '#title' => $user['login'],
          );

          // Show button for each user
          $form['spam_users'][$user['id']]['removememberbutton']['dummyNode'] = array(
            '#type' => 'submit',
            '#value' => 'Remove',
            '#name' => 'remove_' . $user['id'],
            '#submit' => array(
              '::cleantalk_remove_user',
            ),
          );
        }
        $data = array();
        foreach ($spam_users as $user) {
          array_push($data, $user['id']);
        }
        $form['delete_all_spammers_users'] = array(
          '#type' => 'submit',
          '#value' => $this
            ->t('Delete all'),
          '#name' => 'delete_all_' . implode('_', $data),
          '#submit' => array(
            '::cleantalk_delete_all_spammers_users',
          ),
        );
      }
    }
    else {
      $this
        ->messenger()
        ->addError('Access key is not valid.');
    }
    return $form;
  }
  public function cleantalk_find_spammers_users() {
    $ct_authkey = trim(\Drupal::config('cleantalk.settings')
      ->get('cleantalk_authkey'));
    if ($ct_authkey) {
      $ids = \Drupal::entityQuery('user')
        ->execute();
      $users = User::loadMultiple($ids);
      $data = array();
      $spam_users = array();
      foreach ($users as $user) {
        array_push($data, $user
          ->get('mail')->value);
      }
      $data = implode(',', $data);
      $result = CleantalkAPI::method__spam_check_cms($ct_authkey, $data);
      if (isset($result['error_message'])) {
        $this
          ->messenger()
          ->addError($result['error_message']);
      }
      else {
        foreach ($result as $key => $value) {
          if (isset($value['appears']) && $value['appears'] == '1') {
            foreach ($users as $user) {
              if ($user
                ->get('mail')->value == $key) {
                $spam_users[] = $user;
              }
            }
          }
        }
      }
      $storage_array = array();
      $id = 0;
      foreach ($spam_users as $user) {
        $storage_array[$id]['id'] = $user
          ->id();
        $storage_array[$id]['name'] = $user
          ->get('name')->value;
        $storage_array[$id]['mail'] = $user
          ->get('mail')->value;
        $storage_array[$id]['status'] = $user
          ->get('status')->value == 1 ? 'Active' : 'Inactive';
        $storage_array[$id]['created'] = date("Y-m-d H:i:s", $user
          ->get('created')->value);
        $storage_array[$id]['login'] = date("Y-m-d H:i:s", $user
          ->get('login')->value);
        $id++;
      }
      return $storage_array;
    }
  }
  public function cleantalk_remove_user(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
    $userid = $form_state
      ->getTriggeringElement()['#array_parents'][1];
    user_cancel(array(), $userid, 'user_cancel_delete');
  }
  public function cleantalk_delete_all_spammers_users(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
    $post_array = str_replace('delete_all_', '', $form_state
      ->getTriggeringElement()['#name']);
    $ids = explode('_', $post_array);
    foreach ($ids as $id) {
      user_cancel(array(), $id, 'user_cancel_delete');
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CleantalkCheckUsersForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
CleantalkCheckUsersForm::cleantalk_delete_all_spammers_users public function
CleantalkCheckUsersForm::cleantalk_find_spammers_users public function
CleantalkCheckUsersForm::cleantalk_remove_user public function
CleantalkCheckUsersForm::getEditableConfigNames protected function
CleantalkCheckUsersForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
CleantalkCheckUsersForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 87
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.