You are here

public static function ExportUser::exportUserOperation in Open Social 8

Same name and namespace in other branches
  1. 8.2 modules/social_features/social_user_export/src/ExportUser.php \Drupal\social_user_export\ExportUser::exportUserOperation()
  2. 8.3 modules/social_features/social_user_export/src/ExportUser.php \Drupal\social_user_export\ExportUser::exportUserOperation()
  3. 8.4 modules/social_features/social_user_export/src/ExportUser.php \Drupal\social_user_export\ExportUser::exportUserOperation()

Callback of one operation.

Parameters

\Drupal\user\UserInterface $entity: UserInterface entity.

array $context: Context of the operation.

1 call to ExportUser::exportUserOperation()
ExportUser::exportUsersAllOperation in modules/social_features/social_user_export/src/ExportUser.php
Callback of massive operations.

File

modules/social_features/social_user_export/src/ExportUser.php, line 25

Class

ExportUser
Class ExportUser.

Namespace

Drupal\social_user_export

Code

public static function exportUserOperation(UserInterface $entity, array &$context) {
  if (empty($context['results']['file_path'])) {
    $context['results']['file_path'] = self::getFileTemporaryPath();
    $csv = Writer::createFromPath($context['results']['file_path'], 'w');
    $csv
      ->setDelimiter(',');
    $csv
      ->setEnclosure('"');
    $csv
      ->setEscape('\\');

    // Append header.
    $headers = [
      t('ID'),
      t('UUID'),
      t('Email'),
      t('Last login'),
      t('Last access'),
      t('Registration date'),
      t('Status'),
      t('Roles'),
      t('Posts created'),
      t('Comments created'),
      t('Topics created'),
      t('Events enrollments'),
      t('Events created'),
      t('Groups created'),
    ];
    $csv
      ->insertOne($headers);
  }
  else {
    $csv = Writer::createFromPath($context['results']['file_path'], 'a');
  }

  // Add formatter.
  $encoder = \Drupal::service('csv_serialization.encoder.csv');
  $csv
    ->addFormatter([
    $encoder,
    'formatRow',
  ]);
  $roles = $entity
    ->getRoles();
  $status = $entity
    ->get('status')
    ->getValue();

  // Format last login time.
  if ($last_login_time = $entity
    ->getLastLoginTime()) {
    $last_login = \Drupal::service('date.formatter')
      ->format($last_login_time, 'custom', 'Y/m/d - H:i');
  }
  else {
    $last_login = t('never');
  }

  // Format last access time.
  if ($last_access_time = $entity
    ->getLastAccessedTime()) {
    $last_access = \Drupal::service('date.formatter')
      ->format($last_access_time, 'custom', 'Y/m/d - H:i');
  }
  else {
    $last_access = t('never');
  }

  // Add row.
  $csv
    ->insertOne([
    $entity
      ->id(),
    $entity
      ->uuid(),
    $entity
      ->getEmail(),
    $last_login,
    $last_access,
    \Drupal::service('date.formatter')
      ->format($entity
      ->getCreatedTime(), 'custom', 'Y/m/d - H:i'),
    !empty($status[0]['value']) ? t('Active') : t('Blocked'),
    implode(', ', $roles),
    social_user_export_posts_count($entity),
    social_user_export_comments_count($entity),
    social_user_export_nodes_count($entity, 'topic'),
    social_user_export_events_enrollments_count($entity),
    social_user_export_nodes_count($entity, 'event'),
    social_user_export_groups_count($entity),
  ]);
  $context['message'] = t('Exporting: @name', [
    '@name' => $entity
      ->getAccountName(),
  ]);
}