View source
<?php
namespace Drupal\notify\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Messenger\MessengerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Form\FormStateInterface;
class UsersForm extends ConfigFormBase {
protected $messenger;
public function __construct(ConfigFactoryInterface $config_factory, MessengerInterface $messenger) {
parent::__construct($config_factory);
$this->messenger = $messenger;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('messenger'));
}
public function getFormId() {
return 'notify_users';
}
protected function getEditableConfigNames() {
return [
'notify.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
$db_connection = \Drupal::database();
$form['#tree'] = TRUE;
$form['info'] = [
'#markup' => '<p>' . $this
->t('The following table shows all users that have notifications enabled:') . '</p>',
];
$q = $db_connection
->select('notify', 'n');
$q
->join('users', 'u', 'n.uid = u.uid');
$q
->join('users_field_data', 'v', 'n.uid = v.uid');
$q
->fields('v', [
'uid',
'name',
'mail',
'langcode',
]);
$q
->fields('n', [
'status',
'node',
'comment',
'attempts',
]);
$q
->condition('n.status', 1);
$q
->condition('v.status', 1);
$q
->orderBy('v.name');
$uresult = $q
->execute();
$form['settings'] = [
'#type' => 'fieldset',
'#title' => $this
->t('List of users'),
];
$form['settings']['table'] = [
'#tree' => TRUE,
'#type' => 'table',
'#header' => [
$this
->t('Username'),
$this
->t('E-mail Address'),
$this
->t('Content'),
$this
->t('Comment'),
$this
->t('Failed Attempts'),
],
'#id' => 'notify_settings_table',
];
foreach ($uresult as $user) {
$form['settings']['table'][$user->uid]['username'] = [
'#markup' => $user->name,
];
$form['settings']['table'][$user->uid]['mail'] = [
'#markup' => $user->mail,
];
$form['settings']['table'][$user->uid]['node'] = [
'#type' => 'checkbox',
'#default_value' => $user->node,
];
$form['settings']['table'][$user->uid]['comment'] = [
'#type' => 'checkbox',
'#default_value' => $user->comment,
];
$form['settings']['table'][$user->uid]['attempts'] = [
'#markup' => $user->attempts ? intval($user->attempts) : 0,
];
}
$form['info2'] = [
'#markup' => '<p>' . $this
->t("You may check/uncheck the checkboxes to change the users' subscription. Press “Save settings” to save the settings.") . '</p>',
];
$form['bulk'] = [
'#title' => $this
->t('Bulk-subscribe all unsubscribed users'),
'#type' => 'checkbox',
'#default_value' => FALSE,
'#description' => $this
->t('Apply “Default Settings” to <em>all</em> non-blocked users that do not already subscribe to notifications. Users that already has enabled “Receive email notifications” under their “Notify Settings” will not be affected.'),
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$db_connection = \Drupal::database();
$config = $this
->config('notify.settings');
$values = $form_state
->getValues();
if (isset($values['bulk']) && 1 == $values['bulk']) {
$node = $config
->get('notify_def_node');
$comment = $config
->get('notify_def_comment');
$r = $db_connection
->select('notify', 'n');
$r
->fields('n', [
'uid',
]);
$r
->condition('n.status', 0, '=');
$result = $r
->execute();
foreach ($result as $user) {
$db_connection
->update('notify')
->fields([
'status' => 1,
'node' => $node,
'comment' => $comment,
'attempts' => 0,
])
->condition('uid', $user->uid, '=')
->execute();
}
}
elseif (!array_key_exists('settings', $values)) {
$this->messenger
->addMessage($this
->t('No users have notifications enabled.'), 'warning');
return;
}
if (isset($values['settings']['table']) && $values['settings']['table']) {
foreach ($values['settings']['table'] as $uid => $settings) {
$db_connection
->update('notify')
->fields([
'node' => $settings['node'],
'comment' => $settings['comment'],
])
->condition('uid', $uid)
->execute();
}
}
$this->messenger
->addMessage($this
->t('Users notify settings saved.'));
}
}