View source
<?php
namespace Drupal\mail_safety\Form;
use Drupal\Core\Database\Connection;
use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Link;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DashboardForm extends FormBase {
protected $database;
protected $dateFormatter;
protected $moduleHandler;
public function __construct(Connection $database, DateFormatter $date_formatter, ModuleHandler $module_handler) {
$this->database = $database;
$this->dateFormatter = $date_formatter;
$this->moduleHandler = $module_handler;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('database'), $container
->get('date.formatter'), $container
->get('module_handler'));
}
public function getFormId() {
return 'mail_safety_dashboard_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$table_structure = [];
$table_structure['header'] = [
[
'data' => $this
->t('Subject'),
],
[
'data' => $this
->t('Date sent'),
'field' => 'sent',
'sort' => 'desc',
],
[
'data' => $this
->t('To'),
],
[
'data' => $this
->t('CC'),
],
[
'data' => $this
->t('Bcc'),
],
[
'data' => $this
->t('Module'),
],
[
'data' => $this
->t('Key'),
],
[
'data' => $this
->t('Details'),
],
[
'data' => $this
->t('Send to original'),
],
[
'data' => $this
->t('Send to default mail'),
],
[
'data' => $this
->t('Delete'),
],
];
$query = $this->database
->select('mail_safety_dashboard', 'msd')
->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
->limit(50)
->extend('Drupal\\Core\\Database\\Query\\TableSortExtender')
->orderByHeader($table_structure['header'])
->fields('msd', [
'mail_id',
'sent',
'mail',
]);
$results = $query
->execute();
$table_structure['rows'] = [];
foreach ($results as $row) {
$mail = unserialize($row->mail);
$view_url = Url::fromRoute('mail_safety.view', [
'mail_safety' => $row->mail_id,
]);
$details_url = Url::fromRoute('mail_safety.details', [
'mail_safety' => $row->mail_id,
]);
$send_original_url = Url::fromRoute('mail_safety.send_original', [
'mail_safety' => $row->mail_id,
]);
$send_default_url = Url::fromRoute('mail_safety.send_default', [
'mail_safety' => $row->mail_id,
]);
$delete_url = Url::fromRoute('mail_safety.delete', [
'mail_safety' => $row->mail_id,
]);
$cc = $mail['headers']['Cc'] ?? $this
->t('none');
$bcc = $mail['headers']['Bcc'] ?? $this
->t('none');
$table_structure['rows'][$row->mail_id] = [
'data' => [
Link::fromTextAndUrl($mail['subject'], $view_url),
$this->dateFormatter
->format((int) $row->sent, 'short'),
$mail['to'],
$cc,
$bcc,
$mail['module'],
$mail['key'],
Link::fromTextAndUrl($this
->t('Details'), $details_url),
Link::fromTextAndUrl($this
->t('Send to original'), $send_original_url),
Link::fromTextAndUrl($this
->t('Send to default mail'), $send_default_url),
Link::fromTextAndUrl($this
->t('Delete'), $delete_url),
],
];
}
$this->moduleHandler
->alter('mail_safety_table_structure', $table_structure);
$form['mails']['table'] = [
'#theme' => 'table',
'#header' => $table_structure['header'],
'#rows' => $table_structure['rows'],
'#sticky' => TRUE,
'#empty' => $this
->t('No mails found'),
];
$form['mails']['pager'] = [
'#type' => 'pager',
'#tags' => [],
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}