class CleantalkCheckUsersForm in Anti Spam by CleanTalk 9.1.x
Same name and namespace in other branches
- 8.4 src/Form/CleantalkCheckUsersForm.php \Drupal\cleantalk\Form\CleantalkCheckUsersForm
- 8.3 src/Form/CleantalkCheckUsersForm.php \Drupal\cleantalk\Form\CleantalkCheckUsersForm
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\cleantalk\Form\CleantalkCheckUsersForm
Expanded class hierarchy of CleantalkCheckUsersForm
1 string reference to 'CleantalkCheckUsersForm'
File
- src/
Form/ CleantalkCheckUsersForm.php, line 14
Namespace
Drupal\cleantalk\FormView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CleantalkCheckUsersForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
CleantalkCheckUsersForm:: |
public | function | ||
CleantalkCheckUsersForm:: |
public | function | ||
CleantalkCheckUsersForm:: |
public | function | ||
CleantalkCheckUsersForm:: |
protected | function | ||
CleantalkCheckUsersForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
CleantalkCheckUsersForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 3 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 3 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
105 |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function | Returns a redirect response object for the specified route. | |
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
72 |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |