You are here

class UsersExportForm in Users Export 8

Provides UsersExport Form.

Hierarchy

Expanded class hierarchy of UsersExportForm

1 string reference to 'UsersExportForm'
users_export.routing.yml in ./users_export.routing.yml
users_export.routing.yml

File

src/Form/UsersExportForm.php, line 17

Namespace

Drupal\users_export\Form
View source
class UsersExportForm extends ConfigFormBase {
  private $dateFormatter;
  private $dateFormatStorage;

  /**
   * UsersExportForm constructor.
   */
  public function __construct(DateFormatterInterface $date_formatter, EntityStorageInterface $date_format_storage) {
    $this->dateFormatter = $date_formatter;
    $this->dateFormatStorage = $date_format_storage;
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this
      ->config('users_export.settings');
    foreach (Element::children($form) as $variable) {
      $config
        ->set($variable, $form_state
        ->getValue($form[$variable]['#parents']));
    }
    $config
      ->save();
    if (method_exists($this, '_submitForm')) {
      $this
        ->_submitForm($form, $form_state);
    }
    parent::submitForm($form, $form_state);
  }

  /**
   * Form constructor for the users export form.
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $settings = \Drupal::config('users_export.settings');
    $core = \Drupal::service('loft_data_grids.core');
    $options = $core
      ->getExporterOptions(TRUE, FALSE, FALSE);

    // This does NOT work for library_load()
    $form['#attached']['library'][] = 'users_export/core';
    $exporters = $core
      ->getExporters();
    $form_state
      ->set('exporters', $exporters);
    $jsSettings = [];
    foreach ($exporters as $exporter) {
      $jsSettings[$exporter['id']] = array_intersect_key($exporter, array_flip([
        'extension',
      ]));
    }
    $form['#attached']['drupalSettings']['usersExport'] = $jsSettings;
    $class = $settings
      ->get('users_export_type');
    $type = $exporters[$class]['extension'];
    $form['users_export_type'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Export file format'),
      '#default_value' => $class,
      '#options' => $options,
    ];
    $default = $settings
      ->get('users_export_filename');
    if (empty($default)) {
      if (!($name = \Drupal::config('system.site')
        ->get('name'))) {
        $name = 'users_export';
      }
      $default = strtolower(preg_replace('/\\W+/', '_', $name) . '_users');
    }
    $form['users_export_filename'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Filename to save as'),
      '#default_value' => $default,
      '#required' => TRUE,
      '#field_suffix' => $type,
    ];
    $test_mode = $settings
      ->get('users_export_test_mode');
    $form['users_export_test_mode'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Preview mode (Enable to limit the export to only the first 10 users to check formatting.)'),
      '#default_value' => $test_mode,
    ];
    $form['basic_filters'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Basic Filters'),
      '#open' => FALSE,
    ];
    $form['advanced'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Advanced Settings'),
      '#open' => FALSE,
    ];
    $form['basic_filters']['users_export_with_access'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Login Frequency'),
      '#default_value' => $settings
        ->get('users_export_with_access'),
      '#options' => [
        2 => $this
          ->t('All Users'),
        1 => $this
          ->t('Get users who have logged in at least one time'),
        0 => $this
          ->t('Get users who have never logged in'),
      ],
    ];

    // Add field to add 'blocked' users to the export as well.
    $form['basic_filters']['users_export_user_status'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Export users with status'),
      '#default_value' => $settings
        ->get('users_export_with_access'),
      '#options' => [
        1 => $this
          ->t('Active'),
        0 => $this
          ->t('Blocked'),
        2 => $this
          ->t('Both active and blocked'),
      ],
    ];

    // Option to Filter By Role. (Optional).
    $form['basic_filters']['users_export_filter_by_role'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Filter by role?'),
      '#default_value' => $settings
        ->get('users_export_filter_by_role'),
      '#options' => [
        0 => $this
          ->t('No'),
        1 => $this
          ->t('Yes'),
      ],
      '#default_value' => 0,
    ];
    $roles = user_roles(TRUE);
    $roles_options = [];
    foreach ($roles as $role) {
      $role_id = $role
        ->id();
      $role_label = $role
        ->label();
      $roles_options[$role_id] = $role_label;
    }
    $form['basic_filters']['users_export_user_role'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Export users with role'),
      '#default_value' => $settings
        ->get('users_export_with_role'),
      '#options' => $roles_options,
      '#default_value' => array_keys($roles_options),
      '#states' => [
        'invisible' => [
          ':input[name="users_export_filter_by_role"]' => [
            'value' => 0,
          ],
        ],
      ],
    ];
    $form['basic_filters']['users_export_order'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Order of results'),
      '#default_value' => $settings
        ->get('users_export_order'),
      '#options' => [
        0 => $this
          ->t('User ID'),
        1 => $this
          ->t('Username A-Z'),
        2 => $this
          ->t('E-mail A-Z'),
      ],
    ];
    $time = new DrupalDateTime();
    $format_types = $this->dateFormatStorage
      ->loadMultiple();
    $options = [];
    foreach ($format_types as $type => $type_info) {
      $format = $this->dateFormatter
        ->format($time
        ->format('U'), $type);
      $options[$type_info
        ->getPattern()] = $type_info
        ->label() . ' (' . $format . ')';
    }

    // Now add in those we think most users will want to use.
    $options += [
      'Y-m-d' => 'Excel Date (' . date('Y-m-d') . ')',
      'Y-m-d H:i:s' => 'Excel Date & Time (' . date('Y-m-d H:i:s') . ')',
      'Y-m-d\\TH:i:s\\Z' => 'Datetime (' . date('Y-m-d\\TH:i:s\\Z') . ')',
      'U' => 'Unix Timestamp (' . date('U') . ')',
      \DateTime::ISO8601 => 'ISO 8601 (' . date(\DateTime::ISO8601) . ')',
      'Ymd\\THis' => 'iCal (' . date('Ymd\\THis') . ')',
    ];
    $form['advanced']['users_export_date_format'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Date format'),
      '#default_value' => $settings
        ->get('users_export_date_format'),
      '#options' => $options,
    ];
    $options = [
      -1 => $this
        ->t('- Default -'),
    ];
    foreach (range(128, 2048, 32) as $value) {
      $options[$value . 'M'] = format_size($value * 1024 * 1024);
    }
    $form['advanced']['users_export_memory_limit'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Memory Limit', [
        '@size' => format_size($settings
          ->get('users_export_last_export_memory')),
      ]),
      '#description' => $this
        ->t('If you have many users you may need to set this value higher so you the web server does not run out of memory processing the export. <strong>Depending upon your server configuration, this may or may not have any effect!</strong> For more information refer to <a href="http://php.net/manual/en/function.ini-set.php" target="blank">http://php.net/manual/en/function.ini-set.php</a>.'),
      '#default_value' => $settings
        ->get('users_export_memory_limit'),
      '#options' => $options,
    ];
    $options = [
      -1 => $this
        ->t('- Default -'),
    ];
    foreach (range(30, 1800, 30) as $value) {
      $options[$value] = \Drupal::service("date.formatter")
        ->formatInterval($value);
    }
    $form['advanced']['users_export_max_execution'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Maximum Execution Time', [
        '@time' => \Drupal::service("date.formatter")
          ->formatInterval($settings
          ->get('users_export_last_export_time')),
      ]),
      '#description' => $this
        ->t('If you have many users you may need to set this value higher so you the web server does not timeout. <strong>Depending upon your server configuration, this may or may not have any effect!</strong> For more information refer to <a href="http://php.net/manual/en/function.set-time-limit.php" target="blank">http://php.net/manual/en/function.set-time-limit.php</a>.'),
      '#default_value' => $settings
        ->get('users_export_max_execution'),
      '#options' => $options,
    ];
    $form = parent::buildForm($form, $form_state);
    $form['actions']['submit']['#value'] = $this
      ->t('Download File');
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'users_export.settings',
    ];
  }

  /**
   * Manage form submition.
   *
   * @codingStandardsIgnoreStart
   */
  public function _submitForm(array &$form, FormStateInterface $form_state) {

    //@codingStandardsIgnoreEnd
    $values = $form_state
      ->getValues();
    if ($values['users_export_memory_limit'] != -1) {
      ini_set('memory_limit', $values['users_export_memory_limit']);
    }
    if ($values['users_export_max_execution'] != -1) {
      set_time_limit($values['users_export_max_execution']);
    }
    if ($values['users_export_filter_by_role'] == 1 && !empty($values['users_export_user_role'])) {
      $values['users_export_user_role'] = array_filter($values['users_export_user_role']);
    }
    else {
      $values['users_export_user_role'] = FALSE;
    }
    $exporters = $form_state
      ->get('exporters');
    $exporter = $exporters[$values['users_export_type']]['class'];
    $exporter = new $exporter(new ExportData(), $values['users_export_filename']);
    \Drupal::service('users_export.core')
      ->exporterLoadUsers($exporter, [
      'with_access' => $values['users_export_with_access'],
      'limit' => $values['users_export_test_mode'] ? 10 : NULL,
      'order' => $values['users_export_order'],
      'roles' => $values['users_export_user_role'],
      'status' => ($s = $values['users_export_user_status']) === 2 ? NULL : $s * 1,
      'date_format' => $values['users_export_date_format'],
    ]);

    // TODO Should this be wrapped in a Response object?
    // https://www.drupal.org/node/2017339
    // https://www.drupal.org/node/1623114
    $exporter
      ->save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration 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::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.
UsersExportForm::$dateFormatStorage private property
UsersExportForm::$dateFormatter private property
UsersExportForm::buildForm public function Form constructor for the users export form. Overrides ConfigFormBase::buildForm
UsersExportForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
UsersExportForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
UsersExportForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
UsersExportForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
UsersExportForm::_submitForm public function Manage form submition.
UsersExportForm::__construct public function UsersExportForm constructor. Overrides ConfigFormBase::__construct