You are here

class AnonymousPublishingClAdminPrivacy in Anonymous Publishing 8

Hierarchy

Expanded class hierarchy of AnonymousPublishingClAdminPrivacy

1 string reference to 'AnonymousPublishingClAdminPrivacy'
anonymous_publishing_cl.routing.yml in modules/anonymous_publishing_cl/anonymous_publishing_cl.routing.yml
modules/anonymous_publishing_cl/anonymous_publishing_cl.routing.yml

File

modules/anonymous_publishing_cl/src/Form/AnonymousPublishingClAdminPrivacy.php, line 13

Namespace

Drupal\anonymous_publishing_cl\Form
View source
class AnonymousPublishingClAdminPrivacy extends FormBase {

  /**
   * The database connection service.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * The database connection service.
   *
   * @var \Drupal\Core\Datetime\DateFormatter
   */
  protected $dateFormatter;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('database'), $container
      ->get('date.formatter'), $container
      ->get('string_translation'));
  }

  /**
   * Constructs a \Drupal\anonymous_publishing_cl\Form\AnonymousPublishingClAdminModeration object.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   The database connection service.
   * @param \Drupal\Core\Datetime\DateFormatter
   *   The date formatter service.
   */
  public function __construct(Connection $database, DateFormatter $date_formatter, TranslationInterface $string_translation) {
    $this->database = $database;
    $this->dateFormatter = $date_formatter;
    $this->stringTranslation = $string_translation;
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'anonymous_publishing_cl_admin_privacy';
  }
  public function buildForm(array $form, FormStateInterface $form_state) {
    $settings = $this
      ->config('anonymous_publishing_cl.settings');

    // Count the number of used aliases on file.
    $aliases = $this
      ->getNumberOfVerifiedContents();
    $period = [
      0 => t('Delete ASAP'),
      3600 => $this->dateFormatter
        ->formatInterval(3600),
      21600 => $this->dateFormatter
        ->formatInterval(21600),
      43200 => $this->dateFormatter
        ->formatInterval(43200),
      86400 => $this->dateFormatter
        ->formatInterval(86400),
      259200 => $this->dateFormatter
        ->formatInterval(259200),
      604800 => $this->dateFormatter
        ->formatInterval(604800),
      2592000 => $this->dateFormatter
        ->formatInterval(2592000),
      -1 => t('Indefinitely'),
    ];
    $form = [];
    $aliasopt = $settings
      ->get('user_alias');
    if ($aliasopt != 'anon') {
      $disablep = TRUE;
      $warn = '<br/><strong>' . t('Note:') . '</strong> ' . t('Purging is incompatible with having an alias (main settings).  To purge, you need to turn this setting off.');
    }
    elseif (empty($aliases)) {
      $warn = '';
      $disablep = FALSE;
    }
    else {
      $warn = '<br/>' . t('You have @count linking verification email to content. @these will be deleted when these links are purged.', [
        '@count' => $this->stringTranslation
          ->formatPlural(count($aliases), '1 record', '@count records'),
        '@these' => $this->stringTranslation
          ->formatPlural(count($aliases), 'This', 'These'),
      ]);
      $disablep = FALSE;
    }
    $form['anonymous_publishing_privacy'] = [
      '#markup' => '<p>' . t('For enhanced privacy, you can set a limited retention period for identifying information, or purge this information instantly or periodically.') . ' ' . $warn . '</p>',
    ];
    $form['apperiod'] = [
      '#type' => 'fieldset',
      '#title' => t('Retention period'),
      '#collapsible' => FALSE,
    ];
    $form['apperiod']['retain_period'] = [
      '#type' => 'select',
      '#title' => t('Maximum period to retain records that links verification emails, ip-addresses and generated aliases to <em>specific</em> contents:'),
      '#default_value' => $settings
        ->get('retain_period'),
      '#options' => $period,
      '#description' => t('Select &#8220;Indefinitely&#8221; to make the records linking verification email to content persistent.  This is the <em>only</em> setting compatible with a persistent alias as byline.'),
    ];
    $form['apperiod']['submit'] = [
      '#type' => 'submit',
      '#disabled' => $disablep,
      '#value' => t('Save settings'),
      '#name' => 'save',
    ];
    $form['appurge'] = [
      '#type' => 'fieldset',
      '#title' => t('Purge'),
      '#collapsible' => FALSE,
    ];
    $form['appurge']['info'] = [
      '#markup' => t('<p>Press button below to immediately purge all information linking emails, ip-addresses and generated aliases to anonymously published content.  This operation can not be reversed.</p>'),
    ];
    $form['appurge']['submit'] = [
      '#type' => 'submit',
      '#disabled' => $disablep,
      '#value' => t('Purge now'),
      '#name' => 'purge',
    ];
    return $form;
  }
  public function submitForm(array &$form, FormStateInterface $form_state) {
    switch ($form_state
      ->getTriggeringElement()['#name']) {
      case 'purge':

        // First delete completly all that are published.
        $this->database
          ->delete('anonymous_publishing')
          ->condition('verified', 1)
          ->execute();

        // For the rest, delete the IP (we need email for whitelist).
        $this->database
          ->update('anonymous_publishing')
          ->fields(array(
          'ip' => '',
        ))
          ->execute();
        $this
          ->messenger()
          ->addMessage(t('All information linking identifiers to published content have been purged.'));
        break;
      case 'save':
        \Drupal::configFactory()
          ->getEditable('anonymous_publishing_cl.settings')
          ->set('retain_period', $form_state
          ->getValue([
          'retain_period',
        ]))
          ->save();
        $this
          ->messenger()
          ->addMessage(t('Rentention period updated.'));
        break;
      default:
        $this
          ->messenger()
          ->addError(t('Unknown operation.'));
        break;
    }
  }

  /**
   * Get all verified content.
   *
   * @param int $test_id
   *   The test_id to retrieve results of.
   *
   * @return array
   *  Array of results grouped by test_class.
   */
  protected function getNumberOfVerifiedContents() {
    $query = $this->database
      ->select('anonymous_publishing', 'a');
    $query
      ->fields('a');
    $query
      ->where('a.verified > 0');
    $result = $query
      ->execute()
      ->fetchAssoc();
    return $result;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AnonymousPublishingClAdminPrivacy::$database protected property The database connection service.
AnonymousPublishingClAdminPrivacy::$dateFormatter protected property The database connection service.
AnonymousPublishingClAdminPrivacy::buildForm public function Form constructor. Overrides FormInterface::buildForm
AnonymousPublishingClAdminPrivacy::create public static function Instantiates a new instance of this class. Overrides FormBase::create
AnonymousPublishingClAdminPrivacy::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
AnonymousPublishingClAdminPrivacy::getNumberOfVerifiedContents protected function Get all verified content.
AnonymousPublishingClAdminPrivacy::submitForm public function Form submission handler. Overrides FormInterface::submitForm
AnonymousPublishingClAdminPrivacy::__construct public function Constructs a \Drupal\anonymous_publishing_cl\Form\AnonymousPublishingClAdminModeration object.
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::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.